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