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" |
| 3 | |
| 4 | Classes defined here: UnsupportedOperation, BlockingIOError. |
| 5 | Functions defined here: open(). |
| 6 | |
| 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" |
| 62 | "seperation between reading and writing to streams; implementations are\n" |
| 63 | "allowed to throw an IOError if they do not support a given operation.\n" |
| 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 | /* |
| 94 | * BlockingIOError extends IOError |
| 95 | */ |
| 96 | |
| 97 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 98 | blockingioerror_init(PyBlockingIOErrorObject *self, PyObject *args, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 99 | PyObject *kwds) |
| 100 | { |
| 101 | PyObject *myerrno = NULL, *strerror = NULL; |
| 102 | PyObject *baseargs = NULL; |
| 103 | Py_ssize_t written = 0; |
| 104 | |
| 105 | assert(PyTuple_Check(args)); |
| 106 | |
| 107 | self->written = 0; |
| 108 | if (!PyArg_ParseTuple(args, "OO|n:BlockingIOError", |
| 109 | &myerrno, &strerror, &written)) |
| 110 | return -1; |
| 111 | |
| 112 | baseargs = PyTuple_Pack(2, myerrno, strerror); |
| 113 | if (baseargs == NULL) |
| 114 | return -1; |
| 115 | /* This will take care of initializing of myerrno and strerror members */ |
| 116 | if (((PyTypeObject *)PyExc_IOError)->tp_init( |
| 117 | (PyObject *)self, baseargs, kwds) == -1) { |
| 118 | Py_DECREF(baseargs); |
| 119 | return -1; |
| 120 | } |
| 121 | Py_DECREF(baseargs); |
| 122 | |
| 123 | self->written = written; |
| 124 | return 0; |
| 125 | } |
| 126 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 127 | static PyMemberDef blockingioerror_members[] = { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 128 | {"characters_written", T_PYSSIZET, offsetof(PyBlockingIOErrorObject, written), 0}, |
| 129 | {NULL} /* Sentinel */ |
| 130 | }; |
| 131 | |
| 132 | static PyTypeObject _PyExc_BlockingIOError = { |
| 133 | PyVarObject_HEAD_INIT(NULL, 0) |
| 134 | "BlockingIOError", /*tp_name*/ |
| 135 | sizeof(PyBlockingIOErrorObject), /*tp_basicsize*/ |
| 136 | 0, /*tp_itemsize*/ |
| 137 | 0, /*tp_dealloc*/ |
| 138 | 0, /*tp_print*/ |
| 139 | 0, /*tp_getattr*/ |
| 140 | 0, /*tp_setattr*/ |
| 141 | 0, /*tp_compare */ |
| 142 | 0, /*tp_repr*/ |
| 143 | 0, /*tp_as_number*/ |
| 144 | 0, /*tp_as_sequence*/ |
| 145 | 0, /*tp_as_mapping*/ |
| 146 | 0, /*tp_hash */ |
| 147 | 0, /*tp_call*/ |
| 148 | 0, /*tp_str*/ |
| 149 | 0, /*tp_getattro*/ |
| 150 | 0, /*tp_setattro*/ |
| 151 | 0, /*tp_as_buffer*/ |
| 152 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
| 153 | PyDoc_STR("Exception raised when I/O would block " |
| 154 | "on a non-blocking I/O stream"), /* tp_doc */ |
| 155 | 0, /* tp_traverse */ |
| 156 | 0, /* tp_clear */ |
| 157 | 0, /* tp_richcompare */ |
| 158 | 0, /* tp_weaklistoffset */ |
| 159 | 0, /* tp_iter */ |
| 160 | 0, /* tp_iternext */ |
| 161 | 0, /* tp_methods */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 162 | blockingioerror_members, /* tp_members */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 163 | 0, /* tp_getset */ |
| 164 | 0, /* tp_base */ |
| 165 | 0, /* tp_dict */ |
| 166 | 0, /* tp_descr_get */ |
| 167 | 0, /* tp_descr_set */ |
| 168 | 0, /* tp_dictoffset */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 169 | (initproc)blockingioerror_init, /* tp_init */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 170 | 0, /* tp_alloc */ |
| 171 | 0, /* tp_new */ |
| 172 | }; |
| 173 | PyObject *PyExc_BlockingIOError = (PyObject *)&_PyExc_BlockingIOError; |
| 174 | |
| 175 | |
| 176 | /* |
| 177 | * The main open() function |
| 178 | */ |
| 179 | PyDoc_STRVAR(open_doc, |
Georg Brandl | e40ee50 | 2010-07-11 09:33:39 +0000 | [diff] [blame] | 180 | "open(file, mode='r', buffering=-1, encoding=None,\n" |
Georg Brandl | 5e8f6d1 | 2009-12-23 10:30:45 +0000 | [diff] [blame] | 181 | " errors=None, newline=None, closefd=True) -> file object\n" |
| 182 | "\n" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 183 | "Open file and return a stream. Raise IOError upon failure.\n" |
| 184 | "\n" |
| 185 | "file is either a text or byte string giving the name (and the path\n" |
| 186 | "if the file isn't in the current working directory) of the file to\n" |
| 187 | "be opened or an integer file descriptor of the file to be\n" |
| 188 | "wrapped. (If a file descriptor is given, it is closed when the\n" |
| 189 | "returned I/O object is closed, unless closefd is set to False.)\n" |
| 190 | "\n" |
| 191 | "mode is an optional string that specifies the mode in which the file\n" |
| 192 | "is opened. It defaults to 'r' which means open for reading in text\n" |
| 193 | "mode. Other common values are 'w' for writing (truncating the file if\n" |
| 194 | "it already exists), and 'a' for appending (which on some Unix systems,\n" |
| 195 | "means that all writes append to the end of the file regardless of the\n" |
| 196 | "current seek position). In text mode, if encoding is not specified the\n" |
| 197 | "encoding used is platform dependent. (For reading and writing raw\n" |
| 198 | "bytes use binary mode and leave encoding unspecified.) The available\n" |
| 199 | "modes are:\n" |
| 200 | "\n" |
| 201 | "========= ===============================================================\n" |
| 202 | "Character Meaning\n" |
| 203 | "--------- ---------------------------------------------------------------\n" |
| 204 | "'r' open for reading (default)\n" |
| 205 | "'w' open for writing, truncating the file first\n" |
| 206 | "'a' open for writing, appending to the end of the file if it exists\n" |
| 207 | "'b' binary mode\n" |
| 208 | "'t' text mode (default)\n" |
| 209 | "'+' open a disk file for updating (reading and writing)\n" |
| 210 | "'U' universal newline mode (for backwards compatibility; unneeded\n" |
| 211 | " for new code)\n" |
| 212 | "========= ===============================================================\n" |
| 213 | "\n" |
| 214 | "The default mode is 'rt' (open for reading text). For binary random\n" |
| 215 | "access, the mode 'w+b' opens and truncates the file to 0 bytes, while\n" |
| 216 | "'r+b' opens the file without truncation.\n" |
| 217 | "\n" |
| 218 | "Python distinguishes between files opened in binary and text modes,\n" |
| 219 | "even when the underlying operating system doesn't. Files opened in\n" |
| 220 | "binary mode (appending 'b' to the mode argument) return contents as\n" |
| 221 | "bytes objects without any decoding. In text mode (the default, or when\n" |
| 222 | "'t' is appended to the mode argument), the contents of the file are\n" |
| 223 | "returned as strings, the bytes having been first decoded using a\n" |
| 224 | "platform-dependent encoding or using the specified encoding if given.\n" |
| 225 | "\n" |
Antoine Pitrou | d5587bc | 2009-12-19 21:08:31 +0000 | [diff] [blame] | 226 | "buffering is an optional integer used to set the buffering policy.\n" |
| 227 | "Pass 0 to switch buffering off (only allowed in binary mode), 1 to select\n" |
| 228 | "line buffering (only usable in text mode), and an integer > 1 to indicate\n" |
| 229 | "the size of a fixed-size chunk buffer. When no buffering argument is\n" |
| 230 | "given, the default buffering policy works as follows:\n" |
| 231 | "\n" |
| 232 | "* Binary files are buffered in fixed-size chunks; the size of the buffer\n" |
| 233 | " is chosen using a heuristic trying to determine the underlying device's\n" |
| 234 | " \"block size\" and falling back on `io.DEFAULT_BUFFER_SIZE`.\n" |
| 235 | " On many systems, the buffer will typically be 4096 or 8192 bytes long.\n" |
| 236 | "\n" |
| 237 | "* \"Interactive\" text files (files for which isatty() returns True)\n" |
| 238 | " use line buffering. Other text files use the policy described above\n" |
| 239 | " for binary files.\n" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 240 | "\n" |
| 241 | "encoding is the name of the encoding used to decode or encode the\n" |
| 242 | "file. This should only be used in text mode. The default encoding is\n" |
| 243 | "platform dependent, but any encoding supported by Python can be\n" |
| 244 | "passed. See the codecs module for the list of supported encodings.\n" |
| 245 | "\n" |
| 246 | "errors is an optional string that specifies how encoding errors are to\n" |
| 247 | "be handled---this argument should not be used in binary mode. Pass\n" |
| 248 | "'strict' to raise a ValueError exception if there is an encoding error\n" |
| 249 | "(the default of None has the same effect), or pass 'ignore' to ignore\n" |
| 250 | "errors. (Note that ignoring encoding errors can lead to data loss.)\n" |
| 251 | "See the documentation for codecs.register for a list of the permitted\n" |
| 252 | "encoding error strings.\n" |
| 253 | "\n" |
| 254 | "newline controls how universal newlines works (it only applies to text\n" |
| 255 | "mode). It can be None, '', '\\n', '\\r', and '\\r\\n'. It works as\n" |
| 256 | "follows:\n" |
| 257 | "\n" |
| 258 | "* On input, if newline is None, universal newlines mode is\n" |
| 259 | " enabled. Lines in the input can end in '\\n', '\\r', or '\\r\\n', and\n" |
| 260 | " these are translated into '\\n' before being returned to the\n" |
| 261 | " caller. If it is '', universal newline mode is enabled, but line\n" |
| 262 | " endings are returned to the caller untranslated. If it has any of\n" |
| 263 | " the other legal values, input lines are only terminated by the given\n" |
| 264 | " string, and the line ending is returned to the caller untranslated.\n" |
| 265 | "\n" |
| 266 | "* On output, if newline is None, any '\\n' characters written are\n" |
| 267 | " translated to the system default line separator, os.linesep. If\n" |
| 268 | " newline is '', no translation takes place. If newline is any of the\n" |
| 269 | " other legal values, any '\\n' characters written are translated to\n" |
| 270 | " the given string.\n" |
| 271 | "\n" |
| 272 | "If closefd is False, the underlying file descriptor will be kept open\n" |
| 273 | "when the file is closed. This does not work when a file name is given\n" |
| 274 | "and must be True in that case.\n" |
| 275 | "\n" |
| 276 | "open() returns a file object whose type depends on the mode, and\n" |
| 277 | "through which the standard file operations such as reading and writing\n" |
| 278 | "are performed. When open() is used to open a file in a text mode ('w',\n" |
| 279 | "'r', 'wt', 'rt', etc.), it returns a TextIOWrapper. When used to open\n" |
| 280 | "a file in a binary mode, the returned class varies: in read binary\n" |
| 281 | "mode, it returns a BufferedReader; in write binary and append binary\n" |
| 282 | "modes, it returns a BufferedWriter, and in read/write mode, it returns\n" |
| 283 | "a BufferedRandom.\n" |
| 284 | "\n" |
| 285 | "It is also possible to use a string or bytearray as a file for both\n" |
| 286 | "reading and writing. For strings StringIO can be used like a file\n" |
| 287 | "opened in a text mode, and for bytes a BytesIO can be used like a file\n" |
| 288 | "opened in a binary mode.\n" |
| 289 | ); |
| 290 | |
| 291 | static PyObject * |
| 292 | io_open(PyObject *self, PyObject *args, PyObject *kwds) |
| 293 | { |
| 294 | char *kwlist[] = {"file", "mode", "buffering", |
| 295 | "encoding", "errors", "newline", |
| 296 | "closefd", NULL}; |
| 297 | PyObject *file; |
| 298 | char *mode = "r"; |
| 299 | int buffering = -1, closefd = 1; |
| 300 | char *encoding = NULL, *errors = NULL, *newline = NULL; |
| 301 | unsigned i; |
| 302 | |
| 303 | int reading = 0, writing = 0, appending = 0, updating = 0; |
| 304 | int text = 0, binary = 0, universal = 0; |
| 305 | |
| 306 | char rawmode[5], *m; |
| 307 | int line_buffering, isatty; |
| 308 | |
| 309 | PyObject *raw, *modeobj = NULL, *buffer = NULL, *wrapper = NULL; |
| 310 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame^] | 311 | _Py_identifier(isatty); |
| 312 | _Py_identifier(fileno); |
| 313 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 314 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|sizzzi:open", kwlist, |
| 315 | &file, &mode, &buffering, |
| 316 | &encoding, &errors, &newline, |
| 317 | &closefd)) { |
| 318 | return NULL; |
| 319 | } |
| 320 | |
| 321 | if (!PyUnicode_Check(file) && |
| 322 | !PyBytes_Check(file) && |
| 323 | !PyNumber_Check(file)) { |
| 324 | PyErr_Format(PyExc_TypeError, "invalid file: %R", file); |
| 325 | return NULL; |
| 326 | } |
| 327 | |
| 328 | /* Decode mode */ |
| 329 | for (i = 0; i < strlen(mode); i++) { |
| 330 | char c = mode[i]; |
| 331 | |
| 332 | switch (c) { |
| 333 | case 'r': |
| 334 | reading = 1; |
| 335 | break; |
| 336 | case 'w': |
| 337 | writing = 1; |
| 338 | break; |
| 339 | case 'a': |
| 340 | appending = 1; |
| 341 | break; |
| 342 | case '+': |
| 343 | updating = 1; |
| 344 | break; |
| 345 | case 't': |
| 346 | text = 1; |
| 347 | break; |
| 348 | case 'b': |
| 349 | binary = 1; |
| 350 | break; |
| 351 | case 'U': |
| 352 | universal = 1; |
| 353 | reading = 1; |
| 354 | break; |
| 355 | default: |
| 356 | goto invalid_mode; |
| 357 | } |
| 358 | |
| 359 | /* c must not be duplicated */ |
| 360 | if (strchr(mode+i+1, c)) { |
| 361 | invalid_mode: |
| 362 | PyErr_Format(PyExc_ValueError, "invalid mode: '%s'", mode); |
| 363 | return NULL; |
| 364 | } |
| 365 | |
| 366 | } |
| 367 | |
| 368 | m = rawmode; |
| 369 | if (reading) *(m++) = 'r'; |
| 370 | if (writing) *(m++) = 'w'; |
| 371 | if (appending) *(m++) = 'a'; |
| 372 | if (updating) *(m++) = '+'; |
| 373 | *m = '\0'; |
| 374 | |
| 375 | /* Parameters validation */ |
| 376 | if (universal) { |
| 377 | if (writing || appending) { |
| 378 | PyErr_SetString(PyExc_ValueError, |
| 379 | "can't use U and writing mode at once"); |
| 380 | return NULL; |
| 381 | } |
| 382 | reading = 1; |
| 383 | } |
| 384 | |
| 385 | if (text && binary) { |
| 386 | PyErr_SetString(PyExc_ValueError, |
| 387 | "can't have text and binary mode at once"); |
| 388 | return NULL; |
| 389 | } |
| 390 | |
| 391 | if (reading + writing + appending > 1) { |
| 392 | PyErr_SetString(PyExc_ValueError, |
| 393 | "must have exactly one of read/write/append mode"); |
| 394 | return NULL; |
| 395 | } |
| 396 | |
| 397 | if (binary && encoding != NULL) { |
| 398 | PyErr_SetString(PyExc_ValueError, |
| 399 | "binary mode doesn't take an encoding argument"); |
| 400 | return NULL; |
| 401 | } |
| 402 | |
| 403 | if (binary && errors != NULL) { |
| 404 | PyErr_SetString(PyExc_ValueError, |
| 405 | "binary mode doesn't take an errors argument"); |
| 406 | return NULL; |
| 407 | } |
| 408 | |
| 409 | if (binary && newline != NULL) { |
| 410 | PyErr_SetString(PyExc_ValueError, |
| 411 | "binary mode doesn't take a newline argument"); |
| 412 | return NULL; |
| 413 | } |
| 414 | |
| 415 | /* Create the Raw file stream */ |
| 416 | raw = PyObject_CallFunction((PyObject *)&PyFileIO_Type, |
| 417 | "Osi", file, rawmode, closefd); |
| 418 | if (raw == NULL) |
| 419 | return NULL; |
| 420 | |
| 421 | modeobj = PyUnicode_FromString(mode); |
| 422 | if (modeobj == NULL) |
| 423 | goto error; |
| 424 | |
| 425 | /* buffering */ |
| 426 | { |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame^] | 427 | PyObject *res = _PyObject_CallMethodId(raw, &PyId_isatty, NULL); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 428 | if (res == NULL) |
| 429 | goto error; |
| 430 | isatty = PyLong_AsLong(res); |
| 431 | Py_DECREF(res); |
| 432 | if (isatty == -1 && PyErr_Occurred()) |
| 433 | goto error; |
| 434 | } |
| 435 | |
| 436 | if (buffering == 1 || (buffering < 0 && isatty)) { |
| 437 | buffering = -1; |
| 438 | line_buffering = 1; |
| 439 | } |
| 440 | else |
| 441 | line_buffering = 0; |
| 442 | |
| 443 | if (buffering < 0) { |
| 444 | buffering = DEFAULT_BUFFER_SIZE; |
| 445 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
| 446 | { |
| 447 | struct stat st; |
| 448 | long fileno; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame^] | 449 | PyObject *res = _PyObject_CallMethodId(raw, &PyId_fileno, NULL); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 450 | if (res == NULL) |
| 451 | goto error; |
| 452 | |
| 453 | fileno = PyLong_AsLong(res); |
| 454 | Py_DECREF(res); |
| 455 | if (fileno == -1 && PyErr_Occurred()) |
| 456 | goto error; |
| 457 | |
Antoine Pitrou | ea5d17d | 2010-10-27 19:45:43 +0000 | [diff] [blame] | 458 | if (fstat(fileno, &st) >= 0 && st.st_blksize > 1) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 459 | buffering = st.st_blksize; |
| 460 | } |
| 461 | #endif |
| 462 | } |
| 463 | if (buffering < 0) { |
| 464 | PyErr_SetString(PyExc_ValueError, |
| 465 | "invalid buffering size"); |
| 466 | goto error; |
| 467 | } |
| 468 | |
| 469 | /* if not buffering, returns the raw file object */ |
| 470 | if (buffering == 0) { |
| 471 | if (!binary) { |
| 472 | PyErr_SetString(PyExc_ValueError, |
| 473 | "can't have unbuffered text I/O"); |
| 474 | goto error; |
| 475 | } |
| 476 | |
| 477 | Py_DECREF(modeobj); |
| 478 | return raw; |
| 479 | } |
| 480 | |
| 481 | /* wraps into a buffered file */ |
| 482 | { |
| 483 | PyObject *Buffered_class; |
| 484 | |
| 485 | if (updating) |
| 486 | Buffered_class = (PyObject *)&PyBufferedRandom_Type; |
| 487 | else if (writing || appending) |
| 488 | Buffered_class = (PyObject *)&PyBufferedWriter_Type; |
| 489 | else if (reading) |
| 490 | Buffered_class = (PyObject *)&PyBufferedReader_Type; |
| 491 | else { |
| 492 | PyErr_Format(PyExc_ValueError, |
| 493 | "unknown mode: '%s'", mode); |
| 494 | goto error; |
| 495 | } |
| 496 | |
| 497 | buffer = PyObject_CallFunction(Buffered_class, "Oi", raw, buffering); |
| 498 | } |
| 499 | Py_CLEAR(raw); |
| 500 | if (buffer == NULL) |
| 501 | goto error; |
| 502 | |
| 503 | |
| 504 | /* if binary, returns the buffered file */ |
| 505 | if (binary) { |
| 506 | Py_DECREF(modeobj); |
| 507 | return buffer; |
| 508 | } |
| 509 | |
| 510 | /* wraps into a TextIOWrapper */ |
| 511 | wrapper = PyObject_CallFunction((PyObject *)&PyTextIOWrapper_Type, |
| 512 | "Osssi", |
| 513 | buffer, |
| 514 | encoding, errors, newline, |
| 515 | line_buffering); |
| 516 | Py_CLEAR(buffer); |
| 517 | if (wrapper == NULL) |
| 518 | goto error; |
| 519 | |
| 520 | if (PyObject_SetAttrString(wrapper, "mode", modeobj) < 0) |
| 521 | goto error; |
| 522 | Py_DECREF(modeobj); |
| 523 | return wrapper; |
| 524 | |
| 525 | error: |
| 526 | Py_XDECREF(raw); |
| 527 | Py_XDECREF(modeobj); |
| 528 | Py_XDECREF(buffer); |
| 529 | Py_XDECREF(wrapper); |
| 530 | return NULL; |
| 531 | } |
| 532 | |
| 533 | /* |
| 534 | * Private helpers for the io module. |
| 535 | */ |
| 536 | |
| 537 | Py_off_t |
| 538 | PyNumber_AsOff_t(PyObject *item, PyObject *err) |
| 539 | { |
| 540 | Py_off_t result; |
| 541 | PyObject *runerr; |
| 542 | PyObject *value = PyNumber_Index(item); |
| 543 | if (value == NULL) |
| 544 | return -1; |
| 545 | |
| 546 | /* We're done if PyLong_AsSsize_t() returns without error. */ |
| 547 | result = PyLong_AsOff_t(value); |
| 548 | if (result != -1 || !(runerr = PyErr_Occurred())) |
| 549 | goto finish; |
| 550 | |
| 551 | /* Error handling code -- only manage OverflowError differently */ |
| 552 | if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError)) |
| 553 | goto finish; |
| 554 | |
| 555 | PyErr_Clear(); |
| 556 | /* If no error-handling desired then the default clipping |
| 557 | is sufficient. |
| 558 | */ |
| 559 | if (!err) { |
| 560 | assert(PyLong_Check(value)); |
| 561 | /* Whether or not it is less than or equal to |
| 562 | zero is determined by the sign of ob_size |
| 563 | */ |
| 564 | if (_PyLong_Sign(value) < 0) |
| 565 | result = PY_OFF_T_MIN; |
| 566 | else |
| 567 | result = PY_OFF_T_MAX; |
| 568 | } |
| 569 | else { |
| 570 | /* Otherwise replace the error with caller's error object. */ |
| 571 | PyErr_Format(err, |
| 572 | "cannot fit '%.200s' into an offset-sized integer", |
| 573 | item->ob_type->tp_name); |
| 574 | } |
| 575 | |
| 576 | finish: |
| 577 | Py_DECREF(value); |
| 578 | return result; |
| 579 | } |
| 580 | |
Benjamin Peterson | bf5ff76 | 2009-12-13 19:25:34 +0000 | [diff] [blame] | 581 | |
| 582 | /* Basically the "n" format code with the ability to turn None into -1. */ |
| 583 | int |
| 584 | _PyIO_ConvertSsize_t(PyObject *obj, void *result) { |
| 585 | Py_ssize_t limit; |
| 586 | if (obj == Py_None) { |
| 587 | limit = -1; |
| 588 | } |
| 589 | else if (PyNumber_Check(obj)) { |
| 590 | limit = PyNumber_AsSsize_t(obj, PyExc_OverflowError); |
| 591 | if (limit == -1 && PyErr_Occurred()) |
| 592 | return 0; |
| 593 | } |
| 594 | else { |
| 595 | PyErr_Format(PyExc_TypeError, |
| 596 | "integer argument expected, got '%.200s'", |
| 597 | Py_TYPE(obj)->tp_name); |
| 598 | return 0; |
| 599 | } |
| 600 | *((Py_ssize_t *)result) = limit; |
| 601 | return 1; |
| 602 | } |
| 603 | |
| 604 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 605 | static int |
| 606 | iomodule_traverse(PyObject *mod, visitproc visit, void *arg) { |
| 607 | _PyIO_State *state = IO_MOD_STATE(mod); |
| 608 | if (!state->initialized) |
| 609 | return 0; |
| 610 | Py_VISIT(state->os_module); |
| 611 | if (state->locale_module != NULL) { |
| 612 | Py_VISIT(state->locale_module); |
| 613 | } |
| 614 | Py_VISIT(state->unsupported_operation); |
| 615 | return 0; |
| 616 | } |
| 617 | |
Benjamin Peterson | bf5ff76 | 2009-12-13 19:25:34 +0000 | [diff] [blame] | 618 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 619 | static int |
| 620 | iomodule_clear(PyObject *mod) { |
| 621 | _PyIO_State *state = IO_MOD_STATE(mod); |
| 622 | if (!state->initialized) |
| 623 | return 0; |
| 624 | Py_CLEAR(state->os_module); |
| 625 | if (state->locale_module != NULL) |
| 626 | Py_CLEAR(state->locale_module); |
| 627 | Py_CLEAR(state->unsupported_operation); |
| 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | static void |
| 632 | iomodule_free(PyObject *mod) { |
| 633 | iomodule_clear(mod); |
| 634 | } |
| 635 | |
Benjamin Peterson | bf5ff76 | 2009-12-13 19:25:34 +0000 | [diff] [blame] | 636 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 637 | /* |
| 638 | * Module definition |
| 639 | */ |
| 640 | |
| 641 | static PyMethodDef module_methods[] = { |
| 642 | {"open", (PyCFunction)io_open, METH_VARARGS|METH_KEYWORDS, open_doc}, |
| 643 | {NULL, NULL} |
| 644 | }; |
| 645 | |
| 646 | struct PyModuleDef _PyIO_Module = { |
| 647 | PyModuleDef_HEAD_INIT, |
| 648 | "io", |
| 649 | module_doc, |
| 650 | sizeof(_PyIO_State), |
| 651 | module_methods, |
| 652 | NULL, |
| 653 | iomodule_traverse, |
| 654 | iomodule_clear, |
| 655 | (freefunc)iomodule_free, |
| 656 | }; |
| 657 | |
| 658 | PyMODINIT_FUNC |
| 659 | PyInit__io(void) |
| 660 | { |
| 661 | PyObject *m = PyModule_Create(&_PyIO_Module); |
| 662 | _PyIO_State *state = NULL; |
| 663 | if (m == NULL) |
| 664 | return NULL; |
| 665 | state = IO_MOD_STATE(m); |
| 666 | state->initialized = 0; |
| 667 | |
| 668 | /* put os in the module state */ |
| 669 | state->os_module = PyImport_ImportModule("os"); |
| 670 | if (state->os_module == NULL) |
| 671 | goto fail; |
| 672 | |
| 673 | #define ADD_TYPE(type, name) \ |
| 674 | if (PyType_Ready(type) < 0) \ |
| 675 | goto fail; \ |
| 676 | Py_INCREF(type); \ |
| 677 | if (PyModule_AddObject(m, name, (PyObject *)type) < 0) { \ |
| 678 | Py_DECREF(type); \ |
| 679 | goto fail; \ |
| 680 | } |
| 681 | |
| 682 | /* DEFAULT_BUFFER_SIZE */ |
| 683 | if (PyModule_AddIntMacro(m, DEFAULT_BUFFER_SIZE) < 0) |
| 684 | goto fail; |
| 685 | |
| 686 | /* UnsupportedOperation inherits from ValueError and IOError */ |
| 687 | state->unsupported_operation = PyObject_CallFunction( |
| 688 | (PyObject *)&PyType_Type, "s(OO){}", |
| 689 | "UnsupportedOperation", PyExc_ValueError, PyExc_IOError); |
| 690 | if (state->unsupported_operation == NULL) |
| 691 | goto fail; |
| 692 | Py_INCREF(state->unsupported_operation); |
| 693 | if (PyModule_AddObject(m, "UnsupportedOperation", |
| 694 | state->unsupported_operation) < 0) |
| 695 | goto fail; |
| 696 | |
| 697 | /* BlockingIOError */ |
| 698 | _PyExc_BlockingIOError.tp_base = (PyTypeObject *) PyExc_IOError; |
| 699 | ADD_TYPE(&_PyExc_BlockingIOError, "BlockingIOError"); |
| 700 | |
| 701 | /* Concrete base types of the IO ABCs. |
| 702 | (the ABCs themselves are declared through inheritance in io.py) |
| 703 | */ |
| 704 | ADD_TYPE(&PyIOBase_Type, "_IOBase"); |
| 705 | ADD_TYPE(&PyRawIOBase_Type, "_RawIOBase"); |
| 706 | ADD_TYPE(&PyBufferedIOBase_Type, "_BufferedIOBase"); |
| 707 | ADD_TYPE(&PyTextIOBase_Type, "_TextIOBase"); |
| 708 | |
| 709 | /* Implementation of concrete IO objects. */ |
| 710 | /* FileIO */ |
| 711 | PyFileIO_Type.tp_base = &PyRawIOBase_Type; |
| 712 | ADD_TYPE(&PyFileIO_Type, "FileIO"); |
| 713 | |
| 714 | /* BytesIO */ |
| 715 | PyBytesIO_Type.tp_base = &PyBufferedIOBase_Type; |
| 716 | ADD_TYPE(&PyBytesIO_Type, "BytesIO"); |
Antoine Pitrou | 972ee13 | 2010-09-06 18:48:21 +0000 | [diff] [blame] | 717 | if (PyType_Ready(&_PyBytesIOBuffer_Type) < 0) |
| 718 | goto fail; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 719 | |
| 720 | /* StringIO */ |
| 721 | PyStringIO_Type.tp_base = &PyTextIOBase_Type; |
| 722 | ADD_TYPE(&PyStringIO_Type, "StringIO"); |
| 723 | |
| 724 | /* BufferedReader */ |
| 725 | PyBufferedReader_Type.tp_base = &PyBufferedIOBase_Type; |
| 726 | ADD_TYPE(&PyBufferedReader_Type, "BufferedReader"); |
| 727 | |
| 728 | /* BufferedWriter */ |
| 729 | PyBufferedWriter_Type.tp_base = &PyBufferedIOBase_Type; |
| 730 | ADD_TYPE(&PyBufferedWriter_Type, "BufferedWriter"); |
| 731 | |
| 732 | /* BufferedRWPair */ |
| 733 | PyBufferedRWPair_Type.tp_base = &PyBufferedIOBase_Type; |
| 734 | ADD_TYPE(&PyBufferedRWPair_Type, "BufferedRWPair"); |
| 735 | |
| 736 | /* BufferedRandom */ |
| 737 | PyBufferedRandom_Type.tp_base = &PyBufferedIOBase_Type; |
| 738 | ADD_TYPE(&PyBufferedRandom_Type, "BufferedRandom"); |
| 739 | |
| 740 | /* TextIOWrapper */ |
| 741 | PyTextIOWrapper_Type.tp_base = &PyTextIOBase_Type; |
| 742 | ADD_TYPE(&PyTextIOWrapper_Type, "TextIOWrapper"); |
| 743 | |
| 744 | /* IncrementalNewlineDecoder */ |
| 745 | ADD_TYPE(&PyIncrementalNewlineDecoder_Type, "IncrementalNewlineDecoder"); |
| 746 | |
| 747 | /* Interned strings */ |
| 748 | if (!(_PyIO_str_close = PyUnicode_InternFromString("close"))) |
| 749 | goto fail; |
| 750 | if (!(_PyIO_str_closed = PyUnicode_InternFromString("closed"))) |
| 751 | goto fail; |
| 752 | if (!(_PyIO_str_decode = PyUnicode_InternFromString("decode"))) |
| 753 | goto fail; |
| 754 | if (!(_PyIO_str_encode = PyUnicode_InternFromString("encode"))) |
| 755 | goto fail; |
| 756 | if (!(_PyIO_str_fileno = PyUnicode_InternFromString("fileno"))) |
| 757 | goto fail; |
| 758 | if (!(_PyIO_str_flush = PyUnicode_InternFromString("flush"))) |
| 759 | goto fail; |
| 760 | if (!(_PyIO_str_getstate = PyUnicode_InternFromString("getstate"))) |
| 761 | goto fail; |
| 762 | if (!(_PyIO_str_isatty = PyUnicode_InternFromString("isatty"))) |
| 763 | goto fail; |
| 764 | if (!(_PyIO_str_newlines = PyUnicode_InternFromString("newlines"))) |
| 765 | goto fail; |
| 766 | if (!(_PyIO_str_nl = PyUnicode_InternFromString("\n"))) |
| 767 | goto fail; |
| 768 | if (!(_PyIO_str_read = PyUnicode_InternFromString("read"))) |
| 769 | goto fail; |
| 770 | if (!(_PyIO_str_read1 = PyUnicode_InternFromString("read1"))) |
| 771 | goto fail; |
| 772 | if (!(_PyIO_str_readable = PyUnicode_InternFromString("readable"))) |
| 773 | goto fail; |
Victor Stinner | b57f108 | 2011-05-26 00:19:38 +0200 | [diff] [blame] | 774 | if (!(_PyIO_str_readall = PyUnicode_InternFromString("readall"))) |
| 775 | goto fail; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 776 | if (!(_PyIO_str_readinto = PyUnicode_InternFromString("readinto"))) |
| 777 | goto fail; |
| 778 | if (!(_PyIO_str_readline = PyUnicode_InternFromString("readline"))) |
| 779 | goto fail; |
| 780 | if (!(_PyIO_str_reset = PyUnicode_InternFromString("reset"))) |
| 781 | goto fail; |
| 782 | if (!(_PyIO_str_seek = PyUnicode_InternFromString("seek"))) |
| 783 | goto fail; |
| 784 | if (!(_PyIO_str_seekable = PyUnicode_InternFromString("seekable"))) |
| 785 | goto fail; |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 786 | if (!(_PyIO_str_setstate = PyUnicode_InternFromString("setstate"))) |
| 787 | goto fail; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 788 | if (!(_PyIO_str_tell = PyUnicode_InternFromString("tell"))) |
| 789 | goto fail; |
| 790 | if (!(_PyIO_str_truncate = PyUnicode_InternFromString("truncate"))) |
| 791 | goto fail; |
| 792 | if (!(_PyIO_str_write = PyUnicode_InternFromString("write"))) |
| 793 | goto fail; |
| 794 | if (!(_PyIO_str_writable = PyUnicode_InternFromString("writable"))) |
| 795 | goto fail; |
| 796 | |
| 797 | if (!(_PyIO_empty_str = PyUnicode_FromStringAndSize(NULL, 0))) |
| 798 | goto fail; |
| 799 | if (!(_PyIO_empty_bytes = PyBytes_FromStringAndSize(NULL, 0))) |
| 800 | goto fail; |
Antoine Pitrou | e450185 | 2009-05-14 18:55:55 +0000 | [diff] [blame] | 801 | if (!(_PyIO_zero = PyLong_FromLong(0L))) |
| 802 | goto fail; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 803 | |
| 804 | state->initialized = 1; |
| 805 | |
| 806 | return m; |
| 807 | |
| 808 | fail: |
| 809 | Py_XDECREF(state->os_module); |
| 810 | Py_XDECREF(state->unsupported_operation); |
| 811 | Py_DECREF(m); |
| 812 | return NULL; |
| 813 | } |