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