示例#1
0
    def test_interleaved_read_write(self):
        import _io as io
        # Test for issue #12213
        with io.BytesIO(b'abcdefgh') as raw:
            with io.BufferedRandom(raw, 100) as f:
                f.write(b"1")
                assert f.read(1) == b'b'
                f.write(b'2')
                assert f.read1(1) == b'd'
                f.write(b'3')
                buf = bytearray(1)
                f.readinto(buf)
                assert buf == b'f'
                f.write(b'4')
                assert f.peek(1) == b'h'
                f.flush()
                assert raw.getvalue() == b'1b2d3f4h'

        with io.BytesIO(b'abc') as raw:
            with io.BufferedRandom(raw, 100) as f:
                assert f.read(1) == b'a'
                f.write(b"2")
                assert f.read(1) == b'c'
                f.flush()
                assert raw.getvalue() == b'a2c'
示例#2
0
    def _test_incrementalencoder(self):
        import _multibytecodec, _codecs, _io
        with open(self.myfile + '/shift_jis.txt', 'rb') as fid:
            uni_str =  fid.read()
        with open(self.myfile + '/shift_jis-utf8.txt', 'rb') as fid:
            utf8str =  fid.read()
        UTF8Reader = _codecs.lookup('utf-8').streamreader
        for sizehint in [None] + list(range(1, 33)) + \
                        [64, 128, 256, 512, 1024]:
            istream = UTF8Reader(_io.BytesIO(utf8str))
            ostream = _io.BytesIO()
            codec = _multibytecodec.__getcodec('cp932')
            print(dir(codec))
            encoder = codec.incrementalencoder()
            while 1:
                if sizehint is not None:
                    data = istream.read(sizehint)
                else:
                    data = istream.read()

                if not data:
                    break
                e = encoder.encode(data)
                ostream.write(e)
            assert ostream.getvalue() == uni_str
示例#3
0
 def test_pair(self):
     import _io
     pair = _io.BufferedRWPair(_io.BytesIO("abc"), _io.BytesIO())
     assert not pair.closed
     assert pair.readable()
     assert pair.writable()
     assert not pair.isatty()
     assert pair.read() == "abc"
     assert pair.write("abc") == 3
示例#4
0
def test_init():
    raises(TypeError, _io.BytesIO, "12345")
    buf = b"1234567890"
    b = _io.BytesIO(buf)
    assert b.getvalue() == buf
    b = _io.BytesIO(None)
    assert b.getvalue() == b""
    b.__init__(buf * 2)
    assert b.getvalue() == buf * 2
    b.__init__(buf)
    assert b.getvalue() == buf
示例#5
0
def test_repr():
    t = _io.TextIOWrapper(_io.BytesIO(""), encoding="utf-8")
    assert repr(t) == "<_io.TextIOWrapper encoding='utf-8'>"
    t = _io.TextIOWrapper(_io.BytesIO(""), encoding="ascii")
    assert repr(t) == "<_io.TextIOWrapper encoding='ascii'>"
    t = _io.TextIOWrapper(_io.BytesIO(""), encoding=u"utf-8")
    assert repr(t) == "<_io.TextIOWrapper encoding='utf-8'>"
    b = _io.BytesIO("")
    t = _io.TextIOWrapper(b, encoding="utf-8")
    b.name = "dummy"
    assert repr(t) == "<_io.TextIOWrapper name='dummy' encoding='utf-8'>"
示例#6
0
 def test_illegal_decoder(self):
     import _io
     t = _io.TextIOWrapper(_io.BytesIO(b'aaaaaa'), newline='\n',
                          encoding='quopri_codec')
     raises(TypeError, t.read, 1)
     t = _io.TextIOWrapper(_io.BytesIO(b'aaaaaa'), newline='\n',
                          encoding='quopri_codec')
     raises(TypeError, t.readline)
     t = _io.TextIOWrapper(_io.BytesIO(b'aaaaaa'), newline='\n',
                          encoding='quopri_codec')
     raises(TypeError, t.read)
