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