1 | /*
|
---|
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 |
|
---|
26 | PyObject *_PyIO_str_close;
|
---|
27 | PyObject *_PyIO_str_closed;
|
---|
28 | PyObject *_PyIO_str_decode;
|
---|
29 | PyObject *_PyIO_str_encode;
|
---|
30 | PyObject *_PyIO_str_fileno;
|
---|
31 | PyObject *_PyIO_str_flush;
|
---|
32 | PyObject *_PyIO_str_getstate;
|
---|
33 | PyObject *_PyIO_str_isatty;
|
---|
34 | PyObject *_PyIO_str_newlines;
|
---|
35 | PyObject *_PyIO_str_nl;
|
---|
36 | PyObject *_PyIO_str_read;
|
---|
37 | PyObject *_PyIO_str_read1;
|
---|
38 | PyObject *_PyIO_str_readable;
|
---|
39 | PyObject *_PyIO_str_readinto;
|
---|
40 | PyObject *_PyIO_str_readline;
|
---|
41 | PyObject *_PyIO_str_reset;
|
---|
42 | PyObject *_PyIO_str_seek;
|
---|
43 | PyObject *_PyIO_str_seekable;
|
---|
44 | PyObject *_PyIO_str_setstate;
|
---|
45 | PyObject *_PyIO_str_tell;
|
---|
46 | PyObject *_PyIO_str_truncate;
|
---|
47 | PyObject *_PyIO_str_writable;
|
---|
48 | PyObject *_PyIO_str_write;
|
---|
49 |
|
---|
50 | PyObject *_PyIO_empty_str;
|
---|
51 | PyObject *_PyIO_empty_bytes;
|
---|
52 | PyObject *_PyIO_zero;
|
---|
53 |
|
---|
54 | |
---|
55 |
|
---|
56 | PyDoc_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 | "separation between reading and writing to streams; implementations are\n"
|
---|
63 | "allowed to raise 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"
|
---|
66 | "writing of raw bytes to a stream. FileIO subclasses RawIOBase to provide\n"
|
---|
67 | "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 |
|
---|
94 | /*
|
---|
95 | * BlockingIOError extends IOError
|
---|
96 | */
|
---|
97 |
|
---|
98 | static int
|
---|
99 | blockingioerror_init(PyBlockingIOErrorObject *self, PyObject *args,
|
---|
100 | PyObject *kwds)
|
---|
101 | {
|
---|
102 | PyObject *myerrno = NULL, *strerror = NULL;
|
---|
103 | PyObject *baseargs = NULL;
|
---|
104 | Py_ssize_t written = 0;
|
---|
105 |
|
---|
106 | assert(PyTuple_Check(args));
|
---|
107 |
|
---|
108 | self->written = 0;
|
---|
109 | if (!PyArg_ParseTuple(args, "OO|n:BlockingIOError",
|
---|
110 | &myerrno, &strerror, &written))
|
---|
111 | return -1;
|
---|
112 |
|
---|
113 | baseargs = PyTuple_Pack(2, myerrno, strerror);
|
---|
114 | if (baseargs == NULL)
|
---|
115 | return -1;
|
---|
116 | /* This will take care of initializing of myerrno and strerror members */
|
---|
117 | if (((PyTypeObject *)PyExc_IOError)->tp_init(
|
---|
118 | (PyObject *)self, baseargs, kwds) == -1) {
|
---|
119 | Py_DECREF(baseargs);
|
---|
120 | return -1;
|
---|
121 | }
|
---|
122 | Py_DECREF(baseargs);
|
---|
123 |
|
---|
124 | self->written = written;
|
---|
125 | return 0;
|
---|
126 | }
|
---|
127 |
|
---|
128 | static PyMemberDef blockingioerror_members[] = {
|
---|
129 | {"characters_written", T_PYSSIZET, offsetof(PyBlockingIOErrorObject, written), 0},
|
---|
130 | {NULL} /* Sentinel */
|
---|
131 | };
|
---|
132 |
|
---|
133 | static PyTypeObject _PyExc_BlockingIOError = {
|
---|
134 | PyVarObject_HEAD_INIT(NULL, 0)
|
---|
135 | "BlockingIOError", /*tp_name*/
|
---|
136 | sizeof(PyBlockingIOErrorObject), /*tp_basicsize*/
|
---|
137 | 0, /*tp_itemsize*/
|
---|
138 | 0, /*tp_dealloc*/
|
---|
139 | 0, /*tp_print*/
|
---|
140 | 0, /*tp_getattr*/
|
---|
141 | 0, /*tp_setattr*/
|
---|
142 | 0, /*tp_compare */
|
---|
143 | 0, /*tp_repr*/
|
---|
144 | 0, /*tp_as_number*/
|
---|
145 | 0, /*tp_as_sequence*/
|
---|
146 | 0, /*tp_as_mapping*/
|
---|
147 | 0, /*tp_hash */
|
---|
148 | 0, /*tp_call*/
|
---|
149 | 0, /*tp_str*/
|
---|
150 | 0, /*tp_getattro*/
|
---|
151 | 0, /*tp_setattro*/
|
---|
152 | 0, /*tp_as_buffer*/
|
---|
153 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
|
---|
154 | PyDoc_STR("Exception raised when I/O would block "
|
---|
155 | "on a non-blocking I/O stream"), /* tp_doc */
|
---|
156 | 0, /* tp_traverse */
|
---|
157 | 0, /* tp_clear */
|
---|
158 | 0, /* tp_richcompare */
|
---|
159 | 0, /* tp_weaklistoffset */
|
---|
160 | 0, /* tp_iter */
|
---|
161 | 0, /* tp_iternext */
|
---|
162 | 0, /* tp_methods */
|
---|
163 | blockingioerror_members, /* tp_members */
|
---|
164 | 0, /* tp_getset */
|
---|
165 | 0, /* tp_base */
|
---|
166 | 0, /* tp_dict */
|
---|
167 | 0, /* tp_descr_get */
|
---|
168 | 0, /* tp_descr_set */
|
---|
169 | 0, /* tp_dictoffset */
|
---|
170 | (initproc)blockingioerror_init, /* tp_init */
|
---|
171 | 0, /* tp_alloc */
|
---|
172 | 0, /* tp_new */
|
---|
173 | };
|
---|
174 | PyObject *PyExc_BlockingIOError = (PyObject *)&_PyExc_BlockingIOError;
|
---|
175 | |
---|
176 |
|
---|
177 |
|
---|
178 | /*
|
---|
179 | * The main open() function
|
---|
180 | */
|
---|
181 | PyDoc_STRVAR(open_doc,
|
---|
182 | "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"
|
---|
225 | "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"
|
---|
239 | "\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"
|
---|
267 | " newline is '', no translation takes place. If newline is any of the\n"
|
---|
268 | " other legal values, any '\\n' characters written are translated to\n"
|
---|
269 | " the given string.\n"
|
---|
270 | "\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 |
|
---|
290 | static PyObject *
|
---|
291 | io_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;
|
---|
307 | long isatty;
|
---|
308 |
|
---|
309 | PyObject *raw, *modeobj = NULL, *buffer = NULL, *wrapper = NULL;
|
---|
310 |
|
---|
311 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|sizzzi:open", kwlist,
|
---|
312 | &file, &mode, &buffering,
|
---|
313 | &encoding, &errors, &newline,
|
---|
314 | &closefd)) {
|
---|
315 | return NULL;
|
---|
316 | }
|
---|
317 |
|
---|
318 | if (!PyUnicode_Check(file) &&
|
---|
319 | !PyBytes_Check(file) &&
|
---|
320 | !PyNumber_Check(file)) {
|
---|
321 | PyObject *repr = PyObject_Repr(file);
|
---|
322 | if (repr != NULL) {
|
---|
323 | PyErr_Format(PyExc_TypeError, "invalid file: %s",
|
---|
324 | PyString_AS_STRING(repr));
|
---|
325 | Py_DECREF(repr);
|
---|
326 | }
|
---|
327 | return NULL;
|
---|
328 | }
|
---|
329 |
|
---|
330 | /* Decode mode */
|
---|
331 | for (i = 0; i < strlen(mode); i++) {
|
---|
332 | char c = mode[i];
|
---|
333 |
|
---|
334 | switch (c) {
|
---|
335 | case 'r':
|
---|
336 | reading = 1;
|
---|
337 | break;
|
---|
338 | case 'w':
|
---|
339 | writing = 1;
|
---|
340 | break;
|
---|
341 | case 'a':
|
---|
342 | appending = 1;
|
---|
343 | break;
|
---|
344 | case '+':
|
---|
345 | updating = 1;
|
---|
346 | break;
|
---|
347 | case 't':
|
---|
348 | text = 1;
|
---|
349 | break;
|
---|
350 | case 'b':
|
---|
351 | binary = 1;
|
---|
352 | break;
|
---|
353 | case 'U':
|
---|
354 | universal = 1;
|
---|
355 | reading = 1;
|
---|
356 | break;
|
---|
357 | default:
|
---|
358 | goto invalid_mode;
|
---|
359 | }
|
---|
360 |
|
---|
361 | /* c must not be duplicated */
|
---|
362 | if (strchr(mode+i+1, c)) {
|
---|
363 | invalid_mode:
|
---|
364 | PyErr_Format(PyExc_ValueError, "invalid mode: '%s'", mode);
|
---|
365 | return NULL;
|
---|
366 | }
|
---|
367 |
|
---|
368 | }
|
---|
369 |
|
---|
370 | m = rawmode;
|
---|
371 | if (reading) *(m++) = 'r';
|
---|
372 | if (writing) *(m++) = 'w';
|
---|
373 | if (appending) *(m++) = 'a';
|
---|
374 | if (updating) *(m++) = '+';
|
---|
375 | *m = '\0';
|
---|
376 |
|
---|
377 | /* Parameters validation */
|
---|
378 | if (universal) {
|
---|
379 | if (writing || appending) {
|
---|
380 | PyErr_SetString(PyExc_ValueError,
|
---|
381 | "can't use U and writing mode at once");
|
---|
382 | return NULL;
|
---|
383 | }
|
---|
384 | reading = 1;
|
---|
385 | }
|
---|
386 |
|
---|
387 | if (text && binary) {
|
---|
388 | PyErr_SetString(PyExc_ValueError,
|
---|
389 | "can't have text and binary mode at once");
|
---|
390 | return NULL;
|
---|
391 | }
|
---|
392 |
|
---|
393 | if (reading + writing + appending > 1) {
|
---|
394 | PyErr_SetString(PyExc_ValueError,
|
---|
395 | "must have exactly one of read/write/append mode");
|
---|
396 | return NULL;
|
---|
397 | }
|
---|
398 |
|
---|
399 | if (binary && encoding != NULL) {
|
---|
400 | PyErr_SetString(PyExc_ValueError,
|
---|
401 | "binary mode doesn't take an encoding argument");
|
---|
402 | return NULL;
|
---|
403 | }
|
---|
404 |
|
---|
405 | if (binary && errors != NULL) {
|
---|
406 | PyErr_SetString(PyExc_ValueError,
|
---|
407 | "binary mode doesn't take an errors argument");
|
---|
408 | return NULL;
|
---|
409 | }
|
---|
410 |
|
---|
411 | if (binary && newline != NULL) {
|
---|
412 | PyErr_SetString(PyExc_ValueError,
|
---|
413 | "binary mode doesn't take a newline argument");
|
---|
414 | return NULL;
|
---|
415 | }
|
---|
416 |
|
---|
417 | /* Create the Raw file stream */
|
---|
418 | raw = PyObject_CallFunction((PyObject *)&PyFileIO_Type,
|
---|
419 | "Osi", file, rawmode, closefd);
|
---|
420 | if (raw == NULL)
|
---|
421 | return NULL;
|
---|
422 |
|
---|
423 | modeobj = PyUnicode_FromString(mode);
|
---|
424 | if (modeobj == NULL)
|
---|
425 | goto error;
|
---|
426 |
|
---|
427 | /* buffering */
|
---|
428 | {
|
---|
429 | PyObject *res = PyObject_CallMethod(raw, "isatty", NULL);
|
---|
430 | if (res == NULL)
|
---|
431 | goto error;
|
---|
432 | isatty = PyLong_AsLong(res);
|
---|
433 | Py_DECREF(res);
|
---|
434 | if (isatty == -1 && PyErr_Occurred())
|
---|
435 | goto error;
|
---|
436 | }
|
---|
437 |
|
---|
438 | if (buffering == 1 || (buffering < 0 && isatty)) {
|
---|
439 | buffering = -1;
|
---|
440 | line_buffering = 1;
|
---|
441 | }
|
---|
442 | else
|
---|
443 | line_buffering = 0;
|
---|
444 |
|
---|
445 | if (buffering < 0) {
|
---|
446 | buffering = DEFAULT_BUFFER_SIZE;
|
---|
447 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
|
---|
448 | {
|
---|
449 | struct stat st;
|
---|
450 | int fileno;
|
---|
451 | PyObject *res = PyObject_CallMethod(raw, "fileno", NULL);
|
---|
452 | if (res == NULL)
|
---|
453 | goto error;
|
---|
454 |
|
---|
455 | fileno = _PyInt_AsInt(res);
|
---|
456 | Py_DECREF(res);
|
---|
457 | if (fileno == -1 && PyErr_Occurred())
|
---|
458 | goto error;
|
---|
459 |
|
---|
460 | if (fstat(fileno, &st) >= 0 && st.st_blksize > 1)
|
---|
461 | buffering = st.st_blksize;
|
---|
462 | }
|
---|
463 | #endif
|
---|
464 | }
|
---|
465 | if (buffering < 0) {
|
---|
466 | PyErr_SetString(PyExc_ValueError,
|
---|
467 | "invalid buffering size");
|
---|
468 | goto error;
|
---|
469 | }
|
---|
470 |
|
---|
471 | /* if not buffering, returns the raw file object */
|
---|
472 | if (buffering == 0) {
|
---|
473 | if (!binary) {
|
---|
474 | PyErr_SetString(PyExc_ValueError,
|
---|
475 | "can't have unbuffered text I/O");
|
---|
476 | goto error;
|
---|
477 | }
|
---|
478 |
|
---|
479 | Py_DECREF(modeobj);
|
---|
480 | return raw;
|
---|
481 | }
|
---|
482 |
|
---|
483 | /* wraps into a buffered file */
|
---|
484 | {
|
---|
485 | PyObject *Buffered_class;
|
---|
486 |
|
---|
487 | if (updating)
|
---|
488 | Buffered_class = (PyObject *)&PyBufferedRandom_Type;
|
---|
489 | else if (writing || appending)
|
---|
490 | Buffered_class = (PyObject *)&PyBufferedWriter_Type;
|
---|
491 | else if (reading)
|
---|
492 | Buffered_class = (PyObject *)&PyBufferedReader_Type;
|
---|
493 | else {
|
---|
494 | PyErr_Format(PyExc_ValueError,
|
---|
495 | "unknown mode: '%s'", mode);
|
---|
496 | goto error;
|
---|
497 | }
|
---|
498 |
|
---|
499 | buffer = PyObject_CallFunction(Buffered_class, "Oi", raw, buffering);
|
---|
500 | }
|
---|
501 | Py_CLEAR(raw);
|
---|
502 | if (buffer == NULL)
|
---|
503 | goto error;
|
---|
504 |
|
---|
505 |
|
---|
506 | /* if binary, returns the buffered file */
|
---|
507 | if (binary) {
|
---|
508 | Py_DECREF(modeobj);
|
---|
509 | return buffer;
|
---|
510 | }
|
---|
511 |
|
---|
512 | /* wraps into a TextIOWrapper */
|
---|
513 | wrapper = PyObject_CallFunction((PyObject *)&PyTextIOWrapper_Type,
|
---|
514 | "Osssi",
|
---|
515 | buffer,
|
---|
516 | encoding, errors, newline,
|
---|
517 | line_buffering);
|
---|
518 | Py_CLEAR(buffer);
|
---|
519 | if (wrapper == NULL)
|
---|
520 | goto error;
|
---|
521 |
|
---|
522 | if (PyObject_SetAttrString(wrapper, "mode", modeobj) < 0)
|
---|
523 | goto error;
|
---|
524 | Py_DECREF(modeobj);
|
---|
525 | return wrapper;
|
---|
526 |
|
---|
527 | error:
|
---|
528 | Py_XDECREF(raw);
|
---|
529 | Py_XDECREF(modeobj);
|
---|
530 | Py_XDECREF(buffer);
|
---|
531 | Py_XDECREF(wrapper);
|
---|
532 | return NULL;
|
---|
533 | }
|
---|
534 | |
---|
535 |
|
---|
536 | /*
|
---|
537 | * Private helpers for the io module.
|
---|
538 | */
|
---|
539 |
|
---|
540 | Py_off_t
|
---|
541 | PyNumber_AsOff_t(PyObject *item, PyObject *err)
|
---|
542 | {
|
---|
543 | Py_off_t result;
|
---|
544 | PyObject *runerr;
|
---|
545 | PyObject *value = PyNumber_Index(item);
|
---|
546 | if (value == NULL)
|
---|
547 | return -1;
|
---|
548 |
|
---|
549 | if (PyInt_Check(value)) {
|
---|
550 | /* We assume a long always fits in a Py_off_t... */
|
---|
551 | result = (Py_off_t) PyInt_AS_LONG(value);
|
---|
552 | goto finish;
|
---|
553 | }
|
---|
554 |
|
---|
555 | /* We're done if PyLong_AsSsize_t() returns without error. */
|
---|
556 | result = PyLong_AsOff_t(value);
|
---|
557 | if (result != -1 || !(runerr = PyErr_Occurred()))
|
---|
558 | goto finish;
|
---|
559 |
|
---|
560 | /* Error handling code -- only manage OverflowError differently */
|
---|
561 | if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError))
|
---|
562 | goto finish;
|
---|
563 |
|
---|
564 | PyErr_Clear();
|
---|
565 | /* If no error-handling desired then the default clipping
|
---|
566 | is sufficient.
|
---|
567 | */
|
---|
568 | if (!err) {
|
---|
569 | assert(PyLong_Check(value));
|
---|
570 | /* Whether or not it is less than or equal to
|
---|
571 | zero is determined by the sign of ob_size
|
---|
572 | */
|
---|
573 | if (_PyLong_Sign(value) < 0)
|
---|
574 | result = PY_OFF_T_MIN;
|
---|
575 | else
|
---|
576 | result = PY_OFF_T_MAX;
|
---|
577 | }
|
---|
578 | else {
|
---|
579 | /* Otherwise replace the error with caller's error object. */
|
---|
580 | PyErr_Format(err,
|
---|
581 | "cannot fit '%.200s' into an offset-sized integer",
|
---|
582 | item->ob_type->tp_name);
|
---|
583 | }
|
---|
584 |
|
---|
585 | finish:
|
---|
586 | Py_DECREF(value);
|
---|
587 | return result;
|
---|
588 | }
|
---|
589 |
|
---|
590 |
|
---|
591 | /* Basically the "n" format code with the ability to turn None into -1. */
|
---|
592 | int
|
---|
593 | _PyIO_ConvertSsize_t(PyObject *obj, void *result) {
|
---|
594 | Py_ssize_t limit;
|
---|
595 | if (obj == Py_None) {
|
---|
596 | limit = -1;
|
---|
597 | }
|
---|
598 | else if (PyNumber_Check(obj)) {
|
---|
599 | limit = PyNumber_AsSsize_t(obj, PyExc_OverflowError);
|
---|
600 | if (limit == -1 && PyErr_Occurred())
|
---|
601 | return 0;
|
---|
602 | }
|
---|
603 | else {
|
---|
604 | PyErr_Format(PyExc_TypeError,
|
---|
605 | "integer argument expected, got '%.200s'",
|
---|
606 | Py_TYPE(obj)->tp_name);
|
---|
607 | return 0;
|
---|
608 | }
|
---|
609 | *((Py_ssize_t *)result) = limit;
|
---|
610 | return 1;
|
---|
611 | }
|
---|
612 |
|
---|
613 |
|
---|
614 | /*
|
---|
615 | * Module definition
|
---|
616 | */
|
---|
617 |
|
---|
618 | PyObject *_PyIO_os_module = NULL;
|
---|
619 | PyObject *_PyIO_locale_module = NULL;
|
---|
620 | PyObject *_PyIO_unsupported_operation = NULL;
|
---|
621 |
|
---|
622 | static PyMethodDef module_methods[] = {
|
---|
623 | {"open", (PyCFunction)io_open, METH_VARARGS|METH_KEYWORDS, open_doc},
|
---|
624 | {NULL, NULL}
|
---|
625 | };
|
---|
626 |
|
---|
627 | PyMODINIT_FUNC
|
---|
628 | init_io(void)
|
---|
629 | {
|
---|
630 | PyObject *m = Py_InitModule4("_io", module_methods,
|
---|
631 | module_doc, NULL, PYTHON_API_VERSION);
|
---|
632 | if (m == NULL)
|
---|
633 | return;
|
---|
634 |
|
---|
635 | /* put os in the module state */
|
---|
636 | _PyIO_os_module = PyImport_ImportModule("os");
|
---|
637 | if (_PyIO_os_module == NULL)
|
---|
638 | goto fail;
|
---|
639 |
|
---|
640 | #define ADD_TYPE(type, name) \
|
---|
641 | if (PyType_Ready(type) < 0) \
|
---|
642 | goto fail; \
|
---|
643 | Py_INCREF(type); \
|
---|
644 | if (PyModule_AddObject(m, name, (PyObject *)type) < 0) { \
|
---|
645 | Py_DECREF(type); \
|
---|
646 | goto fail; \
|
---|
647 | }
|
---|
648 |
|
---|
649 | /* DEFAULT_BUFFER_SIZE */
|
---|
650 | if (PyModule_AddIntMacro(m, DEFAULT_BUFFER_SIZE) < 0)
|
---|
651 | goto fail;
|
---|
652 |
|
---|
653 | /* UnsupportedOperation inherits from ValueError and IOError */
|
---|
654 | _PyIO_unsupported_operation = PyObject_CallFunction(
|
---|
655 | (PyObject *)&PyType_Type, "s(OO){}",
|
---|
656 | "UnsupportedOperation", PyExc_ValueError, PyExc_IOError);
|
---|
657 | if (_PyIO_unsupported_operation == NULL)
|
---|
658 | goto fail;
|
---|
659 | Py_INCREF(_PyIO_unsupported_operation);
|
---|
660 | if (PyModule_AddObject(m, "UnsupportedOperation",
|
---|
661 | _PyIO_unsupported_operation) < 0)
|
---|
662 | goto fail;
|
---|
663 |
|
---|
664 | /* BlockingIOError */
|
---|
665 | _PyExc_BlockingIOError.tp_base = (PyTypeObject *) PyExc_IOError;
|
---|
666 | ADD_TYPE(&_PyExc_BlockingIOError, "BlockingIOError");
|
---|
667 |
|
---|
668 | /* Concrete base types of the IO ABCs.
|
---|
669 | (the ABCs themselves are declared through inheritance in io.py)
|
---|
670 | */
|
---|
671 | ADD_TYPE(&PyIOBase_Type, "_IOBase");
|
---|
672 | ADD_TYPE(&PyRawIOBase_Type, "_RawIOBase");
|
---|
673 | ADD_TYPE(&PyBufferedIOBase_Type, "_BufferedIOBase");
|
---|
674 | ADD_TYPE(&PyTextIOBase_Type, "_TextIOBase");
|
---|
675 |
|
---|
676 | /* Implementation of concrete IO objects. */
|
---|
677 | /* FileIO */
|
---|
678 | PyFileIO_Type.tp_base = &PyRawIOBase_Type;
|
---|
679 | ADD_TYPE(&PyFileIO_Type, "FileIO");
|
---|
680 |
|
---|
681 | /* BytesIO */
|
---|
682 | PyBytesIO_Type.tp_base = &PyBufferedIOBase_Type;
|
---|
683 | ADD_TYPE(&PyBytesIO_Type, "BytesIO");
|
---|
684 |
|
---|
685 | /* StringIO */
|
---|
686 | PyStringIO_Type.tp_base = &PyTextIOBase_Type;
|
---|
687 | ADD_TYPE(&PyStringIO_Type, "StringIO");
|
---|
688 |
|
---|
689 | /* BufferedReader */
|
---|
690 | PyBufferedReader_Type.tp_base = &PyBufferedIOBase_Type;
|
---|
691 | ADD_TYPE(&PyBufferedReader_Type, "BufferedReader");
|
---|
692 |
|
---|
693 | /* BufferedWriter */
|
---|
694 | PyBufferedWriter_Type.tp_base = &PyBufferedIOBase_Type;
|
---|
695 | ADD_TYPE(&PyBufferedWriter_Type, "BufferedWriter");
|
---|
696 |
|
---|
697 | /* BufferedRWPair */
|
---|
698 | PyBufferedRWPair_Type.tp_base = &PyBufferedIOBase_Type;
|
---|
699 | ADD_TYPE(&PyBufferedRWPair_Type, "BufferedRWPair");
|
---|
700 |
|
---|
701 | /* BufferedRandom */
|
---|
702 | PyBufferedRandom_Type.tp_base = &PyBufferedIOBase_Type;
|
---|
703 | ADD_TYPE(&PyBufferedRandom_Type, "BufferedRandom");
|
---|
704 |
|
---|
705 | /* TextIOWrapper */
|
---|
706 | PyTextIOWrapper_Type.tp_base = &PyTextIOBase_Type;
|
---|
707 | ADD_TYPE(&PyTextIOWrapper_Type, "TextIOWrapper");
|
---|
708 |
|
---|
709 | /* IncrementalNewlineDecoder */
|
---|
710 | ADD_TYPE(&PyIncrementalNewlineDecoder_Type, "IncrementalNewlineDecoder");
|
---|
711 |
|
---|
712 | /* Interned strings */
|
---|
713 | if (!(_PyIO_str_close = PyString_InternFromString("close")))
|
---|
714 | goto fail;
|
---|
715 | if (!(_PyIO_str_closed = PyString_InternFromString("closed")))
|
---|
716 | goto fail;
|
---|
717 | if (!(_PyIO_str_decode = PyString_InternFromString("decode")))
|
---|
718 | goto fail;
|
---|
719 | if (!(_PyIO_str_encode = PyString_InternFromString("encode")))
|
---|
720 | goto fail;
|
---|
721 | if (!(_PyIO_str_fileno = PyString_InternFromString("fileno")))
|
---|
722 | goto fail;
|
---|
723 | if (!(_PyIO_str_flush = PyString_InternFromString("flush")))
|
---|
724 | goto fail;
|
---|
725 | if (!(_PyIO_str_getstate = PyString_InternFromString("getstate")))
|
---|
726 | goto fail;
|
---|
727 | if (!(_PyIO_str_isatty = PyString_InternFromString("isatty")))
|
---|
728 | goto fail;
|
---|
729 | if (!(_PyIO_str_newlines = PyString_InternFromString("newlines")))
|
---|
730 | goto fail;
|
---|
731 | if (!(_PyIO_str_nl = PyString_InternFromString("\n")))
|
---|
732 | goto fail;
|
---|
733 | if (!(_PyIO_str_read = PyString_InternFromString("read")))
|
---|
734 | goto fail;
|
---|
735 | if (!(_PyIO_str_read1 = PyString_InternFromString("read1")))
|
---|
736 | goto fail;
|
---|
737 | if (!(_PyIO_str_readable = PyString_InternFromString("readable")))
|
---|
738 | goto fail;
|
---|
739 | if (!(_PyIO_str_readinto = PyString_InternFromString("readinto")))
|
---|
740 | goto fail;
|
---|
741 | if (!(_PyIO_str_readline = PyString_InternFromString("readline")))
|
---|
742 | goto fail;
|
---|
743 | if (!(_PyIO_str_reset = PyString_InternFromString("reset")))
|
---|
744 | goto fail;
|
---|
745 | if (!(_PyIO_str_seek = PyString_InternFromString("seek")))
|
---|
746 | goto fail;
|
---|
747 | if (!(_PyIO_str_seekable = PyString_InternFromString("seekable")))
|
---|
748 | goto fail;
|
---|
749 | if (!(_PyIO_str_setstate = PyString_InternFromString("setstate")))
|
---|
750 | goto fail;
|
---|
751 | if (!(_PyIO_str_tell = PyString_InternFromString("tell")))
|
---|
752 | goto fail;
|
---|
753 | if (!(_PyIO_str_truncate = PyString_InternFromString("truncate")))
|
---|
754 | goto fail;
|
---|
755 | if (!(_PyIO_str_write = PyString_InternFromString("write")))
|
---|
756 | goto fail;
|
---|
757 | if (!(_PyIO_str_writable = PyString_InternFromString("writable")))
|
---|
758 | goto fail;
|
---|
759 |
|
---|
760 | if (!(_PyIO_empty_str = PyUnicode_FromStringAndSize(NULL, 0)))
|
---|
761 | goto fail;
|
---|
762 | if (!(_PyIO_empty_bytes = PyBytes_FromStringAndSize(NULL, 0)))
|
---|
763 | goto fail;
|
---|
764 | if (!(_PyIO_zero = PyLong_FromLong(0L)))
|
---|
765 | goto fail;
|
---|
766 |
|
---|
767 | return;
|
---|
768 |
|
---|
769 | fail:
|
---|
770 | Py_CLEAR(_PyIO_os_module);
|
---|
771 | Py_CLEAR(_PyIO_unsupported_operation);
|
---|
772 | Py_DECREF(m);
|
---|
773 | }
|
---|