示例#7
0
 def test_init(self):
     import _io
     raises(TypeError, _io.BytesIO, u"12345")
     buf = "1234567890"
     b = _io.BytesIO(buf)
     assert b.getvalue() == buf
     b = _io.BytesIO(None)
     assert b.getvalue() == ""
     b.__init__(buf * 2)
     assert b.getvalue() == buf * 2
     b.__init__(buf)
     assert b.getvalue() == buf
示例#8
0
 def test_seek(self):
     import _io
     f = _io.BytesIO(b"hello")
     assert f.tell() == 0
     assert f.seek(-1, 2) == 4
     assert f.tell() == 4
     assert f.seek(0) == 0
示例#9
0
 def test_read(self):
     import _io
     f = _io.BytesIO(b"hello")
     assert f.read() == b"hello"
     import gc; gc.collect()
     assert f.read(8192) == b""
     f.close()
示例#10
0
    def test_init_kwargs(self):
        import _io

        buf = b"1234567890"
        b = _io.BytesIO(initial_bytes=buf)
        assert b.read() == buf
        raises(TypeError, _io.BytesIO, buf, foo=None)
示例#11
0
def test_read():
    f = _io.BytesIO("hello")
    assert f.read() == "hello"
    import gc
    gc.collect()
    assert f.read(8192) == ""
    f.close()
示例#12
0
 def test_readinto(self):
     import _io
     for methodname in ["readinto", "readinto1"]:
         b = _io.BytesIO(b"hello")
         readinto = getattr(b, methodname)
         a1 = bytearray(b't')
         a2 = bytearray(b'testing')
         assert readinto(a1) == 1
         assert readinto(a2) == 4
         b.seek(0)
         m = memoryview(bytearray(b"world"))
         assert readinto(m) == 5
         #
         exc = raises(TypeError, readinto, u"hello")
         msg = str(exc.value)
         print(msg)
         assert " read-write b" in msg and msg.endswith(", not str")
         #
         exc = raises(TypeError, readinto, memoryview(b"hello"))
         msg = str(exc.value)
         print(msg)
         assert " read-write b" in msg and msg.endswith(", not memoryview")
         #
         b.close()
         assert a1 == b"h"
         assert a2 == b"elloing"
         raises(ValueError, readinto, bytearray(b"hello"))
示例#13
0
def test_reconfigure_line_buffering():
    r = _io.BytesIO()
    b = _io.BufferedWriter(r, 1000)
    t = _io.TextIOWrapper(b, newline="\n", line_buffering=False)
    t.write("AB\nC")
    assert r.getvalue() == b""

    t.reconfigure(line_buffering=True)   # implicit flush
    assert r.getvalue() == b"AB\nC"
    t.write("DEF\nG")
    assert r.getvalue() == b"AB\nCDEF\nG"
    t.write("H")
    assert r.getvalue() == b"AB\nCDEF\nG"
    t.reconfigure(line_buffering=False)   # implicit flush
    assert r.getvalue() == b"AB\nCDEF\nGH"
    t.write("IJ")
    assert r.getvalue() == b"AB\nCDEF\nGH"

    # Keeping default value
    t.reconfigure()
    t.reconfigure(line_buffering=None)
    assert t.line_buffering == False
    assert type(t.line_buffering) is bool
    t.reconfigure(line_buffering=True)
    t.reconfigure()
    t.reconfigure(line_buffering=None)
    assert t.line_buffering == True
示例#14
0
def test_tell():
    
    r = _io.BytesIO(b"abc\ndef\n")
    t = _io.TextIOWrapper(r)
    assert t.tell() == 0
    t.read(4)
    assert t.tell() == 4
示例#15
0
    def test_get_file_content(self):
        request = Request(Environ())
        stream = _io.BytesIO("hello world".encode())
        request.files = {"file": FileStorage(stream=stream, filename="foo")}

        self.assertEqual("hello world",
                         BaseHandler._get_file_content(request).decode())
