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
def test_largewrite(self): import _io raw = _io.FileIO(self.tmpfile, 'w') f = _io.BufferedWriter(raw) f.write("abcd" * 5000) f.close() assert self.readfile() == "abcd" * 5000
def test_write(self): import _io raw = _io.FileIO(self.tmpfile, 'w') f = _io.BufferedWriter(raw) f.write(b"abcd") raises(TypeError, f.write, u"cd") f.close() assert self.readfile() == b"abcd"
def test_truncate_after_close(self): import _io raw = _io.FileIO(self.tmpfile, 'w+') b = _io.BufferedWriter(raw) b.close() with raises(ValueError) as exc: b.truncate() assert exc.value.args[0] == "truncate of closed file"
def test_check_several_writes(self): import _io raw = _io.FileIO(self.tmpfile, 'w') b = _io.BufferedWriter(raw, 13) for i in range(4): assert b.write('x' * 10) == 10 b.flush() assert self.readfile() == 'x' * 40
def w_test_nonblock_pipe_write(self, bufsize): import _io as io class NonBlockingPipe(io._BufferedIOBase): "write() returns None when buffer is full" def __init__(self, buffersize=4096): self.buffersize = buffersize self.buffer = b'' def readable(self): return True def writable(self): return True def write(self, data): available = self.buffersize - len(self.buffer) if available <= 0: return None self.buffer += data[:available] return min(len(data), available) def read(self, size=-1): if not self.buffer: return None if size == -1: size = len(self.buffer) data = self.buffer[:size] self.buffer = self.buffer[size:] return data sent = [] received = [] pipe = NonBlockingPipe() rf = io.BufferedReader(pipe, bufsize) wf = io.BufferedWriter(pipe, bufsize) for N in 9999, 7574: try: i = 0 while True: msg = bytes([i % 26 + 97] * N) sent.append(msg) wf.write(msg) i += 1 except io.BlockingIOError as e: sent[-1] = sent[-1][:e.characters_written] received.append(rf.read()) msg = b'BLOCKED' wf.write(msg) sent.append(msg) while True: try: wf.flush() break except io.BlockingIOError as e: received.append(rf.read()) received += iter(rf.read, None) rf.close() wf.close() sent, received = b''.join(sent), b''.join(received) assert sent == received
def test_deprecated_max_buffer_size(self): import _io, warnings raw = _io.FileIO(self.tmpfile, 'w') with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") f = _io.BufferedWriter(raw, max_buffer_size=8192) f.close() assert len(w) == 1 assert str(w[0].message) == "max_buffer_size is deprecated" assert w[0].category is DeprecationWarning
def test_readinto1(self): import _io class MockIO(_io._IOBase): def readable(self): return True def readinto(self, buf): buf[:3] = b"abc" return 3 def writable(self): return True def write(self, b): return len(b) def seekable(self): return True def seek(self, pos, whence): return 0 bufio = _io.BufferedReader(MockIO(), buffer_size=5) buf = bytearray(10) bufio.read(2) n = bufio.readinto1(buf) assert n == 4 assert buf[:n] == b'cabc' # Yes, CPython's observable behavior depends on buffer_size! bufio = _io.BufferedReader(MockIO(), buffer_size=20) buf = bytearray(10) bufio.read(2) n = bufio.readinto1(buf) assert n == 1 assert buf[:n] == b'c' bufio = _io.BufferedReader(MockIO(), buffer_size=20) buf = bytearray(2) bufio.peek(3) assert bufio.readinto1(buf) == 2 assert buf == b'ab' n = bufio.readinto1(buf) assert n == 1 assert buf[:n] == b'c' bufio = _io.BufferedRandom(MockIO(), buffer_size=10) buf = bytearray(20) bufio.peek(3) assert bufio.readinto1(buf) == 6 assert buf[:6] == b'abcabc' bufio = _io.BufferedWriter(MockIO(), buffer_size=10) raises(_io.UnsupportedOperation, bufio.readinto1, bytearray(10))
def test_write_error_on_close(self): import _io class MockRawIO(_io._RawIOBase): def writable(self): return True def write(self, data): raise IOError() raw = MockRawIO() b = _io.BufferedWriter(raw) b.write(b'spam') raises(IOError, b.close) # exception not swallowed assert b.closed
def test_close_error_on_close(self): import _io class MockRawIO(_io._RawIOBase): def writable(self): return True def close(self): raise IOError('close') def bad_flush(): raise IOError('flush') raw = MockRawIO() b = _io.BufferedWriter(raw) b.flush = bad_flush err = raises(IOError, b.close) # exception not swallowed assert err.value.args == ('close',) assert not b.closed
def test_reentrant_write(self): import thread # Reentrant-safe is only enabled with threads import _io, errno class MockRawIO(_io._RawIOBase): def writable(self): return True def write(self, data): bufio.write("something else") return len(data) rawio = MockRawIO() bufio = _io.BufferedWriter(rawio) bufio.write("test") exc = raises(RuntimeError, bufio.flush) assert "reentrant" in str(exc.value) # And not e.g. recursion limit.
def test_dict(self): import _io 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("abc") bufio.flush() assert f.getvalue() == "ABC"
def open_maybe_gzip(filename, mode='r'): # this _must_ be a str filename = str(filename) if filename.endswith(h5_constants.GZIP_SUFFIX): raw = gzip.open(filename, mode + 'b', 2) elif filename.endswith(h5_constants.LZ4_SUFFIX): raw = lz4.open(filename, mode + 'b') else: return open(filename, mode) bufsize = 1024 * 1024 # 1MB of buffering if mode == 'r': return io.BufferedReader(raw, buffer_size=bufsize) elif mode == 'w': return io.BufferedWriter(raw, buffer_size=bufsize) else: raise ValueError("Unsupported mode for compression: %s" % mode)
def _(remote, file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None): if 'b' in mode: if 'r' in mode: # return remote.File(file) return _io.BufferedReader(remote.File(file)) elif 'w' in mode: return _io.BufferedWriter(remote.File(file)) else: raise NotImplementedError()
def test_write_interrupted(self): import _io, errno class MockRawIO(_io._RawIOBase): def __init__(self): self.count = 0 def writable(self): return True def write(self, data): self.count += 1 if self.count < 3: raise IOError(errno.EINTR, "interrupted") else: return len(data) rawio = MockRawIO() bufio = _io.BufferedWriter(rawio) assert bufio.write("test") == 4 bufio.flush() assert rawio.count == 3
import _io as io try: io.BytesIO io.BufferedWriter except AttributeError: import sys print('SKIP') sys.exit() bts = io.BytesIO() buf = io.BufferedWriter(bts, 8) buf.write(b"foobar") print(bts.getvalue()) buf.write(b"foobar") # CPython has different flushing policy, so value below is different print(bts.getvalue()) buf.flush() print(bts.getvalue()) buf.flush() print(bts.getvalue())
def test_write_non_blocking(self): import _io, io class MockNonBlockWriterIO(io.RawIOBase): def __init__(self): self._write_stack = [] self._blocker_char = None def writable(self): return True closed = False def pop_written(self): s = ''.join(self._write_stack) self._write_stack[:] = [] return s def block_on(self, char): """Block when a given char is encountered.""" self._blocker_char = char def write(self, b): try: b = b.tobytes() except AttributeError: pass n = -1 if self._blocker_char: try: n = b.index(self._blocker_char) except ValueError: pass else: if n > 0: # write data up to the first blocker self._write_stack.append(b[:n]) return n else: # cancel blocker and indicate would block self._blocker_char = None return None self._write_stack.append(b) return len(b) raw = MockNonBlockWriterIO() bufio = _io.BufferedWriter(raw, 8) assert bufio.write("abcd") == 4 assert bufio.write("efghi") == 5 # 1 byte will be written, the rest will be buffered raw.block_on(b"k") assert bufio.write("jklmn") == 5 # 8 bytes will be written, 8 will be buffered and the rest will be lost raw.block_on(b"0") try: bufio.write(b"opqrwxyz0123456789") except _io.BlockingIOError as e: written = e.characters_written else: self.fail("BlockingIOError should have been raised") assert written == 16 assert raw.pop_written() == "abcdefghijklmnopqrwxyz" assert bufio.write("ABCDEFGHI") == 9 s = raw.pop_written() # Previously buffered bytes were flushed assert s.startswith("01234567A")
import sys import _io # ---------------------------------------------------------------------------------------------------------------------- # # patch _io # # ---------------------------------------------------------------------------------------------------------------------- sys.stdin = _io.TextIOWrapper(_io.BufferedReader(sys.stdin), encoding=__graalpython__.stdio_encoding, errors=__graalpython__.stdio_error, line_buffering=True) sys.stdin.mode = "r" sys.__stdin__ = sys.stdin sys.stdout = _io.TextIOWrapper(_io.BufferedWriter(sys.stdout), encoding=__graalpython__.stdio_encoding, errors=__graalpython__.stdio_error, line_buffering=True) sys.stdout.mode = "w" sys.__stdout__ = sys.stdout sys.stderr = _io.TextIOWrapper(_io.BufferedWriter(sys.stderr.file_io), encoding=__graalpython__.stdio_encoding, errors="backslashreplace", line_buffering=True) sys.stderr.mode = "w" sys.__stderr__ = sys.stderr # Try to close the std streams when we exit. # To make this work reliably, we probably have to implement the _io module in Java import atexit