File: apptest_io.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (597 lines) | stat: -rw-r--r-- 17,503 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
# spaceconfig = {"usemodules" : ["_locale", "array", "struct", "_multibytecodec"]}
import _io
import array

import pytest

@pytest.fixture
def tempfile(tmpdir):
    tempfile = (tmpdir / 'tempfile').ensure()
    return str(tempfile)

def test_iobase_overriding():
    class WithIter(_io._IOBase):
        def __iter__(self):
            yield 'foo'
    assert WithIter().readlines() == ['foo']
    assert WithIter().readlines(1) == ['foo']

    class WithNext(_io._IOBase):
        def __next__(self):
            raise StopIteration
    assert WithNext().readlines() == []
    assert WithNext().readlines(1) == []

def test_openclose():
    with _io._BufferedIOBase() as f:
        assert not f.closed
        f._checkClosed()
    assert f.closed
    with pytest.raises(ValueError):
        f._checkClosed()

def test_iter():
    class MyFile(_io._IOBase):
        def __init__(self):
            self.lineno = 0
        def readline(self):
            self.lineno += 1
            if self.lineno == 1:
                return "line1"
            elif self.lineno == 2:
                return "line2"
            return ""

    assert list(MyFile()) == ["line1", "line2"]

def test_exception():
    e = _io.UnsupportedOperation("seek")

def test_default_implementations():
    file = _io._IOBase()
    raises(_io.UnsupportedOperation, file.seek, 0, 1)
    raises(_io.UnsupportedOperation, file.fileno)
    raises(_io.UnsupportedOperation, file.truncate)

def test_blockingerror():
    try:
        raise _io.BlockingIOError(42, "test blocking", 123)
    except OSError as e:
        assert isinstance(e, _io.BlockingIOError)
        assert e.errno == 42
        assert e.strerror == "test blocking"
        assert e.characters_written == 123

def test_dict():
    f = _io.BytesIO()
    f.x = 42
    assert f.x == 42
    #
    def write(data):
        try:
            data = data.tobytes().upper()
        except AttributeError:
            data = data.upper()
        return _io.BytesIO.write(f, data)
    f.write = write
    bufio = _io.BufferedWriter(f)
    bufio.write(b"abc")
    bufio.flush()
    assert f.getvalue() == b"ABC"

def test_destructor_1():
    record = []
    class MyIO(_io._IOBase):
        def __del__(self):
            record.append(1)
            # doesn't call the inherited __del__, so file not closed
        def close(self):
            record.append(2)
            super(MyIO, self).close()
        def flush(self):
            record.append(3)
            super(MyIO, self).flush()
    MyIO()
    import gc; gc.collect()
    assert record == [1]

def test_destructor_2():
    record = []
    class MyIO(_io._IOBase):
        def __del__(self):
            record.append(1)
            super(MyIO, self).__del__()
        def close(self):
            record.append(2)
            super(MyIO, self).close()
        def flush(self):
            record.append(3)
            super(MyIO, self).flush()
    MyIO()
    import gc; gc.collect()
    assert record == [1, 2, 3]

def test_tell():
    class MyIO(_io._IOBase):
        def seek(self, pos, whence=0):
            return 42
    assert MyIO().tell() == 42

def test_weakref():
    import weakref
    f = _io.BytesIO()
    ref = weakref.ref(f)
    assert ref() is f

def test_rawio_read():
    class MockRawIO(_io._RawIOBase):
        stack = [b'abc', b'de', b'']
        def readinto(self, buf):
            data = self.stack.pop(0)
            buf[:len(data)] = data
            return len(data)
    assert MockRawIO().read() == b'abcde'

def test_rawio_read_pieces():
    class MockRawIO(_io._RawIOBase):
        stack = [b'abc', b'de', None, b'fg', b'']
        def readinto(self, buf):
            data = self.stack.pop(0)
            if data is None:
                return None
            if len(data) <= len(buf):
                buf[:len(data)] = data
                return len(data)
            else:
                buf[:] = data[:len(buf)]
                self.stack.insert(0, data[len(buf):])
                return len(buf)
    r = MockRawIO()
    assert r.read(2) == b'ab'
    assert r.read(2) == b'c'
    assert r.read(2) == b'de'
    assert r.read(2) is None
    assert r.read(2) == b'fg'
    assert r.read(2) == b''

def test_rawio_readall_none():
    class MockRawIO(_io._RawIOBase):
        read_stack = [None, None, b"a"]
        def readinto(self, buf):
            v = self.read_stack.pop()
            if v is None:
                return v
            buf[:len(v)] = v
            return len(v)

    r = MockRawIO()
    s = r.readall()
    assert s == b"a"
    s = r.readall()
    assert s is None

