blob: 1a687c517bb36b9226d0b086b3a7042e3e198fde [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)) {
529 PyErr_Format(PyExc_IOError,
530 "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)) {
569 PyErr_Format(PyExc_IOError,
570 "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
621 if (PyObject_Size(line) == 0) {
622 Py_DECREF(line);
623 return NULL;
624 }
625
626 return line;
627}
628
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300629/*[clinic input]
630_io._IOBase.readlines
Serhiy Storchaka762bf402017-03-30 09:15:31 +0300631 hint: Py_ssize_t(accept={int, NoneType}) = -1
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300632 /
633
634Return a list of lines from the stream.
635
636hint can be specified to control the number of lines read: no more
637lines will be read if the total size (in bytes/characters) of all
638lines so far exceeds hint.
639[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000640
641static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300642_io__IOBase_readlines_impl(PyObject *self, Py_ssize_t hint)
Serhiy Storchaka762bf402017-03-30 09:15:31 +0300643/*[clinic end generated code: output=2f50421677fa3dea input=9400c786ea9dc416]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000644{
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300645 Py_ssize_t length = 0;
Xiang Zhang026435c2017-04-15 12:47:28 +0800646 PyObject *result, *it = NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000647
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000648 result = PyList_New(0);
649 if (result == NULL)
650 return NULL;
651
652 if (hint <= 0) {
653 /* XXX special-casing this made sense in the Python version in order
654 to remove the bytecode interpretation overhead, but it could
655 probably be removed here. */
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200656 _Py_IDENTIFIER(extend);
Victor Stinner61bdb0d2016-12-09 15:39:28 +0100657 PyObject *ret = _PyObject_CallMethodIdObjArgs(result, &PyId_extend,
658 self, NULL);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200659
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000660 if (ret == NULL) {
Xiang Zhang026435c2017-04-15 12:47:28 +0800661 goto error;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000662 }
663 Py_DECREF(ret);
664 return result;
665 }
666
Xiang Zhang026435c2017-04-15 12:47:28 +0800667 it = PyObject_GetIter(self);
668 if (it == NULL) {
669 goto error;
670 }
671
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000672 while (1) {
Xiang Zhang026435c2017-04-15 12:47:28 +0800673 PyObject *line = PyIter_Next(it);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000674 if (line == NULL) {
675 if (PyErr_Occurred()) {
Xiang Zhang026435c2017-04-15 12:47:28 +0800676 goto error;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000677 }
678 else
679 break; /* StopIteration raised */
680 }
681
682 if (PyList_Append(result, line) < 0) {
683 Py_DECREF(line);
Xiang Zhang026435c2017-04-15 12:47:28 +0800684 goto error;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000685 }
686 length += PyObject_Size(line);
687 Py_DECREF(line);
688
689 if (length > hint)
690 break;
691 }
Xiang Zhang026435c2017-04-15 12:47:28 +0800692
693 Py_DECREF(it);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000694 return result;
Xiang Zhang026435c2017-04-15 12:47:28 +0800695
696 error:
697 Py_XDECREF(it);
698 Py_DECREF(result);
699 return NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000700}
701
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300702/*[clinic input]
703_io._IOBase.writelines
704 lines: object
705 /
706[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000707
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300708static PyObject *
709_io__IOBase_writelines(PyObject *self, PyObject *lines)
710/*[clinic end generated code: output=976eb0a9b60a6628 input=432e729a8450b3cb]*/
711{
712 PyObject *iter, *res;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000713
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000714 if (_PyIOBase_check_closed(self, Py_True) == NULL)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000715 return NULL;
716
717 iter = PyObject_GetIter(lines);
718 if (iter == NULL)
719 return NULL;
720
721 while (1) {
722 PyObject *line = PyIter_Next(iter);
723 if (line == NULL) {
724 if (PyErr_Occurred()) {
725 Py_DECREF(iter);
726 return NULL;
727 }
728 else
729 break; /* Stop Iteration */
730 }
731
Gregory P. Smithb9817b02013-02-01 13:03:39 -0800732 res = NULL;
733 do {
734 res = PyObject_CallMethodObjArgs(self, _PyIO_str_write, line, NULL);
735 } while (res == NULL && _PyIO_trap_eintr());
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000736 Py_DECREF(line);
737 if (res == NULL) {
738 Py_DECREF(iter);
739 return NULL;
740 }
741 Py_DECREF(res);
742 }
743 Py_DECREF(iter);
744 Py_RETURN_NONE;
745}
746
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300747#include "clinic/iobase.c.h"
748
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000749static PyMethodDef iobase_methods[] = {
750 {"seek", iobase_seek, METH_VARARGS, iobase_seek_doc},
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300751 _IO__IOBASE_TELL_METHODDEF
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000752 {"truncate", iobase_truncate, METH_VARARGS, iobase_truncate_doc},
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300753 _IO__IOBASE_FLUSH_METHODDEF
754 _IO__IOBASE_CLOSE_METHODDEF
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000755
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300756 _IO__IOBASE_SEEKABLE_METHODDEF
757 _IO__IOBASE_READABLE_METHODDEF
758 _IO__IOBASE_WRITABLE_METHODDEF
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000759
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000760 {"_checkClosed", _PyIOBase_check_closed, METH_NOARGS},
761 {"_checkSeekable", _PyIOBase_check_seekable, METH_NOARGS},
762 {"_checkReadable", _PyIOBase_check_readable, METH_NOARGS},
763 {"_checkWritable", _PyIOBase_check_writable, METH_NOARGS},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000764
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300765 _IO__IOBASE_FILENO_METHODDEF
766 _IO__IOBASE_ISATTY_METHODDEF
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000767
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000768 {"__enter__", iobase_enter, METH_NOARGS},
769 {"__exit__", iobase_exit, METH_VARARGS},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000770
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300771 _IO__IOBASE_READLINE_METHODDEF
772 _IO__IOBASE_READLINES_METHODDEF
773 _IO__IOBASE_WRITELINES_METHODDEF
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000774
775 {NULL, NULL}
776};
777
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000778static PyGetSetDef iobase_getset[] = {
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500779 {"__dict__", PyObject_GenericGetDict, NULL, NULL},
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000780 {"closed", (getter)iobase_closed_get, NULL, NULL},
Benjamin Peterson1fea3212009-04-19 03:15:20 +0000781 {NULL}
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000782};
783
784
785PyTypeObject PyIOBase_Type = {
786 PyVarObject_HEAD_INIT(NULL, 0)
787 "_io._IOBase", /*tp_name*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000788 sizeof(iobase), /*tp_basicsize*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000789 0, /*tp_itemsize*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000790 (destructor)iobase_dealloc, /*tp_dealloc*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000791 0, /*tp_print*/
792 0, /*tp_getattr*/
793 0, /*tp_setattr*/
794 0, /*tp_compare */
795 0, /*tp_repr*/
796 0, /*tp_as_number*/
797 0, /*tp_as_sequence*/
798 0, /*tp_as_mapping*/
799 0, /*tp_hash */
800 0, /*tp_call*/
801 0, /*tp_str*/
802 0, /*tp_getattro*/
803 0, /*tp_setattro*/
804 0, /*tp_as_buffer*/
805 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
Antoine Pitrou796564c2013-07-30 19:59:21 +0200806 | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /*tp_flags*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000807 iobase_doc, /* tp_doc */
808 (traverseproc)iobase_traverse, /* tp_traverse */
809 (inquiry)iobase_clear, /* tp_clear */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000810 0, /* tp_richcompare */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000811 offsetof(iobase, weakreflist), /* tp_weaklistoffset */
812 iobase_iter, /* tp_iter */
813 iobase_iternext, /* tp_iternext */
814 iobase_methods, /* tp_methods */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000815 0, /* tp_members */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000816 iobase_getset, /* tp_getset */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000817 0, /* tp_base */
818 0, /* tp_dict */
819 0, /* tp_descr_get */
820 0, /* tp_descr_set */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000821 offsetof(iobase, dict), /* tp_dictoffset */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000822 0, /* tp_init */
823 0, /* tp_alloc */
824 PyType_GenericNew, /* tp_new */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200825 0, /* tp_free */
826 0, /* tp_is_gc */
827 0, /* tp_bases */
828 0, /* tp_mro */
829 0, /* tp_cache */
830 0, /* tp_subclasses */
831 0, /* tp_weaklist */
832 0, /* tp_del */
833 0, /* tp_version_tag */
Victor Stinner928bff02016-03-19 10:36:36 +0100834 iobase_finalize, /* tp_finalize */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000835};
836
837
838/*
839 * RawIOBase class, Inherits from IOBase.
840 */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000841PyDoc_STRVAR(rawiobase_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000842 "Base class for raw binary I/O.");
843
844/*
845 * The read() method is implemented by calling readinto(); derived classes
846 * that want to support read() only need to implement readinto() as a
847 * primitive operation. In general, readinto() can be more efficient than
848 * read().
849 *
850 * (It would be tempting to also provide an implementation of readinto() in
851 * terms of read(), in case the latter is a more suitable primitive operation,
852 * but that would lead to nasty recursion in case a subclass doesn't implement
853 * either.)
854*/
855
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300856/*[clinic input]
857_io._RawIOBase.read
858 size as n: Py_ssize_t = -1
859 /
860[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000861
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300862static PyObject *
863_io__RawIOBase_read_impl(PyObject *self, Py_ssize_t n)
864/*[clinic end generated code: output=6cdeb731e3c9f13c input=b6d0dcf6417d1374]*/
865{
866 PyObject *b, *res;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000867
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200868 if (n < 0) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200869 _Py_IDENTIFIER(readall);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200870
871 return _PyObject_CallMethodId(self, &PyId_readall, NULL);
872 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000873
874 /* TODO: allocate a bytes object directly instead and manually construct
875 a writable memoryview pointing to it. */
876 b = PyByteArray_FromStringAndSize(NULL, n);
877 if (b == NULL)
878 return NULL;
879
880 res = PyObject_CallMethodObjArgs(self, _PyIO_str_readinto, b, NULL);
Antoine Pitrou328ec742010-09-14 18:37:24 +0000881 if (res == NULL || res == Py_None) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000882 Py_DECREF(b);
Antoine Pitrou328ec742010-09-14 18:37:24 +0000883 return res;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000884 }
885
886 n = PyNumber_AsSsize_t(res, PyExc_ValueError);
887 Py_DECREF(res);
888 if (n == -1 && PyErr_Occurred()) {
889 Py_DECREF(b);
890 return NULL;
891 }
892
893 res = PyBytes_FromStringAndSize(PyByteArray_AsString(b), n);
894 Py_DECREF(b);
895 return res;
896}
897
898
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300899/*[clinic input]
900_io._RawIOBase.readall
901
902Read until EOF, using multiple read() call.
903[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000904
905static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300906_io__RawIOBase_readall_impl(PyObject *self)
907/*[clinic end generated code: output=1987b9ce929425a0 input=688874141213622a]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000908{
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000909 int r;
910 PyObject *chunks = PyList_New(0);
911 PyObject *result;
Victor Stinnercc024d12013-10-29 02:23:46 +0100912
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000913 if (chunks == NULL)
914 return NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000915
916 while (1) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200917 PyObject *data = _PyObject_CallMethodId(self, &PyId_read,
918 "i", DEFAULT_BUFFER_SIZE);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000919 if (!data) {
Gregory P. Smith51359922012-06-23 23:55:39 -0700920 /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
921 when EINTR occurs so we needn't do it ourselves. */
922 if (_PyIO_trap_eintr()) {
923 continue;
924 }
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000925 Py_DECREF(chunks);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000926 return NULL;
927 }
Victor Stinnera80987f2011-05-25 22:47:16 +0200928 if (data == Py_None) {
929 if (PyList_GET_SIZE(chunks) == 0) {
930 Py_DECREF(chunks);
931 return data;
932 }
933 Py_DECREF(data);
934 break;
935 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000936 if (!PyBytes_Check(data)) {
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000937 Py_DECREF(chunks);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000938 Py_DECREF(data);
939 PyErr_SetString(PyExc_TypeError, "read() should return bytes");
940 return NULL;
941 }
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000942 if (PyBytes_GET_SIZE(data) == 0) {
943 /* EOF */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000944 Py_DECREF(data);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000945 break;
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000946 }
947 r = PyList_Append(chunks, data);
948 Py_DECREF(data);
949 if (r < 0) {
950 Py_DECREF(chunks);
951 return NULL;
952 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000953 }
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000954 result = _PyBytes_Join(_PyIO_empty_bytes, chunks);
955 Py_DECREF(chunks);
956 return result;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000957}
958
Antoine Pitrou45d61562015-05-20 21:50:59 +0200959static PyObject *
960rawiobase_readinto(PyObject *self, PyObject *args)
961{
962 PyErr_SetNone(PyExc_NotImplementedError);
963 return NULL;
964}
965
966static PyObject *
967rawiobase_write(PyObject *self, PyObject *args)
968{
969 PyErr_SetNone(PyExc_NotImplementedError);
970 return NULL;
971}
972
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000973static PyMethodDef rawiobase_methods[] = {
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300974 _IO__RAWIOBASE_READ_METHODDEF
975 _IO__RAWIOBASE_READALL_METHODDEF
Antoine Pitrou45d61562015-05-20 21:50:59 +0200976 {"readinto", rawiobase_readinto, METH_VARARGS},
977 {"write", rawiobase_write, METH_VARARGS},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000978 {NULL, NULL}
979};
980
981PyTypeObject PyRawIOBase_Type = {
982 PyVarObject_HEAD_INIT(NULL, 0)
983 "_io._RawIOBase", /*tp_name*/
984 0, /*tp_basicsize*/
985 0, /*tp_itemsize*/
986 0, /*tp_dealloc*/
987 0, /*tp_print*/
988 0, /*tp_getattr*/
989 0, /*tp_setattr*/
990 0, /*tp_compare */
991 0, /*tp_repr*/
992 0, /*tp_as_number*/
993 0, /*tp_as_sequence*/
994 0, /*tp_as_mapping*/
995 0, /*tp_hash */
996 0, /*tp_call*/
997 0, /*tp_str*/
998 0, /*tp_getattro*/
999 0, /*tp_setattro*/
1000 0, /*tp_as_buffer*/
Antoine Pitrou796564c2013-07-30 19:59:21 +02001001 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_FINALIZE, /*tp_flags*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001002 rawiobase_doc, /* tp_doc */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001003 0, /* tp_traverse */
1004 0, /* tp_clear */
1005 0, /* tp_richcompare */
1006 0, /* tp_weaklistoffset */
1007 0, /* tp_iter */
1008 0, /* tp_iternext */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001009 rawiobase_methods, /* tp_methods */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001010 0, /* tp_members */
1011 0, /* tp_getset */
1012 &PyIOBase_Type, /* tp_base */
1013 0, /* tp_dict */
1014 0, /* tp_descr_get */
1015 0, /* tp_descr_set */
1016 0, /* tp_dictoffset */
1017 0, /* tp_init */
1018 0, /* tp_alloc */
1019 0, /* tp_new */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001020 0, /* tp_free */
1021 0, /* tp_is_gc */
1022 0, /* tp_bases */
1023 0, /* tp_mro */
1024 0, /* tp_cache */
1025 0, /* tp_subclasses */
1026 0, /* tp_weaklist */
1027 0, /* tp_del */
1028 0, /* tp_version_tag */
1029 0, /* tp_finalize */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001030};