blob: f7986d7e52f957c0c2870d860b67f6a9b97347be [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
23/*[python input]
24class io_ssize_t_converter(CConverter):
25 type = 'Py_ssize_t'
26 converter = '_PyIO_ConvertSsize_t'
27[python start generated code]*/
28/*[python end generated code: output=da39a3ee5e6b4b0d input=d0a811d3cbfd1b33]*/
29
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000030/*
31 * IOBase class, an abstract class
32 */
33
34typedef struct {
35 PyObject_HEAD
Victor Stinnercc024d12013-10-29 02:23:46 +010036
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000037 PyObject *dict;
38 PyObject *weakreflist;
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000039} iobase;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000040
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000041PyDoc_STRVAR(iobase_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000042 "The abstract base class for all I/O classes, acting on streams of\n"
43 "bytes. There is no public constructor.\n"
44 "\n"
45 "This class provides dummy implementations for many methods that\n"
46 "derived classes can override selectively; the default implementations\n"
47 "represent a file that cannot be read, written or seeked.\n"
48 "\n"
49 "Even though IOBase does not declare read, readinto, or write because\n"
50 "their signatures will vary, implementations and clients should\n"
51 "consider those methods part of the interface. Also, implementations\n"
Amaury Forgeot d'Arc616453c2010-09-06 22:31:52 +000052 "may raise UnsupportedOperation when operations they do not support are\n"
53 "called.\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000054 "\n"
55 "The basic type used for binary data read from or written to a file is\n"
Martin Panter6bb91f32016-05-28 00:41:57 +000056 "bytes. Other bytes-like objects are accepted as method arguments too.\n"
57 "In some cases (such as readinto), a writable object is required. Text\n"
58 "I/O classes work with str data.\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000059 "\n"
Andrew Kuchling76466202014-04-15 21:11:36 -040060 "Note that calling any method (except additional calls to close(),\n"
61 "which are ignored) on a closed stream should raise a ValueError.\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000062 "\n"
63 "IOBase (and its subclasses) support the iterator protocol, meaning\n"
64 "that an IOBase object can be iterated over yielding the lines in a\n"
65 "stream.\n"
66 "\n"
67 "IOBase also supports the :keyword:`with` statement. In this example,\n"
Ezio Melotti13925002011-03-16 11:05:33 +020068 "fp is closed after the suite of the with statement is complete:\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000069 "\n"
70 "with open('spam.txt', 'r') as fp:\n"
71 " fp.write('Spam and eggs!')\n");
72
73/* Use this macro whenever you want to check the internal `closed` status
74 of the IOBase object rather than the virtual `closed` attribute as returned
75 by whatever subclass. */
76
Martin v. Löwis767046a2011-10-14 15:35:36 +020077_Py_IDENTIFIER(__IOBase_closed);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000078#define IS_CLOSED(self) \
Martin v. Löwis767046a2011-10-14 15:35:36 +020079 _PyObject_HasAttrId(self, &PyId___IOBase_closed)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000080
Victor Stinner3f36a572013-11-12 21:39:02 +010081_Py_IDENTIFIER(read);
82
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000083/* Internal methods */
84static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000085iobase_unsupported(const char *message)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000086{
Antoine Pitrou712cb732013-12-21 15:51:54 +010087 _PyIO_State *state = IO_STATE();
88 if (state != NULL)
89 PyErr_SetString(state->unsupported_operation, message);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000090 return NULL;
91}
92
Raymond Hettinger15f44ab2016-08-30 10:47:49 -070093/* Positioning */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000094
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000095PyDoc_STRVAR(iobase_seek_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000096 "Change stream position.\n"
97 "\n"
Terry Jan Reedy0158af32013-03-11 17:42:46 -040098 "Change the stream position to the given byte offset. The offset is\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000099 "interpreted relative to the position indicated by whence. Values\n"
100 "for whence are:\n"
101 "\n"
102 "* 0 -- start of stream (the default); offset should be zero or positive\n"
103 "* 1 -- current stream position; offset may be negative\n"
104 "* 2 -- end of stream; offset is usually negative\n"
105 "\n"
106 "Return the new absolute position.");
107
108static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000109iobase_seek(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000110{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000111 return iobase_unsupported("seek");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000112}
113
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300114/*[clinic input]
115_io._IOBase.tell
116
117Return current stream position.
118[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000119
120static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300121_io__IOBase_tell_impl(PyObject *self)
122/*[clinic end generated code: output=89a1c0807935abe2 input=04e615fec128801f]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000123{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200124 _Py_IDENTIFIER(seek);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200125
126 return _PyObject_CallMethodId(self, &PyId_seek, "ii", 0, 1);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000127}
128
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000129PyDoc_STRVAR(iobase_truncate_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000130 "Truncate file to size bytes.\n"
131 "\n"
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000132 "File pointer is left unchanged. Size defaults to the current IO\n"
133 "position as reported by tell(). Returns the new size.");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000134
135static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000136iobase_truncate(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000137{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000138 return iobase_unsupported("truncate");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000139}
140
141/* Flush and close methods */
142
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300143/*[clinic input]
144_io._IOBase.flush
145
146Flush write buffers, if applicable.
147
148This is not implemented for read-only and non-blocking streams.
149[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000150
151static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300152_io__IOBase_flush_impl(PyObject *self)
153/*[clinic end generated code: output=7cef4b4d54656a3b input=773be121abe270aa]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000154{
155 /* XXX Should this return the number of bytes written??? */
156 if (IS_CLOSED(self)) {
157 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file.");
158 return NULL;
159 }
160 Py_RETURN_NONE;
161}
162
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000163static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000164iobase_closed(PyObject *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000165{
166 PyObject *res;
167 int closed;
168 /* This gets the derived attribute, which is *not* __IOBase_closed
169 in most cases! */
170 res = PyObject_GetAttr(self, _PyIO_str_closed);
171 if (res == NULL)
172 return 0;
173 closed = PyObject_IsTrue(res);
174 Py_DECREF(res);
175 return closed;
176}
177
178static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000179iobase_closed_get(PyObject *self, void *context)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000180{
181 return PyBool_FromLong(IS_CLOSED(self));
182}
183
184PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000185_PyIOBase_check_closed(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000186{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000187 if (iobase_closed(self)) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000188 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file.");
189 return NULL;
190 }
191 if (args == Py_True)
192 return Py_None;
193 else
194 Py_RETURN_NONE;
195}
196
197/* XXX: IOBase thinks it has to maintain its own internal state in
198 `__IOBase_closed` and call flush() by itself, but it is redundant with
199 whatever behaviour a non-trivial derived class will implement. */
200
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300201/*[clinic input]
202_io._IOBase.close
203
204Flush and close the IO object.
205
206This method has no effect if the file is already closed.
207[clinic start generated code]*/
208
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000209static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300210_io__IOBase_close_impl(PyObject *self)
211/*[clinic end generated code: output=63c6a6f57d783d6d input=f4494d5c31dbc6b7]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000212{
213 PyObject *res;
214
215 if (IS_CLOSED(self))
216 Py_RETURN_NONE;
217
218 res = PyObject_CallMethodObjArgs(self, _PyIO_str_flush, NULL);
Victor Stinneraa5bbfa2013-11-08 00:29:41 +0100219
220 if (_PyObject_SetAttrId(self, &PyId___IOBase_closed, Py_True) < 0) {
221 Py_XDECREF(res);
Antoine Pitrou6be88762010-05-03 16:48:20 +0000222 return NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000223 }
Victor Stinneraa5bbfa2013-11-08 00:29:41 +0100224
225 if (res == NULL)
226 return NULL;
227
228 Py_DECREF(res);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000229 Py_RETURN_NONE;
230}
231
232/* Finalization and garbage collection support */
233
Antoine Pitrou796564c2013-07-30 19:59:21 +0200234static void
235iobase_finalize(PyObject *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000236{
237 PyObject *res;
Antoine Pitrou796564c2013-07-30 19:59:21 +0200238 PyObject *error_type, *error_value, *error_traceback;
239 int closed;
240 _Py_IDENTIFIER(_finalizing);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000241
Antoine Pitrou796564c2013-07-30 19:59:21 +0200242 /* Save the current exception, if any. */
243 PyErr_Fetch(&error_type, &error_value, &error_traceback);
244
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000245 /* If `closed` doesn't exist or can't be evaluated as bool, then the
246 object is probably in an unusable state, so ignore. */
247 res = PyObject_GetAttr(self, _PyIO_str_closed);
Christian Heimes72f455e2013-07-31 01:33:50 +0200248 if (res == NULL) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000249 PyErr_Clear();
Christian Heimes72f455e2013-07-31 01:33:50 +0200250 closed = -1;
251 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000252 else {
253 closed = PyObject_IsTrue(res);
254 Py_DECREF(res);
255 if (closed == -1)
256 PyErr_Clear();
257 }
258 if (closed == 0) {
Antoine Pitrou796564c2013-07-30 19:59:21 +0200259 /* Signal close() that it was called as part of the object
260 finalization process. */
261 if (_PyObject_SetAttrId(self, &PyId__finalizing, Py_True))
262 PyErr_Clear();
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000263 res = PyObject_CallMethodObjArgs((PyObject *) self, _PyIO_str_close,
264 NULL);
265 /* Silencing I/O errors is bad, but printing spurious tracebacks is
266 equally as bad, and potentially more frequent (because of
267 shutdown issues). */
268 if (res == NULL)
269 PyErr_Clear();
270 else
271 Py_DECREF(res);
272 }
Antoine Pitrou796564c2013-07-30 19:59:21 +0200273
274 /* Restore the saved exception. */
275 PyErr_Restore(error_type, error_value, error_traceback);
276}
277
278int
279_PyIOBase_finalize(PyObject *self)
280{
281 int is_zombie;
282
283 /* If _PyIOBase_finalize() is called from a destructor, we need to
284 resurrect the object as calling close() can invoke arbitrary code. */
285 is_zombie = (Py_REFCNT(self) == 0);
286 if (is_zombie)
287 return PyObject_CallFinalizerFromDealloc(self);
288 else {
289 PyObject_CallFinalizer(self);
290 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000291 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000292}
293
294static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000295iobase_traverse(iobase *self, visitproc visit, void *arg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000296{
297 Py_VISIT(self->dict);
298 return 0;
299}
300
301static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000302iobase_clear(iobase *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000303{
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000304 Py_CLEAR(self->dict);
305 return 0;
306}
307
308/* Destructor */
309
310static void
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000311iobase_dealloc(iobase *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000312{
313 /* NOTE: since IOBaseObject has its own dict, Python-defined attributes
314 are still available here for close() to use.
315 However, if the derived class declares a __slots__, those slots are
316 already gone.
317 */
318 if (_PyIOBase_finalize((PyObject *) self) < 0) {
319 /* When called from a heap type's dealloc, the type will be
320 decref'ed on return (see e.g. subtype_dealloc in typeobject.c). */
321 if (PyType_HasFeature(Py_TYPE(self), Py_TPFLAGS_HEAPTYPE))
322 Py_INCREF(Py_TYPE(self));
323 return;
324 }
325 _PyObject_GC_UNTRACK(self);
326 if (self->weakreflist != NULL)
327 PyObject_ClearWeakRefs((PyObject *) self);
328 Py_CLEAR(self->dict);
329 Py_TYPE(self)->tp_free((PyObject *) self);
330}
331
332/* Inquiry methods */
333
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300334/*[clinic input]
335_io._IOBase.seekable
336
337Return whether object supports random access.
338
Martin Panter754aab22016-03-31 07:21:56 +0000339If False, seek(), tell() and truncate() will raise OSError.
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300340This method may need to do a test seek().
341[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000342
343static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300344_io__IOBase_seekable_impl(PyObject *self)
Martin Panter754aab22016-03-31 07:21:56 +0000345/*[clinic end generated code: output=4c24c67f5f32a43d input=b976622f7fdf3063]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000346{
347 Py_RETURN_FALSE;
348}
349
350PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000351_PyIOBase_check_seekable(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000352{
353 PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_seekable, NULL);
354 if (res == NULL)
355 return NULL;
356 if (res != Py_True) {
357 Py_CLEAR(res);
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000358 iobase_unsupported("File or stream is not seekable.");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000359 return NULL;
360 }
361 if (args == Py_True) {
362 Py_DECREF(res);
363 }
364 return res;
365}
366
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300367/*[clinic input]
368_io._IOBase.readable
369
370Return whether object was opened for reading.
371
Martin Panter754aab22016-03-31 07:21:56 +0000372If False, read() will raise OSError.
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300373[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000374
375static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300376_io__IOBase_readable_impl(PyObject *self)
Martin Panter754aab22016-03-31 07:21:56 +0000377/*[clinic end generated code: output=e48089250686388b input=285b3b866a0ec35f]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000378{
379 Py_RETURN_FALSE;
380}
381
382/* May be called with any object */
383PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000384_PyIOBase_check_readable(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000385{
386 PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_readable, NULL);
387 if (res == NULL)
388 return NULL;
389 if (res != Py_True) {
390 Py_CLEAR(res);
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000391 iobase_unsupported("File or stream is not readable.");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000392 return NULL;
393 }
394 if (args == Py_True) {
395 Py_DECREF(res);
396 }
397 return res;
398}
399
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300400/*[clinic input]
401_io._IOBase.writable
402
403Return whether object was opened for writing.
404
Martin Panter754aab22016-03-31 07:21:56 +0000405If False, write() will raise OSError.
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300406[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000407
408static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300409_io__IOBase_writable_impl(PyObject *self)
Martin Panter754aab22016-03-31 07:21:56 +0000410/*[clinic end generated code: output=406001d0985be14f input=9dcac18a013a05b5]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000411{
412 Py_RETURN_FALSE;
413}
414
415/* May be called with any object */
416PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000417_PyIOBase_check_writable(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000418{
419 PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_writable, NULL);
420 if (res == NULL)
421 return NULL;
422 if (res != Py_True) {
423 Py_CLEAR(res);
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000424 iobase_unsupported("File or stream is not writable.");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000425 return NULL;
426 }
427 if (args == Py_True) {
428 Py_DECREF(res);
429 }
430 return res;
431}
432
433/* Context manager */
434
435static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000436iobase_enter(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000437{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000438 if (_PyIOBase_check_closed(self, Py_True) == NULL)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000439 return NULL;
440
441 Py_INCREF(self);
442 return self;
443}
444
445static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000446iobase_exit(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000447{
448 return PyObject_CallMethodObjArgs(self, _PyIO_str_close, NULL);
449}
450
451/* Lower-level APIs */
452
453/* XXX Should these be present even if unimplemented? */
454
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300455/*[clinic input]
456_io._IOBase.fileno
457
458Returns underlying file descriptor if one exists.
459
Martin Panter754aab22016-03-31 07:21:56 +0000460OSError is raised if the IO object does not use a file descriptor.
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300461[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000462
463static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300464_io__IOBase_fileno_impl(PyObject *self)
Martin Panter754aab22016-03-31 07:21:56 +0000465/*[clinic end generated code: output=7cc0973f0f5f3b73 input=4e37028947dc1cc8]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000466{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000467 return iobase_unsupported("fileno");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000468}
469
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300470/*[clinic input]
471_io._IOBase.isatty
472
473Return whether this is an 'interactive' stream.
474
475Return False if it can't be determined.
476[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000477
478static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300479_io__IOBase_isatty_impl(PyObject *self)
480/*[clinic end generated code: output=60cab77cede41cdd input=9ef76530d368458b]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000481{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000482 if (_PyIOBase_check_closed(self, Py_True) == NULL)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000483 return NULL;
484 Py_RETURN_FALSE;
485}
486
487/* Readline(s) and writelines */
488
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300489/*[clinic input]
490_io._IOBase.readline
491 size as limit: io_ssize_t = -1
492 /
493
494Read and return a line from the stream.
495
496If size is specified, at most size bytes will be read.
497
498The line terminator is always b'\n' for binary files; for text
499files, the newlines argument to open can be used to select the line
500terminator(s) recognized.
501[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000502
503static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300504_io__IOBase_readline_impl(PyObject *self, Py_ssize_t limit)
505/*[clinic end generated code: output=4479f79b58187840 input=df4cc8884f553cab]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000506{
507 /* For backwards compatibility, a (slowish) readline(). */
508
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000509 int has_peek = 0;
510 PyObject *buffer, *result;
511 Py_ssize_t old_size = -1;
Martin v. Löwis767046a2011-10-14 15:35:36 +0200512 _Py_IDENTIFIER(peek);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000513
Martin v. Löwis767046a2011-10-14 15:35:36 +0200514 if (_PyObject_HasAttrId(self, &PyId_peek))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000515 has_peek = 1;
516
517 buffer = PyByteArray_FromStringAndSize(NULL, 0);
518 if (buffer == NULL)
519 return NULL;
520
521 while (limit < 0 || Py_SIZE(buffer) < limit) {
522 Py_ssize_t nreadahead = 1;
523 PyObject *b;
524
525 if (has_peek) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200526 PyObject *readahead = _PyObject_CallMethodId(self, &PyId_peek, "i", 1);
Gregory P. Smith51359922012-06-23 23:55:39 -0700527 if (readahead == NULL) {
528 /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
529 when EINTR occurs so we needn't do it ourselves. */
530 if (_PyIO_trap_eintr()) {
531 continue;
532 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000533 goto fail;
Gregory P. Smith51359922012-06-23 23:55:39 -0700534 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000535 if (!PyBytes_Check(readahead)) {
536 PyErr_Format(PyExc_IOError,
537 "peek() should have returned a bytes object, "
538 "not '%.200s'", Py_TYPE(readahead)->tp_name);
539 Py_DECREF(readahead);
540 goto fail;
541 }
542 if (PyBytes_GET_SIZE(readahead) > 0) {
543 Py_ssize_t n = 0;
544 const char *buf = PyBytes_AS_STRING(readahead);
545 if (limit >= 0) {
546 do {
547 if (n >= PyBytes_GET_SIZE(readahead) || n >= limit)
548 break;
549 if (buf[n++] == '\n')
550 break;
551 } while (1);
552 }
553 else {
554 do {
555 if (n >= PyBytes_GET_SIZE(readahead))
556 break;
557 if (buf[n++] == '\n')
558 break;
559 } while (1);
560 }
561 nreadahead = n;
562 }
563 Py_DECREF(readahead);
564 }
565
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200566 b = _PyObject_CallMethodId(self, &PyId_read, "n", nreadahead);
Gregory P. Smith51359922012-06-23 23:55:39 -0700567 if (b == NULL) {
568 /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
569 when EINTR occurs so we needn't do it ourselves. */
570 if (_PyIO_trap_eintr()) {
571 continue;
572 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000573 goto fail;
Gregory P. Smith51359922012-06-23 23:55:39 -0700574 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000575 if (!PyBytes_Check(b)) {
576 PyErr_Format(PyExc_IOError,
577 "read() should have returned a bytes object, "
578 "not '%.200s'", Py_TYPE(b)->tp_name);
579 Py_DECREF(b);
580 goto fail;
581 }
582 if (PyBytes_GET_SIZE(b) == 0) {
583 Py_DECREF(b);
584 break;
585 }
586
587 old_size = PyByteArray_GET_SIZE(buffer);
Victor Stinnercc024d12013-10-29 02:23:46 +0100588 if (PyByteArray_Resize(buffer, old_size + PyBytes_GET_SIZE(b)) < 0) {
589 Py_DECREF(b);
590 goto fail;
591 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000592 memcpy(PyByteArray_AS_STRING(buffer) + old_size,
593 PyBytes_AS_STRING(b), PyBytes_GET_SIZE(b));
594
595 Py_DECREF(b);
596
597 if (PyByteArray_AS_STRING(buffer)[PyByteArray_GET_SIZE(buffer) - 1] == '\n')
598 break;
599 }
600
601 result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(buffer),
602 PyByteArray_GET_SIZE(buffer));
603 Py_DECREF(buffer);
604 return result;
605 fail:
606 Py_DECREF(buffer);
607 return NULL;
608}
609
610static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000611iobase_iter(PyObject *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000612{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000613 if (_PyIOBase_check_closed(self, Py_True) == NULL)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000614 return NULL;
615
616 Py_INCREF(self);
617 return self;
618}
619
620static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000621iobase_iternext(PyObject *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000622{
623 PyObject *line = PyObject_CallMethodObjArgs(self, _PyIO_str_readline, NULL);
624
625 if (line == NULL)
626 return NULL;
627
628 if (PyObject_Size(line) == 0) {
629 Py_DECREF(line);
630 return NULL;
631 }
632
633 return line;
634}
635
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300636/*[clinic input]
637_io._IOBase.readlines
638 hint: io_ssize_t = -1
639 /
640
641Return a list of lines from the stream.
642
643hint can be specified to control the number of lines read: no more
644lines will be read if the total size (in bytes/characters) of all
645lines so far exceeds hint.
646[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000647
648static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300649_io__IOBase_readlines_impl(PyObject *self, Py_ssize_t hint)
650/*[clinic end generated code: output=2f50421677fa3dea input=1961c4a95e96e661]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000651{
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300652 Py_ssize_t length = 0;
Xiang Zhangd5fa5f32017-04-15 13:25:15 +0800653 PyObject *result, *it = NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000654
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000655 result = PyList_New(0);
656 if (result == NULL)
657 return NULL;
658
659 if (hint <= 0) {
660 /* XXX special-casing this made sense in the Python version in order
661 to remove the bytecode interpretation overhead, but it could
662 probably be removed here. */
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200663 _Py_IDENTIFIER(extend);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200664 PyObject *ret = _PyObject_CallMethodId(result, &PyId_extend, "O", self);
665
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000666 if (ret == NULL) {
Xiang Zhangd5fa5f32017-04-15 13:25:15 +0800667 goto error;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000668 }
669 Py_DECREF(ret);
670 return result;
671 }
672
Xiang Zhangd5fa5f32017-04-15 13:25:15 +0800673 it = PyObject_GetIter(self);
674 if (it == NULL) {
675 goto error;
676 }
677
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000678 while (1) {
Xiang Zhangd5fa5f32017-04-15 13:25:15 +0800679 PyObject *line = PyIter_Next(it);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000680 if (line == NULL) {
681 if (PyErr_Occurred()) {
Xiang Zhangd5fa5f32017-04-15 13:25:15 +0800682 goto error;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000683 }
684 else
685 break; /* StopIteration raised */
686 }
687
688 if (PyList_Append(result, line) < 0) {
689 Py_DECREF(line);
Xiang Zhangd5fa5f32017-04-15 13:25:15 +0800690 goto error;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000691 }
692 length += PyObject_Size(line);
693 Py_DECREF(line);
694
695 if (length > hint)
696 break;
697 }
Xiang Zhangd5fa5f32017-04-15 13:25:15 +0800698
699 Py_DECREF(it);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000700 return result;
Xiang Zhangd5fa5f32017-04-15 13:25:15 +0800701
702 error:
703 Py_XDECREF(it);
704 Py_DECREF(result);
705 return NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000706}
707
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300708/*[clinic input]
709_io._IOBase.writelines
710 lines: object
711 /
712[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000713
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300714static PyObject *
715_io__IOBase_writelines(PyObject *self, PyObject *lines)
716/*[clinic end generated code: output=976eb0a9b60a6628 input=432e729a8450b3cb]*/
717{
718 PyObject *iter, *res;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000719
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000720 if (_PyIOBase_check_closed(self, Py_True) == NULL)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000721 return NULL;
722
723 iter = PyObject_GetIter(lines);
724 if (iter == NULL)
725 return NULL;
726
727 while (1) {
728 PyObject *line = PyIter_Next(iter);
729 if (line == NULL) {
730 if (PyErr_Occurred()) {
731 Py_DECREF(iter);
732 return NULL;
733 }
734 else
735 break; /* Stop Iteration */
736 }
737
Gregory P. Smithb9817b02013-02-01 13:03:39 -0800738 res = NULL;
739 do {
740 res = PyObject_CallMethodObjArgs(self, _PyIO_str_write, line, NULL);
741 } while (res == NULL && _PyIO_trap_eintr());
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000742 Py_DECREF(line);
743 if (res == NULL) {
744 Py_DECREF(iter);
745 return NULL;
746 }
747 Py_DECREF(res);
748 }
749 Py_DECREF(iter);
750 Py_RETURN_NONE;
751}
752
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300753#include "clinic/iobase.c.h"
754
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000755static PyMethodDef iobase_methods[] = {
756 {"seek", iobase_seek, METH_VARARGS, iobase_seek_doc},
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300757 _IO__IOBASE_TELL_METHODDEF
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000758 {"truncate", iobase_truncate, METH_VARARGS, iobase_truncate_doc},
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300759 _IO__IOBASE_FLUSH_METHODDEF
760 _IO__IOBASE_CLOSE_METHODDEF
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000761
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300762 _IO__IOBASE_SEEKABLE_METHODDEF
763 _IO__IOBASE_READABLE_METHODDEF
764 _IO__IOBASE_WRITABLE_METHODDEF
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000765
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000766 {"_checkClosed", _PyIOBase_check_closed, METH_NOARGS},
767 {"_checkSeekable", _PyIOBase_check_seekable, METH_NOARGS},
768 {"_checkReadable", _PyIOBase_check_readable, METH_NOARGS},
769 {"_checkWritable", _PyIOBase_check_writable, METH_NOARGS},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000770
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300771 _IO__IOBASE_FILENO_METHODDEF
772 _IO__IOBASE_ISATTY_METHODDEF
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000773
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000774 {"__enter__", iobase_enter, METH_NOARGS},
775 {"__exit__", iobase_exit, METH_VARARGS},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000776
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300777 _IO__IOBASE_READLINE_METHODDEF
778 _IO__IOBASE_READLINES_METHODDEF
779 _IO__IOBASE_WRITELINES_METHODDEF
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000780
781 {NULL, NULL}
782};
783
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000784static PyGetSetDef iobase_getset[] = {
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500785 {"__dict__", PyObject_GenericGetDict, NULL, NULL},
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000786 {"closed", (getter)iobase_closed_get, NULL, NULL},
Benjamin Peterson1fea3212009-04-19 03:15:20 +0000787 {NULL}
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000788};
789
790
791PyTypeObject PyIOBase_Type = {
792 PyVarObject_HEAD_INIT(NULL, 0)
793 "_io._IOBase", /*tp_name*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000794 sizeof(iobase), /*tp_basicsize*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000795 0, /*tp_itemsize*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000796 (destructor)iobase_dealloc, /*tp_dealloc*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000797 0, /*tp_print*/
798 0, /*tp_getattr*/
799 0, /*tp_setattr*/
800 0, /*tp_compare */
801 0, /*tp_repr*/
802 0, /*tp_as_number*/
803 0, /*tp_as_sequence*/
804 0, /*tp_as_mapping*/
805 0, /*tp_hash */
806 0, /*tp_call*/
807 0, /*tp_str*/
808 0, /*tp_getattro*/
809 0, /*tp_setattro*/
810 0, /*tp_as_buffer*/
811 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
Antoine Pitrou796564c2013-07-30 19:59:21 +0200812 | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /*tp_flags*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000813 iobase_doc, /* tp_doc */
814 (traverseproc)iobase_traverse, /* tp_traverse */
815 (inquiry)iobase_clear, /* tp_clear */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000816 0, /* tp_richcompare */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000817 offsetof(iobase, weakreflist), /* tp_weaklistoffset */
818 iobase_iter, /* tp_iter */
819 iobase_iternext, /* tp_iternext */
820 iobase_methods, /* tp_methods */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000821 0, /* tp_members */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000822 iobase_getset, /* tp_getset */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000823 0, /* tp_base */
824 0, /* tp_dict */
825 0, /* tp_descr_get */
826 0, /* tp_descr_set */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000827 offsetof(iobase, dict), /* tp_dictoffset */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000828 0, /* tp_init */
829 0, /* tp_alloc */
830 PyType_GenericNew, /* tp_new */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200831 0, /* tp_free */
832 0, /* tp_is_gc */
833 0, /* tp_bases */
834 0, /* tp_mro */
835 0, /* tp_cache */
836 0, /* tp_subclasses */
837 0, /* tp_weaklist */
838 0, /* tp_del */
839 0, /* tp_version_tag */
Victor Stinner928bff02016-03-19 10:36:36 +0100840 iobase_finalize, /* tp_finalize */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000841};
842
843
844/*
845 * RawIOBase class, Inherits from IOBase.
846 */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000847PyDoc_STRVAR(rawiobase_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000848 "Base class for raw binary I/O.");
849
850/*
851 * The read() method is implemented by calling readinto(); derived classes
852 * that want to support read() only need to implement readinto() as a
853 * primitive operation. In general, readinto() can be more efficient than
854 * read().
855 *
856 * (It would be tempting to also provide an implementation of readinto() in
857 * terms of read(), in case the latter is a more suitable primitive operation,
858 * but that would lead to nasty recursion in case a subclass doesn't implement
859 * either.)
860*/
861
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300862/*[clinic input]
863_io._RawIOBase.read
864 size as n: Py_ssize_t = -1
865 /
866[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000867
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300868static PyObject *
869_io__RawIOBase_read_impl(PyObject *self, Py_ssize_t n)
870/*[clinic end generated code: output=6cdeb731e3c9f13c input=b6d0dcf6417d1374]*/
871{
872 PyObject *b, *res;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000873
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200874 if (n < 0) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200875 _Py_IDENTIFIER(readall);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200876
877 return _PyObject_CallMethodId(self, &PyId_readall, NULL);
878 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000879
880 /* TODO: allocate a bytes object directly instead and manually construct
881 a writable memoryview pointing to it. */
882 b = PyByteArray_FromStringAndSize(NULL, n);
883 if (b == NULL)
884 return NULL;
885
886 res = PyObject_CallMethodObjArgs(self, _PyIO_str_readinto, b, NULL);
Antoine Pitrou328ec742010-09-14 18:37:24 +0000887 if (res == NULL || res == Py_None) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000888 Py_DECREF(b);
Antoine Pitrou328ec742010-09-14 18:37:24 +0000889 return res;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000890 }
891
892 n = PyNumber_AsSsize_t(res, PyExc_ValueError);
893 Py_DECREF(res);
894 if (n == -1 && PyErr_Occurred()) {
895 Py_DECREF(b);
896 return NULL;
897 }
898
899 res = PyBytes_FromStringAndSize(PyByteArray_AsString(b), n);
900 Py_DECREF(b);
901 return res;
902}
903
904
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300905/*[clinic input]
906_io._RawIOBase.readall
907
908Read until EOF, using multiple read() call.
909[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000910
911static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300912_io__RawIOBase_readall_impl(PyObject *self)
913/*[clinic end generated code: output=1987b9ce929425a0 input=688874141213622a]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000914{
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000915 int r;
916 PyObject *chunks = PyList_New(0);
917 PyObject *result;
Victor Stinnercc024d12013-10-29 02:23:46 +0100918
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000919 if (chunks == NULL)
920 return NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000921
922 while (1) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200923 PyObject *data = _PyObject_CallMethodId(self, &PyId_read,
924 "i", DEFAULT_BUFFER_SIZE);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000925 if (!data) {
Gregory P. Smith51359922012-06-23 23:55:39 -0700926 /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
927 when EINTR occurs so we needn't do it ourselves. */
928 if (_PyIO_trap_eintr()) {
929 continue;
930 }
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000931 Py_DECREF(chunks);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000932 return NULL;
933 }
Victor Stinnera80987f2011-05-25 22:47:16 +0200934 if (data == Py_None) {
935 if (PyList_GET_SIZE(chunks) == 0) {
936 Py_DECREF(chunks);
937 return data;
938 }
939 Py_DECREF(data);
940 break;
941 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000942 if (!PyBytes_Check(data)) {
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000943 Py_DECREF(chunks);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000944 Py_DECREF(data);
945 PyErr_SetString(PyExc_TypeError, "read() should return bytes");
946 return NULL;
947 }
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000948 if (PyBytes_GET_SIZE(data) == 0) {
949 /* EOF */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000950 Py_DECREF(data);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000951 break;
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000952 }
953 r = PyList_Append(chunks, data);
954 Py_DECREF(data);
955 if (r < 0) {
956 Py_DECREF(chunks);
957 return NULL;
958 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000959 }
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000960 result = _PyBytes_Join(_PyIO_empty_bytes, chunks);
961 Py_DECREF(chunks);
962 return result;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000963}
964
Antoine Pitrou45d61562015-05-20 21:50:59 +0200965static PyObject *
966rawiobase_readinto(PyObject *self, PyObject *args)
967{
968 PyErr_SetNone(PyExc_NotImplementedError);
969 return NULL;
970}
971
972static PyObject *
973rawiobase_write(PyObject *self, PyObject *args)
974{
975 PyErr_SetNone(PyExc_NotImplementedError);
976 return NULL;
977}
978
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000979static PyMethodDef rawiobase_methods[] = {
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300980 _IO__RAWIOBASE_READ_METHODDEF
981 _IO__RAWIOBASE_READALL_METHODDEF
Antoine Pitrou45d61562015-05-20 21:50:59 +0200982 {"readinto", rawiobase_readinto, METH_VARARGS},
983 {"write", rawiobase_write, METH_VARARGS},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000984 {NULL, NULL}
985};
986
987PyTypeObject PyRawIOBase_Type = {
988 PyVarObject_HEAD_INIT(NULL, 0)
989 "_io._RawIOBase", /*tp_name*/
990 0, /*tp_basicsize*/
991 0, /*tp_itemsize*/
992 0, /*tp_dealloc*/
993 0, /*tp_print*/
994 0, /*tp_getattr*/
995 0, /*tp_setattr*/
996 0, /*tp_compare */
997 0, /*tp_repr*/
998 0, /*tp_as_number*/
999 0, /*tp_as_sequence*/
1000 0, /*tp_as_mapping*/
1001 0, /*tp_hash */
1002 0, /*tp_call*/
1003 0, /*tp_str*/
1004 0, /*tp_getattro*/
1005 0, /*tp_setattro*/
1006 0, /*tp_as_buffer*/
Antoine Pitrou796564c2013-07-30 19:59:21 +02001007 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_FINALIZE, /*tp_flags*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001008 rawiobase_doc, /* tp_doc */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001009 0, /* tp_traverse */
1010 0, /* tp_clear */
1011 0, /* tp_richcompare */
1012 0, /* tp_weaklistoffset */
1013 0, /* tp_iter */
1014 0, /* tp_iternext */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001015 rawiobase_methods, /* tp_methods */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001016 0, /* tp_members */
1017 0, /* tp_getset */
1018 &PyIOBase_Type, /* tp_base */
1019 0, /* tp_dict */
1020 0, /* tp_descr_get */
1021 0, /* tp_descr_set */
1022 0, /* tp_dictoffset */
1023 0, /* tp_init */
1024 0, /* tp_alloc */
1025 0, /* tp_new */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001026 0, /* tp_free */
1027 0, /* tp_is_gc */
1028 0, /* tp_bases */
1029 0, /* tp_mro */
1030 0, /* tp_cache */
1031 0, /* tp_subclasses */
1032 0, /* tp_weaklist */
1033 0, /* tp_del */
1034 0, /* tp_version_tag */
1035 0, /* tp_finalize */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001036};