def test_read_nonbytes(): class NonbytesStream(_io.StringIO): read1 = _io.StringIO.read t = _io.TextIOWrapper(NonbytesStream(u'a')) raises(TypeError, t.read, 1) t = _io.TextIOWrapper(NonbytesStream(u'a')) raises(TypeError, t.readline) t = _io.TextIOWrapper(NonbytesStream(u'a')) t.read() == u'a'
def test_read_nonbytes(self): import _io class NonbytesStream(_io.StringIO): read1 = _io.StringIO.read t = _io.TextIOWrapper(NonbytesStream(u'a')) raises(TypeError, t.read, 1) t = _io.TextIOWrapper(NonbytesStream(u'a')) raises(TypeError, t.readline) t = _io.TextIOWrapper(NonbytesStream(u'a')) raises(TypeError, t.read)
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'>"
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)
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() # class CustomFile(object): def isatty(self): return 'YES' readable = writable = seekable = lambda self: False t = _io.TextIOWrapper(CustomFile()) assert t.isatty() == 'YES'
def test_constructor(): r = _io.BytesIO(b"\xc3\xa9\n\n") b = _io.BufferedReader(r, 1000) t = _io.TextIOWrapper(b) t.__init__(b, encoding="latin1", newline="\r\n") assert t.encoding == "latin1" assert t.line_buffering == False t.__init__(b, encoding="utf8", line_buffering=True) assert t.encoding == "utf8" assert t.line_buffering == True assert t.readline() == u"\xe9\n" raises(TypeError, t.__init__, b, newline=42) raises(ValueError, t.__init__, b, newline='xyzzy') t = _io.TextIOWrapper(b) assert t.encoding
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_tell(): r = _io.BytesIO(b"abc\ndef\n") t = _io.TextIOWrapper(r) assert t.tell() == 0 t.read(4) assert t.tell() == 4
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"
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
def create_stdio(fd, writing, name, encoding, errors, unbuffered): import _io # stdin is always opened in buffered mode, first because it # shouldn't make a difference in common use cases, second because # TextIOWrapper depends on the presence of a read1() method which # only exists on buffered streams. buffering = 0 if unbuffered and writing else -1 mode = 'w' if writing else 'r' try: buf = _io.open(fd, mode + 'b', buffering, closefd=False) except OSError as e: if e.errno != errno.EBADF: raise return None raw = buf.raw if buffering else buf raw.name = name # We normally use newline='\n' below, which turns off any translation. # However, on Windows (independently of -u), then we must enable # the Universal Newline mode (set by newline = None): on input, \r\n # is translated into \n; on output, \n is translated into \r\n. # We must never enable the Universal Newline mode on POSIX: CPython # never interprets '\r\n' in stdin as meaning just '\n', unlike what # it does if you explicitly open a file in text mode. newline = None if sys.platform == 'win32' else '\n' stream = _io.TextIOWrapper(buf, encoding, errors, newline=newline, line_buffering=unbuffered or raw.isatty()) stream.mode = mode return stream
def test_unreadable(self): import _io class UnReadable(_io.BytesIO): def readable(self): return False txt = _io.TextIOWrapper(UnReadable()) raises(IOError, txt.read)
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()
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
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"
def test_isatty(self): import _io class Tty(_io.BytesIO): def isatty(self): return True txt = _io.TextIOWrapper(Tty()) assert txt.isatty()
def test_read_byteslike(self): import _io as io import array class MemviewBytesIO(io.BytesIO): '''A BytesIO object whose read method returns memoryviews rather than bytes''' def read1(self, len_): return _to_memoryview(super().read1(len_)) def read(self, len_): return _to_memoryview(super().read(len_)) def _to_memoryview(buf): '''Convert bytes-object *buf* to a non-trivial memoryview''' arr = array.array('i') idx = len(buf) - len(buf) % arr.itemsize arr.frombytes(buf[:idx]) return memoryview(arr) r = MemviewBytesIO(b'Just some random string\n') t = io.TextIOWrapper(r, 'utf-8') # TextIOwrapper will not read the full string, because # we truncate it to a multiple of the native int size # so that we can construct a more complex memoryview. bytes_val = _to_memoryview(r.getvalue()).tobytes() assert t.read(200) == bytes_val.decode('utf-8')
def test_read_bug_unicode(): r = _io.BytesIO(b"\xc3\xa4bc\ndef\n") t = _io.TextIOWrapper(r, encoding="utf-8") reads = t.read(4) assert reads == u"äbc\n" reads += t.readline() assert reads == u"äbc\ndef\n"
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
def test_device_encoding(): encoding = os.device_encoding(sys.stderr.fileno()) if not encoding: skip("Requires a result from " "os.device_encoding(sys.stderr.fileno())") f = _io.TextIOWrapper(sys.stderr.buffer) assert f.encoding == encoding
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'>"
def test_read_bug_unicode(): inp = b"\xc3\xa4bc\ndef\n" r = _io.BytesIO(inp) t = _io.TextIOWrapper(r, encoding="utf-8") reads = t.read(4) assert reads == inp[:5].decode("utf-8") reads += t.readline() assert reads == inp.decode("utf-8")
def test_unseekable(self): import _io class Unseekable(_io.BytesIO): def seekable(self): return False txt = _io.TextIOWrapper(Unseekable()) raises(_io.UnsupportedOperation, txt.tell) raises(_io.UnsupportedOperation, txt.seek, 0)
def test_unwritable(self): import _io class UnWritable(_io.BytesIO): def writable(self): return False txt = _io.TextIOWrapper(UnWritable()) raises(_io.UnsupportedOperation, txt.write, "blah") raises(_io.UnsupportedOperation, txt.writelines, ["blah\n"])
def test_rawio_write_through(self): # Issue #12591: with write_through=True, writes don't need a flush import _io raw = self.get_MockRawIO()([b'abc', b'def', b'ghi\njkl\nopq\n']) txt = _io.TextIOWrapper(raw, encoding='ascii', newline='\n', write_through=True) txt.write('1') txt.write('23\n4') txt.write('5') assert b''.join(raw._write_stack) == b'123\n45'
def test_reconfigure_write(): # latin -> utf8 raw = _io.BytesIO() txt = _io.TextIOWrapper(raw, encoding='latin1', newline='\n') txt.write('abc\xe9\n') txt.reconfigure(encoding='utf-8') assert raw.getvalue() == b'abc\xe9\n' txt.write('d\xe9f\n') txt.flush() assert raw.getvalue() == b'abc\xe9\nd\xc3\xa9f\n' # ascii -> utf-8-sig: ensure that no BOM is written in the middle of # the file raw = _io.BytesIO() txt = _io.TextIOWrapper(raw, encoding='ascii', newline='\n') txt.write('abc\n') txt.reconfigure(encoding='utf-8-sig') txt.write('d\xe9f\n') txt.flush() assert raw.getvalue() == b'abc\nd\xc3\xa9f\n'
def test_original_displayhook_unencodable(self): import sys, _io out = _io.BytesIO() savestdout = sys.stdout sys.stdout = _io.TextIOWrapper(out, encoding='ascii') sys.__displayhook__("a=\xe9 b=\uDC80 c=\U00010000 d=\U0010FFFF") assert (out.getvalue() == b"'a=\\xe9 b=\\udc80 c=\\U00010000 d=\\U0010ffff'") sys.stdout = savestdout
def test_detach(self): import _io b = _io.BytesIO() f = _io.TextIOWrapper(b) assert f.detach() is b raises(ValueError, f.fileno) raises(ValueError, f.close) raises(ValueError, f.detach) raises(ValueError, f.flush) assert not b.closed b.close()
def test_rawio(self): # Issue #12591: TextIOWrapper must work with raw I/O objects, so # that subprocess.Popen() can have the required unbuffered # semantics with universal_newlines=True. import _io raw = self.get_MockRawIO()([b'abc', b'def', b'ghi\njkl\nopq\n']) txt = _io.TextIOWrapper(raw, encoding='ascii', newline='\n') # Reads assert txt.read(4) == 'abcd' assert txt.readline() == 'efghi\n' assert list(txt) == ['jkl\n', 'opq\n']
def test_reconfigure_encoding_read(): # latin1 -> utf8 # (latin1 can decode utf-8 encoded string) data = 'abc\xe9\n'.encode('latin1') + 'd\xe9f\n'.encode('utf8') raw = _io.BytesIO(data) txt = _io.TextIOWrapper(raw, encoding='latin1', newline='\n') assert txt.readline() == 'abc\xe9\n' with raises(_io.UnsupportedOperation): txt.reconfigure(encoding='utf-8') with raises(_io.UnsupportedOperation): txt.reconfigure(newline=None)