13257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel"""The io module provides the Python interfaces to stream handling. The 23257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielbuiltin open function is defined in this module. 33257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 43257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielAt the top of the I/O hierarchy is the abstract base class IOBase. It 53257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldefines the basic interface to a stream. Note, however, that there is no 63257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielseparation between reading and writing to streams; implementations are 73257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielallowed to raise an IOError if they do not support a given operation. 83257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 93257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielExtending IOBase is RawIOBase which deals simply with the reading and 103257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielwriting of raw bytes to a stream. FileIO subclasses RawIOBase to provide 113257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielan interface to OS files. 123257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 133257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielBufferedIOBase deals with buffering on a raw byte stream (RawIOBase). Its 143257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielsubclasses, BufferedWriter, BufferedReader, and BufferedRWPair buffer 153257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielstreams that are readable, writable, and both respectively. 163257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielBufferedRandom provides a buffered interface to random access 173257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielstreams. BytesIO is a simple stream of in-memory bytes. 183257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 193257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielAnother IOBase subclass, TextIOBase, deals with the encoding and decoding 203257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielof streams into text. TextIOWrapper, which extends it, is a buffered text 213257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielinterface to a buffered raw stream (`BufferedIOBase`). Finally, StringIO 223257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielis a in-memory stream for text. 233257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 243257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielArgument names are not part of the specification, and only the arguments 253257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielof open() are intended to be used as keyword arguments. 263257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 273257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldata: 283257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 293257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielDEFAULT_BUFFER_SIZE 303257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 313257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel An int containing the default buffer size used by the module's buffered 323257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel I/O classes. open() uses the file's blksize (as obtained by os.stat) if 333257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel possible. 343257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel""" 353257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# New I/O library conforming to PEP 3116. 363257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 373257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel__author__ = ("Guido van Rossum <guido@python.org>, " 383257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "Mike Verdone <mike.verdone@gmail.com>, " 393257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "Mark Russell <mark.russell@zen.co.uk>, " 403257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "Antoine Pitrou <solipsis@pitrou.net>, " 413257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "Amaury Forgeot d'Arc <amauryfa@gmail.com>, " 423257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "Benjamin Peterson <benjamin@python.org>") 433257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 443257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel__all__ = ["BlockingIOError", "open", "IOBase", "RawIOBase", "FileIO", 453257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "BytesIO", "StringIO", "BufferedIOBase", 463257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "BufferedReader", "BufferedWriter", "BufferedRWPair", 473257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "BufferedRandom", "TextIOBase", "TextIOWrapper", 483257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel "UnsupportedOperation", "SEEK_SET", "SEEK_CUR", "SEEK_END"] 493257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 503257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 513257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielimport _io 523257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielimport abc 533257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 543257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielfrom _io import (DEFAULT_BUFFER_SIZE, BlockingIOError, UnsupportedOperation, 553257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel open, FileIO, BytesIO, StringIO, BufferedReader, 563257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel BufferedWriter, BufferedRWPair, BufferedRandom, 573257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel IncrementalNewlineDecoder, TextIOWrapper) 583257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 593257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielOpenWrapper = _io.open # for compatibility with _pyio 603257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 613257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# for seek() 623257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielSEEK_SET = 0 633257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielSEEK_CUR = 1 643257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielSEEK_END = 2 653257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 663257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Declaring ABCs in C is tricky so we do it here. 673257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# Method descriptions and default implementations are inherited from the C 683257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# version however. 693257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielclass IOBase(_io._IOBase): 703257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel __metaclass__ = abc.ABCMeta 713257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel __doc__ = _io._IOBase.__doc__ 723257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 733257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielclass RawIOBase(_io._RawIOBase, IOBase): 743257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel __doc__ = _io._RawIOBase.__doc__ 753257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 763257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielclass BufferedIOBase(_io._BufferedIOBase, IOBase): 773257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel __doc__ = _io._BufferedIOBase.__doc__ 783257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 793257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielclass TextIOBase(_io._TextIOBase, IOBase): 803257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel __doc__ = _io._TextIOBase.__doc__ 813257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 823257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielRawIOBase.register(FileIO) 833257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 843257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielfor klass in (BytesIO, BufferedReader, BufferedWriter, BufferedRandom, 853257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel BufferedRWPair): 863257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel BufferedIOBase.register(klass) 873257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel 883257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielfor klass in (StringIO, TextIOWrapper): 893257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel TextIOBase.register(klass) 903257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldel klass 91