def test_open(tempfile):
    f = _io.open(tempfile, "rb")
    assert f.name.endswith('tempfile')
    assert f.mode == 'rb'
    f.close()

    with _io.open(tempfile, "rt") as f:
        assert f.mode == "rt"

def test_seek_basic(tempfile):
    f = _io.open(tempfile, "wb")
    f.write(b"abcdefghijklmnopqrstu")
    f.close()
    f = _io.open(tempfile, "rb")
    assert f.read(1) == b"a"
    assert f.seek(3, 1) == 4
    s = f.seek(3, 0)
    assert f.tell() == 3
    assert s == 3
    assert f.read(1) == b"d"
    assert f.seek(3, 1) == 7
    assert f.seek(3, 1) == 10
    assert f.read(1) == b"k"
    

def test_open_writable(tempfile):
    f = _io.open(tempfile, "w+b")
    f.close()

def test_valid_mode(tempfile):
    raises(ValueError, _io.open, tempfile, "ww")
    raises(ValueError, _io.open, tempfile, "rwa")
    raises(ValueError, _io.open, tempfile, "b", newline="\n")
    raises(ValueError, _io.open, tempfile, "U+")
    raises(ValueError, _io.open, tempfile, "xU")

def test_array_write(tempfile):
    a = array.array('i', range(10))
    n = len(a.tobytes())
    with _io.open(tempfile, "wb", 0) as f:
        res = f.write(a)
        assert res == n

    with _io.open(tempfile, "wb") as f:
        res = f.write(a)
        assert res == n

def test_attributes(tempfile):
    import warnings
    with _io.open(tempfile, "wb", buffering=0) as f:
        assert f.mode == "wb"

    with _io.open(tempfile, "w+") as f:
        assert f.mode == "w+"
        assert f.buffer.mode == "rb+"
        assert f.buffer.raw.mode == "rb+"

        with _io.open(f.fileno(), "wb", closefd=False) as g:
            assert g.mode == "wb"
            assert g.raw.mode == "wb"
            assert g.name == f.fileno()
            assert g.raw.name == f.fileno()

def test_buffer_warning(tempfile):
    import warnings

    with warnings.catch_warnings(record=True) as collector:
        warnings.simplefilter("always")
        with _io.open(tempfile, "wb", buffering=1) as f:
            pass

    assert len(collector) == 1
    assert isinstance(collector[0].message, RuntimeWarning)

def test_opener(tempfile):
    import os
    with _io.open(tempfile, "w") as f:
        f.write("egg\n")
    fd = os.open(tempfile, os.O_RDONLY)
    def opener(path, flags):
        return fd
    with _io.open("non-existent", "r", opener=opener) as f:
        assert f.read() == "egg\n"

def test_seek_and_tell(tempfile):
    with _io.open(tempfile, "wb") as f:
        f.write(b"abcd")

    with _io.open(tempfile) as f:
        decoded = f.read()

    # seek positions
    for i in range(len(decoded) + 1):
        # read lenghts
        for j in [1, 5, len(decoded) - i]:
            with _io.open(tempfile) as f:
                res = f.read(i)
                assert res == decoded[:i]
                cookie = f.tell()
                res = f.read(j)
                assert res == decoded[i:i + j]
                f.seek(cookie)
                res = f.read()
                assert res == decoded[i:]

def test_telling(tempfile):
    with _io.open(tempfile, "w+", encoding="utf8") as f:
        p0 = f.tell()
        f.write("\xff\n")
        p1 = f.tell()
        f.write("\xff\n")
        p2 = f.tell()
        f.seek(0)
        assert p0 == 0
        assert p1 == 3
        assert p2 == 6

        assert f.tell() == p0
        res = f.readline()
        assert res == "\xff\n"
        assert f.tell() == p1
        res = f.readline()
        assert res == "\xff\n"
        assert f.tell() == p2
        f.seek(0)

        for line in f:
            assert line == "\xff\n"
            raises(IOError, f.tell)
        assert f.tell() == p2

def test_chunk_size(tempfile):
    with _io.open(tempfile) as f:
        assert f._CHUNK_SIZE >= 1
        f._CHUNK_SIZE = 4096
        assert f._CHUNK_SIZE == 4096
        raises(ValueError, setattr, f, "_CHUNK_SIZE", 0)

def test_truncate(tempfile):
    with _io.open(tempfile, "w+") as f:
        f.write("abc")

    with _io.open(tempfile, "w+") as f:
        f.truncate()

    with _io.open(tempfile, "r+") as f:
        res = f.read()
        assert res == ""

def test_errors_property(tempfile):
    with _io.open(tempfile, "w") as f:
        assert f.errors == "strict"
    with _io.open(tempfile, "w", errors="replace") as f:
        assert f.errors == "replace"

    with raises(TypeError):
        _io.open(tempfile, "w", errors=3)

