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