blob: ee6ad474b596b7c65eb363dbc7b0cc7b9fe1d412 [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);
Victor Stinner61bdb0d2016-12-09 15:39:28 +0100664 PyObject *ret = _PyObject_CallMethodIdObjArgs(result, &PyId_extend,
665 self, NULL);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200666
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000667 if (ret == NULL) {
668 Py_DECREF(result);
669 return NULL;
670 }
671 Py_DECREF(ret);
672 return result;
673 }
674
675 while (1) {
676 PyObject *line = PyIter_Next(self);
677 if (line == NULL) {
678 if (PyErr_Occurred()) {
679 Py_DECREF(result);
680 return NULL;
681 }
682 else
683 break; /* StopIteration raised */
684 }
685
686 if (PyList_Append(result, line) < 0) {
687 Py_DECREF(line);
688 Py_DECREF(result);
689 return NULL;
690 }
691 length += PyObject_Size(line);
692 Py_DECREF(line);
693
694 if (length > hint)
695 break;
696 }
697 return result;
698}
699
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300700/*[clinic input]
701_io._IOBase.writelines
702 lines: object
703 /
704[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000705
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300706static PyObject *
707_io__IOBase_writelines(PyObject *self, PyObject *lines)
708/*[clinic end generated code: output=976eb0a9b60a6628 input=432e729a8450b3cb]*/
709{
710 PyObject *iter, *res;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000711
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000712 if (_PyIOBase_check_closed(self, Py_True) == NULL)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000713 return NULL;
714
715 iter = PyObject_GetIter(lines);
716 if (iter == NULL)
717 return NULL;
718
719 while (1) {
720 PyObject *line = PyIter_Next(iter);
721 if (line == NULL) {
722 if (PyErr_Occurred()) {
723 Py_DECREF(iter);
724 return NULL;
725 }
726 else
727 break; /* Stop Iteration */
728 }
729
Gregory P. Smithb9817b02013-02-01 13:03:39 -0800730 res = NULL;
731 do {
732 res = PyObject_CallMethodObjArgs(self, _PyIO_str_write, line, NULL);
733 } while (res == NULL && _PyIO_trap_eintr());
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000734 Py_DECREF(line);
735 if (res == NULL) {
736 Py_DECREF(iter);
737 return NULL;
738 }
739 Py_DECREF(res);
740 }
741 Py_DECREF(iter);
742 Py_RETURN_NONE;
743}
744
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300745#include "clinic/iobase.c.h"
746
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000747static PyMethodDef iobase_methods[] = {
748 {"seek", iobase_seek, METH_VARARGS, iobase_seek_doc},
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300749 _IO__IOBASE_TELL_METHODDEF
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000750 {"truncate", iobase_truncate, METH_VARARGS, iobase_truncate_doc},
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300751 _IO__IOBASE_FLUSH_METHODDEF
752 _IO__IOBASE_CLOSE_METHODDEF
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000753
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300754 _IO__IOBASE_SEEKABLE_METHODDEF
755 _IO__IOBASE_READABLE_METHODDEF
756 _IO__IOBASE_WRITABLE_METHODDEF
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000757
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000758 {"_checkClosed", _PyIOBase_check_closed, METH_NOARGS},
759 {"_checkSeekable", _PyIOBase_check_seekable, METH_NOARGS},
760 {"_checkReadable", _PyIOBase_check_readable, METH_NOARGS},
761 {"_checkWritable", _PyIOBase_check_writable, METH_NOARGS},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000762
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300763 _IO__IOBASE_FILENO_METHODDEF
764 _IO__IOBASE_ISATTY_METHODDEF
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000765
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000766 {"__enter__", iobase_enter, METH_NOARGS},
767 {"__exit__", iobase_exit, METH_VARARGS},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000768
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300769 _IO__IOBASE_READLINE_METHODDEF
770 _IO__IOBASE_READLINES_METHODDEF
771 _IO__IOBASE_WRITELINES_METHODDEF
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000772
773 {NULL, NULL}
774};
775
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000776static PyGetSetDef iobase_getset[] = {
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500777 {"__dict__", PyObject_GenericGetDict, NULL, NULL},
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000778 {"closed", (getter)iobase_closed_get, NULL, NULL},
Benjamin Peterson1fea3212009-04-19 03:15:20 +0000779 {NULL}
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000780};
781
782
783PyTypeObject PyIOBase_Type = {
784 PyVarObject_HEAD_INIT(NULL, 0)
785 "_io._IOBase", /*tp_name*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000786 sizeof(iobase), /*tp_basicsize*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000787 0, /*tp_itemsize*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000788 (destructor)iobase_dealloc, /*tp_dealloc*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000789 0, /*tp_print*/
790 0, /*tp_getattr*/
791 0, /*tp_setattr*/
792 0, /*tp_compare */
793 0, /*tp_repr*/
794 0, /*tp_as_number*/
795 0, /*tp_as_sequence*/
796 0, /*tp_as_mapping*/
797 0, /*tp_hash */
798 0, /*tp_call*/
799 0, /*tp_str*/
800 0, /*tp_getattro*/
801 0, /*tp_setattro*/
802 0, /*tp_as_buffer*/
803 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
Antoine Pitrou796564c2013-07-30 19:59:21 +0200804 | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /*tp_flags*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000805 iobase_doc, /* tp_doc */
806 (traverseproc)iobase_traverse, /* tp_traverse */
807 (inquiry)iobase_clear, /* tp_clear */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000808 0, /* tp_richcompare */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000809 offsetof(iobase, weakreflist), /* tp_weaklistoffset */
810 iobase_iter, /* tp_iter */
811 iobase_iternext, /* tp_iternext */
812 iobase_methods, /* tp_methods */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000813 0, /* tp_members */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000814 iobase_getset, /* tp_getset */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000815 0, /* tp_base */
816 0, /* tp_dict */
817 0, /* tp_descr_get */
818 0, /* tp_descr_set */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000819 offsetof(iobase, dict), /* tp_dictoffset */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000820 0, /* tp_init */
821 0, /* tp_alloc */
822 PyType_GenericNew, /* tp_new */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200823 0, /* tp_free */
824 0, /* tp_is_gc */
825 0, /* tp_bases */
826 0, /* tp_mro */
827 0, /* tp_cache */
828 0, /* tp_subclasses */
829 0, /* tp_weaklist */
830 0, /* tp_del */
831 0, /* tp_version_tag */
Victor Stinner928bff02016-03-19 10:36:36 +0100832 iobase_finalize, /* tp_finalize */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000833};
834
835
836/*
837 * RawIOBase class, Inherits from IOBase.
838 */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000839PyDoc_STRVAR(rawiobase_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000840 "Base class for raw binary I/O.");
841
842/*
843 * The read() method is implemented by calling readinto(); derived classes
844 * that want to support read() only need to implement readinto() as a
845 * primitive operation. In general, readinto() can be more efficient than
846 * read().
847 *
848 * (It would be tempting to also provide an implementation of readinto() in
849 * terms of read(), in case the latter is a more suitable primitive operation,
850 * but that would lead to nasty recursion in case a subclass doesn't implement
851 * either.)
852*/
853
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300854/*[clinic input]
855_io._RawIOBase.read
856 size as n: Py_ssize_t = -1
857 /
858[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000859
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300860static PyObject *
861_io__RawIOBase_read_impl(PyObject *self, Py_ssize_t n)
862/*[clinic end generated code: output=6cdeb731e3c9f13c input=b6d0dcf6417d1374]*/
863{
864 PyObject *b, *res;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000865
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200866 if (n < 0) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200867 _Py_IDENTIFIER(readall);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200868
869 return _PyObject_CallMethodId(self, &PyId_readall, NULL);
870 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000871
872 /* TODO: allocate a bytes object directly instead and manually construct
873 a writable memoryview pointing to it. */
874 b = PyByteArray_FromStringAndSize(NULL, n);
875 if (b == NULL)
876 return NULL;
877
878 res = PyObject_CallMethodObjArgs(self, _PyIO_str_readinto, b, NULL);
Antoine Pitrou328ec742010-09-14 18:37:24 +0000879 if (res == NULL || res == Py_None) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000880 Py_DECREF(b);
Antoine Pitrou328ec742010-09-14 18:37:24 +0000881 return res;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000882 }
883
884 n = PyNumber_AsSsize_t(res, PyExc_ValueError);
885 Py_DECREF(res);
886 if (n == -1 && PyErr_Occurred()) {
887 Py_DECREF(b);
888 return NULL;
889 }
890
891 res = PyBytes_FromStringAndSize(PyByteArray_AsString(b), n);
892 Py_DECREF(b);
893 return res;
894}
895
896
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300897/*[clinic input]
898_io._RawIOBase.readall
899
900Read until EOF, using multiple read() call.
901[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000902
903static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300904_io__RawIOBase_readall_impl(PyObject *self)
905/*[clinic end generated code: output=1987b9ce929425a0 input=688874141213622a]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000906{
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000907 int r;
908 PyObject *chunks = PyList_New(0);
909 PyObject *result;
Victor Stinnercc024d12013-10-29 02:23:46 +0100910
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000911 if (chunks == NULL)
912 return NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000913
914 while (1) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200915 PyObject *data = _PyObject_CallMethodId(self, &PyId_read,
916 "i", DEFAULT_BUFFER_SIZE);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000917 if (!data) {
Gregory P. Smith51359922012-06-23 23:55:39 -0700918 /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
919 when EINTR occurs so we needn't do it ourselves. */
920 if (_PyIO_trap_eintr()) {
921 continue;
922 }
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000923 Py_DECREF(chunks);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000924 return NULL;
925 }
Victor Stinnera80987f2011-05-25 22:47:16 +0200926 if (data == Py_None) {
927 if (PyList_GET_SIZE(chunks) == 0) {
928 Py_DECREF(chunks);
929 return data;
930 }
931 Py_DECREF(data);
932 break;
933 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000934 if (!PyBytes_Check(data)) {
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000935 Py_DECREF(chunks);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000936 Py_DECREF(data);
937 PyErr_SetString(PyExc_TypeError, "read() should return bytes");
938 return NULL;
939 }
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000940 if (PyBytes_GET_SIZE(data) == 0) {
941 /* EOF */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000942 Py_DECREF(data);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000943 break;
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000944 }
945 r = PyList_Append(chunks, data);
946 Py_DECREF(data);
947 if (r < 0) {
948 Py_DECREF(chunks);
949 return NULL;
950 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000951 }
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000952 result = _PyBytes_Join(_PyIO_empty_bytes, chunks);
953 Py_DECREF(chunks);
954 return result;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000955}
956
Antoine Pitrou45d61562015-05-20 21:50:59 +0200957static PyObject *
958rawiobase_readinto(PyObject *self, PyObject *args)
959{
960 PyErr_SetNone(PyExc_NotImplementedError);
961 return NULL;
962}
963
964static PyObject *
965rawiobase_write(PyObject *self, PyObject *args)
966{
967 PyErr_SetNone(PyExc_NotImplementedError);
968 return NULL;
969}
970
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000971static PyMethodDef rawiobase_methods[] = {
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300972 _IO__RAWIOBASE_READ_METHODDEF
973 _IO__RAWIOBASE_READALL_METHODDEF
Antoine Pitrou45d61562015-05-20 21:50:59 +0200974 {"readinto", rawiobase_readinto, METH_VARARGS},
975 {"write", rawiobase_write, METH_VARARGS},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000976 {NULL, NULL}
977};
978
979PyTypeObject PyRawIOBase_Type = {
980 PyVarObject_HEAD_INIT(NULL, 0)
981 "_io._RawIOBase", /*tp_name*/
982 0, /*tp_basicsize*/
983 0, /*tp_itemsize*/
984 0, /*tp_dealloc*/
985 0, /*tp_print*/
986 0, /*tp_getattr*/
987 0, /*tp_setattr*/
988 0, /*tp_compare */
989 0, /*tp_repr*/
990 0, /*tp_as_number*/
991 0, /*tp_as_sequence*/
992 0, /*tp_as_mapping*/
993 0, /*tp_hash */
994 0, /*tp_call*/
995 0, /*tp_str*/
996 0, /*tp_getattro*/
997 0, /*tp_setattro*/
998 0, /*tp_as_buffer*/
Antoine Pitrou796564c2013-07-30 19:59:21 +0200999 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_FINALIZE, /*tp_flags*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001000 rawiobase_doc, /* tp_doc */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001001 0, /* tp_traverse */
1002 0, /* tp_clear */
1003 0, /* tp_richcompare */
1004 0, /* tp_weaklistoffset */
1005 0, /* tp_iter */
1006 0, /* tp_iternext */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001007 rawiobase_methods, /* tp_methods */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001008 0, /* tp_members */
1009 0, /* tp_getset */
1010 &PyIOBase_Type, /* tp_base */
1011 0, /* tp_dict */
1012 0, /* tp_descr_get */
1013 0, /* tp_descr_set */
1014 0, /* tp_dictoffset */
1015 0, /* tp_init */
1016 0, /* tp_alloc */
1017 0, /* tp_new */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001018 0, /* tp_free */
1019 0, /* tp_is_gc */
1020 0, /* tp_bases */
1021 0, /* tp_mro */
1022 0, /* tp_cache */
1023 0, /* tp_subclasses */
1024 0, /* tp_weaklist */
1025 0, /* tp_del */
1026 0, /* tp_version_tag */
1027 0, /* tp_finalize */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001028};