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" |
Serhiy Storchaka | 6787a38 | 2013-11-23 22:12:06 +0200 | [diff] [blame] | 129 | "'U' universal newline mode (deprecated)\n" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 130 | "========= ===============================================================\n" |
| 131 | "\n" |
| 132 | "The default mode is 'rt' (open for reading text). For binary random\n" |
| 133 | "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] | 134 | "'r+b' opens the file without truncation. The 'x' mode implies 'w' and\n" |
| 135 | "raises an `FileExistsError` if the file already exists.\n" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 136 | "\n" |
| 137 | "Python distinguishes between files opened in binary and text modes,\n" |
| 138 | "even when the underlying operating system doesn't. Files opened in\n" |
| 139 | "binary mode (appending 'b' to the mode argument) return contents as\n" |
| 140 | "bytes objects without any decoding. In text mode (the default, or when\n" |
| 141 | "'t' is appended to the mode argument), the contents of the file are\n" |
| 142 | "returned as strings, the bytes having been first decoded using a\n" |
| 143 | "platform-dependent encoding or using the specified encoding if given.\n" |
| 144 | "\n" |
Serhiy Storchaka | 6787a38 | 2013-11-23 22:12:06 +0200 | [diff] [blame] | 145 | "'U' mode is deprecated and will raise an exception in future versions\n" |
| 146 | "of Python. It has no effect in Python 3. Use newline to control\n" |
| 147 | "universal newlines mode.\n" |
| 148 | "\n" |
Antoine Pitrou | d5587bc | 2009-12-19 21:08:31 +0000 | [diff] [blame] | 149 | "buffering is an optional integer used to set the buffering policy.\n" |
| 150 | "Pass 0 to switch buffering off (only allowed in binary mode), 1 to select\n" |
| 151 | "line buffering (only usable in text mode), and an integer > 1 to indicate\n" |
| 152 | "the size of a fixed-size chunk buffer. When no buffering argument is\n" |
| 153 | "given, the default buffering policy works as follows:\n" |
| 154 | "\n" |
| 155 | "* Binary files are buffered in fixed-size chunks; the size of the buffer\n" |
| 156 | " is chosen using a heuristic trying to determine the underlying device's\n" |
| 157 | " \"block size\" and falling back on `io.DEFAULT_BUFFER_SIZE`.\n" |
| 158 | " On many systems, the buffer will typically be 4096 or 8192 bytes long.\n" |
| 159 | "\n" |
| 160 | "* \"Interactive\" text files (files for which isatty() returns True)\n" |
| 161 | " use line buffering. Other text files use the policy described above\n" |
| 162 | " for binary files.\n" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 163 | "\n" |
| 164 | "encoding is the name of the encoding used to decode or encode the\n" |
| 165 | "file. This should only be used in text mode. The default encoding is\n" |
| 166 | "platform dependent, but any encoding supported by Python can be\n" |
| 167 | "passed. See the codecs module for the list of supported encodings.\n" |
| 168 | "\n" |
| 169 | "errors is an optional string that specifies how encoding errors are to\n" |
| 170 | "be handled---this argument should not be used in binary mode. Pass\n" |
| 171 | "'strict' to raise a ValueError exception if there is an encoding error\n" |
| 172 | "(the default of None has the same effect), or pass 'ignore' to ignore\n" |
| 173 | "errors. (Note that ignoring encoding errors can lead to data loss.)\n" |
Andrew Kuchling | c7b6c50 | 2013-06-16 12:58:48 -0400 | [diff] [blame] | 174 | "See the documentation for codecs.register or run 'help(codecs.Codec)'\n" |
| 175 | "for a list of the permitted encoding error strings.\n" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 176 | "\n" |
| 177 | "newline controls how universal newlines works (it only applies to text\n" |
| 178 | "mode). It can be None, '', '\\n', '\\r', and '\\r\\n'. It works as\n" |
| 179 | "follows:\n" |
| 180 | "\n" |
| 181 | "* On input, if newline is None, universal newlines mode is\n" |
| 182 | " enabled. Lines in the input can end in '\\n', '\\r', or '\\r\\n', and\n" |
| 183 | " these are translated into '\\n' before being returned to the\n" |
| 184 | " caller. If it is '', universal newline mode is enabled, but line\n" |
| 185 | " endings are returned to the caller untranslated. If it has any of\n" |
| 186 | " the other legal values, input lines are only terminated by the given\n" |
| 187 | " string, and the line ending is returned to the caller untranslated.\n" |
| 188 | "\n" |
| 189 | "* On output, if newline is None, any '\\n' characters written are\n" |
| 190 | " translated to the system default line separator, os.linesep. If\n" |
Ezio Melotti | 16d2b47 | 2012-09-18 07:20:18 +0300 | [diff] [blame] | 191 | " 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] | 192 | " of the other legal values, any '\\n' characters written are translated\n" |
| 193 | " to the given string.\n" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 194 | "\n" |
| 195 | "If closefd is False, the underlying file descriptor will be kept open\n" |
| 196 | "when the file is closed. This does not work when a file name is given\n" |
| 197 | "and must be True in that case.\n" |
| 198 | "\n" |
Ross Lagerwall | 59142db | 2011-10-31 20:34:46 +0200 | [diff] [blame] | 199 | "A custom opener can be used by passing a callable as *opener*. The\n" |
| 200 | "underlying file descriptor for the file object is then obtained by\n" |
| 201 | "calling *opener* with (*file*, *flags*). *opener* must return an open\n" |
| 202 | "file descriptor (passing os.open as *opener* results in functionality\n" |
| 203 | "similar to passing None).\n" |
| 204 | "\n" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 205 | "open() returns a file object whose type depends on the mode, and\n" |
| 206 | "through which the standard file operations such as reading and writing\n" |
| 207 | "are performed. When open() is used to open a file in a text mode ('w',\n" |
| 208 | "'r', 'wt', 'rt', etc.), it returns a TextIOWrapper. When used to open\n" |
| 209 | "a file in a binary mode, the returned class varies: in read binary\n" |
| 210 | "mode, it returns a BufferedReader; in write binary and append binary\n" |
| 211 | "modes, it returns a BufferedWriter, and in read/write mode, it returns\n" |
| 212 | "a BufferedRandom.\n" |
| 213 | "\n" |
| 214 | "It is also possible to use a string or bytearray as a file for both\n" |
| 215 | "reading and writing. For strings StringIO can be used like a file\n" |
| 216 | "opened in a text mode, and for bytes a BytesIO can be used like a file\n" |
| 217 | "opened in a binary mode.\n" |
| 218 | ); |
| 219 | |
| 220 | static PyObject * |
| 221 | io_open(PyObject *self, PyObject *args, PyObject *kwds) |
| 222 | { |
| 223 | char *kwlist[] = {"file", "mode", "buffering", |
| 224 | "encoding", "errors", "newline", |
Ross Lagerwall | 59142db | 2011-10-31 20:34:46 +0200 | [diff] [blame] | 225 | "closefd", "opener", NULL}; |
| 226 | PyObject *file, *opener = Py_None; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 227 | char *mode = "r"; |
| 228 | int buffering = -1, closefd = 1; |
| 229 | char *encoding = NULL, *errors = NULL, *newline = NULL; |
| 230 | unsigned i; |
| 231 | |
Charles-François Natali | dc3044c | 2012-01-09 22:40:02 +0100 | [diff] [blame] | 232 | int creating = 0, reading = 0, writing = 0, appending = 0, updating = 0; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 233 | int text = 0, binary = 0, universal = 0; |
| 234 | |
Christian Heimes | 89ff3c7 | 2012-09-10 03:50:48 +0200 | [diff] [blame] | 235 | char rawmode[6], *m; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 236 | int line_buffering, isatty; |
| 237 | |
Serhiy Storchaka | f10063e | 2014-06-09 13:32:34 +0300 | [diff] [blame] | 238 | PyObject *raw, *modeobj = NULL, *buffer, *wrapper, *result = NULL; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 239 | |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 240 | _Py_IDENTIFIER(isatty); |
| 241 | _Py_IDENTIFIER(fileno); |
Martin v. Löwis | 767046a | 2011-10-14 15:35:36 +0200 | [diff] [blame] | 242 | _Py_IDENTIFIER(mode); |
Serhiy Storchaka | f10063e | 2014-06-09 13:32:34 +0300 | [diff] [blame] | 243 | _Py_IDENTIFIER(close); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 244 | |
Ross Lagerwall | 59142db | 2011-10-31 20:34:46 +0200 | [diff] [blame] | 245 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|sizzziO:open", kwlist, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 246 | &file, &mode, &buffering, |
| 247 | &encoding, &errors, &newline, |
Ross Lagerwall | 59142db | 2011-10-31 20:34:46 +0200 | [diff] [blame] | 248 | &closefd, &opener)) { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 249 | return NULL; |
| 250 | } |
| 251 | |
| 252 | if (!PyUnicode_Check(file) && |
| 253 | !PyBytes_Check(file) && |
| 254 | !PyNumber_Check(file)) { |
| 255 | PyErr_Format(PyExc_TypeError, "invalid file: %R", file); |
| 256 | return NULL; |
| 257 | } |
| 258 | |
| 259 | /* Decode mode */ |
| 260 | for (i = 0; i < strlen(mode); i++) { |
| 261 | char c = mode[i]; |
| 262 | |
| 263 | switch (c) { |
Charles-François Natali | dc3044c | 2012-01-09 22:40:02 +0100 | [diff] [blame] | 264 | case 'x': |
| 265 | creating = 1; |
| 266 | break; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 267 | case 'r': |
| 268 | reading = 1; |
| 269 | break; |
| 270 | case 'w': |
| 271 | writing = 1; |
| 272 | break; |
| 273 | case 'a': |
| 274 | appending = 1; |
| 275 | break; |
| 276 | case '+': |
| 277 | updating = 1; |
| 278 | break; |
| 279 | case 't': |
| 280 | text = 1; |
| 281 | break; |
| 282 | case 'b': |
| 283 | binary = 1; |
| 284 | break; |
| 285 | case 'U': |
| 286 | universal = 1; |
| 287 | reading = 1; |
| 288 | break; |
| 289 | default: |
| 290 | goto invalid_mode; |
| 291 | } |
| 292 | |
| 293 | /* c must not be duplicated */ |
| 294 | if (strchr(mode+i+1, c)) { |
| 295 | invalid_mode: |
| 296 | PyErr_Format(PyExc_ValueError, "invalid mode: '%s'", mode); |
| 297 | return NULL; |
| 298 | } |
| 299 | |
| 300 | } |
| 301 | |
| 302 | m = rawmode; |
Charles-François Natali | dc3044c | 2012-01-09 22:40:02 +0100 | [diff] [blame] | 303 | if (creating) *(m++) = 'x'; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 304 | if (reading) *(m++) = 'r'; |
| 305 | if (writing) *(m++) = 'w'; |
| 306 | if (appending) *(m++) = 'a'; |
| 307 | if (updating) *(m++) = '+'; |
| 308 | *m = '\0'; |
| 309 | |
| 310 | /* Parameters validation */ |
| 311 | if (universal) { |
| 312 | if (writing || appending) { |
| 313 | PyErr_SetString(PyExc_ValueError, |
| 314 | "can't use U and writing mode at once"); |
| 315 | return NULL; |
| 316 | } |
Serhiy Storchaka | 6787a38 | 2013-11-23 22:12:06 +0200 | [diff] [blame] | 317 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 318 | "'U' mode is deprecated", 1) < 0) |
| 319 | return NULL; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 320 | reading = 1; |
| 321 | } |
| 322 | |
| 323 | if (text && binary) { |
| 324 | PyErr_SetString(PyExc_ValueError, |
| 325 | "can't have text and binary mode at once"); |
| 326 | return NULL; |
| 327 | } |
| 328 | |
Charles-François Natali | dc3044c | 2012-01-09 22:40:02 +0100 | [diff] [blame] | 329 | if (creating + reading + writing + appending > 1) { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 330 | PyErr_SetString(PyExc_ValueError, |
Charles-François Natali | dc3044c | 2012-01-09 22:40:02 +0100 | [diff] [blame] | 331 | "must have exactly one of create/read/write/append mode"); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 332 | return NULL; |
| 333 | } |
| 334 | |
| 335 | if (binary && encoding != NULL) { |
| 336 | PyErr_SetString(PyExc_ValueError, |
| 337 | "binary mode doesn't take an encoding argument"); |
| 338 | return NULL; |
| 339 | } |
| 340 | |
| 341 | if (binary && errors != NULL) { |
| 342 | PyErr_SetString(PyExc_ValueError, |
| 343 | "binary mode doesn't take an errors argument"); |
| 344 | return NULL; |
| 345 | } |
| 346 | |
| 347 | if (binary && newline != NULL) { |
| 348 | PyErr_SetString(PyExc_ValueError, |
| 349 | "binary mode doesn't take a newline argument"); |
| 350 | return NULL; |
| 351 | } |
| 352 | |
| 353 | /* Create the Raw file stream */ |
| 354 | raw = PyObject_CallFunction((PyObject *)&PyFileIO_Type, |
Ross Lagerwall | 59142db | 2011-10-31 20:34:46 +0200 | [diff] [blame] | 355 | "OsiO", file, rawmode, closefd, opener); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 356 | if (raw == NULL) |
| 357 | return NULL; |
Serhiy Storchaka | f10063e | 2014-06-09 13:32:34 +0300 | [diff] [blame] | 358 | result = raw; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 359 | |
| 360 | modeobj = PyUnicode_FromString(mode); |
| 361 | if (modeobj == NULL) |
| 362 | goto error; |
| 363 | |
| 364 | /* buffering */ |
| 365 | { |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 366 | PyObject *res = _PyObject_CallMethodId(raw, &PyId_isatty, NULL); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 367 | if (res == NULL) |
| 368 | goto error; |
| 369 | isatty = PyLong_AsLong(res); |
| 370 | Py_DECREF(res); |
| 371 | if (isatty == -1 && PyErr_Occurred()) |
| 372 | goto error; |
| 373 | } |
| 374 | |
| 375 | if (buffering == 1 || (buffering < 0 && isatty)) { |
| 376 | buffering = -1; |
| 377 | line_buffering = 1; |
| 378 | } |
| 379 | else |
| 380 | line_buffering = 0; |
| 381 | |
| 382 | if (buffering < 0) { |
| 383 | buffering = DEFAULT_BUFFER_SIZE; |
| 384 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
| 385 | { |
| 386 | struct stat st; |
| 387 | long fileno; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 388 | PyObject *res = _PyObject_CallMethodId(raw, &PyId_fileno, NULL); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 389 | if (res == NULL) |
| 390 | goto error; |
| 391 | |
| 392 | fileno = PyLong_AsLong(res); |
| 393 | Py_DECREF(res); |
| 394 | if (fileno == -1 && PyErr_Occurred()) |
| 395 | goto error; |
| 396 | |
Antoine Pitrou | ea5d17d | 2010-10-27 19:45:43 +0000 | [diff] [blame] | 397 | if (fstat(fileno, &st) >= 0 && st.st_blksize > 1) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 398 | buffering = st.st_blksize; |
| 399 | } |
| 400 | #endif |
| 401 | } |
| 402 | if (buffering < 0) { |
| 403 | PyErr_SetString(PyExc_ValueError, |
| 404 | "invalid buffering size"); |
| 405 | goto error; |
| 406 | } |
| 407 | |
| 408 | /* if not buffering, returns the raw file object */ |
| 409 | if (buffering == 0) { |
| 410 | if (!binary) { |
| 411 | PyErr_SetString(PyExc_ValueError, |
| 412 | "can't have unbuffered text I/O"); |
| 413 | goto error; |
| 414 | } |
| 415 | |
| 416 | Py_DECREF(modeobj); |
Serhiy Storchaka | f10063e | 2014-06-09 13:32:34 +0300 | [diff] [blame] | 417 | return result; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | /* wraps into a buffered file */ |
| 421 | { |
| 422 | PyObject *Buffered_class; |
| 423 | |
| 424 | if (updating) |
| 425 | Buffered_class = (PyObject *)&PyBufferedRandom_Type; |
Charles-François Natali | dc3044c | 2012-01-09 22:40:02 +0100 | [diff] [blame] | 426 | else if (creating || writing || appending) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 427 | Buffered_class = (PyObject *)&PyBufferedWriter_Type; |
| 428 | else if (reading) |
| 429 | Buffered_class = (PyObject *)&PyBufferedReader_Type; |
| 430 | else { |
| 431 | PyErr_Format(PyExc_ValueError, |
| 432 | "unknown mode: '%s'", mode); |
| 433 | goto error; |
| 434 | } |
| 435 | |
| 436 | buffer = PyObject_CallFunction(Buffered_class, "Oi", raw, buffering); |
| 437 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 438 | if (buffer == NULL) |
| 439 | goto error; |
Serhiy Storchaka | f10063e | 2014-06-09 13:32:34 +0300 | [diff] [blame] | 440 | result = buffer; |
| 441 | Py_DECREF(raw); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 442 | |
| 443 | |
| 444 | /* if binary, returns the buffered file */ |
| 445 | if (binary) { |
| 446 | Py_DECREF(modeobj); |
Serhiy Storchaka | f10063e | 2014-06-09 13:32:34 +0300 | [diff] [blame] | 447 | return result; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | /* wraps into a TextIOWrapper */ |
| 451 | wrapper = PyObject_CallFunction((PyObject *)&PyTextIOWrapper_Type, |
| 452 | "Osssi", |
| 453 | buffer, |
| 454 | encoding, errors, newline, |
| 455 | line_buffering); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 456 | if (wrapper == NULL) |
| 457 | goto error; |
Serhiy Storchaka | f10063e | 2014-06-09 13:32:34 +0300 | [diff] [blame] | 458 | result = wrapper; |
| 459 | Py_DECREF(buffer); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 460 | |
Martin v. Löwis | 767046a | 2011-10-14 15:35:36 +0200 | [diff] [blame] | 461 | if (_PyObject_SetAttrId(wrapper, &PyId_mode, modeobj) < 0) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 462 | goto error; |
| 463 | Py_DECREF(modeobj); |
Serhiy Storchaka | f10063e | 2014-06-09 13:32:34 +0300 | [diff] [blame] | 464 | return result; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 465 | |
| 466 | error: |
Serhiy Storchaka | f10063e | 2014-06-09 13:32:34 +0300 | [diff] [blame] | 467 | if (result != NULL) { |
| 468 | PyObject *exc, *val, *tb; |
| 469 | PyErr_Fetch(&exc, &val, &tb); |
| 470 | if (_PyObject_CallMethodId(result, &PyId_close, NULL) != NULL) |
| 471 | PyErr_Restore(exc, val, tb); |
| 472 | else { |
Serhiy Storchaka | 76d3f14 | 2014-06-11 07:18:53 +0300 | [diff] [blame] | 473 | PyObject *exc2, *val2, *tb2; |
| 474 | PyErr_Fetch(&exc2, &val2, &tb2); |
Serhiy Storchaka | f10063e | 2014-06-09 13:32:34 +0300 | [diff] [blame] | 475 | PyErr_NormalizeException(&exc, &val, &tb); |
| 476 | Py_XDECREF(exc); |
| 477 | Py_XDECREF(tb); |
Serhiy Storchaka | 76d3f14 | 2014-06-11 07:18:53 +0300 | [diff] [blame] | 478 | PyErr_NormalizeException(&exc2, &val2, &tb2); |
Serhiy Storchaka | f10063e | 2014-06-09 13:32:34 +0300 | [diff] [blame] | 479 | PyException_SetContext(val2, val); |
Serhiy Storchaka | 76d3f14 | 2014-06-11 07:18:53 +0300 | [diff] [blame] | 480 | PyErr_Restore(exc2, val2, tb2); |
Serhiy Storchaka | f10063e | 2014-06-09 13:32:34 +0300 | [diff] [blame] | 481 | } |
| 482 | Py_DECREF(result); |
| 483 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 484 | Py_XDECREF(modeobj); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 485 | return NULL; |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | * Private helpers for the io module. |
| 490 | */ |
| 491 | |
| 492 | Py_off_t |
| 493 | PyNumber_AsOff_t(PyObject *item, PyObject *err) |
| 494 | { |
| 495 | Py_off_t result; |
| 496 | PyObject *runerr; |
| 497 | PyObject *value = PyNumber_Index(item); |
| 498 | if (value == NULL) |
| 499 | return -1; |
| 500 | |
| 501 | /* We're done if PyLong_AsSsize_t() returns without error. */ |
| 502 | result = PyLong_AsOff_t(value); |
| 503 | if (result != -1 || !(runerr = PyErr_Occurred())) |
| 504 | goto finish; |
| 505 | |
| 506 | /* Error handling code -- only manage OverflowError differently */ |
| 507 | if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError)) |
| 508 | goto finish; |
| 509 | |
| 510 | PyErr_Clear(); |
| 511 | /* If no error-handling desired then the default clipping |
| 512 | is sufficient. |
| 513 | */ |
| 514 | if (!err) { |
| 515 | assert(PyLong_Check(value)); |
| 516 | /* Whether or not it is less than or equal to |
| 517 | zero is determined by the sign of ob_size |
| 518 | */ |
| 519 | if (_PyLong_Sign(value) < 0) |
| 520 | result = PY_OFF_T_MIN; |
| 521 | else |
| 522 | result = PY_OFF_T_MAX; |
| 523 | } |
| 524 | else { |
| 525 | /* Otherwise replace the error with caller's error object. */ |
| 526 | PyErr_Format(err, |
| 527 | "cannot fit '%.200s' into an offset-sized integer", |
| 528 | item->ob_type->tp_name); |
| 529 | } |
| 530 | |
| 531 | finish: |
| 532 | Py_DECREF(value); |
| 533 | return result; |
| 534 | } |
| 535 | |
Benjamin Peterson | bf5ff76 | 2009-12-13 19:25:34 +0000 | [diff] [blame] | 536 | |
| 537 | /* 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] | 538 | int |
Benjamin Peterson | bf5ff76 | 2009-12-13 19:25:34 +0000 | [diff] [blame] | 539 | _PyIO_ConvertSsize_t(PyObject *obj, void *result) { |
| 540 | Py_ssize_t limit; |
| 541 | if (obj == Py_None) { |
| 542 | limit = -1; |
| 543 | } |
| 544 | else if (PyNumber_Check(obj)) { |
| 545 | limit = PyNumber_AsSsize_t(obj, PyExc_OverflowError); |
| 546 | if (limit == -1 && PyErr_Occurred()) |
| 547 | return 0; |
| 548 | } |
| 549 | else { |
| 550 | PyErr_Format(PyExc_TypeError, |
| 551 | "integer argument expected, got '%.200s'", |
| 552 | Py_TYPE(obj)->tp_name); |
| 553 | return 0; |
| 554 | } |
| 555 | *((Py_ssize_t *)result) = limit; |
| 556 | return 1; |
| 557 | } |
| 558 | |
| 559 | |
Antoine Pitrou | 712cb73 | 2013-12-21 15:51:54 +0100 | [diff] [blame] | 560 | _PyIO_State * |
| 561 | _PyIO_get_module_state(void) |
| 562 | { |
| 563 | PyObject *mod = PyState_FindModule(&_PyIO_Module); |
| 564 | _PyIO_State *state; |
| 565 | if (mod == NULL || (state = IO_MOD_STATE(mod)) == NULL) { |
| 566 | PyErr_SetString(PyExc_RuntimeError, |
| 567 | "could not find io module state " |
| 568 | "(interpreter shutdown?)"); |
| 569 | return NULL; |
| 570 | } |
| 571 | return state; |
| 572 | } |
| 573 | |
Antoine Pitrou | 932ff83 | 2013-08-01 21:04:50 +0200 | [diff] [blame] | 574 | PyObject * |
| 575 | _PyIO_get_locale_module(_PyIO_State *state) |
| 576 | { |
| 577 | PyObject *mod; |
| 578 | if (state->locale_module != NULL) { |
| 579 | assert(PyWeakref_CheckRef(state->locale_module)); |
| 580 | mod = PyWeakref_GET_OBJECT(state->locale_module); |
| 581 | if (mod != Py_None) { |
| 582 | Py_INCREF(mod); |
| 583 | return mod; |
| 584 | } |
| 585 | Py_CLEAR(state->locale_module); |
| 586 | } |
Antoine Pitrou | fd4722c | 2013-10-12 00:13:50 +0200 | [diff] [blame] | 587 | mod = PyImport_ImportModule("_bootlocale"); |
Antoine Pitrou | 932ff83 | 2013-08-01 21:04:50 +0200 | [diff] [blame] | 588 | if (mod == NULL) |
| 589 | return NULL; |
| 590 | state->locale_module = PyWeakref_NewRef(mod, NULL); |
| 591 | if (state->locale_module == NULL) { |
| 592 | Py_DECREF(mod); |
| 593 | return NULL; |
| 594 | } |
| 595 | return mod; |
| 596 | } |
| 597 | |
| 598 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 599 | static int |
| 600 | iomodule_traverse(PyObject *mod, visitproc visit, void *arg) { |
| 601 | _PyIO_State *state = IO_MOD_STATE(mod); |
| 602 | if (!state->initialized) |
| 603 | return 0; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 604 | if (state->locale_module != NULL) { |
| 605 | Py_VISIT(state->locale_module); |
| 606 | } |
| 607 | Py_VISIT(state->unsupported_operation); |
| 608 | return 0; |
| 609 | } |
| 610 | |
Benjamin Peterson | bf5ff76 | 2009-12-13 19:25:34 +0000 | [diff] [blame] | 611 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 612 | static int |
| 613 | iomodule_clear(PyObject *mod) { |
| 614 | _PyIO_State *state = IO_MOD_STATE(mod); |
| 615 | if (!state->initialized) |
| 616 | return 0; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 617 | if (state->locale_module != NULL) |
| 618 | Py_CLEAR(state->locale_module); |
| 619 | Py_CLEAR(state->unsupported_operation); |
| 620 | return 0; |
| 621 | } |
| 622 | |
| 623 | static void |
| 624 | iomodule_free(PyObject *mod) { |
| 625 | iomodule_clear(mod); |
| 626 | } |
| 627 | |
Benjamin Peterson | bf5ff76 | 2009-12-13 19:25:34 +0000 | [diff] [blame] | 628 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 629 | /* |
| 630 | * Module definition |
| 631 | */ |
| 632 | |
| 633 | static PyMethodDef module_methods[] = { |
| 634 | {"open", (PyCFunction)io_open, METH_VARARGS|METH_KEYWORDS, open_doc}, |
| 635 | {NULL, NULL} |
| 636 | }; |
| 637 | |
| 638 | struct PyModuleDef _PyIO_Module = { |
| 639 | PyModuleDef_HEAD_INIT, |
| 640 | "io", |
| 641 | module_doc, |
| 642 | sizeof(_PyIO_State), |
| 643 | module_methods, |
| 644 | NULL, |
| 645 | iomodule_traverse, |
| 646 | iomodule_clear, |
| 647 | (freefunc)iomodule_free, |
| 648 | }; |
| 649 | |
| 650 | PyMODINIT_FUNC |
| 651 | PyInit__io(void) |
| 652 | { |
| 653 | PyObject *m = PyModule_Create(&_PyIO_Module); |
| 654 | _PyIO_State *state = NULL; |
| 655 | if (m == NULL) |
| 656 | return NULL; |
| 657 | state = IO_MOD_STATE(m); |
| 658 | state->initialized = 0; |
| 659 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 660 | #define ADD_TYPE(type, name) \ |
| 661 | if (PyType_Ready(type) < 0) \ |
| 662 | goto fail; \ |
| 663 | Py_INCREF(type); \ |
| 664 | if (PyModule_AddObject(m, name, (PyObject *)type) < 0) { \ |
| 665 | Py_DECREF(type); \ |
| 666 | goto fail; \ |
| 667 | } |
| 668 | |
| 669 | /* DEFAULT_BUFFER_SIZE */ |
| 670 | if (PyModule_AddIntMacro(m, DEFAULT_BUFFER_SIZE) < 0) |
| 671 | goto fail; |
| 672 | |
| 673 | /* UnsupportedOperation inherits from ValueError and IOError */ |
| 674 | state->unsupported_operation = PyObject_CallFunction( |
| 675 | (PyObject *)&PyType_Type, "s(OO){}", |
| 676 | "UnsupportedOperation", PyExc_ValueError, PyExc_IOError); |
| 677 | if (state->unsupported_operation == NULL) |
| 678 | goto fail; |
| 679 | Py_INCREF(state->unsupported_operation); |
| 680 | if (PyModule_AddObject(m, "UnsupportedOperation", |
| 681 | state->unsupported_operation) < 0) |
| 682 | goto fail; |
| 683 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 684 | /* BlockingIOError, for compatibility */ |
| 685 | Py_INCREF(PyExc_BlockingIOError); |
| 686 | if (PyModule_AddObject(m, "BlockingIOError", |
| 687 | (PyObject *) PyExc_BlockingIOError) < 0) |
| 688 | goto fail; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 689 | |
| 690 | /* Concrete base types of the IO ABCs. |
| 691 | (the ABCs themselves are declared through inheritance in io.py) |
| 692 | */ |
| 693 | ADD_TYPE(&PyIOBase_Type, "_IOBase"); |
| 694 | ADD_TYPE(&PyRawIOBase_Type, "_RawIOBase"); |
| 695 | ADD_TYPE(&PyBufferedIOBase_Type, "_BufferedIOBase"); |
| 696 | ADD_TYPE(&PyTextIOBase_Type, "_TextIOBase"); |
| 697 | |
| 698 | /* Implementation of concrete IO objects. */ |
| 699 | /* FileIO */ |
| 700 | PyFileIO_Type.tp_base = &PyRawIOBase_Type; |
| 701 | ADD_TYPE(&PyFileIO_Type, "FileIO"); |
| 702 | |
| 703 | /* BytesIO */ |
| 704 | PyBytesIO_Type.tp_base = &PyBufferedIOBase_Type; |
| 705 | ADD_TYPE(&PyBytesIO_Type, "BytesIO"); |
Antoine Pitrou | 972ee13 | 2010-09-06 18:48:21 +0000 | [diff] [blame] | 706 | if (PyType_Ready(&_PyBytesIOBuffer_Type) < 0) |
| 707 | goto fail; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 708 | |
| 709 | /* StringIO */ |
| 710 | PyStringIO_Type.tp_base = &PyTextIOBase_Type; |
| 711 | ADD_TYPE(&PyStringIO_Type, "StringIO"); |
| 712 | |
| 713 | /* BufferedReader */ |
| 714 | PyBufferedReader_Type.tp_base = &PyBufferedIOBase_Type; |
| 715 | ADD_TYPE(&PyBufferedReader_Type, "BufferedReader"); |
| 716 | |
| 717 | /* BufferedWriter */ |
| 718 | PyBufferedWriter_Type.tp_base = &PyBufferedIOBase_Type; |
| 719 | ADD_TYPE(&PyBufferedWriter_Type, "BufferedWriter"); |
| 720 | |
| 721 | /* BufferedRWPair */ |
| 722 | PyBufferedRWPair_Type.tp_base = &PyBufferedIOBase_Type; |
| 723 | ADD_TYPE(&PyBufferedRWPair_Type, "BufferedRWPair"); |
| 724 | |
| 725 | /* BufferedRandom */ |
| 726 | PyBufferedRandom_Type.tp_base = &PyBufferedIOBase_Type; |
| 727 | ADD_TYPE(&PyBufferedRandom_Type, "BufferedRandom"); |
| 728 | |
| 729 | /* TextIOWrapper */ |
| 730 | PyTextIOWrapper_Type.tp_base = &PyTextIOBase_Type; |
| 731 | ADD_TYPE(&PyTextIOWrapper_Type, "TextIOWrapper"); |
| 732 | |
| 733 | /* IncrementalNewlineDecoder */ |
| 734 | ADD_TYPE(&PyIncrementalNewlineDecoder_Type, "IncrementalNewlineDecoder"); |
| 735 | |
| 736 | /* Interned strings */ |
Antoine Pitrou | fc1b6f0 | 2012-01-18 16:13:56 +0100 | [diff] [blame] | 737 | #define ADD_INTERNED(name) \ |
| 738 | if (!_PyIO_str_ ## name && \ |
| 739 | !(_PyIO_str_ ## name = PyUnicode_InternFromString(# name))) \ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 740 | goto fail; |
Antoine Pitrou | fc1b6f0 | 2012-01-18 16:13:56 +0100 | [diff] [blame] | 741 | |
| 742 | ADD_INTERNED(close) |
| 743 | ADD_INTERNED(closed) |
| 744 | ADD_INTERNED(decode) |
| 745 | ADD_INTERNED(encode) |
| 746 | ADD_INTERNED(fileno) |
| 747 | ADD_INTERNED(flush) |
| 748 | ADD_INTERNED(getstate) |
| 749 | ADD_INTERNED(isatty) |
| 750 | ADD_INTERNED(newlines) |
| 751 | ADD_INTERNED(read) |
| 752 | ADD_INTERNED(read1) |
| 753 | ADD_INTERNED(readable) |
Antoine Pitrou | bb5b92d | 2012-01-18 16:19:19 +0100 | [diff] [blame] | 754 | ADD_INTERNED(readall) |
Antoine Pitrou | fc1b6f0 | 2012-01-18 16:13:56 +0100 | [diff] [blame] | 755 | ADD_INTERNED(readinto) |
| 756 | ADD_INTERNED(readline) |
| 757 | ADD_INTERNED(reset) |
| 758 | ADD_INTERNED(seek) |
| 759 | ADD_INTERNED(seekable) |
| 760 | ADD_INTERNED(setstate) |
| 761 | ADD_INTERNED(tell) |
| 762 | ADD_INTERNED(truncate) |
| 763 | ADD_INTERNED(write) |
| 764 | ADD_INTERNED(writable) |
| 765 | |
| 766 | if (!_PyIO_str_nl && |
| 767 | !(_PyIO_str_nl = PyUnicode_InternFromString("\n"))) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 768 | goto fail; |
Antoine Pitrou | fc1b6f0 | 2012-01-18 16:13:56 +0100 | [diff] [blame] | 769 | |
| 770 | if (!_PyIO_empty_str && |
| 771 | !(_PyIO_empty_str = PyUnicode_FromStringAndSize(NULL, 0))) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 772 | goto fail; |
Antoine Pitrou | fc1b6f0 | 2012-01-18 16:13:56 +0100 | [diff] [blame] | 773 | if (!_PyIO_empty_bytes && |
| 774 | !(_PyIO_empty_bytes = PyBytes_FromStringAndSize(NULL, 0))) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 775 | goto fail; |
Antoine Pitrou | fc1b6f0 | 2012-01-18 16:13:56 +0100 | [diff] [blame] | 776 | if (!_PyIO_zero && |
| 777 | !(_PyIO_zero = PyLong_FromLong(0L))) |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 778 | goto fail; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 779 | |
| 780 | state->initialized = 1; |
| 781 | |
| 782 | return m; |
| 783 | |
| 784 | fail: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 785 | Py_XDECREF(state->unsupported_operation); |
| 786 | Py_DECREF(m); |
| 787 | return NULL; |
| 788 | } |