def test_append_bom(tempfile):
    # The BOM is not written again when appending to a non-empty file
    for charset in ["utf-8-sig", "utf-16", "utf-32"]:
        with _io.open(tempfile, "w", encoding=charset) as f:
            f.write("aaa")
            pos = f.tell()
        with _io.open(tempfile, "rb") as f:
            res = f.read()
            assert res == "aaa".encode(charset)
        with _io.open(tempfile, "a", encoding=charset) as f:
            f.write("xxx")
        with _io.open(tempfile, "rb") as f:
            res = f.read()
            assert res == "aaaxxx".encode(charset)

def test_newlines_attr(tempfile):
    with _io.open(tempfile, "r") as f:
        assert f.newlines is None

    with _io.open(tempfile, "wb") as f:
        f.write(b"hello\nworld\n")

    with _io.open(tempfile, "r") as f:
        res = f.readline()
        assert res == "hello\n"
        res = f.readline()
        assert res == "world\n"
        assert f.newlines == "\n"
        assert type(f.newlines) is str

def _check_warn_on_dealloc(*args, **kwargs):
    import gc
    import warnings

    f = open(*args, **kwargs)
    r = repr(f)
    gc.collect()
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter('always')
        f = None
        gc.collect()
    assert len(w) == 1, len(w)
    assert r in str(w[0])

def test_warn_on_dealloc(tempfile):
    _check_warn_on_dealloc(tempfile, 'wb', buffering=0)
    _check_warn_on_dealloc(tempfile, 'wb')
    _check_warn_on_dealloc(tempfile, 'w')

def test_pickling(tempfile):
    import pickle
    # Pickling file objects is forbidden
    for kwargs in [
            {"mode": "w"},
            {"mode": "wb"},
            {"mode": "wb", "buffering": 0},
            {"mode": "r"},
            {"mode": "rb"},
            {"mode": "rb", "buffering": 0},
            {"mode": "w+"},
            {"mode": "w+b"},
            {"mode": "w+b", "buffering": 0},
        ]:
        for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
            with _io.open(tempfile, **kwargs) as f:
                raises(TypeError, pickle.dumps, f, protocol)

def test_mod():
    typemods = dict((t, t.__module__) for name, t in vars(_io).items()
                    if isinstance(t, type) and name != '__loader__')
    for t, mod in typemods.items():
        if t is _io.BlockingIOError:
            assert mod == 'builtins'
        elif t is _io.UnsupportedOperation:
            assert mod == 'io'
        else:
            assert mod == '_io'

def test_issue1902(tempfile):
    with _io.open(tempfile, 'w+b', 4096) as f:
        f.write(b'\xff' * 13569)
        f.flush()
        f.seek(0, 0)
        f.read(1)
        f.seek(-1, 1)
        f.write(b'')

def test_issue1902_2(tempfile):
    with _io.open(tempfile, 'w+b', 4096) as f:
        f.write(b'\xff' * 13569)
        f.flush()
        f.seek(0, 0)

        f.read(1)
        f.seek(-1, 1)
        f.write(b'\xff')
        f.seek(1, 0)
        f.read(4123)
        f.seek(-4123, 1)

def test_issue1902_3(tempfile):
    buffer_size = 4096
    with _io.open(tempfile, 'w+b', buffer_size) as f:
        f.write(b'\xff' * buffer_size * 3)
        f.flush()
        f.seek(0, 0)

        f.read(1)
        f.seek(-1, 1)
        f.write(b'\xff')
        f.seek(1, 0)
        f.read(buffer_size * 2)
        assert f.tell() == 1 + buffer_size * 2

def test_open_exclusive(tempfile):
    # XXX: should raise FileExistsError
    FileExistsError = OSError

    filename = tempfile + '_x2'
    raises(ValueError, _io.open, filename, 'xw')
    with _io.open(filename, 'x') as f:
        assert f.mode == 'x'
    raises(FileExistsError, _io.open, filename, 'x')

def test_nonbuffered_textio(tempfile):
    import warnings
    filename = tempfile + '_x2'
    warnings.simplefilter("always", category=ResourceWarning)
    with warnings.catch_warnings(record=True) as recorded:
        raises(ValueError, _io.open, filename, 'w', buffering=0)
    assert recorded == []

def test_invalid_newline(tempfile):
    import warnings
    filename = tempfile + '_x2'
    warnings.simplefilter("always", category=ResourceWarning)
    with warnings.catch_warnings(record=True) as recorded:
        raises(ValueError, _io.open, filename, 'w', newline='invalid')
    assert recorded == []


