blob: bcefb3862c3200fa42b1bccb18a90c222d1544ae [file] [log] [blame]
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001/*
2 An implementation of the I/O abstract base classes hierarchy
3 as defined by PEP 3116 - "New I/O"
Victor Stinnercc024d12013-10-29 02:23:46 +01004
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00005 Classes defined here: IOBase, RawIOBase.
Victor Stinnercc024d12013-10-29 02:23:46 +01006
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00007 Written by Amaury Forgeot d'Arc and Antoine Pitrou
8*/
9
10
11#define PY_SSIZE_T_CLEAN
12#include "Python.h"
13#include "structmember.h"
14#include "_iomodule.h"
15
Serhiy Storchakaf24131f2015-04-16 11:19:43 +030016/*[clinic input]
17module _io
18class _io._IOBase "PyObject *" "&PyIOBase_Type"
19class _io._RawIOBase "PyObject *" "&PyRawIOBase_Type"
20[clinic start generated code]*/
21/*[clinic end generated code: output=da39a3ee5e6b4b0d input=d29a4d076c2b211c]*/
22
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000023/*
24 * IOBase class, an abstract class
25 */
26
27typedef struct {
28 PyObject_HEAD
Victor Stinnercc024d12013-10-29 02:23:46 +010029
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000030 PyObject *dict;
31 PyObject *weakreflist;
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000032} iobase;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000033
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000034PyDoc_STRVAR(iobase_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000035 "The abstract base class for all I/O classes, acting on streams of\n"
36 "bytes. There is no public constructor.\n"
37 "\n"
38 "This class provides dummy implementations for many methods that\n"
39 "derived classes can override selectively; the default implementations\n"
40 "represent a file that cannot be read, written or seeked.\n"
41 "\n"
42 "Even though IOBase does not declare read, readinto, or write because\n"
43 "their signatures will vary, implementations and clients should\n"
44 "consider those methods part of the interface. Also, implementations\n"
Amaury Forgeot d'Arc616453c2010-09-06 22:31:52 +000045 "may raise UnsupportedOperation when operations they do not support are\n"
46 "called.\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000047 "\n"
48 "The basic type used for binary data read from or written to a file is\n"
Martin Panter6bb91f32016-05-28 00:41:57 +000049 "bytes. Other bytes-like objects are accepted as method arguments too.\n"
50 "In some cases (such as readinto), a writable object is required. Text\n"
51 "I/O classes work with str data.\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000052 "\n"
Andrew Kuchling76466202014-04-15 21:11:36 -040053 "Note that calling any method (except additional calls to close(),\n"
54 "which are ignored) on a closed stream should raise a ValueError.\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000055 "\n"
56 "IOBase (and its subclasses) support the iterator protocol, meaning\n"
57 "that an IOBase object can be iterated over yielding the lines in a\n"
58 "stream.\n"
59 "\n"
60 "IOBase also supports the :keyword:`with` statement. In this example,\n"
Ezio Melotti13925002011-03-16 11:05:33 +020061 "fp is closed after the suite of the with statement is complete:\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000062 "\n"
63 "with open('spam.txt', 'r') as fp:\n"
64 " fp.write('Spam and eggs!')\n");
65
66/* Use this macro whenever you want to check the internal `closed` status
67 of the IOBase object rather than the virtual `closed` attribute as returned
68 by whatever subclass. */
69
Martin v. Löwis767046a2011-10-14 15:35:36 +020070_Py_IDENTIFIER(__IOBase_closed);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000071#define IS_CLOSED(self) \
Martin v. Löwis767046a2011-10-14 15:35:36 +020072 _PyObject_HasAttrId(self, &PyId___IOBase_closed)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000073
Victor Stinner3f36a572013-11-12 21:39:02 +010074_Py_IDENTIFIER(read);
75
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000076/* Internal methods */
77static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000078iobase_unsupported(const char *message)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000079{
Antoine Pitrou712cb732013-12-21 15:51:54 +010080 _PyIO_State *state = IO_STATE();
81 if (state != NULL)
82 PyErr_SetString(state->unsupported_operation, message);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000083 return NULL;
84}
85
Raymond Hettinger15f44ab2016-08-30 10:47:49 -070086/* Positioning */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000087
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000088PyDoc_STRVAR(iobase_seek_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000089 "Change stream position.\n"
90 "\n"
Terry Jan Reedy0158af32013-03-11 17:42:46 -040091 "Change the stream position to the given byte offset. The offset is\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000092 "interpreted relative to the position indicated by whence. Values\n"
93 "for whence are:\n"
94 "\n"
95 "* 0 -- start of stream (the default); offset should be zero or positive\n"
96 "* 1 -- current stream position; offset may be negative\n"
97 "* 2 -- end of stream; offset is usually negative\n"
98 "\n"
99 "Return the new absolute position.");
100
101static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000102iobase_seek(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000103{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000104 return iobase_unsupported("seek");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000105}
106
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300107/*[clinic input]
108_io._IOBase.tell
109
110Return current stream position.
111[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000112
113static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300114_io__IOBase_tell_impl(PyObject *self)
115/*[clinic end generated code: output=89a1c0807935abe2 input=04e615fec128801f]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000116{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200117 _Py_IDENTIFIER(seek);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200118
119 return _PyObject_CallMethodId(self, &PyId_seek, "ii", 0, 1);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000120}
121
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000122PyDoc_STRVAR(iobase_truncate_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000123 "Truncate file to size bytes.\n"
124 "\n"
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000125 "File pointer is left unchanged. Size defaults to the current IO\n"
126 "position as reported by tell(). Returns the new size.");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000127
128static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000129iobase_truncate(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000130{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000131 return iobase_unsupported("truncate");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000132}
133
134/* Flush and close methods */
135
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300136/*[clinic input]
137_io._IOBase.flush
138
139Flush write buffers, if applicable.
140
141This is not implemented for read-only and non-blocking streams.
142[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000143
144static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300145_io__IOBase_flush_impl(PyObject *self)
146/*[clinic end generated code: output=7cef4b4d54656a3b input=773be121abe270aa]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000147{
148 /* XXX Should this return the number of bytes written??? */
149 if (IS_CLOSED(self)) {
150 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file.");
151 return NULL;
152 }
153 Py_RETURN_NONE;
154}
155
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000156static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000157iobase_closed(PyObject *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000158{
159 PyObject *res;
160 int closed;
161 /* This gets the derived attribute, which is *not* __IOBase_closed
162 in most cases! */
163 res = PyObject_GetAttr(self, _PyIO_str_closed);
164 if (res == NULL)
165 return 0;
166 closed = PyObject_IsTrue(res);
167 Py_DECREF(res);
168 return closed;
169}
170
171static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000172iobase_closed_get(PyObject *self, void *context)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000173{
174 return PyBool_FromLong(IS_CLOSED(self));
175}
176
177PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000178_PyIOBase_check_closed(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000179{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000180 if (iobase_closed(self)) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000181 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file.");
182 return NULL;
183 }
184 if (args == Py_True)
185 return Py_None;
186 else
187 Py_RETURN_NONE;
188}
189
190/* XXX: IOBase thinks it has to maintain its own internal state in
191 `__IOBase_closed` and call flush() by itself, but it is redundant with
192 whatever behaviour a non-trivial derived class will implement. */
193
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300194/*[clinic input]
195_io._IOBase.close
196
197Flush and close the IO object.
198
199This method has no effect if the file is already closed.
200[clinic start generated code]*/
201
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000202static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300203_io__IOBase_close_impl(PyObject *self)
204/*[clinic end generated code: output=63c6a6f57d783d6d input=f4494d5c31dbc6b7]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000205{
206 PyObject *res;
207
208 if (IS_CLOSED(self))
209 Py_RETURN_NONE;
210
211 res = PyObject_CallMethodObjArgs(self, _PyIO_str_flush, NULL);
Victor Stinneraa5bbfa2013-11-08 00:29:41 +0100212
213 if (_PyObject_SetAttrId(self, &PyId___IOBase_closed, Py_True) < 0) {
214 Py_XDECREF(res);
Antoine Pitrou6be88762010-05-03 16:48:20 +0000215 return NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000216 }
Victor Stinneraa5bbfa2013-11-08 00:29:41 +0100217
218 if (res == NULL)
219 return NULL;
220
221 Py_DECREF(res);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000222 Py_RETURN_NONE;
223}
224
225/* Finalization and garbage collection support */
226
Antoine Pitrou796564c2013-07-30 19:59:21 +0200227static void
228iobase_finalize(PyObject *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000229{
230 PyObject *res;
Antoine Pitrou796564c2013-07-30 19:59:21 +0200231 PyObject *error_type, *error_value, *error_traceback;
232 int closed;
233 _Py_IDENTIFIER(_finalizing);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000234
Antoine Pitrou796564c2013-07-30 19:59:21 +0200235 /* Save the current exception, if any. */
236 PyErr_Fetch(&error_type, &error_value, &error_traceback);
237
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000238 /* If `closed` doesn't exist or can't be evaluated as bool, then the
239 object is probably in an unusable state, so ignore. */
240 res = PyObject_GetAttr(self, _PyIO_str_closed);
Christian Heimes72f455e2013-07-31 01:33:50 +0200241 if (res == NULL) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000242 PyErr_Clear();
Christian Heimes72f455e2013-07-31 01:33:50 +0200243 closed = -1;
244 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000245 else {
246 closed = PyObject_IsTrue(res);
247 Py_DECREF(res);
248 if (closed == -1)
249 PyErr_Clear();
250 }
251 if (closed == 0) {
Antoine Pitrou796564c2013-07-30 19:59:21 +0200252 /* Signal close() that it was called as part of the object
253 finalization process. */
254 if (_PyObject_SetAttrId(self, &PyId__finalizing, Py_True))
255 PyErr_Clear();
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000256 res = PyObject_CallMethodObjArgs((PyObject *) self, _PyIO_str_close,
257 NULL);
258 /* Silencing I/O errors is bad, but printing spurious tracebacks is
259 equally as bad, and potentially more frequent (because of
260 shutdown issues). */
261 if (res == NULL)
262 PyErr_Clear();
263 else
264 Py_DECREF(res);
265 }
Antoine Pitrou796564c2013-07-30 19:59:21 +0200266
267 /* Restore the saved exception. */
268 PyErr_Restore(error_type, error_value, error_traceback);
269}
270
271int
272_PyIOBase_finalize(PyObject *self)
273{
274 int is_zombie;
275
276 /* If _PyIOBase_finalize() is called from a destructor, we need to
277 resurrect the object as calling close() can invoke arbitrary code. */
278 is_zombie = (Py_REFCNT(self) == 0);
279 if (is_zombie)
280 return PyObject_CallFinalizerFromDealloc(self);
281 else {
282 PyObject_CallFinalizer(self);
283 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000284 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000285}
286
287static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000288iobase_traverse(iobase *self, visitproc visit, void *arg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000289{
290 Py_VISIT(self->dict);
291 return 0;
292}
293
294static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000295iobase_clear(iobase *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000296{
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000297 Py_CLEAR(self->dict);
298 return 0;
299}
300
301/* Destructor */
302
303static void
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000304iobase_dealloc(iobase *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000305{
306 /* NOTE: since IOBaseObject has its own dict, Python-defined attributes
307 are still available here for close() to use.
308 However, if the derived class declares a __slots__, those slots are
309 already gone.
310 */
311 if (_PyIOBase_finalize((PyObject *) self) < 0) {
312 /* When called from a heap type's dealloc, the type will be
313 decref'ed on return (see e.g. subtype_dealloc in typeobject.c). */
314 if (PyType_HasFeature(Py_TYPE(self), Py_TPFLAGS_HEAPTYPE))
315 Py_INCREF(Py_TYPE(self));
316 return;
317 }
318 _PyObject_GC_UNTRACK(self);
319 if (self->weakreflist != NULL)
320 PyObject_ClearWeakRefs((PyObject *) self);
321 Py_CLEAR(self->dict);
322 Py_TYPE(self)->tp_free((PyObject *) self);
323}
324
325/* Inquiry methods */
326
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300327/*[clinic input]
328_io._IOBase.seekable
329
330Return whether object supports random access.
331
Martin Panter754aab22016-03-31 07:21:56 +0000332If False, seek(), tell() and truncate() will raise OSError.
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300333This method may need to do a test seek().
334[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000335
336static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300337_io__IOBase_seekable_impl(PyObject *self)
Martin Panter754aab22016-03-31 07:21:56 +0000338/*[clinic end generated code: output=4c24c67f5f32a43d input=b976622f7fdf3063]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000339{
340 Py_RETURN_FALSE;
341}
342
343PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000344_PyIOBase_check_seekable(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000345{
346 PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_seekable, NULL);
347 if (res == NULL)
348 return NULL;
349 if (res != Py_True) {
350 Py_CLEAR(res);
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000351 iobase_unsupported("File or stream is not seekable.");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000352 return NULL;
353 }
354 if (args == Py_True) {
355 Py_DECREF(res);
356 }
357 return res;
358}
359
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300360/*[clinic input]
361_io._IOBase.readable
362
363Return whether object was opened for reading.
364
Martin Panter754aab22016-03-31 07:21:56 +0000365If False, read() will raise OSError.
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300366[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000367
368static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300369_io__IOBase_readable_impl(PyObject *self)
Martin Panter754aab22016-03-31 07:21:56 +0000370/*[clinic end generated code: output=e48089250686388b input=285b3b866a0ec35f]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000371{
372 Py_RETURN_FALSE;
373}
374
375/* May be called with any object */
376PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000377_PyIOBase_check_readable(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000378{
379 PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_readable, NULL);
380 if (res == NULL)
381 return NULL;
382 if (res != Py_True) {
383 Py_CLEAR(res);
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000384 iobase_unsupported("File or stream is not readable.");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000385 return NULL;
386 }
387 if (args == Py_True) {
388 Py_DECREF(res);
389 }
390 return res;
391}
392
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300393/*[clinic input]
394_io._IOBase.writable
395
396Return whether object was opened for writing.
397
Martin Panter754aab22016-03-31 07:21:56 +0000398If False, write() will raise OSError.
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300399[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000400
401static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300402_io__IOBase_writable_impl(PyObject *self)
Martin Panter754aab22016-03-31 07:21:56 +0000403/*[clinic end generated code: output=406001d0985be14f input=9dcac18a013a05b5]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000404{
405 Py_RETURN_FALSE;
406}
407
408/* May be called with any object */
409PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000410_PyIOBase_check_writable(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000411{
412 PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_writable, NULL);
413 if (res == NULL)
414 return NULL;
415 if (res != Py_True) {
416 Py_CLEAR(res);
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000417 iobase_unsupported("File or stream is not writable.");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000418 return NULL;
419 }
420 if (args == Py_True) {
421 Py_DECREF(res);
422 }
423 return res;
424}
425
426/* Context manager */
427
428static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000429iobase_enter(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000430{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000431 if (_PyIOBase_check_closed(self, Py_True) == NULL)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000432 return NULL;
433
434 Py_INCREF(self);
435 return self;
436}
437
438static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000439iobase_exit(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000440{
441 return PyObject_CallMethodObjArgs(self, _PyIO_str_close, NULL);
442}
443
444/* Lower-level APIs */
445
446/* XXX Should these be present even if unimplemented? */
447
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300448/*[clinic input]
449_io._IOBase.fileno
450
451Returns underlying file descriptor if one exists.
452
Martin Panter754aab22016-03-31 07:21:56 +0000453OSError is raised if the IO object does not use a file descriptor.
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300454[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000455
456static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300457_io__IOBase_fileno_impl(PyObject *self)
Martin Panter754aab22016-03-31 07:21:56 +0000458/*[clinic end generated code: output=7cc0973f0f5f3b73 input=4e37028947dc1cc8]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000459{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000460 return iobase_unsupported("fileno");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000461}
462
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300463/*[clinic input]
464_io._IOBase.isatty
465
466Return whether this is an 'interactive' stream.
467
468Return False if it can't be determined.
469[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000470
471static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300472_io__IOBase_isatty_impl(PyObject *self)
473/*[clinic end generated code: output=60cab77cede41cdd input=9ef76530d368458b]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000474{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000475 if (_PyIOBase_check_closed(self, Py_True) == NULL)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000476 return NULL;
477 Py_RETURN_FALSE;
478}
479
480/* Readline(s) and writelines */
481
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300482/*[clinic input]
483_io._IOBase.readline
Serhiy Storchaka762bf402017-03-30 09:15:31 +0300484 size as limit: Py_ssize_t(accept={int, NoneType}) = -1
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300485 /
486
487Read and return a line from the stream.
488
489If size is specified, at most size bytes will be read.
490
491The line terminator is always b'\n' for binary files; for text
492files, the newlines argument to open can be used to select the line
493terminator(s) recognized.
494[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000495
496static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300497_io__IOBase_readline_impl(PyObject *self, Py_ssize_t limit)
Serhiy Storchaka762bf402017-03-30 09:15:31 +0300498/*[clinic end generated code: output=4479f79b58187840 input=d0c596794e877bff]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000499{
500 /* For backwards compatibility, a (slowish) readline(). */
501
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000502 int has_peek = 0;
503 PyObject *buffer, *result;
504 Py_ssize_t old_size = -1;
Martin v. Löwis767046a2011-10-14 15:35:36 +0200505 _Py_IDENTIFIER(peek);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000506
Martin v. Löwis767046a2011-10-14 15:35:36 +0200507 if (_PyObject_HasAttrId(self, &PyId_peek))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000508 has_peek = 1;
509
510 buffer = PyByteArray_FromStringAndSize(NULL, 0);
511 if (buffer == NULL)
512 return NULL;
513
Serhiy Storchakafff9a312017-03-21 08:53:25 +0200514 while (limit < 0 || PyByteArray_GET_SIZE(buffer) < limit) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000515 Py_ssize_t nreadahead = 1;
516 PyObject *b;
517
518 if (has_peek) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200519 PyObject *readahead = _PyObject_CallMethodId(self, &PyId_peek, "i", 1);
Gregory P. Smith51359922012-06-23 23:55:39 -0700520 if (readahead == NULL) {
521 /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
522 when EINTR occurs so we needn't do it ourselves. */
523 if (_PyIO_trap_eintr()) {
524 continue;
525 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000526 goto fail;
Gregory P. Smith51359922012-06-23 23:55:39 -0700527 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000528 if (!PyBytes_Check(readahead)) {
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300529 PyErr_Format(PyExc_OSError,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000530 "peek() should have returned a bytes object, "
531 "not '%.200s'", Py_TYPE(readahead)->tp_name);
532 Py_DECREF(readahead);
533 goto fail;
534 }
535 if (PyBytes_GET_SIZE(readahead) > 0) {
536 Py_ssize_t n = 0;
537 const char *buf = PyBytes_AS_STRING(readahead);
538 if (limit >= 0) {
539 do {
540 if (n >= PyBytes_GET_SIZE(readahead) || n >= limit)
541 break;
542 if (buf[n++] == '\n')
543 break;
544 } while (1);
545 }
546 else {
547 do {
548 if (n >= PyBytes_GET_SIZE(readahead))
549 break;
550 if (buf[n++] == '\n')
551 break;
552 } while (1);
553 }
554 nreadahead = n;
555 }
556 Py_DECREF(readahead);
557 }
558
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200559 b = _PyObject_CallMethodId(self, &PyId_read, "n", nreadahead);
Gregory P. Smith51359922012-06-23 23:55:39 -0700560 if (b == NULL) {
561 /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
562 when EINTR occurs so we needn't do it ourselves. */
563 if (_PyIO_trap_eintr()) {
564 continue;
565 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000566 goto fail;
Gregory P. Smith51359922012-06-23 23:55:39 -0700567 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000568 if (!PyBytes_Check(b)) {
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300569 PyErr_Format(PyExc_OSError,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000570 "read() should have returned a bytes object, "
571 "not '%.200s'", Py_TYPE(b)->tp_name);
572 Py_DECREF(b);
573 goto fail;
574 }
575 if (PyBytes_GET_SIZE(b) == 0) {
576 Py_DECREF(b);
577 break;
578 }
579
580 old_size = PyByteArray_GET_SIZE(buffer);
Victor Stinnercc024d12013-10-29 02:23:46 +0100581 if (PyByteArray_Resize(buffer, old_size + PyBytes_GET_SIZE(b)) < 0) {
582 Py_DECREF(b);
583 goto fail;
584 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000585 memcpy(PyByteArray_AS_STRING(buffer) + old_size,
586 PyBytes_AS_STRING(b), PyBytes_GET_SIZE(b));
587
588 Py_DECREF(b);
589
590 if (PyByteArray_AS_STRING(buffer)[PyByteArray_GET_SIZE(buffer) - 1] == '\n')
591 break;
592 }
593
594 result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(buffer),
595 PyByteArray_GET_SIZE(buffer));
596 Py_DECREF(buffer);
597 return result;
598 fail:
599 Py_DECREF(buffer);
600 return NULL;
601}
602
603static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000604iobase_iter(PyObject *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000605{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000606 if (_PyIOBase_check_closed(self, Py_True) == NULL)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000607 return NULL;
608
609 Py_INCREF(self);
610 return self;
611}
612
613static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000614iobase_iternext(PyObject *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000615{
616 PyObject *line = PyObject_CallMethodObjArgs(self, _PyIO_str_readline, NULL);
617
618 if (line == NULL)
619 return NULL;
620
Serhiy Storchakabf623ae2017-04-19 20:03:52 +0300621 if (PyObject_Size(line) <= 0) {
622 /* Error or empty */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000623 Py_DECREF(line);
624 return NULL;
625 }
626
627 return line;
628}
629
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300630/*[clinic input]
631_io._IOBase.readlines
Serhiy Storchaka762bf402017-03-30 09:15:31 +0300632 hint: Py_ssize_t(accept={int, NoneType}) = -1
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300633 /
634
635Return a list of lines from the stream.
636
637hint can be specified to control the number of lines read: no more
638lines will be read if the total size (in bytes/characters) of all
639lines so far exceeds hint.
640[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000641
642static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300643_io__IOBase_readlines_impl(PyObject *self, Py_ssize_t hint)
Serhiy Storchaka762bf402017-03-30 09:15:31 +0300644/*[clinic end generated code: output=2f50421677fa3dea input=9400c786ea9dc416]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000645{
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300646 Py_ssize_t length = 0;
Xiang Zhang026435c2017-04-15 12:47:28 +0800647 PyObject *result, *it = NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000648
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000649 result = PyList_New(0);
650 if (result == NULL)
651 return NULL;
652
653 if (hint <= 0) {
654 /* XXX special-casing this made sense in the Python version in order
655 to remove the bytecode interpretation overhead, but it could
656 probably be removed here. */
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200657 _Py_IDENTIFIER(extend);
Victor Stinner61bdb0d2016-12-09 15:39:28 +0100658 PyObject *ret = _PyObject_CallMethodIdObjArgs(result, &PyId_extend,
659 self, NULL);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200660
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000661 if (ret == NULL) {
Xiang Zhang026435c2017-04-15 12:47:28 +0800662 goto error;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000663 }
664 Py_DECREF(ret);
665 return result;
666 }
667
Xiang Zhang026435c2017-04-15 12:47:28 +0800668 it = PyObject_GetIter(self);
669 if (it == NULL) {
670 goto error;
671 }
672
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000673 while (1) {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +0300674 Py_ssize_t line_length;
Xiang Zhang026435c2017-04-15 12:47:28 +0800675 PyObject *line = PyIter_Next(it);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000676 if (line == NULL) {
677 if (PyErr_Occurred()) {
Xiang Zhang026435c2017-04-15 12:47:28 +0800678 goto error;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000679 }
680 else
681 break; /* StopIteration raised */
682 }
683
684 if (PyList_Append(result, line) < 0) {
685 Py_DECREF(line);
Xiang Zhang026435c2017-04-15 12:47:28 +0800686 goto error;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000687 }
Serhiy Storchakabf623ae2017-04-19 20:03:52 +0300688 line_length = PyObject_Size(line);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000689 Py_DECREF(line);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +0300690 if (line_length < 0) {
691 goto error;
692 }
693 if (line_length > hint - length)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000694 break;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +0300695 length += line_length;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000696 }
Xiang Zhang026435c2017-04-15 12:47:28 +0800697
698 Py_DECREF(it);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000699 return result;
Xiang Zhang026435c2017-04-15 12:47:28 +0800700
701 error:
702 Py_XDECREF(it);
703 Py_DECREF(result);
704 return NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000705}
706
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300707/*[clinic input]
708_io._IOBase.writelines
709 lines: object
710 /
711[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000712
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300713static PyObject *
714_io__IOBase_writelines(PyObject *self, PyObject *lines)
715/*[clinic end generated code: output=976eb0a9b60a6628 input=432e729a8450b3cb]*/
716{
717 PyObject *iter, *res;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000718
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000719 if (_PyIOBase_check_closed(self, Py_True) == NULL)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000720 return NULL;
721
722 iter = PyObject_GetIter(lines);
723 if (iter == NULL)
724 return NULL;
725
726 while (1) {
727 PyObject *line = PyIter_Next(iter);
728 if (line == NULL) {
729 if (PyErr_Occurred()) {
730 Py_DECREF(iter);
731 return NULL;
732 }
733 else
734 break; /* Stop Iteration */
735 }
736
Gregory P. Smithb9817b02013-02-01 13:03:39 -0800737 res = NULL;
738 do {
739 res = PyObject_CallMethodObjArgs(self, _PyIO_str_write, line, NULL);
740 } while (res == NULL && _PyIO_trap_eintr());
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000741 Py_DECREF(line);
742 if (res == NULL) {
743 Py_DECREF(iter);
744 return NULL;
745 }
746 Py_DECREF(res);
747 }
748 Py_DECREF(iter);
749 Py_RETURN_NONE;
750}
751
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300752#include "clinic/iobase.c.h"
753
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000754static PyMethodDef iobase_methods[] = {
755 {"seek", iobase_seek, METH_VARARGS, iobase_seek_doc},
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300756 _IO__IOBASE_TELL_METHODDEF
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000757 {"truncate", iobase_truncate, METH_VARARGS, iobase_truncate_doc},
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300758 _IO__IOBASE_FLUSH_METHODDEF
759 _IO__IOBASE_CLOSE_METHODDEF
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000760
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300761 _IO__IOBASE_SEEKABLE_METHODDEF
762 _IO__IOBASE_READABLE_METHODDEF
763 _IO__IOBASE_WRITABLE_METHODDEF
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000764
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000765 {"_checkClosed", _PyIOBase_check_closed, METH_NOARGS},
766 {"_checkSeekable", _PyIOBase_check_seekable, METH_NOARGS},
767 {"_checkReadable", _PyIOBase_check_readable, METH_NOARGS},
768 {"_checkWritable", _PyIOBase_check_writable, METH_NOARGS},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000769
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300770 _IO__IOBASE_FILENO_METHODDEF
771 _IO__IOBASE_ISATTY_METHODDEF
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000772
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000773 {"__enter__", iobase_enter, METH_NOARGS},
774 {"__exit__", iobase_exit, METH_VARARGS},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000775
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300776 _IO__IOBASE_READLINE_METHODDEF
777 _IO__IOBASE_READLINES_METHODDEF
778 _IO__IOBASE_WRITELINES_METHODDEF
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000779
780 {NULL, NULL}
781};
782
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000783static PyGetSetDef iobase_getset[] = {
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500784 {"__dict__", PyObject_GenericGetDict, NULL, NULL},
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000785 {"closed", (getter)iobase_closed_get, NULL, NULL},
Benjamin Peterson1fea3212009-04-19 03:15:20 +0000786 {NULL}
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000787};
788
789
790PyTypeObject PyIOBase_Type = {
791 PyVarObject_HEAD_INIT(NULL, 0)
792 "_io._IOBase", /*tp_name*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000793 sizeof(iobase), /*tp_basicsize*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000794 0, /*tp_itemsize*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000795 (destructor)iobase_dealloc, /*tp_dealloc*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000796 0, /*tp_print*/
797 0, /*tp_getattr*/
798 0, /*tp_setattr*/
799 0, /*tp_compare */
800 0, /*tp_repr*/
801 0, /*tp_as_number*/
802 0, /*tp_as_sequence*/
803 0, /*tp_as_mapping*/
804 0, /*tp_hash */
805 0, /*tp_call*/
806 0, /*tp_str*/
807 0, /*tp_getattro*/
808 0, /*tp_setattro*/
809 0, /*tp_as_buffer*/
810 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
Antoine Pitrou796564c2013-07-30 19:59:21 +0200811 | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /*tp_flags*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000812 iobase_doc, /* tp_doc */
813 (traverseproc)iobase_traverse, /* tp_traverse */
814 (inquiry)iobase_clear, /* tp_clear */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000815 0, /* tp_richcompare */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000816 offsetof(iobase, weakreflist), /* tp_weaklistoffset */
817 iobase_iter, /* tp_iter */
818 iobase_iternext, /* tp_iternext */
819 iobase_methods, /* tp_methods */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000820 0, /* tp_members */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000821 iobase_getset, /* tp_getset */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000822 0, /* tp_base */
823 0, /* tp_dict */
824 0, /* tp_descr_get */
825 0, /* tp_descr_set */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000826 offsetof(iobase, dict), /* tp_dictoffset */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000827 0, /* tp_init */
828 0, /* tp_alloc */
829 PyType_GenericNew, /* tp_new */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200830 0, /* tp_free */
831 0, /* tp_is_gc */
832 0, /* tp_bases */
833 0, /* tp_mro */
834 0, /* tp_cache */
835 0, /* tp_subclasses */
836 0, /* tp_weaklist */
837 0, /* tp_del */
838 0, /* tp_version_tag */
Victor Stinner928bff02016-03-19 10:36:36 +0100839 iobase_finalize, /* tp_finalize */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000840};
841
842
843/*
844 * RawIOBase class, Inherits from IOBase.
845 */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000846PyDoc_STRVAR(rawiobase_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000847 "Base class for raw binary I/O.");
848
849/*
850 * The read() method is implemented by calling readinto(); derived classes
851 * that want to support read() only need to implement readinto() as a
852 * primitive operation. In general, readinto() can be more efficient than
853 * read().
854 *
855 * (It would be tempting to also provide an implementation of readinto() in
856 * terms of read(), in case the latter is a more suitable primitive operation,
857 * but that would lead to nasty recursion in case a subclass doesn't implement
858 * either.)
859*/
860
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300861/*[clinic input]
862_io._RawIOBase.read
863 size as n: Py_ssize_t = -1
864 /
865[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000866
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300867static PyObject *
868_io__RawIOBase_read_impl(PyObject *self, Py_ssize_t n)
869/*[clinic end generated code: output=6cdeb731e3c9f13c input=b6d0dcf6417d1374]*/
870{
871 PyObject *b, *res;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000872
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200873 if (n < 0) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200874 _Py_IDENTIFIER(readall);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200875
876 return _PyObject_CallMethodId(self, &PyId_readall, NULL);
877 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000878
879 /* TODO: allocate a bytes object directly instead and manually construct
880 a writable memoryview pointing to it. */
881 b = PyByteArray_FromStringAndSize(NULL, n);
882 if (b == NULL)
883 return NULL;
884
885 res = PyObject_CallMethodObjArgs(self, _PyIO_str_readinto, b, NULL);
Antoine Pitrou328ec742010-09-14 18:37:24 +0000886 if (res == NULL || res == Py_None) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000887 Py_DECREF(b);
Antoine Pitrou328ec742010-09-14 18:37:24 +0000888 return res;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000889 }
890
891 n = PyNumber_AsSsize_t(res, PyExc_ValueError);
892 Py_DECREF(res);
893 if (n == -1 && PyErr_Occurred()) {
894 Py_DECREF(b);
895 return NULL;
896 }
897
898 res = PyBytes_FromStringAndSize(PyByteArray_AsString(b), n);
899 Py_DECREF(b);
900 return res;
901}
902
903
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300904/*[clinic input]
905_io._RawIOBase.readall
906
907Read until EOF, using multiple read() call.
908[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000909
910static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300911_io__RawIOBase_readall_impl(PyObject *self)
912/*[clinic end generated code: output=1987b9ce929425a0 input=688874141213622a]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000913{
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000914 int r;
915 PyObject *chunks = PyList_New(0);
916 PyObject *result;
Victor Stinnercc024d12013-10-29 02:23:46 +0100917
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000918 if (chunks == NULL)
919 return NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000920
921 while (1) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200922 PyObject *data = _PyObject_CallMethodId(self, &PyId_read,
923 "i", DEFAULT_BUFFER_SIZE);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000924 if (!data) {
Gregory P. Smith51359922012-06-23 23:55:39 -0700925 /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
926 when EINTR occurs so we needn't do it ourselves. */
927 if (_PyIO_trap_eintr()) {
928 continue;
929 }
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000930 Py_DECREF(chunks);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000931 return NULL;
932 }
Victor Stinnera80987f2011-05-25 22:47:16 +0200933 if (data == Py_None) {
934 if (PyList_GET_SIZE(chunks) == 0) {
935 Py_DECREF(chunks);
936 return data;
937 }
938 Py_DECREF(data);
939 break;
940 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000941 if (!PyBytes_Check(data)) {
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000942 Py_DECREF(chunks);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000943 Py_DECREF(data);
944 PyErr_SetString(PyExc_TypeError, "read() should return bytes");
945 return NULL;
946 }
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000947 if (PyBytes_GET_SIZE(data) == 0) {
948 /* EOF */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000949 Py_DECREF(data);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000950 break;
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000951 }
952 r = PyList_Append(chunks, data);
953 Py_DECREF(data);
954 if (r < 0) {
955 Py_DECREF(chunks);
956 return NULL;
957 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000958 }
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000959 result = _PyBytes_Join(_PyIO_empty_bytes, chunks);
960 Py_DECREF(chunks);
961 return result;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000962}
963
Antoine Pitrou45d61562015-05-20 21:50:59 +0200964static PyObject *
965rawiobase_readinto(PyObject *self, PyObject *args)
966{
967 PyErr_SetNone(PyExc_NotImplementedError);
968 return NULL;
969}
970
971static PyObject *
972rawiobase_write(PyObject *self, PyObject *args)
973{
974 PyErr_SetNone(PyExc_NotImplementedError);
975 return NULL;
976}
977
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000978static PyMethodDef rawiobase_methods[] = {
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300979 _IO__RAWIOBASE_READ_METHODDEF
980 _IO__RAWIOBASE_READALL_METHODDEF
Antoine Pitrou45d61562015-05-20 21:50:59 +0200981 {"readinto", rawiobase_readinto, METH_VARARGS},
982 {"write", rawiobase_write, METH_VARARGS},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000983 {NULL, NULL}
984};
985
986PyTypeObject PyRawIOBase_Type = {
987 PyVarObject_HEAD_INIT(NULL, 0)
988 "_io._RawIOBase", /*tp_name*/
989 0, /*tp_basicsize*/
990 0, /*tp_itemsize*/
991 0, /*tp_dealloc*/
992 0, /*tp_print*/
993 0, /*tp_getattr*/
994 0, /*tp_setattr*/
995 0, /*tp_compare */
996 0, /*tp_repr*/
997 0, /*tp_as_number*/
998 0, /*tp_as_sequence*/
999 0, /*tp_as_mapping*/
1000 0, /*tp_hash */
1001 0, /*tp_call*/
1002 0, /*tp_str*/
1003 0, /*tp_getattro*/
1004 0, /*tp_setattro*/
1005 0, /*tp_as_buffer*/
Antoine Pitrou796564c2013-07-30 19:59:21 +02001006 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_FINALIZE, /*tp_flags*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001007 rawiobase_doc, /* tp_doc */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001008 0, /* tp_traverse */
1009 0, /* tp_clear */
1010 0, /* tp_richcompare */
1011 0, /* tp_weaklistoffset */
1012 0, /* tp_iter */
1013 0, /* tp_iternext */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001014 rawiobase_methods, /* tp_methods */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001015 0, /* tp_members */
1016 0, /* tp_getset */
1017 &PyIOBase_Type, /* tp_base */
1018 0, /* tp_dict */
1019 0, /* tp_descr_get */
1020 0, /* tp_descr_set */
1021 0, /* tp_dictoffset */
1022 0, /* tp_init */
1023 0, /* tp_alloc */
1024 0, /* tp_new */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001025 0, /* tp_free */
1026 0, /* tp_is_gc */
1027 0, /* tp_bases */
1028 0, /* tp_mro */
1029 0, /* tp_cache */
1030 0, /* tp_subclasses */
1031 0, /* tp_weaklist */
1032 0, /* tp_del */
1033 0, /* tp_version_tag */
1034 0, /* tp_finalize */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001035};