| 1 |
|
| 2 |
import _io |
| 3 |
import array |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
@pytest.fixture |
| 8 |
def tempfile(tmpdir): |
| 9 |
tempfile = (tmpdir / 'tempfile').ensure() |
| 10 |
return str(tempfile) |
| 11 |
|
| 12 |
def test_iobase_overriding(): |
| 13 |
class WithIter(_io._IOBase): |
| 14 |
def __iter__(self): |
| 15 |
yield 'foo' |
| 16 |
assert WithIter().readlines() == ['foo'] |
| 17 |
assert WithIter().readlines(1) == ['foo'] |
| 18 |
|
| 19 |
class WithNext(_io._IOBase): |
| 20 |
def __next__(self): |
| 21 |
raise StopIteration |
| 22 |
assert WithNext().readlines() == [] |
| 23 |
assert WithNext().readlines(1) == [] |
| 24 |
|
| 25 |
def test_openclose(): |
| 26 |
with _io._BufferedIOBase() as f: |
| 27 |
assert not f.closed |
| 28 |
f._checkClosed() |
| 29 |
assert f.closed |
| 30 |
with pytest.raises(ValueError): |
| 31 |
f._checkClosed() |
| 32 |
|
| 33 |
def test_iter(): |
| 34 |
class MyFile(_io._IOBase): |
| 35 |
def __init__(self): |
| 36 |
self.lineno = 0 |
| 37 |
def readline(self): |
| 38 |
self.lineno += 1 |
| 39 |
if self.lineno == 1: |
| 40 |
return "line1" |
| 41 |
elif self.lineno == 2: |
| 42 |
return "line2" |
| 43 |
return "" |
| 44 |
|
| 45 |
assert list(MyFile()) == ["line1", "line2"] |
| 46 |
|
| 47 |
def test_exception(): |
| 48 |
e = _io.UnsupportedOperation("seek") |
| 49 |
|
| 50 |
def test_default_implementations(): |
| 51 |
file = _io._IOBase() |
| 52 |
raises(_io.UnsupportedOperation, file.seek, 0, 1) |
| 53 |
raises(_io.UnsupportedOperation, file.fileno) |
| 54 |
raises(_io.UnsupportedOperation, file.truncate) |
| 55 |
|
| 56 |
def test_blockingerror(): |
| 57 |
try: |
| 58 |
raise _io.BlockingIOError(42, "test blocking", 123) |
| 59 |
except OSError as e: |
| 60 |
assert isinstance(e, _io.BlockingIOError) |
| 61 |
assert e.errno == 42 |
| 62 |
assert e.strerror == "test blocking" |
| 63 |
assert e.characters_written == 123 |
| 64 |
|
| 65 |
def test_dict(): |
| 66 |
f = _io.BytesIO() |
| 67 |
f.x = 42 |
| 68 |
assert f.x == 42 |
| 69 |
|
| 70 |
def write(data): |
| 71 |
try: |
| 72 |
data = data.tobytes().upper() |
| 73 |
except AttributeError: |
| 74 |
data = data.upper() |
| 75 |
return _io.BytesIO.write(f, data) |
| 76 |
f.write = write |
| 77 |
bufio = _io.BufferedWriter(f) |
| 78 |
bufio.write(b"abc") |
| 79 |
bufio.flush() |
| 80 |
assert f.getvalue() == b"ABC" |
| 81 |
|
| 82 |
def test_destructor_1(): |
| 83 |
record = [] |
| 84 |
class MyIO(_io._IOBase): |
| 85 |
def __del__(self): |
| 86 |
record.append(1) |
| 87 |
|
| 88 |
def close(self): |
| 89 |
record.append(2) |
| 90 |
super(MyIO, self).close() |
| 91 |
def flush(self): |
| 92 |
record.append(3) |
| 93 |
super(MyIO, self).flush() |
| 94 |
MyIO() |
| 95 |
import gc; gc.collect() |
| 96 |
assert record == [1] |
| 97 |
|
| 98 |
def test_destructor_2(): |
| 99 |
record = [] |
| 100 |
class MyIO(_io._IOBase): |
| 101 |
def __del__(self): |
| 102 |
record.append(1) |
| 103 |
super(MyIO, self).__del__() |
| 104 |
def close(self): |
| 105 |
record.append(2) |
| 106 |
super(MyIO, self).close() |
| 107 |
def flush(self): |
| 108 |
record.append(3) |
| 109 |
super(MyIO, self).flush() |
| 110 |
MyIO() |
| 111 |
import gc; gc.collect() |
| 112 |
assert record == [1, 2, 3] |
| 113 |
|
| 114 |
def test_tell(): |
| 115 |
class MyIO(_io._IOBase): |
| 116 |
def seek(self, pos, whence=0): |
| 117 |
return 42 |
| 118 |
assert MyIO().tell() == 42 |
| 119 |
|
| 120 |
def test_weakref(): |
| 121 |
import weakref |
| 122 |
f = _io.BytesIO() |
| 123 |
ref = weakref.ref(f) |
| 124 |
assert ref() is f |
| 125 |
|
| 126 |
def test_rawio_read(): |
| 127 |
class MockRawIO(_io._RawIOBase): |
| 128 |
stack = [b'abc', b'de', b''] |
| 129 |
def readinto(self, buf): |
| 130 |
data = self.stack.pop(0) |
| 131 |
buf[:len(data)] = data |
| 132 |
return len(data) |
| 133 |
assert MockRawIO().read() == b'abcde' |
| 134 |
|
| 135 |
def test_rawio_read_pieces(): |
| 136 |
class MockRawIO(_io._RawIOBase): |
| 137 |
stack = [b'abc', b'de', None, b'fg', b''] |
| 138 |
def readinto(self, buf): |
| 139 |
data = self.stack.pop(0) |
| 140 |
if data is None: |
| 141 |
return None |
| 142 |
if len(data) <= len(buf): |
| 143 |
buf[:len(data)] = data |
| 144 |
return len(data) |
| 145 |
else: |
| 146 |
buf[:] = data[:len(buf)] |
| 147 |
self.stack.insert(0, data[len(buf):]) |
| 148 |
return len(buf) |
| 149 |
r = MockRawIO() |
| 150 |
assert r.read(2) == b'ab' |
| 151 |
assert r.read(2) == b'c' |
| 152 |
assert r.read(2) == b'de' |
| 153 |
assert r.read(2) is None |
| 154 |
assert r.read(2) == b'fg' |
| 155 |
assert r.read(2) == b'' |
| 156 |
|
| 157 |
def test_rawio_readall_none(): |
| 158 |
class MockRawIO(_io._RawIOBase): |
| 159 |
read_stack = [None, None, b"a"] |
| 160 |
def readinto(self, buf): |
| 161 |
v = self.read_stack.pop() |
| 162 |
if v is None: |
| 163 |
return v |
| 164 |
buf[:len(v)] = v |
| 165 |
return len(v) |
| 166 |
|
| 167 |
r = MockRawIO() |
| 168 |
s = r.readall() |
| 169 |
assert s == b"a" |
| 170 |
s = r.readall() |
| 171 |
assert s is None |
| 172 |
|
| 173 |
def test_open(tempfile): |
| 174 |
f = _io.open(tempfile, "rb") |
| 175 |
assert f.name.endswith('tempfile') |
| 176 |
assert f.mode == 'rb' |
| 177 |
f.close() |
| 178 |
|
| 179 |
with _io.open(tempfile, "rt") as f: |
| 180 |
assert f.mode == "rt" |
| 181 |
|
| 182 |
def test_seek_basic(tempfile): |
| 183 |
f = _io.open(tempfile, "wb") |
| 184 |
f.write(b"abcdefghijklmnopqrstu") |
| 185 |
f.close() |
| 186 |
f = _io.open(tempfile, "rb") |
| 187 |
assert f.read(1) == b"a" |
| 188 |
assert f.seek(3, 1) == 4 |
| 189 |
s = f.seek(3, 0) |
| 190 |
assert f.tell() == 3 |
| 191 |
assert s == 3 |
| 192 |
assert f.read(1) == b"d" |
| 193 |
assert f.seek(3, 1) == 7 |
| 194 |
assert f.seek(3, 1) == 10 |
| 195 |
assert f.read(1) == b"k" |
| 196 |
|
| 197 |
|
| 198 |
def test_open_writable(tempfile): |
| 199 |
f = _io.open(tempfile, "w+b") |
| 200 |
f.close() |
| 201 |
|
| 202 |
def test_valid_mode(tempfile): |
| 203 |
raises(ValueError, _io.open, tempfile, "ww") |
| 204 |
raises(ValueError, _io.open, tempfile, "rwa") |
| 205 |
raises(ValueError, _io.open, tempfile, "b", newline="\n") |
| 206 |
raises(ValueError, _io.open, tempfile, "U+") |
| 207 |
raises(ValueError, _io.open, tempfile, "xU") |
| 208 |
|
| 209 |
def test_array_write(tempfile): |
| 210 |
a = array.array('i', range(10)) |
| 211 |
n = len(a.tobytes()) |
| 212 |
with _io.open(tempfile, "wb", 0) as f: |
| 213 |
res = f.write(a) |
| 214 |
assert res == n |
| 215 |
|
| 216 |
with _io.open(tempfile, "wb") as f: |
| 217 |
res = f.write(a) |
| 218 |
assert res == n |
| 219 |
|
| 220 |
def test_attributes(tempfile): |
| 221 |
import warnings |
| 222 |
with _io.open(tempfile, "wb", buffering=0) as f: |
| 223 |
assert f.mode == "wb" |
| 224 |
|
| 225 |
with _io.open(tempfile, "w+") as f: |
| 226 |
assert f.mode == "w+" |
| 227 |
assert f.buffer.mode == "rb+" |
| 228 |
assert f.buffer.raw.mode == "rb+" |
| 229 |
|
| 230 |
with _io.open(f.fileno(), "wb", closefd=False) as g: |
| 231 |
assert g.mode == "wb" |
| 232 |
assert g.raw.mode == "wb" |
| 233 |
assert g.name == f.fileno() |
| 234 |
assert g.raw.name == f.fileno() |
| 235 |
|
| 236 |
def test_buffer_warning(tempfile): |
| 237 |
import warnings |
| 238 |
|
| 239 |
with warnings.catch_warnings(record=True) as collector: |
| 240 |
warnings.simplefilter("always") |
| 241 |
with _io.open(tempfile, "wb", buffering=1) as f: |
| 242 |
pass |
| 243 |
|
| 244 |
assert len(collector) == 1 |
| 245 |
assert isinstance(collector[0].message, RuntimeWarning) |
| 246 |
|
| 247 |
def test_opener(tempfile): |
| 248 |
import os |
| 249 |
with _io.open(tempfile, "w") as f: |
| 250 |
f.write("egg\n") |
| 251 |
fd = os.open(tempfile, os.O_RDONLY) |
| 252 |
def opener(path, flags): |
| 253 |
return fd |
| 254 |
with _io.open("non-existent", "r", opener=opener) as f: |
| 255 |
assert f.read() == "egg\n" |
| 256 |
|
| 257 |
def test_seek_and_tell(tempfile): |
| 258 |
with _io.open(tempfile, "wb") as f: |
| 259 |
f.write(b"abcd") |
| 260 |
|
| 261 |
with _io.open(tempfile) as f: |
| 262 |
decoded = f.read() |
| 263 |
|
| 264 |
|
| 265 |
for i in range(len(decoded) + 1): |
| 266 |
|
| 267 |
for j in [1, 5, len(decoded) - i]: |
| 268 |
with _io.open(tempfile) as f: |
| 269 |
res = f.read(i) |
| 270 |
assert res == decoded[:i] |
| 271 |
cookie = f.tell() |
| 272 |
res = f.read(j) |
| 273 |
assert res == decoded[i:i + j] |
| 274 |
f.seek(cookie) |
| 275 |
res = f.read() |
| 276 |
assert res == decoded[i:] |
| 277 |
|
| 278 |
def test_telling(tempfile): |
| 279 |
with _io.open(tempfile, "w+", encoding="utf8") as f: |
| 280 |
p0 = f.tell() |
| 281 |
f.write("\xff\n") |
| 282 |
p1 = f.tell() |
| 283 |
f.write("\xff\n") |
| 284 |
p2 = f.tell() |
| 285 |
f.seek(0) |
| 286 |
assert p0 == 0 |
| 287 |
assert p1 == 3 |
| 288 |
assert p2 == 6 |
| 289 |
|
| 290 |
assert f.tell() == p0 |
| 291 |
res = f.readline() |
| 292 |
assert res == "\xff\n" |
| 293 |
assert f.tell() == p1 |
| 294 |
res = f.readline() |
| 295 |
assert res == "\xff\n" |
| 296 |
assert f.tell() == p2 |
| 297 |
f.seek(0) |
| 298 |
|
| 299 |
for line in f: |
| 300 |
assert line == "\xff\n" |
| 301 |
raises(IOError, f.tell) |
| 302 |
assert f.tell() == p2 |
| 303 |
|
| 304 |
def test_chunk_size(tempfile): |
| 305 |
with _io.open(tempfile) as f: |
| 306 |
assert f._CHUNK_SIZE >= 1 |
| 307 |
f._CHUNK_SIZE = 4096 |
| 308 |
assert f._CHUNK_SIZE == 4096 |
| 309 |
raises(ValueError, setattr, f, "_CHUNK_SIZE", 0) |
| 310 |
|
| 311 |
def test_truncate(tempfile): |
| 312 |
with _io.open(tempfile, "w+") as f: |
| 313 |
f.write("abc") |
| 314 |
|
| 315 |
with _io.open(tempfile, "w+") as f: |
| 316 |
f.truncate() |
| 317 |
|
| 318 |
with _io.open(tempfile, "r+") as f: |
| 319 |
res = f.read() |
| 320 |
assert res == "" |
| 321 |
|
| 322 |
def test_errors_property(tempfile): |
| 323 |
with _io.open(tempfile, "w") as f: |
| 324 |
assert f.errors == "strict" |
| 325 |
with _io.open(tempfile, "w", errors="replace") as f: |
| 326 |
assert f.errors == "replace" |
| 327 |
|
| 328 |
with raises(TypeError): |
| 329 |
_io.open(tempfile, "w", errors=3) |
| 330 |
|
| 331 |
def test_append_bom(tempfile): |
| 332 |
|
| 333 |
for charset in ["utf-8-sig", "utf-16", "utf-32"]: |
| 334 |
with _io.open(tempfile, "w", encoding=charset) as f: |
| 335 |
f.write("aaa") |
| 336 |
pos = f.tell() |
| 337 |
with _io.open(tempfile, "rb") as f: |
| 338 |
res = f.read() |
| 339 |
assert res == "aaa".encode(charset) |
| 340 |
with _io.open(tempfile, "a", encoding=charset) as f: |
| 341 |
f.write("xxx") |
| 342 |
with _io.open(tempfile, "rb") as f: |
| 343 |
res = f.read() |
| 344 |
assert res == "aaaxxx".encode(charset) |
| 345 |
|
| 346 |
def test_newlines_attr(tempfile): |
| 347 |
with _io.open(tempfile, "r") as f: |
| 348 |
assert f.newlines is None |
| 349 |
|
| 350 |
with _io.open(tempfile, "wb") as f: |
| 351 |
f.write(b"hello\nworld\n") |
| 352 |
|
| 353 |
with _io.open(tempfile, "r") as f: |
| 354 |
res = f.readline() |
| 355 |
assert res == "hello\n" |
| 356 |
res = f.readline() |
| 357 |
assert res == "world\n" |
| 358 |
assert f.newlines == "\n" |
| 359 |
assert type(f.newlines) is str |
| 360 |
|
| 361 |
def _check_warn_on_dealloc(*args, **kwargs): |
| 362 |
import gc |
| 363 |
import warnings |
| 364 |
|
| 365 |
f = open(*args, **kwargs) |
| 366 |
r = repr(f) |
| 367 |
gc.collect() |
| 368 |
with warnings.catch_warnings(record=True) as w: |
| 369 |
warnings.simplefilter('always') |
| 370 |
f = None |
| 371 |
gc.collect() |
| 372 |
assert len(w) == 1, len(w) |
| 373 |
assert r in str(w[0]) |
| 374 |
|
| 375 |
def test_warn_on_dealloc(tempfile): |
| 376 |
_check_warn_on_dealloc(tempfile, 'wb', buffering=0) |
| 377 |
_check_warn_on_dealloc(tempfile, 'wb') |
| 378 |
_check_warn_on_dealloc(tempfile, 'w') |
| 379 |
|
| 380 |
def test_pickling(tempfile): |
| 381 |
import pickle |
| 382 |
|
| 383 |
for kwargs in [ |
| 384 |
{"mode": "w"}, |
| 385 |
{"mode": "wb"}, |
| 386 |
{"mode": "wb", "buffering": 0}, |
| 387 |
{"mode": "r"}, |
| 388 |
{"mode": "rb"}, |
| 389 |
{"mode": "rb", "buffering": 0}, |
| 390 |
{"mode": "w+"}, |
| 391 |
{"mode": "w+b"}, |
| 392 |
{"mode": "w+b", "buffering": 0}, |
| 393 |
]: |
| 394 |
for protocol in range(pickle.HIGHEST_PROTOCOL + 1): |
| 395 |
with _io.open(tempfile, **kwargs) as f: |
| 396 |
raises(TypeError, pickle.dumps, f, protocol) |
| 397 |
|
| 398 |
def test_mod(): |
| 399 |
typemods = dict((t, t.__module__) for name, t in vars(_io).items() |
| 400 |
if isinstance(t, type) and name != '__loader__') |
| 401 |
for t, mod in typemods.items(): |
| 402 |
if t is _io.BlockingIOError: |
| 403 |
assert mod == 'builtins' |
| 404 |
elif t is _io.UnsupportedOperation: |
| 405 |
assert mod == 'io' |
| 406 |
else: |
| 407 |
assert mod == '_io' |
| 408 |
|
| 409 |
def test_issue1902(tempfile): |
| 410 |
with _io.open(tempfile, 'w+b', 4096) as f: |
| 411 |
f.write(b'\xff' * 13569) |
| 412 |
f.flush() |
| 413 |
f.seek(0, 0) |
| 414 |
f.read(1) |
| 415 |
f.seek(-1, 1) |
| 416 |
f.write(b'') |
| 417 |
|
| 418 |
def test_issue1902_2(tempfile): |
| 419 |
with _io.open(tempfile, 'w+b', 4096) as f: |
| 420 |
f.write(b'\xff' * 13569) |
| 421 |
f.flush() |
| 422 |
f.seek(0, 0) |
| 423 |
|
| 424 |
f.read(1) |
| 425 |
f.seek(-1, 1) |
| 426 |
f.write(b'\xff') |
| 427 |
f.seek(1, 0) |
| 428 |
f.read(4123) |
| 429 |
f.seek(-4123, 1) |
| 430 |
|
| 431 |
def test_issue1902_3(tempfile): |
| 432 |
buffer_size = 4096 |
| 433 |
with _io.open(tempfile, 'w+b', buffer_size) as f: |
| 434 |
f.write(b'\xff' * buffer_size * 3) |
| 435 |
f.flush() |
| 436 |
f.seek(0, 0) |
| 437 |
|
| 438 |
f.read(1) |
| 439 |
f.seek(-1, 1) |
| 440 |
f.write(b'\xff') |
| 441 |
f.seek(1, 0) |
| 442 |
f.read(buffer_size * 2) |
| 443 |
assert f.tell() == 1 + buffer_size * 2 |
| 444 |
|
| 445 |
def test_open_exclusive(tempfile): |
| 446 |
|
| 447 |
FileExistsError = OSError |
| 448 |
|
| 449 |
filename = tempfile + '_x2' |
| 450 |
raises(ValueError, _io.open, filename, 'xw') |
| 451 |
with _io.open(filename, 'x') as f: |
| 452 |
assert f.mode == 'x' |
| 453 |
raises(FileExistsError, _io.open, filename, 'x') |
| 454 |
|
| 455 |
def test_nonbuffered_textio(tempfile): |
| 456 |
import warnings |
| 457 |
filename = tempfile + '_x2' |
| 458 |
warnings.simplefilter("always", category=ResourceWarning) |
| 459 |
with warnings.catch_warnings(record=True) as recorded: |
| 460 |
raises(ValueError, _io.open, filename, 'w', buffering=0) |
| 461 |
assert recorded == [] |
| 462 |
|
| 463 |
def test_invalid_newline(tempfile): |
| 464 |
import warnings |
| 465 |
filename = tempfile + '_x2' |
| 466 |
warnings.simplefilter("always", category=ResourceWarning) |
| 467 |
with warnings.catch_warnings(record=True) as recorded: |
| 468 |
raises(ValueError, _io.open, filename, 'w', newline='invalid') |
| 469 |
assert recorded == [] |
| 470 |
|
| 471 |
|
| 472 |
def test_io_after_close(tempfile): |
| 473 |
for kwargs in [ |
| 474 |
{"mode": "w"}, |
| 475 |
{"mode": "wb"}, |
| 476 |
{"mode": "w", "buffering": 1}, |
| 477 |
{"mode": "w", "buffering": 2}, |
| 478 |
{"mode": "wb", "buffering": 0}, |
| 479 |
{"mode": "r"}, |
| 480 |
{"mode": "rb"}, |
| 481 |
{"mode": "r", "buffering": 1}, |
| 482 |
{"mode": "r", "buffering": 2}, |
| 483 |
{"mode": "rb", "buffering": 0}, |
| 484 |
{"mode": "w+"}, |
| 485 |
{"mode": "w+b"}, |
| 486 |
{"mode": "w+", "buffering": 1}, |
| 487 |
{"mode": "w+", "buffering": 2}, |
| 488 |
{"mode": "w+b", "buffering": 0}, |
| 489 |
]: |
| 490 |
if "b" not in kwargs["mode"]: |
| 491 |
kwargs["encoding"] = "ascii" |
| 492 |
f = _io.open(tempfile, **kwargs) |
| 493 |
f.close() |
| 494 |
raises(ValueError, f.flush) |
| 495 |
raises(ValueError, f.fileno) |
| 496 |
raises(ValueError, f.isatty) |
| 497 |
raises(ValueError, f.__iter__) |
| 498 |
if hasattr(f, "peek"): |
| 499 |
raises(ValueError, f.peek, 1) |
| 500 |
raises(ValueError, f.read) |
| 501 |
if hasattr(f, "read1"): |
| 502 |
raises(ValueError, f.read1, 1024) |
| 503 |
if hasattr(f, "readall"): |
| 504 |
raises(ValueError, f.readall) |
| 505 |
if hasattr(f, "readinto"): |
| 506 |
raises(ValueError, f.readinto, bytearray(1024)) |
| 507 |
raises(ValueError, f.readline) |
| 508 |
raises(ValueError, f.readlines) |
| 509 |
raises(ValueError, f.seek, 0) |
| 510 |
raises(ValueError, f.tell) |
| 511 |
raises(ValueError, f.truncate) |
| 512 |
raises(ValueError, f.write, b"" if "b" in kwargs['mode'] else u"") |
| 513 |
raises(ValueError, f.writelines, []) |
| 514 |
raises(ValueError, next, f) |
| 515 |
|
| 516 |
def test_bug_newlines_rr_at_end(): |
| 517 |
import io |
| 518 |
lines = io.TextIOWrapper(io.BytesIO(b"\r\r"), newline="").readlines() |
| 519 |
assert lines == [u'\r', u'\r'] |
| 520 |
lines = io.TextIOWrapper(io.BytesIO(b"\r\r\r\r"), newline="").readlines() |
| 521 |
assert lines == [u'\r', u'\r', u'\r', u'\r'] |
| 522 |
lines = io.TextIOWrapper(io.BytesIO(b"a\r\rb\r\r"), newline="").readlines() |
| 523 |
assert lines == [u'a\r', u'\r', u'b\r', u'\r'] |
| 524 |
lines = io.TextIOWrapper(io.BytesIO(b"a\r\rb\r\rc"), newline="").readlines() |
| 525 |
assert lines == [u'a\r', u'\r', u'b\r', u'\r', u'c'] |
| 526 |
lines = io.TextIOWrapper(io.BytesIO(b"a\r\rb\r\n"), newline="").readlines() |
| 527 |
assert lines == [u'a\r', u'\r', u'b\r\n'] |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
FATX = 'x' * (2**14) |
| 532 |
|
| 533 |
DATA_TEMPLATE = [ |
| 534 |
"line1=1", |
| 535 |
"line2='this is a very long line designed to go past any default " + |
| 536 |
"buffer limits that exist in io.py but we also want to test " + |
| 537 |
"the uncommon case, naturally.'", |
| 538 |
"def line3():pass", |
| 539 |
"line4 = '%s'" % FATX, |
| 540 |
] |
| 541 |
|
| 542 |
DATA_CRLF = "\r\n".join(DATA_TEMPLATE) + "\r\n" |
| 543 |
|
| 544 |
|
| 545 |
def test_tell_univnewlines(tempfile): |
| 546 |
import io |
| 547 |
import sys |
| 548 |
|
| 549 |
NEWLINE = '\r\n' |
| 550 |
|
| 551 |
with io.open(tempfile, "w") as fp: |
| 552 |
fp.write(DATA_CRLF) |
| 553 |
|
| 554 |
with io.open(tempfile, "r") as fp: |
| 555 |
assert repr(fp.newlines) == repr(None) |
| 556 |
data = fp.readline() |
| 557 |
pos = fp.tell() |
| 558 |
if "__pypy__" in sys.modules: |
| 559 |
assert repr(fp.newlines) == repr(NEWLINE) |
| 560 |
|
| 561 |
def test_tell_various(): |
| 562 |
|
| 563 |
import io |
| 564 |
for t in (b'\r', u'\x80\n'.encode('utf-8')): |
| 565 |
b = io.BytesIO(t) |
| 566 |
w = io.TextIOWrapper(b, encoding='utf-8') |
| 567 |
res1 = w.read() |
| 568 |
tell = w.tell() |
| 569 |
|
| 570 |
|
| 571 |
w.seek(0) |
| 572 |
w.seek(tell) |
| 573 |
res2 = w.read() |
| 574 |
assert len(res2) == 0 |
| 575 |
assert res1.encode('utf8') == t.replace(b'\r', b'\n') |
| 576 |
|
| 577 |
def test_seek_with_encoder_state(): |
| 578 |
|
| 579 |
import io |
| 580 |
import encodings |
| 581 |
b = io.BytesIO(b"") |
| 582 |
w = io.TextIOWrapper(b, encoding='euc_jis_2004') |
| 583 |
w.write(u"\u00e6\u0300") |
| 584 |
p0 = w.tell() |
| 585 |
w.write(u"\u00e6") |
| 586 |
p1_ = w.seek(p0, 0) |
| 587 |
p1 = w.tell() |
| 588 |
assert p1_ == p1 |
| 589 |
w.write(u"\u0300") |
| 590 |
|
| 591 |
p2 = w.seek(0, 0) |
| 592 |
assert p2 == 0 |
| 593 |
out = w.readline() |
| 594 |
expected = u"\u00e6\u0300\u0300" |
| 595 |
|
| 596 |
|
| 597 |
assert out == expected |