示例#16
0
    def test_bufio_write_through(self):
        import _io as io
        # Issue #21396: write_through=True doesn't force a flush()
        # on the underlying binary buffered object.
        flush_called, write_called = [], []

        class BufferedWriter(io.BufferedWriter):
            def flush(self, *args, **kwargs):
                flush_called.append(True)
                return super().flush(*args, **kwargs)

            def write(self, *args, **kwargs):
                write_called.append(True)
                return super().write(*args, **kwargs)

        rawio = io.BytesIO()
        data = b"a"
        bufio = BufferedWriter(rawio, len(data) * 2)
        textio = io.TextIOWrapper(bufio, encoding='ascii', write_through=True)
        # write to the buffered io but don't overflow the buffer
        text = data.decode('ascii')
        textio.write(text)

        # buffer.flush is not called with write_through=True
        assert not flush_called
        # buffer.write *is* called with write_through=True
        assert write_called
        assert rawio.getvalue() == b""  # no flush

        write_called = []  # reset
        textio.write(text * 10)  # total content is larger than bufio buffer
        assert write_called
        assert rawio.getvalue() == data * 11  # all flushed
示例#17
0
    def test_read1(self):
        import _io

        class RecordingFileIO(_io.FileIO):
            def read(self, size=-1):
                self.nbreads += 1
                return _io.FileIO.read(self, size)

            def readinto(self, buf):
                self.nbreads += 1
                return _io.FileIO.readinto(self, buf)

        raw = RecordingFileIO(self.tmpfile)
        raw.nbreads = 0
        f = _io.BufferedReader(raw, buffer_size=3)
        assert f.read(1) == b'a'
        assert f.read1(1) == b'\n'
        assert raw.nbreads == 1
        assert f.read1(100) == b'b'
        assert raw.nbreads == 1
        assert f.read1(100) == b'\nc'
        assert raw.nbreads == 2
        assert f.read1(100) == b''
        assert raw.nbreads == 3
        f.close()

        # a negative argument (or no argument) leads to using the default
        # buffer size
        raw = _io.BytesIO(b'aaaa\nbbbb\ncccc\n')
        f = _io.BufferedReader(raw, buffer_size=3)
        assert f.read1(-1) == b'aaa'
        assert f.read1() == b'a\nb'
示例#18
0
 def test_capabilities(self):
     import _io
     f = _io.BytesIO()
     assert f.readable()
     assert f.writable()
     assert f.seekable()
     f.close()
示例#19
0
 def test_write(self):
     import _io
     f = _io.BytesIO()
     assert f.write("hello") == 5
     import gc; gc.collect()
     assert f.getvalue() == "hello"
     f.close()
示例#20
0
def test_read_some_then_readline():
    
    r = _io.BytesIO(b"abc\ndef\n")
    t = _io.TextIOWrapper(r)
    reads = t.read(4)
    reads += t.readline()
    assert reads == "abc\ndef\n"
示例#21
0
    def test_constructor_with_not_writable(self):
        import _io
        class NotWritable:
            def writable(self):
                return False

        raises(IOError, _io.BufferedRWPair, _io.BytesIO(), NotWritable())
示例#22
0
 def test_read_some_then_readline(self):
     import _io
     r = _io.BytesIO("abc\ndef\n")
     t = _io.TextIOWrapper(r)
     reads = t.read(4)
     reads += t.readline()
     assert reads == u"abc\ndef\n"
示例#23
0
    def test_recv_into(self):
        import socket
        import array
        import _io
        MSG = b'dupa was here\n'
        cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        cli.connect(self.serv.getsockname())
        fileno, addr = self.serv._accept()
        conn = socket.socket(fileno=fileno)
        buf = memoryview(MSG)
        conn.send(buf)
        buf = array.array('b', b' ' * 1024)
        nbytes = cli.recv_into(buf)
        assert nbytes == len(MSG)
        msg = buf.tobytes()[:len(MSG)]
        assert msg == MSG

        conn.send(MSG)
        buf = bytearray(1024)
        nbytes = cli.recv_into(memoryview(buf))
        assert nbytes == len(MSG)
        msg = buf[:len(MSG)]
        assert msg == MSG

        # A case where rwbuffer.get_raw_address() fails
        conn.send(MSG)
        buf = _io.BytesIO(b' ' * 1024)
        m = buf.getbuffer()
        nbytes = cli.recv_into(m)
        assert nbytes == len(MSG)
        msg = buf.getvalue()[:len(MSG)]
        assert msg == MSG
        conn.close()
