Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | An implementation of the new I/O lib as defined by PEP 3116 - "New I/O" |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 3 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 4 | Classes defined here: UnsupportedOperation, BlockingIOError. |
| 5 | Functions defined here: open(). |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 6 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 7 | Mostly written by Amaury Forgeot d'Arc |
| 8 | */ |
| 9 | |
| 10 | #define PY_SSIZE_T_CLEAN |
| 11 | #include "Python.h" |
| 12 | #include "structmember.h" |
| 13 | #include "_iomodule.h" |
| 14 | |
| 15 | #ifdef HAVE_SYS_TYPES_H |
| 16 | #include <sys/types.h> |
| 17 | #endif /* HAVE_SYS_TYPES_H */ |
| 18 | |
| 19 | #ifdef HAVE_SYS_STAT_H |
| 20 | #include <sys/stat.h> |
| 21 | #endif /* HAVE_SYS_STAT_H */ |
| 22 | |
| 23 | |
| 24 | /* Various interned strings */ |
| 25 | |
| 26 | PyObject *_PyIO_str_close; |
| 27 | PyObject *_PyIO_str_closed; |
| 28 | PyObject *_PyIO_str_decode; |
| 29 | PyObject *_PyIO_str_encode; |
| 30 | PyObject *_PyIO_str_fileno; |
| 31 | PyObject *_PyIO_str_flush; |
| 32 | PyObject *_PyIO_str_getstate; |
| 33 | PyObject *_PyIO_str_isatty; |
| 34 | PyObject *_PyIO_str_newlines; |
| 35 | PyObject *_PyIO_str_nl; |
| 36 | PyObject *_PyIO_str_read; |
| 37 | PyObject *_PyIO_str_read1; |
| 38 | PyObject *_PyIO_str_readable; |
Victor Stinner | b57f108 | 2011-05-26 00:19:38 +0200 | [diff] [blame] | 39 | PyObject *_PyIO_str_readall; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 40 | PyObject *_PyIO_str_readinto; |
| 41 | PyObject *_PyIO_str_readline; |
| 42 | PyObject *_PyIO_str_reset; |
| 43 | PyObject *_PyIO_str_seek; |
| 44 | PyObject *_PyIO_str_seekable; |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 45 | PyObject *_PyIO_str_setstate; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 46 | PyObject *_PyIO_str_tell; |
| 47 | PyObject *_PyIO_str_truncate; |
| 48 | PyObject *_PyIO_str_writable; |
| 49 | PyObject *_PyIO_str_write; |
| 50 | |
| 51 | PyObject *_PyIO_empty_str; |
| 52 | PyObject *_PyIO_empty_bytes; |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 53 | PyObject *_PyIO_zero; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 54 | |
| 55 | |
| 56 | PyDoc_STRVAR(module_doc, |
| 57 | "The io module provides the Python interfaces to stream handling. The\n" |
| 58 | "builtin open function is defined in this module.\n" |
| 59 | "\n" |
| 60 | "At the top of the I/O hierarchy is the abstract base class IOBase. It\n" |
| 61 | "defines the basic interface to a stream. Note, however, that there is no\n" |
Éric Araujo | fab9766 | 2012-02-26 02:14:08 +0100 | [diff] [blame] | 62 | "separation between reading and writing to streams; implementations are\n" |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 63 | "allowed to raise an IOError if they do not support a given operation.\n" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 64 | "\n" |
| 65 | "Extending IOBase is RawIOBase which deals simply with the reading and\n" |
Benjamin Peterson | 8f2b665 | 2009-04-05 00:46:27 +0000 | [diff] [blame] | 66 | "writing of raw bytes to a stream. FileIO subclasses RawIOBase to provide\n" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 67 | "an interface to OS files.\n" |
| 68 | "\n" |
| 69 | "BufferedIOBase deals with buffering on a raw byte stream (RawIOBase). Its\n" |
| 70 | "subclasses, BufferedWriter, BufferedReader, and BufferedRWPair buffer\n" |
| 71 | "streams that are readable, writable, and both respectively.\n" |
| 72 | "BufferedRandom provides a buffered interface to random access\n" |
| 73 | "streams. BytesIO is a simple stream of in-memory bytes.\n" |
| 74 | "\n" |
| 75 | "Another IOBase subclass, TextIOBase, deals with the encoding and decoding\n" |
| 76 | "of streams into text. TextIOWrapper, which extends it, is a buffered text\n" |
| 77 | "interface to a buffered raw stream (`BufferedIOBase`). Finally, StringIO\n" |
| 78 | "is a in-memory stream for text.\n" |
| 79 | "\n" |
| 80 | "Argument names are not part of the specification, and only the arguments\n" |
| 81 | "of open() are intended to be used as keyword arguments.\n" |
| 82 | "\n" |
| 83 | "data:\n" |
| 84 | "\n" |
| 85 | "DEFAULT_BUFFER_SIZE\n" |
| 86 | "\n" |
| 87 | " An int containing the default buffer size used by the module's buffered\n" |
| 88 | " I/O classes. open() uses the file's blksize (as obtained by os.stat) if\n" |
| 89 | " possible.\n" |
| 90 | ); |
| 91 | |
| 92 | |
| 93 | /* |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 94 | * The main open() function |
| 95 | */ |
| 96 | PyDoc_STRVAR(open_doc, |
Georg Brandl | e40ee50 | 2010-07-11 09:33:39 +0000 | [diff] [blame] | 97 | "open(file, mode='r', buffering=-1, encoding=None,\n" |
Ross Lagerwall | 59142db | 2011-10-31 20:34:46 +0200 | [diff] [blame] | 98 | " errors=None, newline=None, closefd=True, opener=None) -> file object\n" |
Georg Brandl | 5e8f6d1 | 2009-12-23 10:30:45 +0000 | [diff] [blame] | 99 | "\n" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 100 | "Open file and return a stream. Raise IOError upon failure.\n" |
| 101 | "\n" |
| 102 | "file is either a text or byte string giving the name (and the path\n" |
| 103 | "if the file isn't in the current working directory) of the file to\n" |
| 104 | "be opened or an integer file descriptor of the file to be\n" |
| 105 | "wrapped. (If a file descriptor is given, it is closed when the\n" |
| 106 | "returned I/O object is closed, unless closefd is set to False.)\n" |
| 107 | "\n" |
| 108 | "mode is an optional string that specifies the mode in which the file\n" |
| 109 | "is opened. It defaults to 'r' which means open for reading in text\n" |
| 110 | "mode. Other common values are 'w' for writing (truncating the file if\n" |
Charles-François Natali | dc3044c | 2012-01-09 22:40:02 +0100 | [diff] [blame] | 111 | "it already exists), 'x' for creating and writing to a new file, and\n" |
| 112 | "'a' for appending (which on some Unix systems, means that all writes\n" |
| 113 | "append to the end of the file regardless of the current seek position).\n" |
| 114 | "In text mode, if encoding is not specified the encoding used is platform\n" |
Victor Stinner | f86a5e8 | 2012-06-05 13:43:22 +0200 | [diff] [blame] | 115 | "dependent: locale.getpreferredencoding(False) is called to get the\n" |
| 116 | "current locale encoding. (For reading and writing raw bytes use binary\n" |
| 117 | "mode and leave encoding unspecified.) The available modes are:\n" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 118 | "\n" |
| 119 | "========= ===============================================================\n" |
| 120 | "Character Meaning\n" |
| 121 | "--------- ---------------------------------------------------------------\n" |
| 122 | "'r' open for reading (default)\n" |
| 123 | "'w' open for writing, truncating the file first\n" |
Charles-François Natali | dc3044c | 2012-01-09 22:40:02 +0100 | [diff] [blame] | 124 | "'x' create a new file and open it for writing\n" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 125 | "'a' open for writing, appending to the end of the file if it exists\n" |
| 126 | "'b' binary mode\n" |
| 127 | "'t' text mode (default)\n" |
| 128 | "'+' open a disk file for updating (reading and writing)\n" |
| 129 | "'U' universal newline mode (for backwards compatibility; unneeded\n" |
| 130 | " for new code)\n" |
| 131 | "========= ===============================================================\n" |
| 132 | "\n" |
| 133 | "The default mode is 'rt' (open for reading text). For binary random\n" |
| 134 | "access, the mode 'w+b' opens and truncates the file to 0 bytes, while\n" |
Charles-François Natali | dc3044c | 2012-01-09 22:40:02 +0100 | [diff] [blame] | 135 | "'r+b' opens the file without truncation. The 'x' mode implies 'w' and\n" |
| 136 | "raises an `FileExistsError` if the file already exists.\n" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 137 | "\n" |
| 138 | "Python distinguishes between files opened in binary and text modes,\n" |
| 139 | "even when the underlying operating system doesn't. Files opened in\n" |
| 140 | "binary mode (appending 'b' to the mode argument) return contents as\n" |
| 141 | "bytes objects without any decoding. In text mode (the default, or when\n" |
| 142 | "'t' is appended to the mode argument), the contents of the file are\n" |
| 143 | "returned as strings, the bytes having been first decoded using a\n" |
| 144 | "platform-dependent encoding or using the specified encoding if given.\n" |
| 145 | "\n" |
Antoine Pitrou | d5587bc | 2009-12-19 21:08:31 +0000 | [diff] [blame] | 146 | "buffering is an optional integer used to set the buffering policy.\n" |
| 147 | "Pass 0 to switch buffering off (only allowed in binary mode), 1 to select\n" |
| 148 | "line buffering (only usable in text mode), and an integer > 1 to indicate\n" |
| 149 | "the size of a fixed-size chunk buffer. When no buffering argument is\n" |
| 150 | "given, the default buffering policy works as follows:\n" |
| 151 | "\n" |
| 152 | "* Binary files are buffered in fixed-size chunks; the size of the buffer\n" |
| 153 | " is chosen using a heuristic trying to determine the underlying device's\n" |
| 154 | " \"block size\" and falling back on `io.DEFAULT_BUFFER_SIZE`.\n" |
| 155 | " On many systems, the buffer will typically be 4096 or 8192 bytes long.\n" |
| 156 | "\n" |
| 157 | "* \"Interactive\" text files (files for which isatty() returns True)\n" |
| 158 | " use line buffering. Other text files use the policy described above\n" |
| 159 | " for binary files.\n" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 160 | "\n" |
| 161 | "encoding is the name of the encoding used to decode or encode the\n" |
| 162 | "file. This should only be used in text mode. The default encoding is\n" |
| 163 | "platform dependent, but any encoding supported by Python can be\n" |
| 164 | "passed. See the codecs module for the list of supported encodings.\n" |
| 165 | "\n" |
| 166 | "errors is an optional string that specifies how encoding errors are to\n" |
| 167 | "be handled---this argument should not be used in binary mode. Pass\n" |
| 168 | "'strict' to raise a ValueError exception if there is an encoding error\n" |
| 169 | "(the default of None has the same effect), or pass 'ignore' to ignore\n" |
| 170 | "errors. (Note that ignoring encoding errors can lead to data loss.)\n" |
Andrew Kuchling | c7b6c50 | 2013-06-16 12:58:48 -0400 | [diff] [blame^] | 171 | "See the documentation for codecs.register or run 'help(codecs.Codec)'\n" |
| 172 | "for a list of the permitted encoding error strings.\n" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 173 | "\n" |
| 174 | "newline controls how universal newlines works (it only applies to text\n" |
| 175 | "mode). It can be None, '', '\\n', '\\r', and '\\r\\n'. It works as\n" |
| 176 | "follows:\n" |
| 177 | "\n" |
| 178 | "* On input, if newline is None, universal newlines mode is\n" |
| 179 | " enabled. Lines in the input can end in '\\n', '\\r', or '\\r\\n', and\n" |
| 180 | " these are translated into '\\n' before being returned to the\n" |
| 181 | " caller. If it is '', universal newline mode is enabled, but line\n" |
| 182 | " endings are returned to the caller untranslated. If it has any of\n" |
| 183 | " the other legal values, input lines are only terminated by the given\n" |
| 184 | " string, and the line ending is returned to the caller untranslated.\n" |
| 185 | "\n" |
| 186 | "* On output, if newline is None, any '\\n' characters written are\n" |
| 187 | " translated to the system default line separator, os.linesep. If\n" |
Ezio Melotti | 16d2b47 | 2012-09-18 07:20:18 +0300 | [diff] [blame] | 188 | " newline is '' or '\\n', no translation takes place. If newline is any\n" |
Victor Stinner | 401e17d | 2012-08-04 01:18:56 +0200 | [diff] [blame] | 189 | " of the other legal values, any '\\n' characters written are translated\n" |
| 190 | " to the given string.\n" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 191 | "\n" |
| 192 | "If closefd is False, the underlying file descriptor will be kept open\n" |
| 193 | "when the file is closed. This does not work when a file name is given\n" |
| 194 | "and must be True in that case.\n" |
| 195 | "\n" |
Ross Lagerwall | 59142db | 2011-10-31 20:34:46 +0200 | [diff] [blame] | 196 | "A custom opener can be used by passing a callable as *opener*. The\n" |
| 197 | "underlying file descriptor for the file object is then obtained by\n" |
| 198 | "calling *opener* with (*file*, *flags*). *opener* must return an open\n" |
| 199 | "file descriptor (passing os.open as *opener* results in functionality\n" |
| 200 | "similar to passing None).\n" |
| 201 | "\n" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 202 | "open() returns a file object whose type depends on the mode, and\n" |
| 203 | "through which the standard file operations such as reading and writing\n" |
| 204 | "are performed. When open() is used to open a file in a text mode ('w',\n" |
| 205 | "'r', 'wt', 'rt', etc.), it returns a TextIOWrapper. When used to open\n" |
| 206 | "a file in a binary mode, the returned class varies: in read binary\n" |
| 207 | "mode, it returns a BufferedReader; in write binary and append binary\n" |
| 208 | "modes, it returns a BufferedWriter, and in read/write mode, it returns\n" |
| 209 | "a BufferedRandom.\n" |
| 210 | "\n" |
| 211 | "It is also possible to use a string or bytearray as a file for both\n" |
| 212 | "reading and writing. For strings StringIO can be used like a file\n" |
| 213 | "opened in a text mode, and for bytes a BytesIO can be used like a file\n" |
| 214 | "opened in a binary mode.\n" |
| 215 | ); |
| 216 | |
| 217 | static PyObject * |
| 218 | io_open(PyObject *self, PyObject *args, PyObject *kwds) |
| 219 | { |
| 220 | char *kwlist[] = {"file", "mode", "buffering", |
| 221 | "encoding", "errors", "newline", |
Ross Lagerwall | 59142db | 2011-10-31 20:34:46 +0200 | [diff] [blame] | 222 | "closefd", "opener", NULL}; |
| 223 | PyObject *file, *opener = Py_None; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 224 | char *mode = "r"; |
| 225 | int buffering = -1, closefd = 1; |
| 226 | char *encoding = NULL, *errors = NULL, *newline = NULL; |
| 227 | unsigned i; |
| 228 | |
Charles-François Natali | dc3044c | 2012-01-09 22:40:02 +0100 | [diff] [blame] | 229 | int creating = 0, reading = 0, writing = 0, appending = 0, updating = 0; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 230 | int text = 0, binary = 0, universal = 0; |
| 231 | |
Christian Heimes | 89ff3c7 | 2012-09-10 03:50:48 +0200 | [diff] [blame] | 232 | char rawmode[6], *m; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 233 | int line_buffering, isatty; |
| 234 | |
| 235 | PyObject *raw, *modeobj = NULL, *buffer = NULL, *wrapper = NULL; |
| 236 | |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 237 | _Py_IDENTIFIER(isatty); |
| 238 | _Py_IDENTIFIER(fileno); |
Martin v. Löwis | 767046a | 2011-10-14 15:35:36 +0200 | [diff] [blame] | 239 | _Py_IDENTIFIER(mode); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 240 | |
Ross Lagerwall | 59142db | 2011-10-31 20:34:46 +0200 | [diff] [blame] | 241 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|sizzziO:open", kwlist, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 242 | &file, &mode, &buffering, |
| 243 | &encoding, &errors, &newline, |
Ross Lagerwall | 59142db | 2011-10-31 20:34:46 +0200 | [diff] [blame] | 244 | &closefd, &opener)) { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 245 | return NULL; |
| 246 | } |
| 247 | |
| 248 | if (!PyUnicode_Check(file) && |
| 249 | !PyBytes_Check(file) && |
| 250 | !PyNumber_Check(file)) { |
| 251 | PyErr_Format(PyExc_TypeError, "invalid file: %R", file); |
| 252 | return NULL; |
| 253 | } |
| 254 | |
| 255 | /* Decode mode */ |
| 256 | for (i = 0; i < strlen(mode); i++) { |
| 257 | char c = mode[i]; |
| 258 | |
| 259 | switch (c) { |
Charles-François Natali | dc3044c | 2012-01-09 22:40:02 +0100 | [diff] [blame] | 260 | case 'x': |
| 261 | creating = 1; |
| 262 | break; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 263 | case 'r': |
| 264 | reading = 1; |
| 265 | break; |
| 266 | case 'w': |
| 267 | writing = 1; |
| 268 | break; |
| 269 | case 'a': |
| 270 | appending = 1; |
| 271 | break; |
| 272 | case '+': |
| 273 | updating = 1; |
| 274 | break; |
| 275 | case 't': |
| 276 | text = 1; |
| 277 | break; |
| 278 | case 'b': |
| 279 | binary = 1; |
| 280 | break; |
| 281 | case 'U': |
| 282 | universal = 1; |
| 283 | reading = 1; |
| 284 | break; |
| 285 | default: |
| 286 | goto invalid_mode; |
| 287 | } |
| 288 | |
| 289 | /* c must not be duplicated */ |
| 290 | if (strchr(mode+i+1, c)) { |
| 291 | invalid_mode: |
| 292 | PyErr_Format(PyExc_ValueError, "invalid mode: '%s'", mode); |
| 293 | return NULL; |
| 294 | } |
| 295 | |
| 296 | } |
| 297 | |
| 298 | m = rawmode; |
Charles-François Natali | dc3044c | 2012-01-09 22:40:02 +0100 | [diff] [blame] | 299 | if (creating) *(m++) = 'x'; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 300 | if (reading) *(m++) = 'r'; |
| 301 | if (writing) *(m++) = 'w'; |
| 302 | if (appending) *(m++) = 'a'; |
| 303 | if (updating) *(m++) = '+'; |
| 304 | *m = '\0'; |
| 305 | |
| 306 | /* Parameters validation */ |
| 307 | if (universal) { |
| 308 | if (writing || appending) { |
| 309 | PyErr_SetString(PyExc_ValueError, |
| 310 | "can't use U and writing mode at once"); |
| 311 | return NULL; |
| 312 | } |
| 313 | reading = 1; |
| 314 | } |
| 315 | |
| 316 | if (text && binary) { |
| 317 | PyErr_SetString(PyExc_ValueError, |
| 318 | "can't have text and binary mode at once"); |
| 319 | return NULL; |
| 320 | } |
| 321 | |
Charles-François Natali | dc3044c | 2012-01-09 22:40:02 +0100 | [diff] [blame] | 322 | if (creating + reading + writing + appending > 1) { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 323 | PyErr_SetString(PyExc_ValueError, |
Charles-François Natali | dc3044c | 2012-01-09 22:40:02 +0100 | [diff] [blame] | 324 | "must have exactly one of create/read/write/append mode"); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 325 | return NULL; |
| 326 | } |
| 327 | |
| 328 | if (binary && encoding != NULL) { |
| 329 | PyErr_SetString(PyExc_ValueError, |
| 330 | "binary mode doesn't take an encoding argument"); |
| 331 | return NULL; |
| 332 | } |
| 333 | |
| 334 | if (binary && errors != NULL) { |
| 335 | PyErr_SetString(PyExc_ValueError, |
| 336 | "binary mode doesn't take an errors argument"); |
| 337 | return NULL; |
| 338 | } |
| 339 | |
| 340 | if (binary && newline != NULL) { |
| 341 | PyErr_SetString(PyExc_ValueError, |
| 342 | "binary mode doesn't take a newline argument"); |
| 343 | return NULL; |
| 344 | } |
| 345 | |
| 346 | /* Create the Raw file stream */ |
| 347 | raw = PyObject_CallFunction((PyObject *)&PyFileIO_Type, |
Ross Lagerwall | 59142db | 2011-10-31 20:34:46 +0200 | [diff] [blame] | 348 | "OsiO", file, rawmode, closefd, opener); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 349 | if (raw == NULL) |
| 350 | return NULL; |
| 351 | |
| 352 | modeobj = PyUnicode_FromString(mode); |
| 353 | if (modeobj == NULL) |
| 354 | goto error; |
| 355 | |
| 356 | /* buffering */ |
| 357 | { |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 358 | PyObject *res = _PyObject_CallMethodId(raw, &PyId_isatty, NULL); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 359 | if (res == NULL) |
| 360 | goto error; |
| 361 | isatty = PyLong_AsLong(res); |
| 362 | Py_DECREF(res); |
| 363 | if (isatty == -1 && PyErr_Occurred()) |
| 364 | goto error; |
| 365 | } |
| 366 | |
| 367 | if (buffering == 1 || (buffering < 0 && isatty)) { |
| 368 | buffering = -1; |
| 369 | line_buffering = 1; |
| 370 | } |
| 371 | else |
| 372 | line_buffering = 0; |
| 373 | |
| 374 | if (buffering < 0) { |
| 375 | buffering = DEFAULT_BUFFER_SIZE; |
| 376 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
| 377 | { |
| 378 | struct stat st; |
| 379 | long fileno; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 380 | PyObject *res = _PyObject_CallMethodId(raw, &PyId_fileno, NULL); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 381 | if (res == NULL) |
| 382 | goto error; |
| 383 | |
| 384 | fileno = PyLong_AsLong(res); |
| 385 | Py_DECREF(res); |
| 386 | if (fileno == -1 && PyErr_Occurred()) |
| 387 | goto error; |
| 388 | |
Antoine Pitrou | ea5d17d | 2010-10-27 19:45:43 +0000 | [diff] [blame] | 389 | if (fstat(fileno, &st) >= 0 && st.st_blksize > 1) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 390 | buffering = st.st_blksize; |
| 391 | } |
| 392 | #endif |
| 393 | } |
| 394 | if (buffering < 0) { |
| 395 | PyErr_SetString(PyExc_ValueError, |
| 396 | "invalid buffering size"); |
| 397 | goto error; |
| 398 | } |
| 399 | |
| 400 | /* if not buffering, returns the raw file object */ |
| 401 | if (buffering == 0) { |
| 402 | if (!binary) { |
| 403 | PyErr_SetString(PyExc_ValueError, |
| 404 | "can't have unbuffered text I/O"); |
| 405 | goto error; |
| 406 | } |
| 407 | |
| 408 | Py_DECREF(modeobj); |
| 409 | return raw; |
| 410 | } |
| 411 | |
| 412 | /* wraps into a buffered file */ |
| 413 | { |
| 414 | PyObject *Buffered_class; |
| 415 | |
| 416 | if (updating) |
| 417 | Buffered_class = (PyObject *)&PyBufferedRandom_Type; |
Charles-François Natali | dc3044c | 2012-01-09 22:40:02 +0100 | [diff] [blame] | 418 | else if (creating || writing || appending) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 419 | Buffered_class = (PyObject *)&PyBufferedWriter_Type; |
| 420 | else if (reading) |
| 421 | Buffered_class = (PyObject *)&PyBufferedReader_Type; |
| 422 | else { |
| 423 | PyErr_Format(PyExc_ValueError, |
| 424 | "unknown mode: '%s'", mode); |
| 425 | goto error; |
| 426 | } |
| 427 | |
| 428 | buffer = PyObject_CallFunction(Buffered_class, "Oi", raw, buffering); |
| 429 | } |
| 430 | Py_CLEAR(raw); |
| 431 | if (buffer == NULL) |
| 432 | goto error; |
| 433 | |
| 434 | |
| 435 | /* if binary, returns the buffered file */ |
| 436 | if (binary) { |
| 437 | Py_DECREF(modeobj); |
| 438 | return buffer; |
| 439 | } |
| 440 | |
| 441 | /* wraps into a TextIOWrapper */ |
| 442 | wrapper = PyObject_CallFunction((PyObject *)&PyTextIOWrapper_Type, |
| 443 | "Osssi", |
| 444 | buffer, |
| 445 | encoding, errors, newline, |
| 446 | line_buffering); |
| 447 | Py_CLEAR(buffer); |
| 448 | if (wrapper == NULL) |
| 449 | goto error; |
| 450 | |
Martin v. Löwis | 767046a | 2011-10-14 15:35:36 +0200 | [diff] [blame] | 451 | if (_PyObject_SetAttrId(wrapper, &PyId_mode, modeobj) < 0) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 452 | goto error; |
| 453 | Py_DECREF(modeobj); |
| 454 | return wrapper; |
| 455 | |
| 456 | error: |
| 457 | Py_XDECREF(raw); |
| 458 | Py_XDECREF(modeobj); |
| 459 | Py_XDECREF(buffer); |
| 460 | Py_XDECREF(wrapper); |
| 461 | return NULL; |
| 462 | } |
| 463 | |
| 464 | /* |
| 465 | * Private helpers for the io module. |
| 466 | */ |
| 467 | |
| 468 | Py_off_t |
| 469 | PyNumber_AsOff_t(PyObject *item, PyObject *err) |
| 470 | { |
| 471 | Py_off_t result; |
| 472 | PyObject *runerr; |
| 473 | PyObject *value = PyNumber_Index(item); |
| 474 | if (value == NULL) |
| 475 | return -1; |
| 476 | |
| 477 | /* We're done if PyLong_AsSsize_t() returns without error. */ |
| 478 | result = PyLong_AsOff_t(value); |
| 479 | if (result != -1 || !(runerr = PyErr_Occurred())) |
| 480 | goto finish; |
| 481 | |
| 482 | /* Error handling code -- only manage OverflowError differently */ |
| 483 | if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError)) |
| 484 | goto finish; |
| 485 | |
| 486 | PyErr_Clear(); |
| 487 | /* If no error-handling desired then the default clipping |
| 488 | is sufficient. |
| 489 | */ |
| 490 | if (!err) { |
| 491 | assert(PyLong_Check(value)); |
| 492 | /* Whether or not it is less than or equal to |
| 493 | zero is determined by the sign of ob_size |
| 494 | */ |
| 495 | if (_PyLong_Sign(value) < 0) |
| 496 | result = PY_OFF_T_MIN; |
| 497 | else |
| 498 | result = PY_OFF_T_MAX; |
| 499 | } |
| 500 | else { |
| 501 | /* Otherwise replace the error with caller's error object. */ |
| 502 | PyErr_Format(err, |
| 503 | "cannot fit '%.200s' into an offset-sized integer", |
| 504 | item->ob_type->tp_name); |
| 505 | } |
| 506 | |
| 507 | finish: |
| 508 | Py_DECREF(value); |
| 509 | return result; |
| 510 | } |
| 511 | |
Benjamin Peterson | bf5ff76 | 2009-12-13 19:25:34 +0000 | [diff] [blame] | 512 | |
| 513 | /* Basically the "n" format code with the ability to turn None into -1. */ |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 514 | int |
Benjamin Peterson | bf5ff76 | 2009-12-13 19:25:34 +0000 | [diff] [blame] | 515 | _PyIO_ConvertSsize_t(PyObject *obj, void *result) { |
| 516 | Py_ssize_t limit; |
| 517 | if (obj == Py_None) { |
| 518 | limit = -1; |
| 519 | } |
| 520 | else if (PyNumber_Check(obj)) { |
| 521 | limit = PyNumber_AsSsize_t(obj, PyExc_OverflowError); |
| 522 | if (limit == -1 && PyErr_Occurred()) |
| 523 | return 0; |
| 524 | } |
| 525 | else { |
| 526 | PyErr_Format(PyExc_TypeError, |
| 527 | "integer argument expected, got '%.200s'", |
| 528 | Py_TYPE(obj)->tp_name); |
| 529 | return 0; |
| 530 | } |
| 531 | *((Py_ssize_t *)result) = limit; |
| 532 | return 1; |
| 533 | } |
| 534 | |
| 535 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 536 | static int |
| 537 | iomodule_traverse(PyObject *mod, visitproc visit, void *arg) { |
| 538 | _PyIO_State *state = IO_MOD_STATE(mod); |
| 539 | if (!state->initialized) |
| 540 | return 0; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 541 | if (state->locale_module != NULL) { |
| 542 | Py_VISIT(state->locale_module); |
| 543 | } |
| 544 | Py_VISIT(state->unsupported_operation); |
| 545 | return 0; |
| 546 | } |
| 547 | |
Benjamin Peterson | bf5ff76 | 2009-12-13 19:25:34 +0000 | [diff] [blame] | 548 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 549 | static int |
| 550 | iomodule_clear(PyObject *mod) { |
| 551 | _PyIO_State *state = IO_MOD_STATE(mod); |
| 552 | if (!state->initialized) |
| 553 | return 0; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 554 | if (state->locale_module != NULL) |
| 555 | Py_CLEAR(state->locale_module); |
| 556 | Py_CLEAR(state->unsupported_operation); |
| 557 | return 0; |
| 558 | } |
| 559 | |
| 560 | static void |
| 561 | iomodule_free(PyObject *mod) { |
| 562 | iomodule_clear(mod); |
| 563 | } |
| 564 | |
Benjamin Peterson | bf5ff76 | 2009-12-13 19:25:34 +0000 | [diff] [blame] | 565 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 566 | /* |
| 567 | * Module definition |
| 568 | */ |
| 569 | |
| 570 | static PyMethodDef module_methods[] = { |
| 571 | {"open", (PyCFunction)io_open, METH_VARARGS|METH_KEYWORDS, open_doc}, |
| 572 | {NULL, NULL} |
| 573 | }; |
| 574 | |
| 575 | struct PyModuleDef _PyIO_Module = { |
| 576 | PyModuleDef_HEAD_INIT, |
| 577 | "io", |
| 578 | module_doc, |
| 579 | sizeof(_PyIO_State), |
| 580 | module_methods, |
| 581 | NULL, |
| 582 | iomodule_traverse, |
| 583 | iomodule_clear, |
| 584 | (freefunc)iomodule_free, |
| 585 | }; |
| 586 | |
| 587 | PyMODINIT_FUNC |
| 588 | PyInit__io(void) |
| 589 | { |
| 590 | PyObject *m = PyModule_Create(&_PyIO_Module); |
| 591 | _PyIO_State *state = NULL; |
| 592 | if (m == NULL) |
| 593 | return NULL; |
| 594 | state = IO_MOD_STATE(m); |
| 595 | state->initialized = 0; |
| 596 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 597 | #define ADD_TYPE(type, name) \ |
| 598 | if (PyType_Ready(type) < 0) \ |
| 599 | goto fail; \ |
| 600 | Py_INCREF(type); \ |
| 601 | if (PyModule_AddObject(m, name, (PyObject *)type) < 0) { \ |
| 602 | Py_DECREF(type); \ |
| 603 | goto fail; \ |
| 604 | } |
| 605 | |
| 606 | /* DEFAULT_BUFFER_SIZE */ |
| 607 | if (PyModule_AddIntMacro(m, DEFAULT_BUFFER_SIZE) < 0) |
| 608 | goto fail; |
| 609 | |
| 610 | /* UnsupportedOperation inherits from ValueError and IOError */ |
| 611 | state->unsupported_operation = PyObject_CallFunction( |
| 612 | (PyObject *)&PyType_Type, "s(OO){}", |
| 613 | "UnsupportedOperation", PyExc_ValueError, PyExc_IOError); |
| 614 | if (state->unsupported_operation == NULL) |
| 615 | goto fail; |
| 616 | Py_INCREF(state->unsupported_operation); |
| 617 | if (PyModule_AddObject(m, "UnsupportedOperation", |
| 618 | state->unsupported_operation) < 0) |
| 619 | goto fail; |
| 620 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 621 | /* BlockingIOError, for compatibility */ |
| 622 | Py_INCREF(PyExc_BlockingIOError); |
| 623 | if (PyModule_AddObject(m, "BlockingIOError", |
| 624 | (PyObject *) PyExc_BlockingIOError) < 0) |
| 625 | goto fail; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 626 | |
| 627 | /* Concrete base types of the IO ABCs. |
| 628 | (the ABCs themselves are declared through inheritance in io.py) |
| 629 | */ |
| 630 | ADD_TYPE(&PyIOBase_Type, "_IOBase"); |
| 631 | ADD_TYPE(&PyRawIOBase_Type, "_RawIOBase"); |
| 632 | ADD_TYPE(&PyBufferedIOBase_Type, "_BufferedIOBase"); |
| 633 | ADD_TYPE(&PyTextIOBase_Type, "_TextIOBase"); |
| 634 | |
| 635 | /* Implementation of concrete IO objects. */ |
| 636 | /* FileIO */ |
| 637 | PyFileIO_Type.tp_base = &PyRawIOBase_Type; |
| 638 | ADD_TYPE(&PyFileIO_Type, "FileIO"); |
| 639 | |
| 640 | /* BytesIO */ |
| 641 | PyBytesIO_Type.tp_base = &PyBufferedIOBase_Type; |
| 642 | ADD_TYPE(&PyBytesIO_Type, "BytesIO"); |
Antoine Pitrou | 972ee13 | 2010-09-06 18:48:21 +0000 | [diff] [blame] | 643 | if (PyType_Ready(&_PyBytesIOBuffer_Type) < 0) |
| 644 | goto fail; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 645 | |
| 646 | /* StringIO */ |
| 647 | PyStringIO_Type.tp_base = &PyTextIOBase_Type; |
| 648 | ADD_TYPE(&PyStringIO_Type, "StringIO"); |
| 649 | |
| 650 | /* BufferedReader */ |
| 651 | PyBufferedReader_Type.tp_base = &PyBufferedIOBase_Type; |
| 652 | ADD_TYPE(&PyBufferedReader_Type, "BufferedReader"); |
| 653 | |
| 654 | /* BufferedWriter */ |
| 655 | PyBufferedWriter_Type.tp_base = &PyBufferedIOBase_Type; |
| 656 | ADD_TYPE(&PyBufferedWriter_Type, "BufferedWriter"); |
| 657 | |
| 658 | /* BufferedRWPair */ |
| 659 | PyBufferedRWPair_Type.tp_base = &PyBufferedIOBase_Type; |
| 660 | ADD_TYPE(&PyBufferedRWPair_Type, "BufferedRWPair"); |
| 661 | |
| 662 | /* BufferedRandom */ |
| 663 | PyBufferedRandom_Type.tp_base = &PyBufferedIOBase_Type; |
| 664 | ADD_TYPE(&PyBufferedRandom_Type, "BufferedRandom"); |
| 665 | |
| 666 | /* TextIOWrapper */ |
| 667 | PyTextIOWrapper_Type.tp_base = &PyTextIOBase_Type; |
| 668 | ADD_TYPE(&PyTextIOWrapper_Type, "TextIOWrapper"); |
| 669 | |
| 670 | /* IncrementalNewlineDecoder */ |
| 671 | ADD_TYPE(&PyIncrementalNewlineDecoder_Type, "IncrementalNewlineDecoder"); |
| 672 | |
| 673 | /* Interned strings */ |
Antoine Pitrou | fc1b6f0 | 2012-01-18 16:13:56 +0100 | [diff] [blame] | 674 | #define ADD_INTERNED(name) \ |
| 675 | if (!_PyIO_str_ ## name && \ |
| 676 | !(_PyIO_str_ ## name = PyUnicode_InternFromString(# name))) \ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 677 | goto fail; |
Antoine Pitrou | fc1b6f0 | 2012-01-18 16:13:56 +0100 | [diff] [blame] | 678 | |
| 679 | ADD_INTERNED(close) |
| 680 | ADD_INTERNED(closed) |
| 681 | ADD_INTERNED(decode) |
| 682 | ADD_INTERNED(encode) |
| 683 | ADD_INTERNED(fileno) |
| 684 | ADD_INTERNED(flush) |
| 685 | ADD_INTERNED(getstate) |
| 686 | ADD_INTERNED(isatty) |
| 687 | ADD_INTERNED(newlines) |
| 688 | ADD_INTERNED(read) |
| 689 | ADD_INTERNED(read1) |
| 690 | ADD_INTERNED(readable) |
Antoine Pitrou | bb5b92d | 2012-01-18 16:19:19 +0100 | [diff] [blame] | 691 | ADD_INTERNED(readall) |
Antoine Pitrou | fc1b6f0 | 2012-01-18 16:13:56 +0100 | [diff] [blame] | 692 | ADD_INTERNED(readinto) |
| 693 | ADD_INTERNED(readline) |
| 694 | ADD_INTERNED(reset) |
| 695 | ADD_INTERNED(seek) |
| 696 | ADD_INTERNED(seekable) |
| 697 | ADD_INTERNED(setstate) |
| 698 | ADD_INTERNED(tell) |
| 699 | ADD_INTERNED(truncate) |
| 700 | ADD_INTERNED(write) |
| 701 | ADD_INTERNED(writable) |
| 702 | |
| 703 | if (!_PyIO_str_nl && |
| 704 | !(_PyIO_str_nl = PyUnicode_InternFromString("\n"))) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 705 | goto fail; |
Antoine Pitrou | fc1b6f0 | 2012-01-18 16:13:56 +0100 | [diff] [blame] | 706 | |
| 707 | if (!_PyIO_empty_str && |
| 708 | !(_PyIO_empty_str = PyUnicode_FromStringAndSize(NULL, 0))) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 709 | goto fail; |
Antoine Pitrou | fc1b6f0 | 2012-01-18 16:13:56 +0100 | [diff] [blame] | 710 | if (!_PyIO_empty_bytes && |
| 711 | !(_PyIO_empty_bytes = PyBytes_FromStringAndSize(NULL, 0))) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 712 | goto fail; |
Antoine Pitrou | fc1b6f0 | 2012-01-18 16:13:56 +0100 | [diff] [blame] | 713 | if (!_PyIO_zero && |
| 714 | !(_PyIO_zero = PyLong_FromLong(0L))) |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 715 | goto fail; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 716 | |
| 717 | state->initialized = 1; |
| 718 | |
| 719 | return m; |
| 720 | |
| 721 | fail: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 722 | Py_XDECREF(state->unsupported_operation); |
| 723 | Py_DECREF(m); |
| 724 | return NULL; |
| 725 | } |