| LEFT | RIGHT |
|---|---|
| 1 """The io module provides the Python interfaces to stream handling. The | 1 """The io module provides the Python interfaces to stream handling. The |
| 2 builtin open function is defined in this module. | 2 builtin open function is defined in this module. |
| 3 | 3 |
| 4 At the top of the I/O hierarchy is the abstract base class IOBase. It | 4 At the top of the I/O hierarchy is the abstract base class IOBase. It |
| 5 defines the basic interface to a stream. Note, however, that there is no | 5 defines the basic interface to a stream. Note, however, that there is no |
| 6 seperation between reading and writing to streams; implementations are | 6 seperation between reading and writing to streams; implementations are |
| 7 allowed to throw an IOError if they do not support a given operation. | 7 allowed to throw an IOError if they do not support a given operation. |
| 8 | 8 |
| 9 Extending IOBase is RawIOBase which deals simply with the reading and | 9 Extending IOBase is RawIOBase which deals simply with the reading and |
| 10 writing of raw bytes to a stream. FileIO subclasses RawIOBase to provide | 10 writing of raw bytes to a stream. FileIO subclasses RawIOBase to provide |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 75 """Exception raised when I/O would block on a non-blocking I/O stream.""" | 75 """Exception raised when I/O would block on a non-blocking I/O stream.""" |
| 76 | 76 |
| 77 def __init__(self, errno, strerror, characters_written=0): | 77 def __init__(self, errno, strerror, characters_written=0): |
| 78 IOError.__init__(self, errno, strerror) | 78 IOError.__init__(self, errno, strerror) |
| 79 self.characters_written = characters_written | 79 self.characters_written = characters_written |
| 80 | 80 |
| 81 | 81 |
| 82 def open(file, mode="r", buffering=None, encoding=None, errors=None, | 82 def open(file, mode="r", buffering=None, encoding=None, errors=None, |
| 83 newline=None, closefd=True): | 83 newline=None, closefd=True): |
| 84 | 84 |
| 85 r"""Open file and return a stream. If the file cannot be opened, an IOError is | 85 r"""Open file and return a stream. Raise IOError upon failure. |
| 86 raised. | 86 |
| 87 | 87 file is either a text or byte string giving the name (and the path |
| 88 file is either a string giving the name (and the path if the file | 88 if the file isn't in the current working directory) of the file to |
| 89 isn't in the current working directory) of the file to be opened or an | 89 be opened or an integer file descriptor of the file to be |
| 90 integer file descriptor of the file to be wrapped. (If a file | 90 wrapped. (If a file descriptor is given, it is closed when the |
| 91 descriptor is given, it is closed when the returned I/O object is | 91 returned I/O object is closed, unless closefd is set to False.) |
| 92 closed, unless closefd is set to False.) | |
| 93 | 92 |
| 94 mode is an optional string that specifies the mode in which the file | 93 mode is an optional string that specifies the mode in which the file |
| 95 is opened. It defaults to 'r' which means open for reading in text | 94 is opened. It defaults to 'r' which means open for reading in text |
| 96 mode. Other common values are 'w' for writing (truncating the file if | 95 mode. Other common values are 'w' for writing (truncating the file if |
| 97 it already exists), and 'a' for appending (which on some Unix systems, | 96 it already exists), and 'a' for appending (which on some Unix systems, |
| 98 means that all writes append to the end of the file regardless of the | 97 means that all writes append to the end of the file regardless of the |
| 99 current seek position). In text mode, if encoding is not specified the | 98 current seek position). In text mode, if encoding is not specified the |
| 100 encoding used is platform dependent. (For reading and writing raw | 99 encoding used is platform dependent. (For reading and writing raw |
| 101 bytes use binary mode and leave encoding unspecified.) The available | 100 bytes use binary mode and leave encoding unspecified.) The available |
| 102 modes are: | 101 modes are: |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 173 a file in a binary mode, the returned class varies: in read binary | 172 a file in a binary mode, the returned class varies: in read binary |
| 174 mode, it returns a BufferedReader; in write binary and append binary | 173 mode, it returns a BufferedReader; in write binary and append binary |
| 175 modes, it returns a BufferedWriter, and in read/write mode, it returns | 174 modes, it returns a BufferedWriter, and in read/write mode, it returns |
| 176 a BufferedRandom. | 175 a BufferedRandom. |
| 177 | 176 |
| 178 It is also possible to use a string or bytearray as a file for both | 177 It is also possible to use a string or bytearray as a file for both |
| 179 reading and writing. For strings StringIO can be used like a file | 178 reading and writing. For strings StringIO can be used like a file |
| 180 opened in a text mode, and for bytes a BytesIO can be used like a file | 179 opened in a text mode, and for bytes a BytesIO can be used like a file |
| 181 opened in a binary mode. | 180 opened in a binary mode. |
| 182 """ | 181 """ |
| 183 if not isinstance(file, (str, bytes, int)): | 182 if not isinstance(file, (str, bytes, int)): |
|
GvR
2008/08/22 19:04:51
Good catch. However I'm not sure if the file is a
| |
| 184 raise TypeError("invalid file: %r" % file) | 183 raise TypeError("invalid file: %r" % file) |
| 185 if not isinstance(mode, str): | 184 if not isinstance(mode, str): |
| 186 raise TypeError("invalid mode: %r" % mode) | 185 raise TypeError("invalid mode: %r" % mode) |
| 187 if buffering is not None and not isinstance(buffering, int): | 186 if buffering is not None and not isinstance(buffering, int): |
| 188 raise TypeError("invalid buffering: %r" % buffering) | 187 raise TypeError("invalid buffering: %r" % buffering) |
| 189 if encoding is not None and not isinstance(encoding, str): | 188 if encoding is not None and not isinstance(encoding, str): |
| 190 raise TypeError("invalid encoding: %r" % encoding) | 189 raise TypeError("invalid encoding: %r" % encoding) |
| 191 if errors is not None and not isinstance(errors, str): | 190 if errors is not None and not isinstance(errors, str): |
| 192 raise TypeError("invalid errors: %r" % errors) | 191 raise TypeError("invalid errors: %r" % errors) |
| 193 modes = set(mode) | 192 modes = set(mode) |
| (...skipping 1933 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2127 ("\r", "\n"), | 2126 ("\r", "\n"), |
| 2128 "\r\n", | 2127 "\r\n", |
| 2129 ("\n", "\r\n"), | 2128 ("\n", "\r\n"), |
| 2130 ("\r", "\r\n"), | 2129 ("\r", "\r\n"), |
| 2131 ("\r", "\n", "\r\n") | 2130 ("\r", "\n", "\r\n") |
| 2132 )[self._seennl] | 2131 )[self._seennl] |
| 2133 | 2132 |
| 2134 | 2133 |
| 2135 except ImportError: | 2134 except ImportError: |
| 2136 StringIO = _StringIO | 2135 StringIO = _StringIO |
| LEFT | RIGHT |