| 1 |
|
| 2 |
from pypy.interpreter.gateway import interp2app |
| 3 |
from rpython.tool.udir import udir |
| 4 |
import os |
| 5 |
|
| 6 |
|
| 7 |
class AppTestFileIO: |
| 8 |
spaceconfig = dict(usemodules=['_io', 'array']) |
| 9 |
if os.name != 'nt': |
| 10 |
spaceconfig['usemodules'].append('fcntl') |
| 11 |
|
| 12 |
def setup_method(self, meth): |
| 13 |
tmpfile = udir.join('tmpfile') |
| 14 |
tmpfile.write("a\nb\nc", mode='wb') |
| 15 |
self.w_tmpfile = self.space.wrap(str(tmpfile)) |
| 16 |
self.w_tmpdir = self.space.wrap(str(udir)) |
| 17 |
self.w_posix = self.space.appexec([], """(): |
| 18 |
import %s as m; |
| 19 |
return m""" % os.name) |
| 20 |
if meth == self.test_readinto_optimized: |
| 21 |
bigfile = udir.join('bigfile') |
| 22 |
bigfile.write('a' * 1000, mode='wb') |
| 23 |
self.w_bigfile = self.space.wrap(self.space.wrap(str(bigfile))) |
| 24 |
|
| 25 |
def test_constructor(self): |
| 26 |
import _io |
| 27 |
f = _io.FileIO(self.tmpfile, 'a') |
| 28 |
assert f.name.endswith('tmpfile') |
| 29 |
assert f.mode == 'ab' |
| 30 |
assert f.closefd is True |
| 31 |
assert f._blksize >= 1024 |
| 32 |
assert f._blksize % 1024 == 0 |
| 33 |
f.close() |
| 34 |
|
| 35 |
def test_invalid_fd(self): |
| 36 |
import _io |
| 37 |
raises(ValueError, _io.FileIO, -10) |
| 38 |
raises(TypeError, _io.FileIO, 2 ** 31) |
| 39 |
raises(TypeError, _io.FileIO, -2 ** 31 - 1) |
| 40 |
|
| 41 |
def test_weakrefable(self): |
| 42 |
import _io |
| 43 |
from weakref import proxy |
| 44 |
f = _io.FileIO(self.tmpfile) |
| 45 |
p = proxy(f) |
| 46 |
assert p.mode == 'rb' |
| 47 |
f.close() |
| 48 |
|
| 49 |
def test_open_fd(self): |
| 50 |
import _io |
| 51 |
os = self.posix |
| 52 |
fd = os.open(self.tmpfile, os.O_RDONLY, 0o666) |
| 53 |
f = _io.FileIO(fd, "rb", closefd=False) |
| 54 |
assert f.fileno() == fd |
| 55 |
assert f.closefd is False |
| 56 |
f.close() |
| 57 |
os.close(fd) |
| 58 |
|
| 59 |
def test_open_directory(self): |
| 60 |
import _io |
| 61 |
import os, warnings |
| 62 |
with warnings.catch_warnings(record=True) as w: |
| 63 |
warnings.simplefilter('always', ResourceWarning) |
| 64 |
raises(IOError, _io.FileIO, self.tmpdir, "rb") |
| 65 |
assert len(w) == 0 |
| 66 |
if os.name != 'nt': |
| 67 |
fd = os.open(self.tmpdir, os.O_RDONLY) |
| 68 |
raises(IOError, _io.FileIO, fd, "rb") |
| 69 |
os.close(fd) |
| 70 |
|
| 71 |
def test_open_non_existent_unicode(self): |
| 72 |
import _io |
| 73 |
import os |
| 74 |
path = os.path.join(self.tmpdir, '_pypy-日本') |
| 75 |
try: |
| 76 |
os.fsencode(path) |
| 77 |
except UnicodeEncodeError: |
| 78 |
import sys |
| 79 |
skip("can't run this test with %s as filesystem encoding" % |
| 80 |
sys.getfilesystemencoding()) |
| 81 |
exc = raises(IOError, _io.FileIO, path) |
| 82 |
expected = "[Errno 2] No such file or directory: %r" % path |
| 83 |
assert str(exc.value) == expected |
| 84 |
|
| 85 |
def test_readline(self): |
| 86 |
import _io |
| 87 |
f = _io.FileIO(self.tmpfile, 'rb') |
| 88 |
assert f.readline() == b'a\n' |
| 89 |
assert f.readline() == b'b\n' |
| 90 |
assert f.readline() == b'c' |
| 91 |
assert f.readline() == b'' |
| 92 |
f.close() |
| 93 |
|
| 94 |
def test_support_fspath(self): |
| 95 |
import _io |
| 96 |
class P(object): |
| 97 |
def __fspath__(x): |
| 98 |
return self.tmpfile |
| 99 |
f = _io.FileIO(P(), 'rb') |
| 100 |
assert f.readline() == b'a\n' |
| 101 |
f.close() |
| 102 |
|
| 103 |
def test_readlines(self): |
| 104 |
import _io |
| 105 |
f = _io.FileIO(self.tmpfile, 'rb') |
| 106 |
assert f.readlines() == [b"a\n", b"b\n", b"c"] |
| 107 |
f.seek(0) |
| 108 |
assert f.readlines(3) == [b"a\n", b"b\n"] |
| 109 |
f.close() |
| 110 |
|
| 111 |
def test_readall(self): |
| 112 |
import _io |
| 113 |
f = _io.FileIO(self.tmpfile, 'rb') |
| 114 |
assert f.readall() == b"a\nb\nc" |
| 115 |
f.close() |
| 116 |
|
| 117 |
def test_write(self): |
| 118 |
import _io |
| 119 |
filename = self.tmpfile + '_w' |
| 120 |
f = _io.FileIO(filename, 'wb') |
| 121 |
f.write(b"test") |
| 122 |
|
| 123 |
f2 = _io.FileIO(filename, 'rb') |
| 124 |
assert f2.read() == b"test" |
| 125 |
f.close() |
| 126 |
f2.close() |
| 127 |
|
| 128 |
def test_writelines(self): |
| 129 |
import _io |
| 130 |
filename = self.tmpfile + '_w' |
| 131 |
f = _io.FileIO(filename, 'wb') |
| 132 |
f.writelines([b"line1\n", b"line2", b"line3"]) |
| 133 |
f2 = _io.FileIO(filename, 'rb') |
| 134 |
assert f2.read() == b"line1\nline2line3" |
| 135 |
f.close() |
| 136 |
f2.close() |
| 137 |
|
| 138 |
def test_seek(self): |
| 139 |
import _io |
| 140 |
f = _io.FileIO(self.tmpfile, 'rb') |
| 141 |
f.seek(0) |
| 142 |
self.posix.close(f.fileno()) |
| 143 |
raises(IOError, f.seek, 0) |
| 144 |
|
| 145 |
def test_tell(self): |
| 146 |
import _io |
| 147 |
f = _io.FileIO(self.tmpfile, 'rb') |
| 148 |
f.seek(3) |
| 149 |
assert f.tell() == 3 |
| 150 |
f.close() |
| 151 |
|
| 152 |
def test_truncate(self): |
| 153 |
import _io |
| 154 |
f = _io.FileIO(self.tmpfile, 'r+b') |
| 155 |
assert f.truncate(100) == 100 |
| 156 |
f.close() |
| 157 |
f = _io.FileIO(self.tmpfile) |
| 158 |
assert len(f.read()) == 100 |
| 159 |
f.close() |
| 160 |
|
| 161 |
f = _io.FileIO(self.tmpfile, 'r+b') |
| 162 |
f.seek(50) |
| 163 |
assert f.truncate() == 50 |
| 164 |
f.close() |
| 165 |
f = _io.FileIO(self.tmpfile) |
| 166 |
assert len(f.read()) == 50 |
| 167 |
f.close() |
| 168 |
|
| 169 |
def test_readinto(self): |
| 170 |
import _io |
| 171 |
a = bytearray(b'x' * 10) |
| 172 |
f = _io.FileIO(self.tmpfile, 'r+') |
| 173 |
assert f.readinto(a) == 5 |
| 174 |
f.seek(0) |
| 175 |
m = memoryview(bytearray(b"helloworld")) |
| 176 |
assert f.readinto(m) == 5 |
| 177 |
|
| 178 |
exc = raises(TypeError, f.readinto, u"hello") |
| 179 |
msg = str(exc.value) |
| 180 |
|
| 181 |
assert " read-write b" in msg and msg.endswith(", not str") |
| 182 |
|
| 183 |
exc = raises(TypeError, f.readinto, memoryview(b"hello")) |
| 184 |
msg = str(exc.value) |
| 185 |
|
| 186 |
assert " read-write b" in msg and msg.endswith(", not memoryview") |
| 187 |
|
| 188 |
f.close() |
| 189 |
assert a == b'a\nb\ncxxxxx' |
| 190 |
|
| 191 |
a = bytearray(b'x' * 10) |
| 192 |
f = _io.FileIO(self.tmpfile, 'r+') |
| 193 |
f.truncate(3) |
| 194 |
assert f.readinto(a) == 3 |
| 195 |
f.close() |
| 196 |
assert a == b'a\nbxxxxxxx' |
| 197 |
|
| 198 |
def test_readinto_optimized(self): |
| 199 |
import _io |
| 200 |
a = bytearray(b'x' * 1024) |
| 201 |
f = _io.FileIO(self.bigfile, 'r+') |
| 202 |
assert f.readinto(a) == 1000 |
| 203 |
assert a == b'a' * 1000 + b'x' * 24 |
| 204 |
|
| 205 |
def test_readinto_array(self): |
| 206 |
import _io, array |
| 207 |
buffer = array.array('i', [0]*10) |
| 208 |
m = memoryview(buffer) |
| 209 |
f = _io.FileIO(self.tmpfile, 'r+') |
| 210 |
assert f.readinto(m[1:9]) == 5 |
| 211 |
assert buffer[1] in (0x610a620a, 0x0a620a61) |
| 212 |
|
| 213 |
def test_nonblocking_read(self): |
| 214 |
try: |
| 215 |
import os, fcntl |
| 216 |
except ImportError: |
| 217 |
skip("need fcntl to set nonblocking mode") |
| 218 |
r_fd, w_fd = os.pipe() |
| 219 |
|
| 220 |
fcntl.fcntl(r_fd, fcntl.F_SETFL, os.O_NONBLOCK) |
| 221 |
import _io |
| 222 |
f = _io.FileIO(r_fd, 'r') |
| 223 |
|
| 224 |
assert f.read() is None |
| 225 |
assert f.read(10) is None |
| 226 |
a = bytearray(b'x' * 10) |
| 227 |
assert f.readinto(a) is None |
| 228 |
a2 = bytearray(b'x' * 1024) |
| 229 |
assert f.readinto(a2) is None |
| 230 |
|
| 231 |
def test_pipe_append(self): |
| 232 |
import os |
| 233 |
r, w = os.pipe() |
| 234 |
try: |
| 235 |
try: |
| 236 |
f = open(w, 'a') |
| 237 |
finally: |
| 238 |
f.close() |
| 239 |
finally: |
| 240 |
os.close(r) |
| 241 |
|
| 242 |
def test_repr(self): |
| 243 |
import _io |
| 244 |
f = _io.FileIO(self.tmpfile, 'r') |
| 245 |
assert repr(f) == ("<_io.FileIO name=%r mode='%s' closefd=True>" |
| 246 |
% (f.name, f.mode)) |
| 247 |
del f.name |
| 248 |
assert repr(f) == ("<_io.FileIO fd=%r mode='%s' closefd=True>" |
| 249 |
% (f.fileno(), f.mode)) |
| 250 |
f.close() |
| 251 |
assert repr(f) == "<_io.FileIO [closed]>" |
| 252 |
|
| 253 |
def test_unclosed_fd_on_exception(self): |
| 254 |
import _io |
| 255 |
import os |
| 256 |
class MyException(Exception): pass |
| 257 |
class MyFileIO(_io.FileIO): |
| 258 |
def __setattr__(self, name, value): |
| 259 |
if name == "name": |
| 260 |
raise MyException("blocked setting name") |
| 261 |
return super(MyFileIO, self).__setattr__(name, value) |
| 262 |
fd = os.open(self.tmpfile, os.O_RDONLY) |
| 263 |
raises(MyException, MyFileIO, fd) |
| 264 |
os.close(fd) |
| 265 |
|
| 266 |
def test_mode_strings(self): |
| 267 |
import _io |
| 268 |
import os |
| 269 |
for modes in [('w', 'wb'), ('wb', 'wb'), ('wb+', 'rb+'), |
| 270 |
('w+b', 'rb+'), ('a', 'ab'), ('ab', 'ab'), |
| 271 |
('ab+', 'ab+'), ('a+b', 'ab+'), ('r', 'rb'), |
| 272 |
('rb', 'rb'), ('rb+', 'rb+'), ('r+b', 'rb+')]: |
| 273 |
|
| 274 |
with _io.FileIO(self.tmpfile, modes[0]) as f: |
| 275 |
assert f.mode == modes[1] |
| 276 |
|
| 277 |
def test_flush_error_on_close(self): |
| 278 |
|
| 279 |
|
| 280 |
import _io, os |
| 281 |
fd = os.open(self.tmpfile, os.O_RDONLY, 0o666) |
| 282 |
f = _io.FileIO(fd, 'r', closefd=False) |
| 283 |
closed = [] |
| 284 |
def bad_flush(): |
| 285 |
closed[:] = [f.closed] |
| 286 |
raise IOError() |
| 287 |
f.flush = bad_flush |
| 288 |
raises(IOError, f.close) |
| 289 |
assert f.closed |
| 290 |
assert closed |
| 291 |
assert not closed[0] |
| 292 |
os.close(fd) |
| 293 |
|
| 294 |
def test_open_exclusive(self): |
| 295 |
|
| 296 |
FileExistsError = OSError |
| 297 |
|
| 298 |
import _io |
| 299 |
filename = self.tmpfile + '_x1' |
| 300 |
raises(ValueError, _io.FileIO, filename, 'xw') |
| 301 |
with _io.FileIO(filename, 'x') as f: |
| 302 |
assert f.mode == 'xb' |
| 303 |
raises(FileExistsError, _io.FileIO, filename, 'x') |
| 304 |
|
| 305 |
def test_non_inheritable(self): |
| 306 |
import _io |
| 307 |
os = self.posix |
| 308 |
f = _io.FileIO(self.tmpfile, 'r') |
| 309 |
assert os.get_inheritable(f.fileno()) == False |
| 310 |
f.close() |
| 311 |
|
| 312 |
def test_FileIO_fd_does_not_change_inheritable(self): |
| 313 |
import _io |
| 314 |
os = self.posix |
| 315 |
fd1, fd2 = os.pipe() |
| 316 |
os.set_inheritable(fd1, True) |
| 317 |
os.set_inheritable(fd2, False) |
| 318 |
f1 = _io.FileIO(fd1, 'r') |
| 319 |
f2 = _io.FileIO(fd2, 'w') |
| 320 |
assert os.get_inheritable(fd1) == True |
| 321 |
assert os.get_inheritable(fd2) == False |
| 322 |
f1.close() |
| 323 |
f2.close() |
| 324 |
|
| 325 |
def test_close_upon_reinit(self): |
| 326 |
import _io |
| 327 |
os = self.posix |
| 328 |
f = _io.FileIO(self.tmpfile, 'r') |
| 329 |
fd1 = f.fileno() |
| 330 |
f.__init__(self.tmpfile, 'w') |
| 331 |
fd2 = f.fileno() |
| 332 |
if fd1 != fd2: |
| 333 |
raises(OSError, os.close, fd1) |
| 334 |
|
| 335 |
def test_opener_negative(self): |
| 336 |
import _io |
| 337 |
def opener(*args): |
| 338 |
return -1 |
| 339 |
raises(ValueError, _io.FileIO, "foo", 'r', opener=opener) |
| 340 |
|
| 341 |
def test_seek_bom(self): |
| 342 |
|
| 343 |
import _io |
| 344 |
filename = self.tmpfile + '_x3' |
| 345 |
for charset in ('utf-8-sig', 'utf-16', 'utf-32'): |
| 346 |
with _io.open(filename, 'w', encoding=charset) as f: |
| 347 |
f.write('aaa') |
| 348 |
pos = f.tell() |
| 349 |
with _io.open(filename, 'r+', encoding=charset) as f: |
| 350 |
f.seek(pos) |
| 351 |
f.write('zzz') |
| 352 |
f.seek(0) |
| 353 |
f.write('bbb') |
| 354 |
with _io.open(filename, 'rb') as f: |
| 355 |
assert f.read() == 'bbbzzz'.encode(charset) |
| 356 |
|
| 357 |
def test_seek_append_bom(self): |
| 358 |
|
| 359 |
import _io, os |
| 360 |
filename = self.tmpfile + '_x3' |
| 361 |
for charset in ('utf-8-sig', 'utf-16', 'utf-32'): |
| 362 |
with _io.open(filename, 'w', encoding=charset) as f: |
| 363 |
f.write('aaa') |
| 364 |
with _io.open(filename, 'a', encoding=charset) as f: |
| 365 |
f.seek(0) |
| 366 |
f.seek(0, os.SEEK_END) |
| 367 |
f.write('xxx') |
| 368 |
with _io.open(filename, 'rb') as f: |
| 369 |
assert f.read() == 'aaaxxx'.encode(charset) |
| 370 |
|
| 371 |
|
| 372 |
def test_flush_at_exit(): |
| 373 |
from pypy import conftest |
| 374 |
from pypy.tool.option import make_config, make_objspace |
| 375 |
from rpython.tool.udir import udir |
| 376 |
|
| 377 |
tmpfile = udir.join('test_flush_at_exit') |
| 378 |
config = make_config(conftest.option) |
| 379 |
space = make_objspace(config) |
| 380 |
space.appexec([space.wrap(str(tmpfile))], """(tmpfile): |
| 381 |
import io |
| 382 |
f = io.open(tmpfile, 'w', encoding='ascii') |
| 383 |
f.write(u'42') |
| 384 |
# no flush() and no close() |
| 385 |
import sys; sys._keepalivesomewhereobscure = f |
| 386 |
""") |
| 387 |
space.finish() |
| 388 |
assert tmpfile.read() == '42' |
| 389 |
|
| 390 |
|
| 391 |
def test_flush_at_exit_IOError_and_ValueError(): |
| 392 |
from pypy import conftest |
| 393 |
from pypy.tool.option import make_config, make_objspace |
| 394 |
|
| 395 |
config = make_config(conftest.option) |
| 396 |
space = make_objspace(config) |
| 397 |
space.appexec([], """(): |
| 398 |
import io |
| 399 |
class MyStream(io.IOBase): |
| 400 |
def flush(self): |
| 401 |
raise IOError |
| 402 |
|
| 403 |
class MyStream2(io.IOBase): |
| 404 |
def flush(self): |
| 405 |
raise ValueError |
| 406 |
|
| 407 |
s = MyStream() |
| 408 |
s2 = MyStream2() |
| 409 |
import sys; sys._keepalivesomewhereobscure = s |
| 410 |
""") |
| 411 |
space.finish() |
| 412 |
|
| 413 |
def monkeypatch_may_ignore_finalize(monkeypatch): |
| 414 |
from rpython.rlib import rgc |
| 415 |
orig_func = rgc.may_ignore_finalizer |
| 416 |
|
| 417 |
calls = [] |
| 418 |
def may_ignore_finalizer(obj): |
| 419 |
calls.append(obj) |
| 420 |
return orig_func(obj) |
| 421 |
|
| 422 |
monkeypatch.setattr(rgc, "may_ignore_finalizer", may_ignore_finalizer) |
| 423 |
return calls |
| 424 |
|
| 425 |
def test_close_unregisters_finalizer(space, monkeypatch): |
| 426 |
from pypy.module._io.interp_iobase import W_IOBase |
| 427 |
calls = monkeypatch_may_ignore_finalize(monkeypatch) |
| 428 |
|
| 429 |
w_base = W_IOBase(space, add_to_autoflusher=False) |
| 430 |
w_base.close_w(space) |
| 431 |
assert calls == [w_base] |
| 432 |
|
| 433 |
w_base.close_w(space) |
| 434 |
assert calls == [w_base] |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
w_s = space.appexec([], """(): |
| 439 |
import _io |
| 440 |
class SubclassWithDel(_io._IOBase): |
| 441 |
pass |
| 442 |
s = SubclassWithDel() |
| 443 |
s.close() |
| 444 |
return s |
| 445 |
""") |
| 446 |
|
| 447 |
assert w_s not in calls |
| 448 |
|
| 449 |
|
| 450 |
def test_close_unregisters_finalizer_fileio(space, monkeypatch): |
| 451 |
from pypy.module._io.interp_fileio import W_FileIO |
| 452 |
calls = monkeypatch_may_ignore_finalize(monkeypatch) |
| 453 |
|
| 454 |
w_base = W_FileIO(space) |
| 455 |
w_base.close_w(space) |
| 456 |
assert calls == [w_base] |
| 457 |
|
| 458 |
w_base.close_w(space) |
| 459 |
assert calls == [w_base] |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
w_s = space.appexec([space.newtext(__file__)], """(fn): |
| 464 |
import _io |
| 465 |
class SubclassWithDel(_io.FileIO): |
| 466 |
pass |
| 467 |
s = SubclassWithDel(fn) |
| 468 |
s.close() |
| 469 |
return s |
| 470 |
""") |
| 471 |
|
| 472 |
assert w_s not in calls |
| 473 |
|
| 474 |
|
| 475 |
def test_close_unregisters_finalizer_open(space, monkeypatch): |
| 476 |
from rpython.rlib import rgc |
| 477 |
calls = monkeypatch_may_ignore_finalize(monkeypatch) |
| 478 |
|
| 479 |
w_f = space.appexec([space.newtext(__file__)], """(fn): |
| 480 |
f = open(fn, "r", encoding="utf-8") |
| 481 |
f.close() |
| 482 |
return f |
| 483 |
""") |
| 484 |
|
| 485 |
assert w_f in calls |
| 486 |
assert w_f.w_buffer in calls |
| 487 |
assert w_f.w_buffer.w_raw in calls |