blob: 6f7af41382da28666a74e8cab42111a505ebce61 [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"
Brett Cannonefb00c02012-02-29 18:31:31 -05003
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00004 Classes defined here: UnsupportedOperation, BlockingIOError.
5 Functions defined here: open().
Brett Cannonefb00c02012-02-29 18:31:31 -05006
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00007 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"
Éric Araujofab97662012-02-26 02:14:08 +010062"separation between reading and writing to streams; implementations are\n"
Andrew Svetlov737fb892012-12-18 21:14:22 +020063"allowed to raise an IOError if they do not support a given operation.\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000064"\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"
Ross Lagerwall59142db2011-10-31 20:34:46 +020098" errors=None, newline=None, closefd=True, opener=None) -> file object\n"
Georg Brandl5e8f6d12009-12-23 10:30:45 +000099"\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"
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100111"it already exists), 'x' for creating and writing to a new file, and\n"
112"'a' for appending (which on some Unix systems, means that all writes\n"
113"append to the end of the file regardless of the current seek position).\n"
114"In text mode, if encoding is not specified the encoding used is platform\n"
Victor Stinnerf86a5e82012-06-05 13:43:22 +0200115"dependent: locale.getpreferredencoding(False) is called to get the\n"
116"current locale encoding. (For reading and writing raw bytes use binary\n"
117"mode and leave encoding unspecified.) The available modes are:\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000118"\n"
119"========= ===============================================================\n"
120"Character Meaning\n"
121"--------- ---------------------------------------------------------------\n"
122"'r' open for reading (default)\n"
123"'w' open for writing, truncating the file first\n"
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100124"'x' create a new file and open it for writing\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000125"'a' open for writing, appending to the end of the file if it exists\n"
126"'b' binary mode\n"
127"'t' text mode (default)\n"
128"'+' open a disk file for updating (reading and writing)\n"
Serhiy Storchaka6787a382013-11-23 22:12:06 +0200129"'U' universal newline mode (deprecated)\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000130"========= ===============================================================\n"
131"\n"
132"The default mode is 'rt' (open for reading text). For binary random\n"
133"access, the mode 'w+b' opens and truncates the file to 0 bytes, while\n"
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100134"'r+b' opens the file without truncation. The 'x' mode implies 'w' and\n"
135"raises an `FileExistsError` if the file already exists.\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000136"\n"
137"Python distinguishes between files opened in binary and text modes,\n"
138"even when the underlying operating system doesn't. Files opened in\n"
139"binary mode (appending 'b' to the mode argument) return contents as\n"
140"bytes objects without any decoding. In text mode (the default, or when\n"
141"'t' is appended to the mode argument), the contents of the file are\n"
142"returned as strings, the bytes having been first decoded using a\n"
143"platform-dependent encoding or using the specified encoding if given.\n"
144"\n"
Serhiy Storchaka6787a382013-11-23 22:12:06 +0200145"'U' mode is deprecated and will raise an exception in future versions\n"
146"of Python. It has no effect in Python 3. Use newline to control\n"
147"universal newlines mode.\n"
148"\n"
Antoine Pitroud5587bc2009-12-19 21:08:31 +0000149"buffering is an optional integer used to set the buffering policy.\n"
150"Pass 0 to switch buffering off (only allowed in binary mode), 1 to select\n"
151"line buffering (only usable in text mode), and an integer > 1 to indicate\n"
152"the size of a fixed-size chunk buffer. When no buffering argument is\n"
153"given, the default buffering policy works as follows:\n"
154"\n"
155"* Binary files are buffered in fixed-size chunks; the size of the buffer\n"
156" is chosen using a heuristic trying to determine the underlying device's\n"
157" \"block size\" and falling back on `io.DEFAULT_BUFFER_SIZE`.\n"
158" On many systems, the buffer will typically be 4096 or 8192 bytes long.\n"
159"\n"
160"* \"Interactive\" text files (files for which isatty() returns True)\n"
161" use line buffering. Other text files use the policy described above\n"
162" for binary files.\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000163"\n"
164"encoding is the name of the encoding used to decode or encode the\n"
165"file. This should only be used in text mode. The default encoding is\n"
166"platform dependent, but any encoding supported by Python can be\n"
167"passed. See the codecs module for the list of supported encodings.\n"
168"\n"
169"errors is an optional string that specifies how encoding errors are to\n"
170"be handled---this argument should not be used in binary mode. Pass\n"
171"'strict' to raise a ValueError exception if there is an encoding error\n"
172"(the default of None has the same effect), or pass 'ignore' to ignore\n"
173"errors. (Note that ignoring encoding errors can lead to data loss.)\n"
Andrew Kuchlingc7b6c502013-06-16 12:58:48 -0400174"See the documentation for codecs.register or run 'help(codecs.Codec)'\n"
175"for a list of the permitted encoding error strings.\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000176"\n"
177"newline controls how universal newlines works (it only applies to text\n"
178"mode). It can be None, '', '\\n', '\\r', and '\\r\\n'. It works as\n"
179"follows:\n"
180"\n"
181"* On input, if newline is None, universal newlines mode is\n"
182" enabled. Lines in the input can end in '\\n', '\\r', or '\\r\\n', and\n"
183" these are translated into '\\n' before being returned to the\n"
184" caller. If it is '', universal newline mode is enabled, but line\n"
185" endings are returned to the caller untranslated. If it has any of\n"
186" the other legal values, input lines are only terminated by the given\n"
187" string, and the line ending is returned to the caller untranslated.\n"
188"\n"
189"* On output, if newline is None, any '\\n' characters written are\n"
190" translated to the system default line separator, os.linesep. If\n"
Ezio Melotti16d2b472012-09-18 07:20:18 +0300191" newline is '' or '\\n', no translation takes place. If newline is any\n"
Victor Stinner401e17d2012-08-04 01:18:56 +0200192" of the other legal values, any '\\n' characters written are translated\n"
193" to the given string.\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000194"\n"
195"If closefd is False, the underlying file descriptor will be kept open\n"
196"when the file is closed. This does not work when a file name is given\n"
197"and must be True in that case.\n"
198"\n"
Ross Lagerwall59142db2011-10-31 20:34:46 +0200199"A custom opener can be used by passing a callable as *opener*. The\n"
200"underlying file descriptor for the file object is then obtained by\n"
201"calling *opener* with (*file*, *flags*). *opener* must return an open\n"
202"file descriptor (passing os.open as *opener* results in functionality\n"
203"similar to passing None).\n"
204"\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000205"open() returns a file object whose type depends on the mode, and\n"
206"through which the standard file operations such as reading and writing\n"
207"are performed. When open() is used to open a file in a text mode ('w',\n"
208"'r', 'wt', 'rt', etc.), it returns a TextIOWrapper. When used to open\n"
209"a file in a binary mode, the returned class varies: in read binary\n"
210"mode, it returns a BufferedReader; in write binary and append binary\n"
211"modes, it returns a BufferedWriter, and in read/write mode, it returns\n"
212"a BufferedRandom.\n"
213"\n"
214"It is also possible to use a string or bytearray as a file for both\n"
215"reading and writing. For strings StringIO can be used like a file\n"
216"opened in a text mode, and for bytes a BytesIO can be used like a file\n"
217"opened in a binary mode.\n"
218 );
219
220static PyObject *
221io_open(PyObject *self, PyObject *args, PyObject *kwds)
222{
223 char *kwlist[] = {"file", "mode", "buffering",
224 "encoding", "errors", "newline",
Ross Lagerwall59142db2011-10-31 20:34:46 +0200225 "closefd", "opener", NULL};
226 PyObject *file, *opener = Py_None;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000227 char *mode = "r";
228 int buffering = -1, closefd = 1;
229 char *encoding = NULL, *errors = NULL, *newline = NULL;
230 unsigned i;
231
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100232 int creating = 0, reading = 0, writing = 0, appending = 0, updating = 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000233 int text = 0, binary = 0, universal = 0;
234
Christian Heimes89ff3c72012-09-10 03:50:48 +0200235 char rawmode[6], *m;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000236 int line_buffering, isatty;
237
Serhiy Storchakaf10063e2014-06-09 13:32:34 +0300238 PyObject *raw, *modeobj = NULL, *buffer, *wrapper, *result = NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000239
Antoine Pitroude687222014-06-29 20:07:28 -0400240 _Py_IDENTIFIER(_blksize);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200241 _Py_IDENTIFIER(isatty);
Martin v. Löwis767046a2011-10-14 15:35:36 +0200242 _Py_IDENTIFIER(mode);
Serhiy Storchakaf10063e2014-06-09 13:32:34 +0300243 _Py_IDENTIFIER(close);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200244
Ross Lagerwall59142db2011-10-31 20:34:46 +0200245 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|sizzziO:open", kwlist,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000246 &file, &mode, &buffering,
247 &encoding, &errors, &newline,
Ross Lagerwall59142db2011-10-31 20:34:46 +0200248 &closefd, &opener)) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000249 return NULL;
250 }
251
252 if (!PyUnicode_Check(file) &&
253 !PyBytes_Check(file) &&
254 !PyNumber_Check(file)) {
255 PyErr_Format(PyExc_TypeError, "invalid file: %R", file);
256 return NULL;
257 }
258
259 /* Decode mode */
260 for (i = 0; i < strlen(mode); i++) {
261 char c = mode[i];
262
263 switch (c) {
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100264 case 'x':
265 creating = 1;
266 break;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000267 case 'r':
268 reading = 1;
269 break;
270 case 'w':
271 writing = 1;
272 break;
273 case 'a':
274 appending = 1;
275 break;
276 case '+':
277 updating = 1;
278 break;
279 case 't':
280 text = 1;
281 break;
282 case 'b':
283 binary = 1;
284 break;
285 case 'U':
286 universal = 1;
287 reading = 1;
288 break;
289 default:
290 goto invalid_mode;
291 }
292
293 /* c must not be duplicated */
294 if (strchr(mode+i+1, c)) {
295 invalid_mode:
296 PyErr_Format(PyExc_ValueError, "invalid mode: '%s'", mode);
297 return NULL;
298 }
299
300 }
301
302 m = rawmode;
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100303 if (creating) *(m++) = 'x';
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000304 if (reading) *(m++) = 'r';
305 if (writing) *(m++) = 'w';
306 if (appending) *(m++) = 'a';
307 if (updating) *(m++) = '+';
308 *m = '\0';
309
310 /* Parameters validation */
311 if (universal) {
312 if (writing || appending) {
313 PyErr_SetString(PyExc_ValueError,
314 "can't use U and writing mode at once");
315 return NULL;
316 }
Serhiy Storchaka6787a382013-11-23 22:12:06 +0200317 if (PyErr_WarnEx(PyExc_DeprecationWarning,
318 "'U' mode is deprecated", 1) < 0)
319 return NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000320 reading = 1;
321 }
322
323 if (text && binary) {
324 PyErr_SetString(PyExc_ValueError,
325 "can't have text and binary mode at once");
326 return NULL;
327 }
328
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100329 if (creating + reading + writing + appending > 1) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000330 PyErr_SetString(PyExc_ValueError,
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100331 "must have exactly one of create/read/write/append mode");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000332 return NULL;
333 }
334
335 if (binary && encoding != NULL) {
336 PyErr_SetString(PyExc_ValueError,
337 "binary mode doesn't take an encoding argument");
338 return NULL;
339 }
340
341 if (binary && errors != NULL) {
342 PyErr_SetString(PyExc_ValueError,
343 "binary mode doesn't take an errors argument");
344 return NULL;
345 }
346
347 if (binary && newline != NULL) {
348 PyErr_SetString(PyExc_ValueError,
349 "binary mode doesn't take a newline argument");
350 return NULL;
351 }
352
353 /* Create the Raw file stream */
354 raw = PyObject_CallFunction((PyObject *)&PyFileIO_Type,
Ross Lagerwall59142db2011-10-31 20:34:46 +0200355 "OsiO", file, rawmode, closefd, opener);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000356 if (raw == NULL)
357 return NULL;
Serhiy Storchakaf10063e2014-06-09 13:32:34 +0300358 result = raw;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000359
360 modeobj = PyUnicode_FromString(mode);
361 if (modeobj == NULL)
362 goto error;
363
364 /* buffering */
365 {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200366 PyObject *res = _PyObject_CallMethodId(raw, &PyId_isatty, NULL);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000367 if (res == NULL)
368 goto error;
369 isatty = PyLong_AsLong(res);
370 Py_DECREF(res);
371 if (isatty == -1 && PyErr_Occurred())
372 goto error;
373 }
374
375 if (buffering == 1 || (buffering < 0 && isatty)) {
376 buffering = -1;
377 line_buffering = 1;
378 }
379 else
380 line_buffering = 0;
381
382 if (buffering < 0) {
Antoine Pitroude687222014-06-29 20:07:28 -0400383 PyObject *blksize_obj;
384 blksize_obj = _PyObject_GetAttrId(raw, &PyId__blksize);
385 if (blksize_obj == NULL)
386 goto error;
387 buffering = PyLong_AsLong(blksize_obj);
388 Py_DECREF(blksize_obj);
389 if (buffering == -1 && PyErr_Occurred())
390 goto error;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000391 }
392 if (buffering < 0) {
393 PyErr_SetString(PyExc_ValueError,
394 "invalid buffering size");
395 goto error;
396 }
397
398 /* if not buffering, returns the raw file object */
399 if (buffering == 0) {
400 if (!binary) {
401 PyErr_SetString(PyExc_ValueError,
402 "can't have unbuffered text I/O");
403 goto error;
404 }
405
406 Py_DECREF(modeobj);
Serhiy Storchakaf10063e2014-06-09 13:32:34 +0300407 return result;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000408 }
409
410 /* wraps into a buffered file */
411 {
412 PyObject *Buffered_class;
413
414 if (updating)
415 Buffered_class = (PyObject *)&PyBufferedRandom_Type;
Charles-François Natalidc3044c2012-01-09 22:40:02 +0100416 else if (creating || writing || appending)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000417 Buffered_class = (PyObject *)&PyBufferedWriter_Type;
418 else if (reading)
419 Buffered_class = (PyObject *)&PyBufferedReader_Type;
420 else {
421 PyErr_Format(PyExc_ValueError,
422 "unknown mode: '%s'", mode);
423 goto error;
424 }
425
426 buffer = PyObject_CallFunction(Buffered_class, "Oi", raw, buffering);
427 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000428 if (buffer == NULL)
429 goto error;
Serhiy Storchakaf10063e2014-06-09 13:32:34 +0300430 result = buffer;
431 Py_DECREF(raw);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000432
433
434 /* if binary, returns the buffered file */
435 if (binary) {
436 Py_DECREF(modeobj);
Serhiy Storchakaf10063e2014-06-09 13:32:34 +0300437 return result;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000438 }
439
440 /* wraps into a TextIOWrapper */
441 wrapper = PyObject_CallFunction((PyObject *)&PyTextIOWrapper_Type,
442 "Osssi",
443 buffer,
444 encoding, errors, newline,
445 line_buffering);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000446 if (wrapper == NULL)
447 goto error;
Serhiy Storchakaf10063e2014-06-09 13:32:34 +0300448 result = wrapper;
449 Py_DECREF(buffer);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000450
Martin v. Löwis767046a2011-10-14 15:35:36 +0200451 if (_PyObject_SetAttrId(wrapper, &PyId_mode, modeobj) < 0)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000452 goto error;
453 Py_DECREF(modeobj);
Serhiy Storchakaf10063e2014-06-09 13:32:34 +0300454 return result;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000455
456 error:
Serhiy Storchakaf10063e2014-06-09 13:32:34 +0300457 if (result != NULL) {
Benjamin Peterson4f654fb2014-07-04 17:00:25 -0700458 PyObject *exc, *val, *tb, *close_result;
Serhiy Storchakaf10063e2014-06-09 13:32:34 +0300459 PyErr_Fetch(&exc, &val, &tb);
Benjamin Peterson4f654fb2014-07-04 17:00:25 -0700460 close_result = _PyObject_CallMethodId(result, &PyId_close, NULL);
461 if (close_result != NULL) {
462 Py_DECREF(close_result);
Serhiy Storchakaf10063e2014-06-09 13:32:34 +0300463 PyErr_Restore(exc, val, tb);
Benjamin Peterson4f654fb2014-07-04 17:00:25 -0700464 } else {
Serhiy Storchaka76d3f142014-06-11 07:18:53 +0300465 PyObject *exc2, *val2, *tb2;
466 PyErr_Fetch(&exc2, &val2, &tb2);
Serhiy Storchakaf10063e2014-06-09 13:32:34 +0300467 PyErr_NormalizeException(&exc, &val, &tb);
468 Py_XDECREF(exc);
469 Py_XDECREF(tb);
Serhiy Storchaka76d3f142014-06-11 07:18:53 +0300470 PyErr_NormalizeException(&exc2, &val2, &tb2);
Serhiy Storchakaf10063e2014-06-09 13:32:34 +0300471 PyException_SetContext(val2, val);
Serhiy Storchaka76d3f142014-06-11 07:18:53 +0300472 PyErr_Restore(exc2, val2, tb2);
Serhiy Storchakaf10063e2014-06-09 13:32:34 +0300473 }
474 Py_DECREF(result);
475 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000476 Py_XDECREF(modeobj);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000477 return NULL;
478}
479
480/*
481 * Private helpers for the io module.
482 */
483
484Py_off_t
485PyNumber_AsOff_t(PyObject *item, PyObject *err)
486{
487 Py_off_t result;
488 PyObject *runerr;
489 PyObject *value = PyNumber_Index(item);
490 if (value == NULL)
491 return -1;
492
493 /* We're done if PyLong_AsSsize_t() returns without error. */
494 result = PyLong_AsOff_t(value);
495 if (result != -1 || !(runerr = PyErr_Occurred()))
496 goto finish;
497
498 /* Error handling code -- only manage OverflowError differently */
499 if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError))
500 goto finish;
501
502 PyErr_Clear();
503 /* If no error-handling desired then the default clipping
504 is sufficient.
505 */
506 if (!err) {
507 assert(PyLong_Check(value));
508 /* Whether or not it is less than or equal to
509 zero is determined by the sign of ob_size
510 */
511 if (_PyLong_Sign(value) < 0)
512 result = PY_OFF_T_MIN;
513 else
514 result = PY_OFF_T_MAX;
515 }
516 else {
517 /* Otherwise replace the error with caller's error object. */
518 PyErr_Format(err,
519 "cannot fit '%.200s' into an offset-sized integer",
520 item->ob_type->tp_name);
521 }
522
523 finish:
524 Py_DECREF(value);
525 return result;
526}
527
Benjamin Petersonbf5ff762009-12-13 19:25:34 +0000528
529/* Basically the "n" format code with the ability to turn None into -1. */
Brett Cannonefb00c02012-02-29 18:31:31 -0500530int
Benjamin Petersonbf5ff762009-12-13 19:25:34 +0000531_PyIO_ConvertSsize_t(PyObject *obj, void *result) {
532 Py_ssize_t limit;
533 if (obj == Py_None) {
534 limit = -1;
535 }
536 else if (PyNumber_Check(obj)) {
537 limit = PyNumber_AsSsize_t(obj, PyExc_OverflowError);
538 if (limit == -1 && PyErr_Occurred())
539 return 0;
540 }
541 else {
542 PyErr_Format(PyExc_TypeError,
543 "integer argument expected, got '%.200s'",
544 Py_TYPE(obj)->tp_name);
545 return 0;
546 }
547 *((Py_ssize_t *)result) = limit;
548 return 1;
549}
550
551
Antoine Pitrou712cb732013-12-21 15:51:54 +0100552_PyIO_State *
553_PyIO_get_module_state(void)
554{
555 PyObject *mod = PyState_FindModule(&_PyIO_Module);
556 _PyIO_State *state;
557 if (mod == NULL || (state = IO_MOD_STATE(mod)) == NULL) {
558 PyErr_SetString(PyExc_RuntimeError,
559 "could not find io module state "
560 "(interpreter shutdown?)");
561 return NULL;
562 }
563 return state;
564}
565
Antoine Pitrou932ff832013-08-01 21:04:50 +0200566PyObject *
567_PyIO_get_locale_module(_PyIO_State *state)
568{
569 PyObject *mod;
570 if (state->locale_module != NULL) {
571 assert(PyWeakref_CheckRef(state->locale_module));
572 mod = PyWeakref_GET_OBJECT(state->locale_module);
573 if (mod != Py_None) {
574 Py_INCREF(mod);
575 return mod;
576 }
577 Py_CLEAR(state->locale_module);
578 }
Antoine Pitroufd4722c2013-10-12 00:13:50 +0200579 mod = PyImport_ImportModule("_bootlocale");
Antoine Pitrou932ff832013-08-01 21:04:50 +0200580 if (mod == NULL)
581 return NULL;
582 state->locale_module = PyWeakref_NewRef(mod, NULL);
583 if (state->locale_module == NULL) {
584 Py_DECREF(mod);
585 return NULL;
586 }
587 return mod;
588}
589
590
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000591static int
592iomodule_traverse(PyObject *mod, visitproc visit, void *arg) {
593 _PyIO_State *state = IO_MOD_STATE(mod);
594 if (!state->initialized)
595 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000596 if (state->locale_module != NULL) {
597 Py_VISIT(state->locale_module);
598 }
599 Py_VISIT(state->unsupported_operation);
600 return 0;
601}
602
Benjamin Petersonbf5ff762009-12-13 19:25:34 +0000603
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000604static int
605iomodule_clear(PyObject *mod) {
606 _PyIO_State *state = IO_MOD_STATE(mod);
607 if (!state->initialized)
608 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000609 if (state->locale_module != NULL)
610 Py_CLEAR(state->locale_module);
611 Py_CLEAR(state->unsupported_operation);
612 return 0;
613}
614
615static void
616iomodule_free(PyObject *mod) {
617 iomodule_clear(mod);
618}
619
Benjamin Petersonbf5ff762009-12-13 19:25:34 +0000620
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000621/*
622 * Module definition
623 */
624
625static PyMethodDef module_methods[] = {
626 {"open", (PyCFunction)io_open, METH_VARARGS|METH_KEYWORDS, open_doc},
627 {NULL, NULL}
628};
629
630struct PyModuleDef _PyIO_Module = {
631 PyModuleDef_HEAD_INIT,
632 "io",
633 module_doc,
634 sizeof(_PyIO_State),
635 module_methods,
636 NULL,
637 iomodule_traverse,
638 iomodule_clear,
639 (freefunc)iomodule_free,
640};
641
642PyMODINIT_FUNC
643PyInit__io(void)
644{
645 PyObject *m = PyModule_Create(&_PyIO_Module);
646 _PyIO_State *state = NULL;
647 if (m == NULL)
648 return NULL;
649 state = IO_MOD_STATE(m);
650 state->initialized = 0;
651
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000652#define ADD_TYPE(type, name) \
653 if (PyType_Ready(type) < 0) \
654 goto fail; \
655 Py_INCREF(type); \
656 if (PyModule_AddObject(m, name, (PyObject *)type) < 0) { \
657 Py_DECREF(type); \
658 goto fail; \
659 }
660
661 /* DEFAULT_BUFFER_SIZE */
662 if (PyModule_AddIntMacro(m, DEFAULT_BUFFER_SIZE) < 0)
663 goto fail;
664
665 /* UnsupportedOperation inherits from ValueError and IOError */
666 state->unsupported_operation = PyObject_CallFunction(
667 (PyObject *)&PyType_Type, "s(OO){}",
668 "UnsupportedOperation", PyExc_ValueError, PyExc_IOError);
669 if (state->unsupported_operation == NULL)
670 goto fail;
671 Py_INCREF(state->unsupported_operation);
672 if (PyModule_AddObject(m, "UnsupportedOperation",
673 state->unsupported_operation) < 0)
674 goto fail;
675
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200676 /* BlockingIOError, for compatibility */
677 Py_INCREF(PyExc_BlockingIOError);
678 if (PyModule_AddObject(m, "BlockingIOError",
679 (PyObject *) PyExc_BlockingIOError) < 0)
680 goto fail;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000681
682 /* Concrete base types of the IO ABCs.
683 (the ABCs themselves are declared through inheritance in io.py)
684 */
685 ADD_TYPE(&PyIOBase_Type, "_IOBase");
686 ADD_TYPE(&PyRawIOBase_Type, "_RawIOBase");
687 ADD_TYPE(&PyBufferedIOBase_Type, "_BufferedIOBase");
688 ADD_TYPE(&PyTextIOBase_Type, "_TextIOBase");
689
690 /* Implementation of concrete IO objects. */
691 /* FileIO */
692 PyFileIO_Type.tp_base = &PyRawIOBase_Type;
693 ADD_TYPE(&PyFileIO_Type, "FileIO");
694
695 /* BytesIO */
696 PyBytesIO_Type.tp_base = &PyBufferedIOBase_Type;
697 ADD_TYPE(&PyBytesIO_Type, "BytesIO");
Antoine Pitrou972ee132010-09-06 18:48:21 +0000698 if (PyType_Ready(&_PyBytesIOBuffer_Type) < 0)
699 goto fail;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000700
701 /* StringIO */
702 PyStringIO_Type.tp_base = &PyTextIOBase_Type;
703 ADD_TYPE(&PyStringIO_Type, "StringIO");
704
705 /* BufferedReader */
706 PyBufferedReader_Type.tp_base = &PyBufferedIOBase_Type;
707 ADD_TYPE(&PyBufferedReader_Type, "BufferedReader");
708
709 /* BufferedWriter */
710 PyBufferedWriter_Type.tp_base = &PyBufferedIOBase_Type;
711 ADD_TYPE(&PyBufferedWriter_Type, "BufferedWriter");
712
713 /* BufferedRWPair */
714 PyBufferedRWPair_Type.tp_base = &PyBufferedIOBase_Type;
715 ADD_TYPE(&PyBufferedRWPair_Type, "BufferedRWPair");
716
717 /* BufferedRandom */
718 PyBufferedRandom_Type.tp_base = &PyBufferedIOBase_Type;
719 ADD_TYPE(&PyBufferedRandom_Type, "BufferedRandom");
720
721 /* TextIOWrapper */
722 PyTextIOWrapper_Type.tp_base = &PyTextIOBase_Type;
723 ADD_TYPE(&PyTextIOWrapper_Type, "TextIOWrapper");
724
725 /* IncrementalNewlineDecoder */
726 ADD_TYPE(&PyIncrementalNewlineDecoder_Type, "IncrementalNewlineDecoder");
727
728 /* Interned strings */
Antoine Pitroufc1b6f02012-01-18 16:13:56 +0100729#define ADD_INTERNED(name) \
730 if (!_PyIO_str_ ## name && \
731 !(_PyIO_str_ ## name = PyUnicode_InternFromString(# name))) \
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000732 goto fail;
Antoine Pitroufc1b6f02012-01-18 16:13:56 +0100733
734 ADD_INTERNED(close)
735 ADD_INTERNED(closed)
736 ADD_INTERNED(decode)
737 ADD_INTERNED(encode)
738 ADD_INTERNED(fileno)
739 ADD_INTERNED(flush)
740 ADD_INTERNED(getstate)
741 ADD_INTERNED(isatty)
742 ADD_INTERNED(newlines)
743 ADD_INTERNED(read)
744 ADD_INTERNED(read1)
745 ADD_INTERNED(readable)
Antoine Pitroubb5b92d2012-01-18 16:19:19 +0100746 ADD_INTERNED(readall)
Antoine Pitroufc1b6f02012-01-18 16:13:56 +0100747 ADD_INTERNED(readinto)
748 ADD_INTERNED(readline)
749 ADD_INTERNED(reset)
750 ADD_INTERNED(seek)
751 ADD_INTERNED(seekable)
752 ADD_INTERNED(setstate)
753 ADD_INTERNED(tell)
754 ADD_INTERNED(truncate)
755 ADD_INTERNED(write)
756 ADD_INTERNED(writable)
757
758 if (!_PyIO_str_nl &&
759 !(_PyIO_str_nl = PyUnicode_InternFromString("\n")))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000760 goto fail;
Antoine Pitroufc1b6f02012-01-18 16:13:56 +0100761
762 if (!_PyIO_empty_str &&
763 !(_PyIO_empty_str = PyUnicode_FromStringAndSize(NULL, 0)))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000764 goto fail;
Antoine Pitroufc1b6f02012-01-18 16:13:56 +0100765 if (!_PyIO_empty_bytes &&
766 !(_PyIO_empty_bytes = PyBytes_FromStringAndSize(NULL, 0)))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000767 goto fail;
Antoine Pitroufc1b6f02012-01-18 16:13:56 +0100768 if (!_PyIO_zero &&
769 !(_PyIO_zero = PyLong_FromLong(0L)))
Antoine Pitroue4501852009-05-14 18:55:55 +0000770 goto fail;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000771
772 state->initialized = 1;
773
774 return m;
775
776 fail:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000777 Py_XDECREF(state->unsupported_operation);
778 Py_DECREF(m);
779 return NULL;
780}