示例#24
0
 def test_tell(self):
     import _io
     r = _io.BytesIO("abc\ndef\n")
     t = _io.TextIOWrapper(r)
     assert t.tell() == 0
     t.read(4)
     assert t.tell() == 4
示例#25
0
 def test_properties(self):
     import _io
     r = _io.BytesIO(b"\xc3\xa9\n\n")
     b = _io.BufferedReader(r, 1000)
     t = _io.TextIOWrapper(b)
     assert t.readable()
     assert t.seekable()
示例#26
0
    def test_write_rewind_write(self):
        # Various combinations of reading / writing / seeking
        # backwards / writing again
        import _io, errno

        def mutate(bufio, pos1, pos2):
            assert pos2 >= pos1
            # Fill the buffer
            bufio.seek(pos1)
            bufio.read(pos2 - pos1)
            bufio.write(b'\x02')
            # This writes earlier than the previous write, but still inside
            # the buffer.
            bufio.seek(pos1)
            bufio.write(b'\x01')

        b = b"\x80\x81\x82\x83\x84"
        for i in range(0, len(b)):
            for j in range(i, len(b)):
                raw = _io.BytesIO(b)
                bufio = _io.BufferedRandom(raw, 100)
                mutate(bufio, i, j)
                bufio.flush()
                expected = bytearray(b)
                expected[j] = 2
                expected[i] = 1
                assert raw.getvalue() == str(expected)
示例#27
0
 def test_device_encoding_ovf(self):
     import _io
     b = _io.BytesIO()
     b.fileno = lambda: self.INT_MAX + 1
     raises(OverflowError, _io.TextIOWrapper, b)
     b.fileno = lambda: self.UINT_MAX + 1
     raises(OverflowError, _io.TextIOWrapper, b)
示例#28
0
 def test_from_buffer_BytesIO(self):
     from _cffi_backend import FFI
     import _io
     ffi = FFI()
     a = _io.BytesIO(b"Hello, world!")
     buf = a.getbuffer()
     # used to segfault
     raises(TypeError, ffi.from_buffer, buf)
示例#29
0
 def test_flush_error_on_close(self):
     import _io
     txt = _io.TextIOWrapper(_io.BytesIO(b""), encoding="ascii")
     def bad_flush():
         raise IOError()
     txt.flush = bad_flush
     raises(IOError, txt.close)  # exception not swallowed
     assert txt.closed
示例#30
0
    def test_repr(self):
        import _io

        t = _io.TextIOWrapper(_io.BytesIO(b""), encoding="utf-8")
        assert repr(t) == "<_io.TextIOWrapper encoding='utf-8'>"
        t = _io.TextIOWrapper(_io.BytesIO(b""), encoding="ascii")
        assert repr(t) == "<_io.TextIOWrapper encoding='ascii'>"
        t = _io.TextIOWrapper(_io.BytesIO(b""), encoding="utf-8")
        assert repr(t) == "<_io.TextIOWrapper encoding='utf-8'>"
        b = _io.BytesIO(b"")
        t = _io.TextIOWrapper(b, encoding="utf-8")
        b.name = "dummy"
        assert repr(t) == "<_io.TextIOWrapper name='dummy' encoding='utf-8'>"
        t.mode = "r"
        assert repr(t) == "<_io.TextIOWrapper name='dummy' mode='r' encoding='utf-8'>"
        b.name = b"dummy"
        assert repr(t) == "<_io.TextIOWrapper name=b'dummy' mode='r' encoding='utf-8'>"