io
As in CPython, io is split in two: the native _io provides StringIO, BytesIO, the file classes open()
returns, open, open_code, text_encoding, DEFAULT_BUFFER_SIZE, UnsupportedOperation and BlockingIOError,
and io is CPython’s Lib/io.py, which re-exports them and adds SEEK_SET / SEEK_CUR / SEEK_END
(see modules.md).
open() files have their own page, open.md.
io.pyis patched to drop theIOBase,RawIOBase,BufferedIOBaseandTextIOBaseABCs and theReaderandWriterprotocols, since Monty has no metaclasses orabc. They raiseAttributeError, soisinstance(f, io.TextIOBase)-style checks fail, andio.__all__omits them.FileIO,BufferedRWPairandIncrementalNewlineDecoderare missing from both modules, as are_io’s_IOBase,_RawIOBase,_BufferedIOBase,_TextIOBaseand_BytesIOBuffer.- The type checker’s stubs omit the same names, except
_io’s private base classes, which type the stream methods. - Every import of
_iobuilds a fresh module, soio._io is _ioisFalse(see modules.md).
TextIOWrapper,BufferedReader,BufferedWriterandBufferedRandomare the types ofopen()files, fortype()andisinstance()checks. Calling one raisesTypeError: cannot create '_io.{name}' instances(e.g.'_io.BufferedReader'), where CPython wraps the buffer or raw stream it is given.
- No subclasses.
class S(io.StringIO)raisesTypeError, as for every native base (see classes.md). - No instance attributes.
s.x = 1raisesAttributeError: '_io.StringIO' object has no attribute 'x' and no __dict__ for setting new attributes; CPython’s instances have a__dict__. - Methods are call-only, like every builtin method (see builtins.md):
w = s.writeraisesAttributeError. - Missing methods:
BytesIO.getbuffer(),readinto(),readinto1(),__getstate__()and__setstate__(). Monty has nobytearrayormemoryviewfor the first three to use.copy.copy()andcopy.deepcopy()work without the last two. BytesIOtakes onlybytes. The constructor,write()andwritelines()rejectbytearrayandmemoryview, which Monty does not have.
json.load(), json.dump(), csv.reader(), csv.writer(), csv.DictReader(), csv.DictWriter() and
print(file=...) take a StringIO or BytesIO and call its read() or write() as CPython does.
Only json.load() can use a BytesIO: the writers pass str to BytesIO.write(), which raises TypeError, and
the CSV readers raise csv.Error on its bytes lines, both as in CPython.
json.dump() makes fewer write() calls than CPython, writing the whole document once (see json.md).
A stream’s write() returns the length written, so the visible result is the same.
io.text_encoding(None)returns'locale', as CPython does outside UTF-8 mode.open()acceptsencoding='locale'and reads it as UTF-8, the sandbox’s only encoding (see open.md).io.open_code(path)isopen(path, 'rb'); there is noPyFile_SetOpenCodeHookto change it.
- Nothing in Monty raises it, since there are no non-blocking streams.
- Its constructor accepts at most one string, like every exception constructor (see
exceptions.md), so
BlockingIOError(errno, strerror, characters_written)and thecharacters_writtenattribute are unavailable.