def test_io_after_close(tempfile):
    for kwargs in [
            {"mode": "w"},
            {"mode": "wb"},
            {"mode": "w", "buffering": 1},
            {"mode": "w", "buffering": 2},
            {"mode": "wb", "buffering": 0},
            {"mode": "r"},
            {"mode": "rb"},
            {"mode": "r", "buffering": 1},
            {"mode": "r", "buffering": 2},
            {"mode": "rb", "buffering": 0},
            {"mode": "w+"},
            {"mode": "w+b"},
            {"mode": "w+", "buffering": 1},
            {"mode": "w+", "buffering": 2},
            {"mode": "w+b", "buffering": 0},
        ]:
        if "b" not in kwargs["mode"]:
            kwargs["encoding"] = "ascii"
        f = _io.open(tempfile, **kwargs)
        f.close()
        raises(ValueError, f.flush)
        raises(ValueError, f.fileno)
        raises(ValueError, f.isatty)
        raises(ValueError, f.__iter__)
        if hasattr(f, "peek"):
            raises(ValueError, f.peek, 1)
        raises(ValueError, f.read)
        if hasattr(f, "read1"):
            raises(ValueError, f.read1, 1024)
        if hasattr(f, "readall"):
            raises(ValueError, f.readall)
        if hasattr(f, "readinto"):
            raises(ValueError, f.readinto, bytearray(1024))
        raises(ValueError, f.readline)
        raises(ValueError, f.readlines)
        raises(ValueError, f.seek, 0)
        raises(ValueError, f.tell)
        raises(ValueError, f.truncate)
        raises(ValueError, f.write, b"" if "b" in kwargs['mode'] else u"")
        raises(ValueError, f.writelines, [])
        raises(ValueError, next, f)

def test_bug_newlines_rr_at_end():
    import io
    lines = io.TextIOWrapper(io.BytesIO(b"\r\r"), newline="").readlines()
    assert lines == [u'\r', u'\r']
    lines = io.TextIOWrapper(io.BytesIO(b"\r\r\r\r"), newline="").readlines()
    assert lines == [u'\r', u'\r', u'\r', u'\r']
    lines = io.TextIOWrapper(io.BytesIO(b"a\r\rb\r\r"), newline="").readlines()
    assert lines == [u'a\r', u'\r', u'b\r', u'\r']
    lines = io.TextIOWrapper(io.BytesIO(b"a\r\rb\r\rc"), newline="").readlines()
    assert lines == [u'a\r', u'\r', u'b\r', u'\r', u'c']
    lines = io.TextIOWrapper(io.BytesIO(b"a\r\rb\r\n"), newline="").readlines()
    assert lines == [u'a\r', u'\r', u'b\r\n']

# taken from test_univnewlines.py

FATX = 'x' * (2**14)

DATA_TEMPLATE = [
    "line1=1",
    "line2='this is a very long line designed to go past any default " +
        "buffer limits that exist in io.py but we also want to test " +
        "the uncommon case, naturally.'",
    "def line3():pass",
    "line4 = '%s'" % FATX,
    ]

DATA_CRLF = "\r\n".join(DATA_TEMPLATE) + "\r\n"


def test_tell_univnewlines(tempfile):
    import io
    import sys
        
    NEWLINE = '\r\n'

    with io.open(tempfile, "w") as fp:
        fp.write(DATA_CRLF)

    with io.open(tempfile, "r") as fp:
        assert repr(fp.newlines) == repr(None)
        data = fp.readline()
        pos = fp.tell()
    if "__pypy__" in sys.modules:
        assert repr(fp.newlines)  == repr(NEWLINE)

def test_tell_various():
    # at some point, a failing cases the test_interp_textio hypothesis test
    import io
    for t  in (b'\r', u'\x80\n'.encode('utf-8')):
        b = io.BytesIO(t)
        w = io.TextIOWrapper(b, encoding='utf-8')
        res1 = w.read()
        tell = w.tell()
        # the cookie is an opaque bigint with encoded bits. It may differ between
        # CPython and PyPy
        w.seek(0)
        w.seek(tell)
        res2 = w.read()
        assert len(res2) == 0
        assert res1.encode('utf8') == t.replace(b'\r', b'\n')

def test_seek_with_encoder_state():
    # Copied from lib-python/test/test_io.py
    import io
    import encodings  # will allow using euc_jis_2004 when untranslated
    b = io.BytesIO(b"")
    w = io.TextIOWrapper(b, encoding='euc_jis_2004')
    w.write(u"\u00e6\u0300")
    p0 = w.tell()
    w.write(u"\u00e6")
    p1_ = w.seek(p0, 0)
    p1 = w.tell()
    assert p1_ == p1
    w.write(u"\u0300")

    p2 = w.seek(0, 0)
    assert p2 == 0
    out = w.readline()
    expected = u"\u00e6\u0300\u0300"
    # print([hex(ord(s)) for s in out])
    # print([hex(ord(s)) for s in expected])
    assert out == expected