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