blob: 472ef3b97cd38ea49086700bcf049870aaddc82a [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;
Benjamin Peterson05516132009-12-13 19:28:09 +0000653 PyObject *result;
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) {
667 Py_DECREF(result);
668 return NULL;
669 }
670 Py_DECREF(ret);
671 return result;
672 }
673
674 while (1) {
675 PyObject *line = PyIter_Next(self);
676 if (line == NULL) {
677 if (PyErr_Occurred()) {
678 Py_DECREF(result);
679 return NULL;
680 }
681 else
682 break; /* StopIteration raised */
683 }
684
685 if (PyList_Append(result, line) < 0) {
686 Py_DECREF(line);
687 Py_DECREF(result);
688 return NULL;
689 }
690 length += PyObject_Size(line);
691 Py_DECREF(line);
692
693 if (length > hint)
694 break;
695 }
696 return result;
697}
698
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300699/*[clinic input]
700_io._IOBase.writelines
701 lines: object
702 /
703[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000704
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300705static PyObject *
706_io__IOBase_writelines(PyObject *self, PyObject *lines)
707/*[clinic end generated code: output=976eb0a9b60a6628 input=432e729a8450b3cb]*/
708{
709 PyObject *iter, *res;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000710
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000711 if (_PyIOBase_check_closed(self, Py_True) == NULL)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000712 return NULL;
713
714 iter = PyObject_GetIter(lines);
715 if (iter == NULL)
716 return NULL;
717
718 while (1) {
719 PyObject *line = PyIter_Next(iter);
720 if (line == NULL) {
721 if (PyErr_Occurred()) {
722 Py_DECREF(iter);
723 return NULL;
724 }
725 else
726 break; /* Stop Iteration */
727 }
728
Gregory P. Smithb9817b02013-02-01 13:03:39 -0800729 res = NULL;
730 do {
731 res = PyObject_CallMethodObjArgs(self, _PyIO_str_write, line, NULL);
732 } while (res == NULL && _PyIO_trap_eintr());
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000733 Py_DECREF(line);
734 if (res == NULL) {
735 Py_DECREF(iter);
736 return NULL;
737 }
738 Py_DECREF(res);
739 }
740 Py_DECREF(iter);
741 Py_RETURN_NONE;
742}
743
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300744#include "clinic/iobase.c.h"
745
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000746static PyMethodDef iobase_methods[] = {
747 {"seek", iobase_seek, METH_VARARGS, iobase_seek_doc},
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300748 _IO__IOBASE_TELL_METHODDEF
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000749 {"truncate", iobase_truncate, METH_VARARGS, iobase_truncate_doc},
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300750 _IO__IOBASE_FLUSH_METHODDEF
751 _IO__IOBASE_CLOSE_METHODDEF
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000752
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300753 _IO__IOBASE_SEEKABLE_METHODDEF
754 _IO__IOBASE_READABLE_METHODDEF
755 _IO__IOBASE_WRITABLE_METHODDEF
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000756
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000757 {"_checkClosed", _PyIOBase_check_closed, METH_NOARGS},
758 {"_checkSeekable", _PyIOBase_check_seekable, METH_NOARGS},
759 {"_checkReadable", _PyIOBase_check_readable, METH_NOARGS},
760 {"_checkWritable", _PyIOBase_check_writable, METH_NOARGS},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000761
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300762 _IO__IOBASE_FILENO_METHODDEF
763 _IO__IOBASE_ISATTY_METHODDEF
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000764
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000765 {"__enter__", iobase_enter, METH_NOARGS},
766 {"__exit__", iobase_exit, METH_VARARGS},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000767
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300768 _IO__IOBASE_READLINE_METHODDEF
769 _IO__IOBASE_READLINES_METHODDEF
770 _IO__IOBASE_WRITELINES_METHODDEF
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000771
772 {NULL, NULL}
773};
774
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000775static PyGetSetDef iobase_getset[] = {
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500776 {"__dict__", PyObject_GenericGetDict, NULL, NULL},
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000777 {"closed", (getter)iobase_closed_get, NULL, NULL},
Benjamin Peterson1fea3212009-04-19 03:15:20 +0000778 {NULL}
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000779};
780
781
782PyTypeObject PyIOBase_Type = {
783 PyVarObject_HEAD_INIT(NULL, 0)
784 "_io._IOBase", /*tp_name*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000785 sizeof(iobase), /*tp_basicsize*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000786 0, /*tp_itemsize*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000787 (destructor)iobase_dealloc, /*tp_dealloc*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000788 0, /*tp_print*/
789 0, /*tp_getattr*/
790 0, /*tp_setattr*/
791 0, /*tp_compare */
792 0, /*tp_repr*/
793 0, /*tp_as_number*/
794 0, /*tp_as_sequence*/
795 0, /*tp_as_mapping*/
796 0, /*tp_hash */
797 0, /*tp_call*/
798 0, /*tp_str*/
799 0, /*tp_getattro*/
800 0, /*tp_setattro*/
801 0, /*tp_as_buffer*/
802 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
Antoine Pitrou796564c2013-07-30 19:59:21 +0200803 | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /*tp_flags*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000804 iobase_doc, /* tp_doc */
805 (traverseproc)iobase_traverse, /* tp_traverse */
806 (inquiry)iobase_clear, /* tp_clear */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000807 0, /* tp_richcompare */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000808 offsetof(iobase, weakreflist), /* tp_weaklistoffset */
809 iobase_iter, /* tp_iter */
810 iobase_iternext, /* tp_iternext */
811 iobase_methods, /* tp_methods */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000812 0, /* tp_members */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000813 iobase_getset, /* tp_getset */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000814 0, /* tp_base */
815 0, /* tp_dict */
816 0, /* tp_descr_get */
817 0, /* tp_descr_set */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000818 offsetof(iobase, dict), /* tp_dictoffset */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000819 0, /* tp_init */
820 0, /* tp_alloc */
821 PyType_GenericNew, /* tp_new */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200822 0, /* tp_free */
823 0, /* tp_is_gc */
824 0, /* tp_bases */
825 0, /* tp_mro */
826 0, /* tp_cache */
827 0, /* tp_subclasses */
828 0, /* tp_weaklist */
829 0, /* tp_del */
830 0, /* tp_version_tag */
Victor Stinner928bff02016-03-19 10:36:36 +0100831 iobase_finalize, /* tp_finalize */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000832};
833
834
835/*
836 * RawIOBase class, Inherits from IOBase.
837 */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000838PyDoc_STRVAR(rawiobase_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000839 "Base class for raw binary I/O.");
840
841/*
842 * The read() method is implemented by calling readinto(); derived classes
843 * that want to support read() only need to implement readinto() as a
844 * primitive operation. In general, readinto() can be more efficient than
845 * read().
846 *
847 * (It would be tempting to also provide an implementation of readinto() in
848 * terms of read(), in case the latter is a more suitable primitive operation,
849 * but that would lead to nasty recursion in case a subclass doesn't implement
850 * either.)
851*/
852
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300853/*[clinic input]
854_io._RawIOBase.read
855 size as n: Py_ssize_t = -1
856 /
857[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000858
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300859static PyObject *
860_io__RawIOBase_read_impl(PyObject *self, Py_ssize_t n)
861/*[clinic end generated code: output=6cdeb731e3c9f13c input=b6d0dcf6417d1374]*/
862{
863 PyObject *b, *res;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000864
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200865 if (n < 0) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200866 _Py_IDENTIFIER(readall);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200867
868 return _PyObject_CallMethodId(self, &PyId_readall, NULL);
869 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000870
871 /* TODO: allocate a bytes object directly instead and manually construct
872 a writable memoryview pointing to it. */
873 b = PyByteArray_FromStringAndSize(NULL, n);
874 if (b == NULL)
875 return NULL;
876
877 res = PyObject_CallMethodObjArgs(self, _PyIO_str_readinto, b, NULL);
Antoine Pitrou328ec742010-09-14 18:37:24 +0000878 if (res == NULL || res == Py_None) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000879 Py_DECREF(b);
Antoine Pitrou328ec742010-09-14 18:37:24 +0000880 return res;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000881 }
882
883 n = PyNumber_AsSsize_t(res, PyExc_ValueError);
884 Py_DECREF(res);
885 if (n == -1 && PyErr_Occurred()) {
886 Py_DECREF(b);
887 return NULL;
888 }
889
890 res = PyBytes_FromStringAndSize(PyByteArray_AsString(b), n);
891 Py_DECREF(b);
892 return res;
893}
894
895
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300896/*[clinic input]
897_io._RawIOBase.readall
898
899Read until EOF, using multiple read() call.
900[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000901
902static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300903_io__RawIOBase_readall_impl(PyObject *self)
904/*[clinic end generated code: output=1987b9ce929425a0 input=688874141213622a]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000905{
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000906 int r;
907 PyObject *chunks = PyList_New(0);
908 PyObject *result;
Victor Stinnercc024d12013-10-29 02:23:46 +0100909
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000910 if (chunks == NULL)
911 return NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000912
913 while (1) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200914 PyObject *data = _PyObject_CallMethodId(self, &PyId_read,
915 "i", DEFAULT_BUFFER_SIZE);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000916 if (!data) {
Gregory P. Smith51359922012-06-23 23:55:39 -0700917 /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
918 when EINTR occurs so we needn't do it ourselves. */
919 if (_PyIO_trap_eintr()) {
920 continue;
921 }
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000922 Py_DECREF(chunks);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000923 return NULL;
924 }
Victor Stinnera80987f2011-05-25 22:47:16 +0200925 if (data == Py_None) {
926 if (PyList_GET_SIZE(chunks) == 0) {
927 Py_DECREF(chunks);
928 return data;
929 }
930 Py_DECREF(data);
931 break;
932 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000933 if (!PyBytes_Check(data)) {
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000934 Py_DECREF(chunks);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000935 Py_DECREF(data);
936 PyErr_SetString(PyExc_TypeError, "read() should return bytes");
937 return NULL;
938 }
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000939 if (PyBytes_GET_SIZE(data) == 0) {
940 /* EOF */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000941 Py_DECREF(data);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000942 break;
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000943 }
944 r = PyList_Append(chunks, data);
945 Py_DECREF(data);
946 if (r < 0) {
947 Py_DECREF(chunks);
948 return NULL;
949 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000950 }
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000951 result = _PyBytes_Join(_PyIO_empty_bytes, chunks);
952 Py_DECREF(chunks);
953 return result;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000954}
955
Antoine Pitrou45d61562015-05-20 21:50:59 +0200956static PyObject *
957rawiobase_readinto(PyObject *self, PyObject *args)
958{
959 PyErr_SetNone(PyExc_NotImplementedError);
960 return NULL;
961}
962
963static PyObject *
964rawiobase_write(PyObject *self, PyObject *args)
965{
966 PyErr_SetNone(PyExc_NotImplementedError);
967 return NULL;
968}
969
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000970static PyMethodDef rawiobase_methods[] = {
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300971 _IO__RAWIOBASE_READ_METHODDEF
972 _IO__RAWIOBASE_READALL_METHODDEF
Antoine Pitrou45d61562015-05-20 21:50:59 +0200973 {"readinto", rawiobase_readinto, METH_VARARGS},
974 {"write", rawiobase_write, METH_VARARGS},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000975 {NULL, NULL}
976};
977
978PyTypeObject PyRawIOBase_Type = {
979 PyVarObject_HEAD_INIT(NULL, 0)
980 "_io._RawIOBase", /*tp_name*/
981 0, /*tp_basicsize*/
982 0, /*tp_itemsize*/
983 0, /*tp_dealloc*/
984 0, /*tp_print*/
985 0, /*tp_getattr*/
986 0, /*tp_setattr*/
987 0, /*tp_compare */
988 0, /*tp_repr*/
989 0, /*tp_as_number*/
990 0, /*tp_as_sequence*/
991 0, /*tp_as_mapping*/
992 0, /*tp_hash */
993 0, /*tp_call*/
994 0, /*tp_str*/
995 0, /*tp_getattro*/
996 0, /*tp_setattro*/
997 0, /*tp_as_buffer*/
Antoine Pitrou796564c2013-07-30 19:59:21 +0200998 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_FINALIZE, /*tp_flags*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000999 rawiobase_doc, /* tp_doc */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001000 0, /* tp_traverse */
1001 0, /* tp_clear */
1002 0, /* tp_richcompare */
1003 0, /* tp_weaklistoffset */
1004 0, /* tp_iter */
1005 0, /* tp_iternext */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001006 rawiobase_methods, /* tp_methods */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001007 0, /* tp_members */
1008 0, /* tp_getset */
1009 &PyIOBase_Type, /* tp_base */
1010 0, /* tp_dict */
1011 0, /* tp_descr_get */
1012 0, /* tp_descr_set */
1013 0, /* tp_dictoffset */
1014 0, /* tp_init */
1015 0, /* tp_alloc */
1016 0, /* tp_new */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001017 0, /* tp_free */
1018 0, /* tp_is_gc */
1019 0, /* tp_bases */
1020 0, /* tp_mro */
1021 0, /* tp_cache */
1022 0, /* tp_subclasses */
1023 0, /* tp_weaklist */
1024 0, /* tp_del */
1025 0, /* tp_version_tag */
1026 0, /* tp_finalize */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001027};