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