blob: 025007e40dc2b5fba3b8f5030f8947e2d275cb6a [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"
56 "bytes. bytearrays are accepted too, and in some cases (such as\n"
57 "readinto) needed. Text I/O classes work with str data.\n"
58 "\n"
Andrew Kuchling76466202014-04-15 21:11:36 -040059 "Note that calling any method (except additional calls to close(),\n"
60 "which are ignored) on a closed stream should raise a ValueError.\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000061 "\n"
62 "IOBase (and its subclasses) support the iterator protocol, meaning\n"
63 "that an IOBase object can be iterated over yielding the lines in a\n"
64 "stream.\n"
65 "\n"
66 "IOBase also supports the :keyword:`with` statement. In this example,\n"
Ezio Melotti13925002011-03-16 11:05:33 +020067 "fp is closed after the suite of the with statement is complete:\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000068 "\n"
69 "with open('spam.txt', 'r') as fp:\n"
70 " fp.write('Spam and eggs!')\n");
71
72/* Use this macro whenever you want to check the internal `closed` status
73 of the IOBase object rather than the virtual `closed` attribute as returned
74 by whatever subclass. */
75
Martin v. Löwis767046a2011-10-14 15:35:36 +020076_Py_IDENTIFIER(__IOBase_closed);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000077#define IS_CLOSED(self) \
Martin v. Löwis767046a2011-10-14 15:35:36 +020078 _PyObject_HasAttrId(self, &PyId___IOBase_closed)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000079
Victor Stinner3f36a572013-11-12 21:39:02 +010080_Py_IDENTIFIER(read);
81
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000082/* Internal methods */
83static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000084iobase_unsupported(const char *message)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000085{
Antoine Pitrou712cb732013-12-21 15:51:54 +010086 _PyIO_State *state = IO_STATE();
87 if (state != NULL)
88 PyErr_SetString(state->unsupported_operation, message);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000089 return NULL;
90}
91
92/* Positionning */
93
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000094PyDoc_STRVAR(iobase_seek_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000095 "Change stream position.\n"
96 "\n"
Terry Jan Reedy0158af32013-03-11 17:42:46 -040097 "Change the stream position to the given byte offset. The offset is\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000098 "interpreted relative to the position indicated by whence. Values\n"
99 "for whence are:\n"
100 "\n"
101 "* 0 -- start of stream (the default); offset should be zero or positive\n"
102 "* 1 -- current stream position; offset may be negative\n"
103 "* 2 -- end of stream; offset is usually negative\n"
104 "\n"
105 "Return the new absolute position.");
106
107static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000108iobase_seek(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000109{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000110 return iobase_unsupported("seek");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000111}
112
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300113/*[clinic input]
114_io._IOBase.tell
115
116Return current stream position.
117[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000118
119static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300120_io__IOBase_tell_impl(PyObject *self)
121/*[clinic end generated code: output=89a1c0807935abe2 input=04e615fec128801f]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000122{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200123 _Py_IDENTIFIER(seek);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200124
125 return _PyObject_CallMethodId(self, &PyId_seek, "ii", 0, 1);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000126}
127
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000128PyDoc_STRVAR(iobase_truncate_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000129 "Truncate file to size bytes.\n"
130 "\n"
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000131 "File pointer is left unchanged. Size defaults to the current IO\n"
132 "position as reported by tell(). Returns the new size.");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000133
134static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000135iobase_truncate(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000136{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000137 return iobase_unsupported("truncate");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000138}
139
140/* Flush and close methods */
141
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300142/*[clinic input]
143_io._IOBase.flush
144
145Flush write buffers, if applicable.
146
147This is not implemented for read-only and non-blocking streams.
148[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000149
150static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300151_io__IOBase_flush_impl(PyObject *self)
152/*[clinic end generated code: output=7cef4b4d54656a3b input=773be121abe270aa]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000153{
154 /* XXX Should this return the number of bytes written??? */
155 if (IS_CLOSED(self)) {
156 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file.");
157 return NULL;
158 }
159 Py_RETURN_NONE;
160}
161
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000162static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000163iobase_closed(PyObject *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000164{
165 PyObject *res;
166 int closed;
167 /* This gets the derived attribute, which is *not* __IOBase_closed
168 in most cases! */
169 res = PyObject_GetAttr(self, _PyIO_str_closed);
170 if (res == NULL)
171 return 0;
172 closed = PyObject_IsTrue(res);
173 Py_DECREF(res);
174 return closed;
175}
176
177static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000178iobase_closed_get(PyObject *self, void *context)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000179{
180 return PyBool_FromLong(IS_CLOSED(self));
181}
182
183PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000184_PyIOBase_check_closed(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000185{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000186 if (iobase_closed(self)) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000187 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file.");
188 return NULL;
189 }
190 if (args == Py_True)
191 return Py_None;
192 else
193 Py_RETURN_NONE;
194}
195
196/* XXX: IOBase thinks it has to maintain its own internal state in
197 `__IOBase_closed` and call flush() by itself, but it is redundant with
198 whatever behaviour a non-trivial derived class will implement. */
199
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300200/*[clinic input]
201_io._IOBase.close
202
203Flush and close the IO object.
204
205This method has no effect if the file is already closed.
206[clinic start generated code]*/
207
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000208static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300209_io__IOBase_close_impl(PyObject *self)
210/*[clinic end generated code: output=63c6a6f57d783d6d input=f4494d5c31dbc6b7]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000211{
212 PyObject *res;
213
214 if (IS_CLOSED(self))
215 Py_RETURN_NONE;
216
217 res = PyObject_CallMethodObjArgs(self, _PyIO_str_flush, NULL);
Victor Stinneraa5bbfa2013-11-08 00:29:41 +0100218
219 if (_PyObject_SetAttrId(self, &PyId___IOBase_closed, Py_True) < 0) {
220 Py_XDECREF(res);
Antoine Pitrou6be88762010-05-03 16:48:20 +0000221 return NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000222 }
Victor Stinneraa5bbfa2013-11-08 00:29:41 +0100223
224 if (res == NULL)
225 return NULL;
226
227 Py_DECREF(res);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000228 Py_RETURN_NONE;
229}
230
231/* Finalization and garbage collection support */
232
Antoine Pitrou796564c2013-07-30 19:59:21 +0200233static void
234iobase_finalize(PyObject *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000235{
236 PyObject *res;
Antoine Pitrou796564c2013-07-30 19:59:21 +0200237 PyObject *error_type, *error_value, *error_traceback;
238 int closed;
239 _Py_IDENTIFIER(_finalizing);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000240
Antoine Pitrou796564c2013-07-30 19:59:21 +0200241 /* Save the current exception, if any. */
242 PyErr_Fetch(&error_type, &error_value, &error_traceback);
243
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000244 /* If `closed` doesn't exist or can't be evaluated as bool, then the
245 object is probably in an unusable state, so ignore. */
246 res = PyObject_GetAttr(self, _PyIO_str_closed);
Christian Heimes72f455e2013-07-31 01:33:50 +0200247 if (res == NULL) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000248 PyErr_Clear();
Christian Heimes72f455e2013-07-31 01:33:50 +0200249 closed = -1;
250 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000251 else {
252 closed = PyObject_IsTrue(res);
253 Py_DECREF(res);
254 if (closed == -1)
255 PyErr_Clear();
256 }
257 if (closed == 0) {
Antoine Pitrou796564c2013-07-30 19:59:21 +0200258 /* Signal close() that it was called as part of the object
259 finalization process. */
260 if (_PyObject_SetAttrId(self, &PyId__finalizing, Py_True))
261 PyErr_Clear();
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000262 res = PyObject_CallMethodObjArgs((PyObject *) self, _PyIO_str_close,
263 NULL);
264 /* Silencing I/O errors is bad, but printing spurious tracebacks is
265 equally as bad, and potentially more frequent (because of
266 shutdown issues). */
267 if (res == NULL)
268 PyErr_Clear();
269 else
270 Py_DECREF(res);
271 }
Antoine Pitrou796564c2013-07-30 19:59:21 +0200272
273 /* Restore the saved exception. */
274 PyErr_Restore(error_type, error_value, error_traceback);
275}
276
277int
278_PyIOBase_finalize(PyObject *self)
279{
280 int is_zombie;
281
282 /* If _PyIOBase_finalize() is called from a destructor, we need to
283 resurrect the object as calling close() can invoke arbitrary code. */
284 is_zombie = (Py_REFCNT(self) == 0);
285 if (is_zombie)
286 return PyObject_CallFinalizerFromDealloc(self);
287 else {
288 PyObject_CallFinalizer(self);
289 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000290 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000291}
292
293static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000294iobase_traverse(iobase *self, visitproc visit, void *arg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000295{
296 Py_VISIT(self->dict);
297 return 0;
298}
299
300static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000301iobase_clear(iobase *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000302{
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000303 Py_CLEAR(self->dict);
304 return 0;
305}
306
307/* Destructor */
308
309static void
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000310iobase_dealloc(iobase *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000311{
312 /* NOTE: since IOBaseObject has its own dict, Python-defined attributes
313 are still available here for close() to use.
314 However, if the derived class declares a __slots__, those slots are
315 already gone.
316 */
317 if (_PyIOBase_finalize((PyObject *) self) < 0) {
318 /* When called from a heap type's dealloc, the type will be
319 decref'ed on return (see e.g. subtype_dealloc in typeobject.c). */
320 if (PyType_HasFeature(Py_TYPE(self), Py_TPFLAGS_HEAPTYPE))
321 Py_INCREF(Py_TYPE(self));
322 return;
323 }
324 _PyObject_GC_UNTRACK(self);
325 if (self->weakreflist != NULL)
326 PyObject_ClearWeakRefs((PyObject *) self);
327 Py_CLEAR(self->dict);
328 Py_TYPE(self)->tp_free((PyObject *) self);
329}
330
331/* Inquiry methods */
332
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300333/*[clinic input]
334_io._IOBase.seekable
335
336Return whether object supports random access.
337
338If False, seek(), tell() and truncate() will raise UnsupportedOperation.
339This method may need to do a test seek().
340[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000341
342static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300343_io__IOBase_seekable_impl(PyObject *self)
344/*[clinic end generated code: output=4c24c67f5f32a43d input=22676eebb81dcf1e]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000345{
346 Py_RETURN_FALSE;
347}
348
349PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000350_PyIOBase_check_seekable(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000351{
352 PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_seekable, NULL);
353 if (res == NULL)
354 return NULL;
355 if (res != Py_True) {
356 Py_CLEAR(res);
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000357 iobase_unsupported("File or stream is not seekable.");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000358 return NULL;
359 }
360 if (args == Py_True) {
361 Py_DECREF(res);
362 }
363 return res;
364}
365
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300366/*[clinic input]
367_io._IOBase.readable
368
369Return whether object was opened for reading.
370
371If False, read() will raise UnsupportedOperation.
372[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000373
374static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300375_io__IOBase_readable_impl(PyObject *self)
376/*[clinic end generated code: output=e48089250686388b input=12fc3d8f6be46434]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000377{
378 Py_RETURN_FALSE;
379}
380
381/* May be called with any object */
382PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000383_PyIOBase_check_readable(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000384{
385 PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_readable, NULL);
386 if (res == NULL)
387 return NULL;
388 if (res != Py_True) {
389 Py_CLEAR(res);
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000390 iobase_unsupported("File or stream is not readable.");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000391 return NULL;
392 }
393 if (args == Py_True) {
394 Py_DECREF(res);
395 }
396 return res;
397}
398
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300399/*[clinic input]
400_io._IOBase.writable
401
402Return whether object was opened for writing.
403
404If False, write() will raise UnsupportedOperation.
405[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000406
407static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300408_io__IOBase_writable_impl(PyObject *self)
409/*[clinic end generated code: output=406001d0985be14f input=c17a0bb6a8dfc590]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000410{
411 Py_RETURN_FALSE;
412}
413
414/* May be called with any object */
415PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000416_PyIOBase_check_writable(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000417{
418 PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_writable, NULL);
419 if (res == NULL)
420 return NULL;
421 if (res != Py_True) {
422 Py_CLEAR(res);
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000423 iobase_unsupported("File or stream is not writable.");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000424 return NULL;
425 }
426 if (args == Py_True) {
427 Py_DECREF(res);
428 }
429 return res;
430}
431
432/* Context manager */
433
434static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000435iobase_enter(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000436{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000437 if (_PyIOBase_check_closed(self, Py_True) == NULL)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000438 return NULL;
439
440 Py_INCREF(self);
441 return self;
442}
443
444static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000445iobase_exit(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000446{
447 return PyObject_CallMethodObjArgs(self, _PyIO_str_close, NULL);
448}
449
450/* Lower-level APIs */
451
452/* XXX Should these be present even if unimplemented? */
453
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300454/*[clinic input]
455_io._IOBase.fileno
456
457Returns underlying file descriptor if one exists.
458
459An IOError is raised if the IO object does not use a file descriptor.
460[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000461
462static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300463_io__IOBase_fileno_impl(PyObject *self)
464/*[clinic end generated code: output=7cc0973f0f5f3b73 input=32773c5df4b7eede]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000465{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000466 return iobase_unsupported("fileno");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000467}
468
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300469/*[clinic input]
470_io._IOBase.isatty
471
472Return whether this is an 'interactive' stream.
473
474Return False if it can't be determined.
475[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000476
477static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300478_io__IOBase_isatty_impl(PyObject *self)
479/*[clinic end generated code: output=60cab77cede41cdd input=9ef76530d368458b]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000480{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000481 if (_PyIOBase_check_closed(self, Py_True) == NULL)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000482 return NULL;
483 Py_RETURN_FALSE;
484}
485
486/* Readline(s) and writelines */
487
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300488/*[clinic input]
489_io._IOBase.readline
490 size as limit: io_ssize_t = -1
491 /
492
493Read and return a line from the stream.
494
495If size is specified, at most size bytes will be read.
496
497The line terminator is always b'\n' for binary files; for text
498files, the newlines argument to open can be used to select the line
499terminator(s) recognized.
500[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000501
502static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300503_io__IOBase_readline_impl(PyObject *self, Py_ssize_t limit)
504/*[clinic end generated code: output=4479f79b58187840 input=df4cc8884f553cab]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000505{
506 /* For backwards compatibility, a (slowish) readline(). */
507
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000508 int has_peek = 0;
509 PyObject *buffer, *result;
510 Py_ssize_t old_size = -1;
Martin v. Löwis767046a2011-10-14 15:35:36 +0200511 _Py_IDENTIFIER(peek);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000512
Martin v. Löwis767046a2011-10-14 15:35:36 +0200513 if (_PyObject_HasAttrId(self, &PyId_peek))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000514 has_peek = 1;
515
516 buffer = PyByteArray_FromStringAndSize(NULL, 0);
517 if (buffer == NULL)
518 return NULL;
519
520 while (limit < 0 || Py_SIZE(buffer) < limit) {
521 Py_ssize_t nreadahead = 1;
522 PyObject *b;
523
524 if (has_peek) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200525 PyObject *readahead = _PyObject_CallMethodId(self, &PyId_peek, "i", 1);
Gregory P. Smith51359922012-06-23 23:55:39 -0700526 if (readahead == NULL) {
527 /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
528 when EINTR occurs so we needn't do it ourselves. */
529 if (_PyIO_trap_eintr()) {
530 continue;
531 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000532 goto fail;
Gregory P. Smith51359922012-06-23 23:55:39 -0700533 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000534 if (!PyBytes_Check(readahead)) {
535 PyErr_Format(PyExc_IOError,
536 "peek() should have returned a bytes object, "
537 "not '%.200s'", Py_TYPE(readahead)->tp_name);
538 Py_DECREF(readahead);
539 goto fail;
540 }
541 if (PyBytes_GET_SIZE(readahead) > 0) {
542 Py_ssize_t n = 0;
543 const char *buf = PyBytes_AS_STRING(readahead);
544 if (limit >= 0) {
545 do {
546 if (n >= PyBytes_GET_SIZE(readahead) || n >= limit)
547 break;
548 if (buf[n++] == '\n')
549 break;
550 } while (1);
551 }
552 else {
553 do {
554 if (n >= PyBytes_GET_SIZE(readahead))
555 break;
556 if (buf[n++] == '\n')
557 break;
558 } while (1);
559 }
560 nreadahead = n;
561 }
562 Py_DECREF(readahead);
563 }
564
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200565 b = _PyObject_CallMethodId(self, &PyId_read, "n", nreadahead);
Gregory P. Smith51359922012-06-23 23:55:39 -0700566 if (b == NULL) {
567 /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
568 when EINTR occurs so we needn't do it ourselves. */
569 if (_PyIO_trap_eintr()) {
570 continue;
571 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000572 goto fail;
Gregory P. Smith51359922012-06-23 23:55:39 -0700573 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000574 if (!PyBytes_Check(b)) {
575 PyErr_Format(PyExc_IOError,
576 "read() should have returned a bytes object, "
577 "not '%.200s'", Py_TYPE(b)->tp_name);
578 Py_DECREF(b);
579 goto fail;
580 }
581 if (PyBytes_GET_SIZE(b) == 0) {
582 Py_DECREF(b);
583 break;
584 }
585
586 old_size = PyByteArray_GET_SIZE(buffer);
Victor Stinnercc024d12013-10-29 02:23:46 +0100587 if (PyByteArray_Resize(buffer, old_size + PyBytes_GET_SIZE(b)) < 0) {
588 Py_DECREF(b);
589 goto fail;
590 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000591 memcpy(PyByteArray_AS_STRING(buffer) + old_size,
592 PyBytes_AS_STRING(b), PyBytes_GET_SIZE(b));
593
594 Py_DECREF(b);
595
596 if (PyByteArray_AS_STRING(buffer)[PyByteArray_GET_SIZE(buffer) - 1] == '\n')
597 break;
598 }
599
600 result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(buffer),
601 PyByteArray_GET_SIZE(buffer));
602 Py_DECREF(buffer);
603 return result;
604 fail:
605 Py_DECREF(buffer);
606 return NULL;
607}
608
609static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000610iobase_iter(PyObject *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000611{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000612 if (_PyIOBase_check_closed(self, Py_True) == NULL)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000613 return NULL;
614
615 Py_INCREF(self);
616 return self;
617}
618
619static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000620iobase_iternext(PyObject *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000621{
622 PyObject *line = PyObject_CallMethodObjArgs(self, _PyIO_str_readline, NULL);
623
624 if (line == NULL)
625 return NULL;
626
627 if (PyObject_Size(line) == 0) {
628 Py_DECREF(line);
629 return NULL;
630 }
631
632 return line;
633}
634
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300635/*[clinic input]
636_io._IOBase.readlines
637 hint: io_ssize_t = -1
638 /
639
640Return a list of lines from the stream.
641
642hint can be specified to control the number of lines read: no more
643lines will be read if the total size (in bytes/characters) of all
644lines so far exceeds hint.
645[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000646
647static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300648_io__IOBase_readlines_impl(PyObject *self, Py_ssize_t hint)
649/*[clinic end generated code: output=2f50421677fa3dea input=1961c4a95e96e661]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000650{
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300651 Py_ssize_t length = 0;
Benjamin Peterson05516132009-12-13 19:28:09 +0000652 PyObject *result;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000653
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000654 result = PyList_New(0);
655 if (result == NULL)
656 return NULL;
657
658 if (hint <= 0) {
659 /* XXX special-casing this made sense in the Python version in order
660 to remove the bytecode interpretation overhead, but it could
661 probably be removed here. */
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200662 _Py_IDENTIFIER(extend);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200663 PyObject *ret = _PyObject_CallMethodId(result, &PyId_extend, "O", self);
664
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000665 if (ret == NULL) {
666 Py_DECREF(result);
667 return NULL;
668 }
669 Py_DECREF(ret);
670 return result;
671 }
672
673 while (1) {
674 PyObject *line = PyIter_Next(self);
675 if (line == NULL) {
676 if (PyErr_Occurred()) {
677 Py_DECREF(result);
678 return NULL;
679 }
680 else
681 break; /* StopIteration raised */
682 }
683
684 if (PyList_Append(result, line) < 0) {
685 Py_DECREF(line);
686 Py_DECREF(result);
687 return NULL;
688 }
689 length += PyObject_Size(line);
690 Py_DECREF(line);
691
692 if (length > hint)
693 break;
694 }
695 return result;
696}
697
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300698/*[clinic input]
699_io._IOBase.writelines
700 lines: object
701 /
702[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000703
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300704static PyObject *
705_io__IOBase_writelines(PyObject *self, PyObject *lines)
706/*[clinic end generated code: output=976eb0a9b60a6628 input=432e729a8450b3cb]*/
707{
708 PyObject *iter, *res;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000709
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000710 if (_PyIOBase_check_closed(self, Py_True) == NULL)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000711 return NULL;
712
713 iter = PyObject_GetIter(lines);
714 if (iter == NULL)
715 return NULL;
716
717 while (1) {
718 PyObject *line = PyIter_Next(iter);
719 if (line == NULL) {
720 if (PyErr_Occurred()) {
721 Py_DECREF(iter);
722 return NULL;
723 }
724 else
725 break; /* Stop Iteration */
726 }
727
Gregory P. Smithb9817b02013-02-01 13:03:39 -0800728 res = NULL;
729 do {
730 res = PyObject_CallMethodObjArgs(self, _PyIO_str_write, line, NULL);
731 } while (res == NULL && _PyIO_trap_eintr());
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000732 Py_DECREF(line);
733 if (res == NULL) {
734 Py_DECREF(iter);
735 return NULL;
736 }
737 Py_DECREF(res);
738 }
739 Py_DECREF(iter);
740 Py_RETURN_NONE;
741}
742
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300743#include "clinic/iobase.c.h"
744
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000745static PyMethodDef iobase_methods[] = {
746 {"seek", iobase_seek, METH_VARARGS, iobase_seek_doc},
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300747 _IO__IOBASE_TELL_METHODDEF
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000748 {"truncate", iobase_truncate, METH_VARARGS, iobase_truncate_doc},
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300749 _IO__IOBASE_FLUSH_METHODDEF
750 _IO__IOBASE_CLOSE_METHODDEF
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000751
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300752 _IO__IOBASE_SEEKABLE_METHODDEF
753 _IO__IOBASE_READABLE_METHODDEF
754 _IO__IOBASE_WRITABLE_METHODDEF
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000755
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000756 {"_checkClosed", _PyIOBase_check_closed, METH_NOARGS},
757 {"_checkSeekable", _PyIOBase_check_seekable, METH_NOARGS},
758 {"_checkReadable", _PyIOBase_check_readable, METH_NOARGS},
759 {"_checkWritable", _PyIOBase_check_writable, METH_NOARGS},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000760
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300761 _IO__IOBASE_FILENO_METHODDEF
762 _IO__IOBASE_ISATTY_METHODDEF
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000763
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000764 {"__enter__", iobase_enter, METH_NOARGS},
765 {"__exit__", iobase_exit, METH_VARARGS},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000766
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300767 _IO__IOBASE_READLINE_METHODDEF
768 _IO__IOBASE_READLINES_METHODDEF
769 _IO__IOBASE_WRITELINES_METHODDEF
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000770
771 {NULL, NULL}
772};
773
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000774static PyGetSetDef iobase_getset[] = {
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500775 {"__dict__", PyObject_GenericGetDict, NULL, NULL},
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000776 {"closed", (getter)iobase_closed_get, NULL, NULL},
Benjamin Peterson1fea3212009-04-19 03:15:20 +0000777 {NULL}
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000778};
779
780
781PyTypeObject PyIOBase_Type = {
782 PyVarObject_HEAD_INIT(NULL, 0)
783 "_io._IOBase", /*tp_name*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000784 sizeof(iobase), /*tp_basicsize*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000785 0, /*tp_itemsize*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000786 (destructor)iobase_dealloc, /*tp_dealloc*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000787 0, /*tp_print*/
788 0, /*tp_getattr*/
789 0, /*tp_setattr*/
790 0, /*tp_compare */
791 0, /*tp_repr*/
792 0, /*tp_as_number*/
793 0, /*tp_as_sequence*/
794 0, /*tp_as_mapping*/
795 0, /*tp_hash */
796 0, /*tp_call*/
797 0, /*tp_str*/
798 0, /*tp_getattro*/
799 0, /*tp_setattro*/
800 0, /*tp_as_buffer*/
801 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
Antoine Pitrou796564c2013-07-30 19:59:21 +0200802 | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /*tp_flags*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000803 iobase_doc, /* tp_doc */
804 (traverseproc)iobase_traverse, /* tp_traverse */
805 (inquiry)iobase_clear, /* tp_clear */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000806 0, /* tp_richcompare */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000807 offsetof(iobase, weakreflist), /* tp_weaklistoffset */
808 iobase_iter, /* tp_iter */
809 iobase_iternext, /* tp_iternext */
810 iobase_methods, /* tp_methods */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000811 0, /* tp_members */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000812 iobase_getset, /* tp_getset */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000813 0, /* tp_base */
814 0, /* tp_dict */
815 0, /* tp_descr_get */
816 0, /* tp_descr_set */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000817 offsetof(iobase, dict), /* tp_dictoffset */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000818 0, /* tp_init */
819 0, /* tp_alloc */
820 PyType_GenericNew, /* tp_new */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200821 0, /* tp_free */
822 0, /* tp_is_gc */
823 0, /* tp_bases */
824 0, /* tp_mro */
825 0, /* tp_cache */
826 0, /* tp_subclasses */
827 0, /* tp_weaklist */
828 0, /* tp_del */
829 0, /* tp_version_tag */
830 (destructor)iobase_finalize, /* tp_finalize */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000831};
832
833
834/*
835 * RawIOBase class, Inherits from IOBase.
836 */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000837PyDoc_STRVAR(rawiobase_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000838 "Base class for raw binary I/O.");
839
840/*
841 * The read() method is implemented by calling readinto(); derived classes
842 * that want to support read() only need to implement readinto() as a
843 * primitive operation. In general, readinto() can be more efficient than
844 * read().
845 *
846 * (It would be tempting to also provide an implementation of readinto() in
847 * terms of read(), in case the latter is a more suitable primitive operation,
848 * but that would lead to nasty recursion in case a subclass doesn't implement
849 * either.)
850*/
851
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300852/*[clinic input]
853_io._RawIOBase.read
854 size as n: Py_ssize_t = -1
855 /
856[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000857
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300858static PyObject *
859_io__RawIOBase_read_impl(PyObject *self, Py_ssize_t n)
860/*[clinic end generated code: output=6cdeb731e3c9f13c input=b6d0dcf6417d1374]*/
861{
862 PyObject *b, *res;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000863
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200864 if (n < 0) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200865 _Py_IDENTIFIER(readall);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200866
867 return _PyObject_CallMethodId(self, &PyId_readall, NULL);
868 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000869
870 /* TODO: allocate a bytes object directly instead and manually construct
871 a writable memoryview pointing to it. */
872 b = PyByteArray_FromStringAndSize(NULL, n);
873 if (b == NULL)
874 return NULL;
875
876 res = PyObject_CallMethodObjArgs(self, _PyIO_str_readinto, b, NULL);
Antoine Pitrou328ec742010-09-14 18:37:24 +0000877 if (res == NULL || res == Py_None) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000878 Py_DECREF(b);
Antoine Pitrou328ec742010-09-14 18:37:24 +0000879 return res;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000880 }
881
882 n = PyNumber_AsSsize_t(res, PyExc_ValueError);
883 Py_DECREF(res);
884 if (n == -1 && PyErr_Occurred()) {
885 Py_DECREF(b);
886 return NULL;
887 }
888
889 res = PyBytes_FromStringAndSize(PyByteArray_AsString(b), n);
890 Py_DECREF(b);
891 return res;
892}
893
894
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300895/*[clinic input]
896_io._RawIOBase.readall
897
898Read until EOF, using multiple read() call.
899[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000900
901static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300902_io__RawIOBase_readall_impl(PyObject *self)
903/*[clinic end generated code: output=1987b9ce929425a0 input=688874141213622a]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000904{
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000905 int r;
906 PyObject *chunks = PyList_New(0);
907 PyObject *result;
Victor Stinnercc024d12013-10-29 02:23:46 +0100908
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000909 if (chunks == NULL)
910 return NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000911
912 while (1) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200913 PyObject *data = _PyObject_CallMethodId(self, &PyId_read,
914 "i", DEFAULT_BUFFER_SIZE);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000915 if (!data) {
Gregory P. Smith51359922012-06-23 23:55:39 -0700916 /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
917 when EINTR occurs so we needn't do it ourselves. */
918 if (_PyIO_trap_eintr()) {
919 continue;
920 }
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000921 Py_DECREF(chunks);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000922 return NULL;
923 }
Victor Stinnera80987f2011-05-25 22:47:16 +0200924 if (data == Py_None) {
925 if (PyList_GET_SIZE(chunks) == 0) {
926 Py_DECREF(chunks);
927 return data;
928 }
929 Py_DECREF(data);
930 break;
931 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000932 if (!PyBytes_Check(data)) {
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000933 Py_DECREF(chunks);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000934 Py_DECREF(data);
935 PyErr_SetString(PyExc_TypeError, "read() should return bytes");
936 return NULL;
937 }
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000938 if (PyBytes_GET_SIZE(data) == 0) {
939 /* EOF */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000940 Py_DECREF(data);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000941 break;
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000942 }
943 r = PyList_Append(chunks, data);
944 Py_DECREF(data);
945 if (r < 0) {
946 Py_DECREF(chunks);
947 return NULL;
948 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000949 }
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000950 result = _PyBytes_Join(_PyIO_empty_bytes, chunks);
951 Py_DECREF(chunks);
952 return result;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000953}
954
Antoine Pitrou45d61562015-05-20 21:50:59 +0200955static PyObject *
956rawiobase_readinto(PyObject *self, PyObject *args)
957{
958 PyErr_SetNone(PyExc_NotImplementedError);
959 return NULL;
960}
961
962static PyObject *
963rawiobase_write(PyObject *self, PyObject *args)
964{
965 PyErr_SetNone(PyExc_NotImplementedError);
966 return NULL;
967}
968
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000969static PyMethodDef rawiobase_methods[] = {
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300970 _IO__RAWIOBASE_READ_METHODDEF
971 _IO__RAWIOBASE_READALL_METHODDEF
Antoine Pitrou45d61562015-05-20 21:50:59 +0200972 {"readinto", rawiobase_readinto, METH_VARARGS},
973 {"write", rawiobase_write, METH_VARARGS},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000974 {NULL, NULL}
975};
976
977PyTypeObject PyRawIOBase_Type = {
978 PyVarObject_HEAD_INIT(NULL, 0)
979 "_io._RawIOBase", /*tp_name*/
980 0, /*tp_basicsize*/
981 0, /*tp_itemsize*/
982 0, /*tp_dealloc*/
983 0, /*tp_print*/
984 0, /*tp_getattr*/
985 0, /*tp_setattr*/
986 0, /*tp_compare */
987 0, /*tp_repr*/
988 0, /*tp_as_number*/
989 0, /*tp_as_sequence*/
990 0, /*tp_as_mapping*/
991 0, /*tp_hash */
992 0, /*tp_call*/
993 0, /*tp_str*/
994 0, /*tp_getattro*/
995 0, /*tp_setattro*/
996 0, /*tp_as_buffer*/
Antoine Pitrou796564c2013-07-30 19:59:21 +0200997 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_FINALIZE, /*tp_flags*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000998 rawiobase_doc, /* tp_doc */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000999 0, /* tp_traverse */
1000 0, /* tp_clear */
1001 0, /* tp_richcompare */
1002 0, /* tp_weaklistoffset */
1003 0, /* tp_iter */
1004 0, /* tp_iternext */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001005 rawiobase_methods, /* tp_methods */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001006 0, /* tp_members */
1007 0, /* tp_getset */
1008 &PyIOBase_Type, /* tp_base */
1009 0, /* tp_dict */
1010 0, /* tp_descr_get */
1011 0, /* tp_descr_set */
1012 0, /* tp_dictoffset */
1013 0, /* tp_init */
1014 0, /* tp_alloc */
1015 0, /* tp_new */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001016 0, /* tp_free */
1017 0, /* tp_is_gc */
1018 0, /* tp_bases */
1019 0, /* tp_mro */
1020 0, /* tp_cache */
1021 0, /* tp_subclasses */
1022 0, /* tp_weaklist */
1023 0, /* tp_del */
1024 0, /* tp_version_tag */
1025 0, /* tp_finalize */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001026};