]>
Commit | Line | Data |
---|---|---|
7eb75bcc DM |
1 | /*\r |
2 | An implementation of the I/O abstract base classes hierarchy\r | |
3 | as defined by PEP 3116 - "New I/O"\r | |
4 | \r | |
5 | Classes defined here: IOBase, RawIOBase.\r | |
6 | \r | |
7 | Written by Amaury Forgeot d'Arc and Antoine Pitrou\r | |
8 | */\r | |
9 | \r | |
10 | \r | |
11 | #define PY_SSIZE_T_CLEAN\r | |
12 | #include "Python.h"\r | |
13 | #include "structmember.h"\r | |
14 | #include "_iomodule.h"\r | |
15 | \r | |
16 | /*\r | |
17 | * IOBase class, an abstract class\r | |
18 | */\r | |
19 | \r | |
20 | typedef struct {\r | |
21 | PyObject_HEAD\r | |
22 | \r | |
23 | PyObject *dict;\r | |
24 | PyObject *weakreflist;\r | |
25 | } iobase;\r | |
26 | \r | |
27 | PyDoc_STRVAR(iobase_doc,\r | |
28 | "The abstract base class for all I/O classes, acting on streams of\n"\r | |
29 | "bytes. There is no public constructor.\n"\r | |
30 | "\n"\r | |
31 | "This class provides dummy implementations for many methods that\n"\r | |
32 | "derived classes can override selectively; the default implementations\n"\r | |
33 | "represent a file that cannot be read, written or seeked.\n"\r | |
34 | "\n"\r | |
35 | "Even though IOBase does not declare read, readinto, or write because\n"\r | |
36 | "their signatures will vary, implementations and clients should\n"\r | |
37 | "consider those methods part of the interface. Also, implementations\n"\r | |
38 | "may raise a IOError when operations they do not support are called.\n"\r | |
39 | "\n"\r | |
40 | "The basic type used for binary data read from or written to a file is\n"\r | |
41 | "bytes. bytearrays are accepted too, and in some cases (such as\n"\r | |
42 | "readinto) needed. Text I/O classes work with str data.\n"\r | |
43 | "\n"\r | |
44 | "Note that calling any method (except additional calls to close(),\n"\r | |
45 | "which are ignored) on a closed stream should raise a ValueError.\n"\r | |
46 | "\n"\r | |
47 | "IOBase (and its subclasses) support the iterator protocol, meaning\n"\r | |
48 | "that an IOBase object can be iterated over yielding the lines in a\n"\r | |
49 | "stream.\n"\r | |
50 | "\n"\r | |
51 | "IOBase also supports the :keyword:`with` statement. In this example,\n"\r | |
52 | "fp is closed after the suite of the with statement is complete:\n"\r | |
53 | "\n"\r | |
54 | "with open('spam.txt', 'r') as fp:\n"\r | |
55 | " fp.write('Spam and eggs!')\n");\r | |
56 | \r | |
57 | /* Use this macro whenever you want to check the internal `closed` status\r | |
58 | of the IOBase object rather than the virtual `closed` attribute as returned\r | |
59 | by whatever subclass. */\r | |
60 | \r | |
61 | #define IS_CLOSED(self) \\r | |
62 | PyObject_HasAttrString(self, "__IOBase_closed")\r | |
63 | \r | |
64 | /* Internal methods */\r | |
65 | static PyObject *\r | |
66 | iobase_unsupported(const char *message)\r | |
67 | {\r | |
68 | PyErr_SetString(_PyIO_unsupported_operation, message);\r | |
69 | return NULL;\r | |
70 | }\r | |
71 | \r | |
72 | /* Positionning */\r | |
73 | \r | |
74 | PyDoc_STRVAR(iobase_seek_doc,\r | |
75 | "Change stream position.\n"\r | |
76 | "\n"\r | |
77 | "Change the stream position to the given byte offset. The offset is\n"\r | |
78 | "interpreted relative to the position indicated by whence. Values\n"\r | |
79 | "for whence are:\n"\r | |
80 | "\n"\r | |
81 | "* 0 -- start of stream (the default); offset should be zero or positive\n"\r | |
82 | "* 1 -- current stream position; offset may be negative\n"\r | |
83 | "* 2 -- end of stream; offset is usually negative\n"\r | |
84 | "\n"\r | |
85 | "Return the new absolute position.");\r | |
86 | \r | |
87 | static PyObject *\r | |
88 | iobase_seek(PyObject *self, PyObject *args)\r | |
89 | {\r | |
90 | return iobase_unsupported("seek");\r | |
91 | }\r | |
92 | \r | |
93 | PyDoc_STRVAR(iobase_tell_doc,\r | |
94 | "Return current stream position.");\r | |
95 | \r | |
96 | static PyObject *\r | |
97 | iobase_tell(PyObject *self, PyObject *args)\r | |
98 | {\r | |
99 | return PyObject_CallMethod(self, "seek", "ii", 0, 1);\r | |
100 | }\r | |
101 | \r | |
102 | PyDoc_STRVAR(iobase_truncate_doc,\r | |
103 | "Truncate file to size bytes.\n"\r | |
104 | "\n"\r | |
105 | "File pointer is left unchanged. Size defaults to the current IO\n"\r | |
106 | "position as reported by tell(). Returns the new size.");\r | |
107 | \r | |
108 | static PyObject *\r | |
109 | iobase_truncate(PyObject *self, PyObject *args)\r | |
110 | {\r | |
111 | return iobase_unsupported("truncate");\r | |
112 | }\r | |
113 | \r | |
114 | /* Flush and close methods */\r | |
115 | \r | |
116 | PyDoc_STRVAR(iobase_flush_doc,\r | |
117 | "Flush write buffers, if applicable.\n"\r | |
118 | "\n"\r | |
119 | "This is not implemented for read-only and non-blocking streams.\n");\r | |
120 | \r | |
121 | static PyObject *\r | |
122 | iobase_flush(PyObject *self, PyObject *args)\r | |
123 | {\r | |
124 | /* XXX Should this return the number of bytes written??? */\r | |
125 | if (IS_CLOSED(self)) {\r | |
126 | PyErr_SetString(PyExc_ValueError, "I/O operation on closed file.");\r | |
127 | return NULL;\r | |
128 | }\r | |
129 | Py_RETURN_NONE;\r | |
130 | }\r | |
131 | \r | |
132 | PyDoc_STRVAR(iobase_close_doc,\r | |
133 | "Flush and close the IO object.\n"\r | |
134 | "\n"\r | |
135 | "This method has no effect if the file is already closed.\n");\r | |
136 | \r | |
137 | static int\r | |
138 | iobase_closed(PyObject *self)\r | |
139 | {\r | |
140 | PyObject *res;\r | |
141 | int closed;\r | |
142 | /* This gets the derived attribute, which is *not* __IOBase_closed\r | |
143 | in most cases! */\r | |
144 | res = PyObject_GetAttr(self, _PyIO_str_closed);\r | |
145 | if (res == NULL)\r | |
146 | return 0;\r | |
147 | closed = PyObject_IsTrue(res);\r | |
148 | Py_DECREF(res);\r | |
149 | return closed;\r | |
150 | }\r | |
151 | \r | |
152 | static PyObject *\r | |
153 | iobase_closed_get(PyObject *self, void *context)\r | |
154 | {\r | |
155 | return PyBool_FromLong(IS_CLOSED(self));\r | |
156 | }\r | |
157 | \r | |
158 | PyObject *\r | |
159 | _PyIOBase_check_closed(PyObject *self, PyObject *args)\r | |
160 | {\r | |
161 | if (iobase_closed(self)) {\r | |
162 | PyErr_SetString(PyExc_ValueError, "I/O operation on closed file.");\r | |
163 | return NULL;\r | |
164 | }\r | |
165 | if (args == Py_True)\r | |
166 | return Py_None;\r | |
167 | else\r | |
168 | Py_RETURN_NONE;\r | |
169 | }\r | |
170 | \r | |
171 | /* XXX: IOBase thinks it has to maintain its own internal state in\r | |
172 | `__IOBase_closed` and call flush() by itself, but it is redundant with\r | |
173 | whatever behaviour a non-trivial derived class will implement. */\r | |
174 | \r | |
175 | static PyObject *\r | |
176 | iobase_close(PyObject *self, PyObject *args)\r | |
177 | {\r | |
178 | PyObject *res;\r | |
179 | \r | |
180 | if (IS_CLOSED(self))\r | |
181 | Py_RETURN_NONE;\r | |
182 | \r | |
183 | res = PyObject_CallMethodObjArgs(self, _PyIO_str_flush, NULL);\r | |
184 | PyObject_SetAttrString(self, "__IOBase_closed", Py_True);\r | |
185 | if (res == NULL) {\r | |
186 | return NULL;\r | |
187 | }\r | |
188 | Py_XDECREF(res);\r | |
189 | Py_RETURN_NONE;\r | |
190 | }\r | |
191 | \r | |
192 | /* Finalization and garbage collection support */\r | |
193 | \r | |
194 | int\r | |
195 | _PyIOBase_finalize(PyObject *self)\r | |
196 | {\r | |
197 | PyObject *res;\r | |
198 | PyObject *tp, *v, *tb;\r | |
199 | int closed = 1;\r | |
200 | int is_zombie;\r | |
201 | \r | |
202 | /* If _PyIOBase_finalize() is called from a destructor, we need to\r | |
203 | resurrect the object as calling close() can invoke arbitrary code. */\r | |
204 | is_zombie = (Py_REFCNT(self) == 0);\r | |
205 | if (is_zombie) {\r | |
206 | ++Py_REFCNT(self);\r | |
207 | }\r | |
208 | PyErr_Fetch(&tp, &v, &tb);\r | |
209 | /* If `closed` doesn't exist or can't be evaluated as bool, then the\r | |
210 | object is probably in an unusable state, so ignore. */\r | |
211 | res = PyObject_GetAttr(self, _PyIO_str_closed);\r | |
212 | if (res == NULL)\r | |
213 | PyErr_Clear();\r | |
214 | else {\r | |
215 | closed = PyObject_IsTrue(res);\r | |
216 | Py_DECREF(res);\r | |
217 | if (closed == -1)\r | |
218 | PyErr_Clear();\r | |
219 | }\r | |
220 | if (closed == 0) {\r | |
221 | res = PyObject_CallMethodObjArgs((PyObject *) self, _PyIO_str_close,\r | |
222 | NULL);\r | |
223 | /* Silencing I/O errors is bad, but printing spurious tracebacks is\r | |
224 | equally as bad, and potentially more frequent (because of\r | |
225 | shutdown issues). */\r | |
226 | if (res == NULL)\r | |
227 | PyErr_Clear();\r | |
228 | else\r | |
229 | Py_DECREF(res);\r | |
230 | }\r | |
231 | PyErr_Restore(tp, v, tb);\r | |
232 | if (is_zombie) {\r | |
233 | if (--Py_REFCNT(self) != 0) {\r | |
234 | /* The object lives again. The following code is taken from\r | |
235 | slot_tp_del in typeobject.c. */\r | |
236 | Py_ssize_t refcnt = Py_REFCNT(self);\r | |
237 | _Py_NewReference(self);\r | |
238 | Py_REFCNT(self) = refcnt;\r | |
239 | /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so\r | |
240 | * we need to undo that. */\r | |
241 | _Py_DEC_REFTOTAL;\r | |
242 | /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object\r | |
243 | * chain, so no more to do there.\r | |
244 | * If COUNT_ALLOCS, the original decref bumped tp_frees, and\r | |
245 | * _Py_NewReference bumped tp_allocs: both of those need to be\r | |
246 | * undone.\r | |
247 | */\r | |
248 | #ifdef COUNT_ALLOCS\r | |
249 | --Py_TYPE(self)->tp_frees;\r | |
250 | --Py_TYPE(self)->tp_allocs;\r | |
251 | #endif\r | |
252 | return -1;\r | |
253 | }\r | |
254 | }\r | |
255 | return 0;\r | |
256 | }\r | |
257 | \r | |
258 | static int\r | |
259 | iobase_traverse(iobase *self, visitproc visit, void *arg)\r | |
260 | {\r | |
261 | Py_VISIT(self->dict);\r | |
262 | return 0;\r | |
263 | }\r | |
264 | \r | |
265 | static int\r | |
266 | iobase_clear(iobase *self)\r | |
267 | {\r | |
268 | if (_PyIOBase_finalize((PyObject *) self) < 0)\r | |
269 | return -1;\r | |
270 | Py_CLEAR(self->dict);\r | |
271 | return 0;\r | |
272 | }\r | |
273 | \r | |
274 | /* Destructor */\r | |
275 | \r | |
276 | static void\r | |
277 | iobase_dealloc(iobase *self)\r | |
278 | {\r | |
279 | /* NOTE: since IOBaseObject has its own dict, Python-defined attributes\r | |
280 | are still available here for close() to use.\r | |
281 | However, if the derived class declares a __slots__, those slots are\r | |
282 | already gone.\r | |
283 | */\r | |
284 | if (_PyIOBase_finalize((PyObject *) self) < 0) {\r | |
285 | /* When called from a heap type's dealloc, the type will be\r | |
286 | decref'ed on return (see e.g. subtype_dealloc in typeobject.c). */\r | |
287 | if (PyType_HasFeature(Py_TYPE(self), Py_TPFLAGS_HEAPTYPE))\r | |
288 | Py_INCREF(Py_TYPE(self));\r | |
289 | return;\r | |
290 | }\r | |
291 | _PyObject_GC_UNTRACK(self);\r | |
292 | if (self->weakreflist != NULL)\r | |
293 | PyObject_ClearWeakRefs((PyObject *) self);\r | |
294 | Py_CLEAR(self->dict);\r | |
295 | Py_TYPE(self)->tp_free((PyObject *) self);\r | |
296 | }\r | |
297 | \r | |
298 | /* Inquiry methods */\r | |
299 | \r | |
300 | PyDoc_STRVAR(iobase_seekable_doc,\r | |
301 | "Return whether object supports random access.\n"\r | |
302 | "\n"\r | |
303 | "If False, seek(), tell() and truncate() will raise IOError.\n"\r | |
304 | "This method may need to do a test seek().");\r | |
305 | \r | |
306 | static PyObject *\r | |
307 | iobase_seekable(PyObject *self, PyObject *args)\r | |
308 | {\r | |
309 | Py_RETURN_FALSE;\r | |
310 | }\r | |
311 | \r | |
312 | PyObject *\r | |
313 | _PyIOBase_check_seekable(PyObject *self, PyObject *args)\r | |
314 | {\r | |
315 | PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_seekable, NULL);\r | |
316 | if (res == NULL)\r | |
317 | return NULL;\r | |
318 | if (res != Py_True) {\r | |
319 | Py_CLEAR(res);\r | |
320 | PyErr_SetString(PyExc_IOError, "File or stream is not seekable.");\r | |
321 | return NULL;\r | |
322 | }\r | |
323 | if (args == Py_True) {\r | |
324 | Py_DECREF(res);\r | |
325 | }\r | |
326 | return res;\r | |
327 | }\r | |
328 | \r | |
329 | PyDoc_STRVAR(iobase_readable_doc,\r | |
330 | "Return whether object was opened for reading.\n"\r | |
331 | "\n"\r | |
332 | "If False, read() will raise IOError.");\r | |
333 | \r | |
334 | static PyObject *\r | |
335 | iobase_readable(PyObject *self, PyObject *args)\r | |
336 | {\r | |
337 | Py_RETURN_FALSE;\r | |
338 | }\r | |
339 | \r | |
340 | /* May be called with any object */\r | |
341 | PyObject *\r | |
342 | _PyIOBase_check_readable(PyObject *self, PyObject *args)\r | |
343 | {\r | |
344 | PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_readable, NULL);\r | |
345 | if (res == NULL)\r | |
346 | return NULL;\r | |
347 | if (res != Py_True) {\r | |
348 | Py_CLEAR(res);\r | |
349 | PyErr_SetString(PyExc_IOError, "File or stream is not readable.");\r | |
350 | return NULL;\r | |
351 | }\r | |
352 | if (args == Py_True) {\r | |
353 | Py_DECREF(res);\r | |
354 | }\r | |
355 | return res;\r | |
356 | }\r | |
357 | \r | |
358 | PyDoc_STRVAR(iobase_writable_doc,\r | |
359 | "Return whether object was opened for writing.\n"\r | |
360 | "\n"\r | |
361 | "If False, read() will raise IOError.");\r | |
362 | \r | |
363 | static PyObject *\r | |
364 | iobase_writable(PyObject *self, PyObject *args)\r | |
365 | {\r | |
366 | Py_RETURN_FALSE;\r | |
367 | }\r | |
368 | \r | |
369 | /* May be called with any object */\r | |
370 | PyObject *\r | |
371 | _PyIOBase_check_writable(PyObject *self, PyObject *args)\r | |
372 | {\r | |
373 | PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_writable, NULL);\r | |
374 | if (res == NULL)\r | |
375 | return NULL;\r | |
376 | if (res != Py_True) {\r | |
377 | Py_CLEAR(res);\r | |
378 | PyErr_SetString(PyExc_IOError, "File or stream is not writable.");\r | |
379 | return NULL;\r | |
380 | }\r | |
381 | if (args == Py_True) {\r | |
382 | Py_DECREF(res);\r | |
383 | }\r | |
384 | return res;\r | |
385 | }\r | |
386 | \r | |
387 | /* Context manager */\r | |
388 | \r | |
389 | static PyObject *\r | |
390 | iobase_enter(PyObject *self, PyObject *args)\r | |
391 | {\r | |
392 | if (_PyIOBase_check_closed(self, Py_True) == NULL)\r | |
393 | return NULL;\r | |
394 | \r | |
395 | Py_INCREF(self);\r | |
396 | return self;\r | |
397 | }\r | |
398 | \r | |
399 | static PyObject *\r | |
400 | iobase_exit(PyObject *self, PyObject *args)\r | |
401 | {\r | |
402 | return PyObject_CallMethodObjArgs(self, _PyIO_str_close, NULL);\r | |
403 | }\r | |
404 | \r | |
405 | /* Lower-level APIs */\r | |
406 | \r | |
407 | /* XXX Should these be present even if unimplemented? */\r | |
408 | \r | |
409 | PyDoc_STRVAR(iobase_fileno_doc,\r | |
410 | "Returns underlying file descriptor if one exists.\n"\r | |
411 | "\n"\r | |
412 | "An IOError is raised if the IO object does not use a file descriptor.\n");\r | |
413 | \r | |
414 | static PyObject *\r | |
415 | iobase_fileno(PyObject *self, PyObject *args)\r | |
416 | {\r | |
417 | return iobase_unsupported("fileno");\r | |
418 | }\r | |
419 | \r | |
420 | PyDoc_STRVAR(iobase_isatty_doc,\r | |
421 | "Return whether this is an 'interactive' stream.\n"\r | |
422 | "\n"\r | |
423 | "Return False if it can't be determined.\n");\r | |
424 | \r | |
425 | static PyObject *\r | |
426 | iobase_isatty(PyObject *self, PyObject *args)\r | |
427 | {\r | |
428 | if (_PyIOBase_check_closed(self, Py_True) == NULL)\r | |
429 | return NULL;\r | |
430 | Py_RETURN_FALSE;\r | |
431 | }\r | |
432 | \r | |
433 | /* Readline(s) and writelines */\r | |
434 | \r | |
435 | PyDoc_STRVAR(iobase_readline_doc,\r | |
436 | "Read and return a line from the stream.\n"\r | |
437 | "\n"\r | |
438 | "If limit is specified, at most limit bytes will be read.\n"\r | |
439 | "\n"\r | |
440 | "The line terminator is always b'\\n' for binary files; for text\n"\r | |
441 | "files, the newlines argument to open can be used to select the line\n"\r | |
442 | "terminator(s) recognized.\n");\r | |
443 | \r | |
444 | static PyObject *\r | |
445 | iobase_readline(PyObject *self, PyObject *args)\r | |
446 | {\r | |
447 | /* For backwards compatibility, a (slowish) readline(). */\r | |
448 | \r | |
449 | Py_ssize_t limit = -1;\r | |
450 | int has_peek = 0;\r | |
451 | PyObject *buffer, *result;\r | |
452 | Py_ssize_t old_size = -1;\r | |
453 | \r | |
454 | if (!PyArg_ParseTuple(args, "|O&:readline", &_PyIO_ConvertSsize_t, &limit)) {\r | |
455 | return NULL;\r | |
456 | }\r | |
457 | \r | |
458 | if (PyObject_HasAttrString(self, "peek"))\r | |
459 | has_peek = 1;\r | |
460 | \r | |
461 | buffer = PyByteArray_FromStringAndSize(NULL, 0);\r | |
462 | if (buffer == NULL)\r | |
463 | return NULL;\r | |
464 | \r | |
465 | while (limit < 0 || Py_SIZE(buffer) < limit) {\r | |
466 | Py_ssize_t nreadahead = 1;\r | |
467 | PyObject *b;\r | |
468 | \r | |
469 | if (has_peek) {\r | |
470 | PyObject *readahead = PyObject_CallMethod(self, "peek", "i", 1);\r | |
471 | if (readahead == NULL) {\r | |
472 | /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()\r | |
473 | when EINTR occurs so we needn't do it ourselves. */\r | |
474 | if (_PyIO_trap_eintr()) {\r | |
475 | continue;\r | |
476 | }\r | |
477 | goto fail;\r | |
478 | }\r | |
479 | if (!PyBytes_Check(readahead)) {\r | |
480 | PyErr_Format(PyExc_IOError,\r | |
481 | "peek() should have returned a bytes object, "\r | |
482 | "not '%.200s'", Py_TYPE(readahead)->tp_name);\r | |
483 | Py_DECREF(readahead);\r | |
484 | goto fail;\r | |
485 | }\r | |
486 | if (PyBytes_GET_SIZE(readahead) > 0) {\r | |
487 | Py_ssize_t n = 0;\r | |
488 | const char *buf = PyBytes_AS_STRING(readahead);\r | |
489 | if (limit >= 0) {\r | |
490 | do {\r | |
491 | if (n >= PyBytes_GET_SIZE(readahead) || n >= limit)\r | |
492 | break;\r | |
493 | if (buf[n++] == '\n')\r | |
494 | break;\r | |
495 | } while (1);\r | |
496 | }\r | |
497 | else {\r | |
498 | do {\r | |
499 | if (n >= PyBytes_GET_SIZE(readahead))\r | |
500 | break;\r | |
501 | if (buf[n++] == '\n')\r | |
502 | break;\r | |
503 | } while (1);\r | |
504 | }\r | |
505 | nreadahead = n;\r | |
506 | }\r | |
507 | Py_DECREF(readahead);\r | |
508 | }\r | |
509 | \r | |
510 | b = PyObject_CallMethod(self, "read", "n", nreadahead);\r | |
511 | if (b == NULL) {\r | |
512 | /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()\r | |
513 | when EINTR occurs so we needn't do it ourselves. */\r | |
514 | if (_PyIO_trap_eintr()) {\r | |
515 | continue;\r | |
516 | }\r | |
517 | goto fail;\r | |
518 | }\r | |
519 | if (!PyBytes_Check(b)) {\r | |
520 | PyErr_Format(PyExc_IOError,\r | |
521 | "read() should have returned a bytes object, "\r | |
522 | "not '%.200s'", Py_TYPE(b)->tp_name);\r | |
523 | Py_DECREF(b);\r | |
524 | goto fail;\r | |
525 | }\r | |
526 | if (PyBytes_GET_SIZE(b) == 0) {\r | |
527 | Py_DECREF(b);\r | |
528 | break;\r | |
529 | }\r | |
530 | \r | |
531 | old_size = PyByteArray_GET_SIZE(buffer);\r | |
532 | PyByteArray_Resize(buffer, old_size + PyBytes_GET_SIZE(b));\r | |
533 | memcpy(PyByteArray_AS_STRING(buffer) + old_size,\r | |
534 | PyBytes_AS_STRING(b), PyBytes_GET_SIZE(b));\r | |
535 | \r | |
536 | Py_DECREF(b);\r | |
537 | \r | |
538 | if (PyByteArray_AS_STRING(buffer)[PyByteArray_GET_SIZE(buffer) - 1] == '\n')\r | |
539 | break;\r | |
540 | }\r | |
541 | \r | |
542 | result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(buffer),\r | |
543 | PyByteArray_GET_SIZE(buffer));\r | |
544 | Py_DECREF(buffer);\r | |
545 | return result;\r | |
546 | fail:\r | |
547 | Py_DECREF(buffer);\r | |
548 | return NULL;\r | |
549 | }\r | |
550 | \r | |
551 | static PyObject *\r | |
552 | iobase_iter(PyObject *self)\r | |
553 | {\r | |
554 | if (_PyIOBase_check_closed(self, Py_True) == NULL)\r | |
555 | return NULL;\r | |
556 | \r | |
557 | Py_INCREF(self);\r | |
558 | return self;\r | |
559 | }\r | |
560 | \r | |
561 | static PyObject *\r | |
562 | iobase_iternext(PyObject *self)\r | |
563 | {\r | |
564 | PyObject *line = PyObject_CallMethodObjArgs(self, _PyIO_str_readline, NULL);\r | |
565 | \r | |
566 | if (line == NULL)\r | |
567 | return NULL;\r | |
568 | \r | |
569 | if (PyObject_Size(line) == 0) {\r | |
570 | Py_DECREF(line);\r | |
571 | return NULL;\r | |
572 | }\r | |
573 | \r | |
574 | return line;\r | |
575 | }\r | |
576 | \r | |
577 | PyDoc_STRVAR(iobase_readlines_doc,\r | |
578 | "Return a list of lines from the stream.\n"\r | |
579 | "\n"\r | |
580 | "hint can be specified to control the number of lines read: no more\n"\r | |
581 | "lines will be read if the total size (in bytes/characters) of all\n"\r | |
582 | "lines so far exceeds hint.");\r | |
583 | \r | |
584 | static PyObject *\r | |
585 | iobase_readlines(PyObject *self, PyObject *args)\r | |
586 | {\r | |
587 | Py_ssize_t hint = -1, length = 0;\r | |
588 | PyObject *result;\r | |
589 | \r | |
590 | if (!PyArg_ParseTuple(args, "|O&:readlines", &_PyIO_ConvertSsize_t, &hint)) {\r | |
591 | return NULL;\r | |
592 | }\r | |
593 | \r | |
594 | result = PyList_New(0);\r | |
595 | if (result == NULL)\r | |
596 | return NULL;\r | |
597 | \r | |
598 | if (hint <= 0) {\r | |
599 | /* XXX special-casing this made sense in the Python version in order\r | |
600 | to remove the bytecode interpretation overhead, but it could\r | |
601 | probably be removed here. */\r | |
602 | PyObject *ret = PyObject_CallMethod(result, "extend", "O", self);\r | |
603 | if (ret == NULL) {\r | |
604 | Py_DECREF(result);\r | |
605 | return NULL;\r | |
606 | }\r | |
607 | Py_DECREF(ret);\r | |
608 | return result;\r | |
609 | }\r | |
610 | \r | |
611 | while (1) {\r | |
612 | PyObject *line = PyIter_Next(self);\r | |
613 | if (line == NULL) {\r | |
614 | if (PyErr_Occurred()) {\r | |
615 | Py_DECREF(result);\r | |
616 | return NULL;\r | |
617 | }\r | |
618 | else\r | |
619 | break; /* StopIteration raised */\r | |
620 | }\r | |
621 | \r | |
622 | if (PyList_Append(result, line) < 0) {\r | |
623 | Py_DECREF(line);\r | |
624 | Py_DECREF(result);\r | |
625 | return NULL;\r | |
626 | }\r | |
627 | length += PyObject_Size(line);\r | |
628 | Py_DECREF(line);\r | |
629 | \r | |
630 | if (length > hint)\r | |
631 | break;\r | |
632 | }\r | |
633 | return result;\r | |
634 | }\r | |
635 | \r | |
636 | static PyObject *\r | |
637 | iobase_writelines(PyObject *self, PyObject *args)\r | |
638 | {\r | |
639 | PyObject *lines, *iter, *res;\r | |
640 | \r | |
641 | if (!PyArg_ParseTuple(args, "O:writelines", &lines)) {\r | |
642 | return NULL;\r | |
643 | }\r | |
644 | \r | |
645 | if (_PyIOBase_check_closed(self, Py_True) == NULL)\r | |
646 | return NULL;\r | |
647 | \r | |
648 | iter = PyObject_GetIter(lines);\r | |
649 | if (iter == NULL)\r | |
650 | return NULL;\r | |
651 | \r | |
652 | while (1) {\r | |
653 | PyObject *line = PyIter_Next(iter);\r | |
654 | if (line == NULL) {\r | |
655 | if (PyErr_Occurred()) {\r | |
656 | Py_DECREF(iter);\r | |
657 | return NULL;\r | |
658 | }\r | |
659 | else\r | |
660 | break; /* Stop Iteration */\r | |
661 | }\r | |
662 | \r | |
663 | res = NULL;\r | |
664 | do {\r | |
665 | res = PyObject_CallMethodObjArgs(self, _PyIO_str_write, line, NULL);\r | |
666 | } while (res == NULL && _PyIO_trap_eintr());\r | |
667 | Py_DECREF(line);\r | |
668 | if (res == NULL) {\r | |
669 | Py_DECREF(iter);\r | |
670 | return NULL;\r | |
671 | }\r | |
672 | Py_DECREF(res);\r | |
673 | }\r | |
674 | Py_DECREF(iter);\r | |
675 | Py_RETURN_NONE;\r | |
676 | }\r | |
677 | \r | |
678 | static PyMethodDef iobase_methods[] = {\r | |
679 | {"seek", iobase_seek, METH_VARARGS, iobase_seek_doc},\r | |
680 | {"tell", iobase_tell, METH_NOARGS, iobase_tell_doc},\r | |
681 | {"truncate", iobase_truncate, METH_VARARGS, iobase_truncate_doc},\r | |
682 | {"flush", iobase_flush, METH_NOARGS, iobase_flush_doc},\r | |
683 | {"close", iobase_close, METH_NOARGS, iobase_close_doc},\r | |
684 | \r | |
685 | {"seekable", iobase_seekable, METH_NOARGS, iobase_seekable_doc},\r | |
686 | {"readable", iobase_readable, METH_NOARGS, iobase_readable_doc},\r | |
687 | {"writable", iobase_writable, METH_NOARGS, iobase_writable_doc},\r | |
688 | \r | |
689 | {"_checkClosed", _PyIOBase_check_closed, METH_NOARGS},\r | |
690 | {"_checkSeekable", _PyIOBase_check_seekable, METH_NOARGS},\r | |
691 | {"_checkReadable", _PyIOBase_check_readable, METH_NOARGS},\r | |
692 | {"_checkWritable", _PyIOBase_check_writable, METH_NOARGS},\r | |
693 | \r | |
694 | {"fileno", iobase_fileno, METH_NOARGS, iobase_fileno_doc},\r | |
695 | {"isatty", iobase_isatty, METH_NOARGS, iobase_isatty_doc},\r | |
696 | \r | |
697 | {"__enter__", iobase_enter, METH_NOARGS},\r | |
698 | {"__exit__", iobase_exit, METH_VARARGS},\r | |
699 | \r | |
700 | {"readline", iobase_readline, METH_VARARGS, iobase_readline_doc},\r | |
701 | {"readlines", iobase_readlines, METH_VARARGS, iobase_readlines_doc},\r | |
702 | {"writelines", iobase_writelines, METH_VARARGS},\r | |
703 | \r | |
704 | {NULL, NULL}\r | |
705 | };\r | |
706 | \r | |
707 | static PyGetSetDef iobase_getset[] = {\r | |
708 | {"closed", (getter)iobase_closed_get, NULL, NULL},\r | |
709 | {NULL}\r | |
710 | };\r | |
711 | \r | |
712 | \r | |
713 | PyTypeObject PyIOBase_Type = {\r | |
714 | PyVarObject_HEAD_INIT(NULL, 0)\r | |
715 | "_io._IOBase", /*tp_name*/\r | |
716 | sizeof(iobase), /*tp_basicsize*/\r | |
717 | 0, /*tp_itemsize*/\r | |
718 | (destructor)iobase_dealloc, /*tp_dealloc*/\r | |
719 | 0, /*tp_print*/\r | |
720 | 0, /*tp_getattr*/\r | |
721 | 0, /*tp_setattr*/\r | |
722 | 0, /*tp_compare */\r | |
723 | 0, /*tp_repr*/\r | |
724 | 0, /*tp_as_number*/\r | |
725 | 0, /*tp_as_sequence*/\r | |
726 | 0, /*tp_as_mapping*/\r | |
727 | 0, /*tp_hash */\r | |
728 | 0, /*tp_call*/\r | |
729 | 0, /*tp_str*/\r | |
730 | 0, /*tp_getattro*/\r | |
731 | 0, /*tp_setattro*/\r | |
732 | 0, /*tp_as_buffer*/\r | |
733 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE\r | |
734 | | Py_TPFLAGS_HAVE_GC, /*tp_flags*/\r | |
735 | iobase_doc, /* tp_doc */\r | |
736 | (traverseproc)iobase_traverse, /* tp_traverse */\r | |
737 | (inquiry)iobase_clear, /* tp_clear */\r | |
738 | 0, /* tp_richcompare */\r | |
739 | offsetof(iobase, weakreflist), /* tp_weaklistoffset */\r | |
740 | iobase_iter, /* tp_iter */\r | |
741 | iobase_iternext, /* tp_iternext */\r | |
742 | iobase_methods, /* tp_methods */\r | |
743 | 0, /* tp_members */\r | |
744 | iobase_getset, /* tp_getset */\r | |
745 | 0, /* tp_base */\r | |
746 | 0, /* tp_dict */\r | |
747 | 0, /* tp_descr_get */\r | |
748 | 0, /* tp_descr_set */\r | |
749 | offsetof(iobase, dict), /* tp_dictoffset */\r | |
750 | 0, /* tp_init */\r | |
751 | 0, /* tp_alloc */\r | |
752 | PyType_GenericNew, /* tp_new */\r | |
753 | };\r | |
754 | \r | |
755 | \r | |
756 | /*\r | |
757 | * RawIOBase class, Inherits from IOBase.\r | |
758 | */\r | |
759 | PyDoc_STRVAR(rawiobase_doc,\r | |
760 | "Base class for raw binary I/O.");\r | |
761 | \r | |
762 | /*\r | |
763 | * The read() method is implemented by calling readinto(); derived classes\r | |
764 | * that want to support read() only need to implement readinto() as a\r | |
765 | * primitive operation. In general, readinto() can be more efficient than\r | |
766 | * read().\r | |
767 | *\r | |
768 | * (It would be tempting to also provide an implementation of readinto() in\r | |
769 | * terms of read(), in case the latter is a more suitable primitive operation,\r | |
770 | * but that would lead to nasty recursion in case a subclass doesn't implement\r | |
771 | * either.)\r | |
772 | */\r | |
773 | \r | |
774 | static PyObject *\r | |
775 | rawiobase_read(PyObject *self, PyObject *args)\r | |
776 | {\r | |
777 | Py_ssize_t n = -1;\r | |
778 | PyObject *b, *res;\r | |
779 | \r | |
780 | if (!PyArg_ParseTuple(args, "|n:read", &n)) {\r | |
781 | return NULL;\r | |
782 | }\r | |
783 | \r | |
784 | if (n < 0)\r | |
785 | return PyObject_CallMethod(self, "readall", NULL);\r | |
786 | \r | |
787 | /* TODO: allocate a bytes object directly instead and manually construct\r | |
788 | a writable memoryview pointing to it. */\r | |
789 | b = PyByteArray_FromStringAndSize(NULL, n);\r | |
790 | if (b == NULL)\r | |
791 | return NULL;\r | |
792 | \r | |
793 | res = PyObject_CallMethodObjArgs(self, _PyIO_str_readinto, b, NULL);\r | |
794 | if (res == NULL || res == Py_None) {\r | |
795 | Py_DECREF(b);\r | |
796 | return res;\r | |
797 | }\r | |
798 | \r | |
799 | n = PyNumber_AsSsize_t(res, PyExc_ValueError);\r | |
800 | Py_DECREF(res);\r | |
801 | if (n == -1 && PyErr_Occurred()) {\r | |
802 | Py_DECREF(b);\r | |
803 | return NULL;\r | |
804 | }\r | |
805 | \r | |
806 | res = PyBytes_FromStringAndSize(PyByteArray_AsString(b), n);\r | |
807 | Py_DECREF(b);\r | |
808 | return res;\r | |
809 | }\r | |
810 | \r | |
811 | \r | |
812 | PyDoc_STRVAR(rawiobase_readall_doc,\r | |
813 | "Read until EOF, using multiple read() call.");\r | |
814 | \r | |
815 | static PyObject *\r | |
816 | rawiobase_readall(PyObject *self, PyObject *args)\r | |
817 | {\r | |
818 | int r;\r | |
819 | PyObject *chunks = PyList_New(0);\r | |
820 | PyObject *result;\r | |
821 | \r | |
822 | if (chunks == NULL)\r | |
823 | return NULL;\r | |
824 | \r | |
825 | while (1) {\r | |
826 | PyObject *data = PyObject_CallMethod(self, "read",\r | |
827 | "i", DEFAULT_BUFFER_SIZE);\r | |
828 | if (!data) {\r | |
829 | /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()\r | |
830 | when EINTR occurs so we needn't do it ourselves. */\r | |
831 | if (_PyIO_trap_eintr()) {\r | |
832 | continue;\r | |
833 | }\r | |
834 | Py_DECREF(chunks);\r | |
835 | return NULL;\r | |
836 | }\r | |
837 | if (data == Py_None) {\r | |
838 | if (PyList_GET_SIZE(chunks) == 0) {\r | |
839 | Py_DECREF(chunks);\r | |
840 | return data;\r | |
841 | }\r | |
842 | Py_DECREF(data);\r | |
843 | break;\r | |
844 | }\r | |
845 | if (!PyBytes_Check(data)) {\r | |
846 | Py_DECREF(chunks);\r | |
847 | Py_DECREF(data);\r | |
848 | PyErr_SetString(PyExc_TypeError, "read() should return bytes");\r | |
849 | return NULL;\r | |
850 | }\r | |
851 | if (PyBytes_GET_SIZE(data) == 0) {\r | |
852 | /* EOF */\r | |
853 | Py_DECREF(data);\r | |
854 | break;\r | |
855 | }\r | |
856 | r = PyList_Append(chunks, data);\r | |
857 | Py_DECREF(data);\r | |
858 | if (r < 0) {\r | |
859 | Py_DECREF(chunks);\r | |
860 | return NULL;\r | |
861 | }\r | |
862 | }\r | |
863 | result = _PyBytes_Join(_PyIO_empty_bytes, chunks);\r | |
864 | Py_DECREF(chunks);\r | |
865 | return result;\r | |
866 | }\r | |
867 | \r | |
868 | static PyMethodDef rawiobase_methods[] = {\r | |
869 | {"read", rawiobase_read, METH_VARARGS},\r | |
870 | {"readall", rawiobase_readall, METH_NOARGS, rawiobase_readall_doc},\r | |
871 | {NULL, NULL}\r | |
872 | };\r | |
873 | \r | |
874 | PyTypeObject PyRawIOBase_Type = {\r | |
875 | PyVarObject_HEAD_INIT(NULL, 0)\r | |
876 | "_io._RawIOBase", /*tp_name*/\r | |
877 | 0, /*tp_basicsize*/\r | |
878 | 0, /*tp_itemsize*/\r | |
879 | 0, /*tp_dealloc*/\r | |
880 | 0, /*tp_print*/\r | |
881 | 0, /*tp_getattr*/\r | |
882 | 0, /*tp_setattr*/\r | |
883 | 0, /*tp_compare */\r | |
884 | 0, /*tp_repr*/\r | |
885 | 0, /*tp_as_number*/\r | |
886 | 0, /*tp_as_sequence*/\r | |
887 | 0, /*tp_as_mapping*/\r | |
888 | 0, /*tp_hash */\r | |
889 | 0, /*tp_call*/\r | |
890 | 0, /*tp_str*/\r | |
891 | 0, /*tp_getattro*/\r | |
892 | 0, /*tp_setattro*/\r | |
893 | 0, /*tp_as_buffer*/\r | |
894 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/\r | |
895 | rawiobase_doc, /* tp_doc */\r | |
896 | 0, /* tp_traverse */\r | |
897 | 0, /* tp_clear */\r | |
898 | 0, /* tp_richcompare */\r | |
899 | 0, /* tp_weaklistoffset */\r | |
900 | 0, /* tp_iter */\r | |
901 | 0, /* tp_iternext */\r | |
902 | rawiobase_methods, /* tp_methods */\r | |
903 | 0, /* tp_members */\r | |
904 | 0, /* tp_getset */\r | |
905 | &PyIOBase_Type, /* tp_base */\r | |
906 | 0, /* tp_dict */\r | |
907 | 0, /* tp_descr_get */\r | |
908 | 0, /* tp_descr_set */\r | |
909 | 0, /* tp_dictoffset */\r | |
910 | 0, /* tp_init */\r | |
911 | 0, /* tp_alloc */\r | |
912 | 0, /* tp_new */\r | |
913 | };\r |