blob: ef06b43ca6e8b2760677dd987ba06fd69626266b [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
16/*
17 * IOBase class, an abstract class
18 */
19
20typedef struct {
21 PyObject_HEAD
Victor Stinnercc024d12013-10-29 02:23:46 +010022
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000023 PyObject *dict;
24 PyObject *weakreflist;
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000025} iobase;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000026
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000027PyDoc_STRVAR(iobase_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000028 "The abstract base class for all I/O classes, acting on streams of\n"
29 "bytes. There is no public constructor.\n"
30 "\n"
31 "This class provides dummy implementations for many methods that\n"
32 "derived classes can override selectively; the default implementations\n"
33 "represent a file that cannot be read, written or seeked.\n"
34 "\n"
35 "Even though IOBase does not declare read, readinto, or write because\n"
36 "their signatures will vary, implementations and clients should\n"
37 "consider those methods part of the interface. Also, implementations\n"
Amaury Forgeot d'Arc616453c2010-09-06 22:31:52 +000038 "may raise UnsupportedOperation when operations they do not support are\n"
39 "called.\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000040 "\n"
41 "The basic type used for binary data read from or written to a file is\n"
42 "bytes. bytearrays are accepted too, and in some cases (such as\n"
43 "readinto) needed. Text I/O classes work with str data.\n"
44 "\n"
Andrew Kuchling76466202014-04-15 21:11:36 -040045 "Note that calling any method (except additional calls to close(),\n"
46 "which are ignored) on a closed stream should raise a ValueError.\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000047 "\n"
48 "IOBase (and its subclasses) support the iterator protocol, meaning\n"
49 "that an IOBase object can be iterated over yielding the lines in a\n"
50 "stream.\n"
51 "\n"
52 "IOBase also supports the :keyword:`with` statement. In this example,\n"
Ezio Melotti13925002011-03-16 11:05:33 +020053 "fp is closed after the suite of the with statement is complete:\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000054 "\n"
55 "with open('spam.txt', 'r') as fp:\n"
56 " fp.write('Spam and eggs!')\n");
57
58/* Use this macro whenever you want to check the internal `closed` status
59 of the IOBase object rather than the virtual `closed` attribute as returned
60 by whatever subclass. */
61
Martin v. Löwis767046a2011-10-14 15:35:36 +020062_Py_IDENTIFIER(__IOBase_closed);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000063#define IS_CLOSED(self) \
Martin v. Löwis767046a2011-10-14 15:35:36 +020064 _PyObject_HasAttrId(self, &PyId___IOBase_closed)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000065
Victor Stinner3f36a572013-11-12 21:39:02 +010066_Py_IDENTIFIER(read);
67
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000068/* Internal methods */
69static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000070iobase_unsupported(const char *message)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000071{
Antoine Pitrou712cb732013-12-21 15:51:54 +010072 _PyIO_State *state = IO_STATE();
73 if (state != NULL)
74 PyErr_SetString(state->unsupported_operation, message);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000075 return NULL;
76}
77
78/* Positionning */
79
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000080PyDoc_STRVAR(iobase_seek_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000081 "Change stream position.\n"
82 "\n"
Terry Jan Reedy0158af32013-03-11 17:42:46 -040083 "Change the stream position to the given byte offset. The offset is\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000084 "interpreted relative to the position indicated by whence. Values\n"
85 "for whence are:\n"
86 "\n"
87 "* 0 -- start of stream (the default); offset should be zero or positive\n"
88 "* 1 -- current stream position; offset may be negative\n"
89 "* 2 -- end of stream; offset is usually negative\n"
90 "\n"
91 "Return the new absolute position.");
92
93static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000094iobase_seek(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000095{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000096 return iobase_unsupported("seek");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000097}
98
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000099PyDoc_STRVAR(iobase_tell_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000100 "Return current stream position.");
101
102static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000103iobase_tell(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000104{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200105 _Py_IDENTIFIER(seek);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200106
107 return _PyObject_CallMethodId(self, &PyId_seek, "ii", 0, 1);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000108}
109
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000110PyDoc_STRVAR(iobase_truncate_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000111 "Truncate file to size bytes.\n"
112 "\n"
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000113 "File pointer is left unchanged. Size defaults to the current IO\n"
114 "position as reported by tell(). Returns the new size.");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000115
116static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000117iobase_truncate(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000118{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000119 return iobase_unsupported("truncate");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000120}
121
122/* Flush and close methods */
123
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000124PyDoc_STRVAR(iobase_flush_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000125 "Flush write buffers, if applicable.\n"
126 "\n"
127 "This is not implemented for read-only and non-blocking streams.\n");
128
129static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000130iobase_flush(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000131{
132 /* XXX Should this return the number of bytes written??? */
133 if (IS_CLOSED(self)) {
134 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file.");
135 return NULL;
136 }
137 Py_RETURN_NONE;
138}
139
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000140PyDoc_STRVAR(iobase_close_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000141 "Flush and close the IO object.\n"
142 "\n"
143 "This method has no effect if the file is already closed.\n");
144
145static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000146iobase_closed(PyObject *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000147{
148 PyObject *res;
149 int closed;
150 /* This gets the derived attribute, which is *not* __IOBase_closed
151 in most cases! */
152 res = PyObject_GetAttr(self, _PyIO_str_closed);
153 if (res == NULL)
154 return 0;
155 closed = PyObject_IsTrue(res);
156 Py_DECREF(res);
157 return closed;
158}
159
160static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000161iobase_closed_get(PyObject *self, void *context)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000162{
163 return PyBool_FromLong(IS_CLOSED(self));
164}
165
166PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000167_PyIOBase_check_closed(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000168{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000169 if (iobase_closed(self)) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000170 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file.");
171 return NULL;
172 }
173 if (args == Py_True)
174 return Py_None;
175 else
176 Py_RETURN_NONE;
177}
178
179/* XXX: IOBase thinks it has to maintain its own internal state in
180 `__IOBase_closed` and call flush() by itself, but it is redundant with
181 whatever behaviour a non-trivial derived class will implement. */
182
183static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000184iobase_close(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000185{
186 PyObject *res;
187
188 if (IS_CLOSED(self))
189 Py_RETURN_NONE;
190
191 res = PyObject_CallMethodObjArgs(self, _PyIO_str_flush, NULL);
Victor Stinneraa5bbfa2013-11-08 00:29:41 +0100192
193 if (_PyObject_SetAttrId(self, &PyId___IOBase_closed, Py_True) < 0) {
194 Py_XDECREF(res);
Antoine Pitrou6be88762010-05-03 16:48:20 +0000195 return NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000196 }
Victor Stinneraa5bbfa2013-11-08 00:29:41 +0100197
198 if (res == NULL)
199 return NULL;
200
201 Py_DECREF(res);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000202 Py_RETURN_NONE;
203}
204
205/* Finalization and garbage collection support */
206
Antoine Pitrou796564c2013-07-30 19:59:21 +0200207static void
208iobase_finalize(PyObject *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000209{
210 PyObject *res;
Antoine Pitrou796564c2013-07-30 19:59:21 +0200211 PyObject *error_type, *error_value, *error_traceback;
212 int closed;
213 _Py_IDENTIFIER(_finalizing);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000214
Antoine Pitrou796564c2013-07-30 19:59:21 +0200215 /* Save the current exception, if any. */
216 PyErr_Fetch(&error_type, &error_value, &error_traceback);
217
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000218 /* If `closed` doesn't exist or can't be evaluated as bool, then the
219 object is probably in an unusable state, so ignore. */
220 res = PyObject_GetAttr(self, _PyIO_str_closed);
Christian Heimes72f455e2013-07-31 01:33:50 +0200221 if (res == NULL) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000222 PyErr_Clear();
Christian Heimes72f455e2013-07-31 01:33:50 +0200223 closed = -1;
224 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000225 else {
226 closed = PyObject_IsTrue(res);
227 Py_DECREF(res);
228 if (closed == -1)
229 PyErr_Clear();
230 }
231 if (closed == 0) {
Antoine Pitrou796564c2013-07-30 19:59:21 +0200232 /* Signal close() that it was called as part of the object
233 finalization process. */
234 if (_PyObject_SetAttrId(self, &PyId__finalizing, Py_True))
235 PyErr_Clear();
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000236 res = PyObject_CallMethodObjArgs((PyObject *) self, _PyIO_str_close,
237 NULL);
238 /* Silencing I/O errors is bad, but printing spurious tracebacks is
239 equally as bad, and potentially more frequent (because of
240 shutdown issues). */
241 if (res == NULL)
242 PyErr_Clear();
243 else
244 Py_DECREF(res);
245 }
Antoine Pitrou796564c2013-07-30 19:59:21 +0200246
247 /* Restore the saved exception. */
248 PyErr_Restore(error_type, error_value, error_traceback);
249}
250
251int
252_PyIOBase_finalize(PyObject *self)
253{
254 int is_zombie;
255
256 /* If _PyIOBase_finalize() is called from a destructor, we need to
257 resurrect the object as calling close() can invoke arbitrary code. */
258 is_zombie = (Py_REFCNT(self) == 0);
259 if (is_zombie)
260 return PyObject_CallFinalizerFromDealloc(self);
261 else {
262 PyObject_CallFinalizer(self);
263 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000264 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000265}
266
267static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000268iobase_traverse(iobase *self, visitproc visit, void *arg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000269{
270 Py_VISIT(self->dict);
271 return 0;
272}
273
274static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000275iobase_clear(iobase *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000276{
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000277 Py_CLEAR(self->dict);
278 return 0;
279}
280
281/* Destructor */
282
283static void
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000284iobase_dealloc(iobase *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000285{
286 /* NOTE: since IOBaseObject has its own dict, Python-defined attributes
287 are still available here for close() to use.
288 However, if the derived class declares a __slots__, those slots are
289 already gone.
290 */
291 if (_PyIOBase_finalize((PyObject *) self) < 0) {
292 /* When called from a heap type's dealloc, the type will be
293 decref'ed on return (see e.g. subtype_dealloc in typeobject.c). */
294 if (PyType_HasFeature(Py_TYPE(self), Py_TPFLAGS_HEAPTYPE))
295 Py_INCREF(Py_TYPE(self));
296 return;
297 }
298 _PyObject_GC_UNTRACK(self);
299 if (self->weakreflist != NULL)
300 PyObject_ClearWeakRefs((PyObject *) self);
301 Py_CLEAR(self->dict);
302 Py_TYPE(self)->tp_free((PyObject *) self);
303}
304
305/* Inquiry methods */
306
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000307PyDoc_STRVAR(iobase_seekable_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000308 "Return whether object supports random access.\n"
309 "\n"
Amaury Forgeot d'Arc616453c2010-09-06 22:31:52 +0000310 "If False, seek(), tell() and truncate() will raise UnsupportedOperation.\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000311 "This method may need to do a test seek().");
312
313static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000314iobase_seekable(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000315{
316 Py_RETURN_FALSE;
317}
318
319PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000320_PyIOBase_check_seekable(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000321{
322 PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_seekable, NULL);
323 if (res == NULL)
324 return NULL;
325 if (res != Py_True) {
326 Py_CLEAR(res);
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000327 iobase_unsupported("File or stream is not seekable.");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000328 return NULL;
329 }
330 if (args == Py_True) {
331 Py_DECREF(res);
332 }
333 return res;
334}
335
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000336PyDoc_STRVAR(iobase_readable_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000337 "Return whether object was opened for reading.\n"
338 "\n"
Amaury Forgeot d'Arc616453c2010-09-06 22:31:52 +0000339 "If False, read() will raise UnsupportedOperation.");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000340
341static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000342iobase_readable(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000343{
344 Py_RETURN_FALSE;
345}
346
347/* May be called with any object */
348PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000349_PyIOBase_check_readable(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000350{
351 PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_readable, NULL);
352 if (res == NULL)
353 return NULL;
354 if (res != Py_True) {
355 Py_CLEAR(res);
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000356 iobase_unsupported("File or stream is not readable.");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000357 return NULL;
358 }
359 if (args == Py_True) {
360 Py_DECREF(res);
361 }
362 return res;
363}
364
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000365PyDoc_STRVAR(iobase_writable_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000366 "Return whether object was opened for writing.\n"
367 "\n"
Amaury Forgeot d'Arc616453c2010-09-06 22:31:52 +0000368 "If False, write() will raise UnsupportedOperation.");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000369
370static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000371iobase_writable(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000372{
373 Py_RETURN_FALSE;
374}
375
376/* May be called with any object */
377PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000378_PyIOBase_check_writable(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000379{
380 PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_writable, NULL);
381 if (res == NULL)
382 return NULL;
383 if (res != Py_True) {
384 Py_CLEAR(res);
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000385 iobase_unsupported("File or stream is not writable.");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000386 return NULL;
387 }
388 if (args == Py_True) {
389 Py_DECREF(res);
390 }
391 return res;
392}
393
394/* Context manager */
395
396static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000397iobase_enter(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000398{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000399 if (_PyIOBase_check_closed(self, Py_True) == NULL)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000400 return NULL;
401
402 Py_INCREF(self);
403 return self;
404}
405
406static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000407iobase_exit(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000408{
409 return PyObject_CallMethodObjArgs(self, _PyIO_str_close, NULL);
410}
411
412/* Lower-level APIs */
413
414/* XXX Should these be present even if unimplemented? */
415
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000416PyDoc_STRVAR(iobase_fileno_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000417 "Returns underlying file descriptor if one exists.\n"
418 "\n"
419 "An IOError is raised if the IO object does not use a file descriptor.\n");
420
421static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000422iobase_fileno(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000423{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000424 return iobase_unsupported("fileno");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000425}
426
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000427PyDoc_STRVAR(iobase_isatty_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000428 "Return whether this is an 'interactive' stream.\n"
429 "\n"
430 "Return False if it can't be determined.\n");
431
432static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000433iobase_isatty(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000434{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000435 if (_PyIOBase_check_closed(self, Py_True) == NULL)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000436 return NULL;
437 Py_RETURN_FALSE;
438}
439
440/* Readline(s) and writelines */
441
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000442PyDoc_STRVAR(iobase_readline_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000443 "Read and return a line from the stream.\n"
444 "\n"
445 "If limit is specified, at most limit bytes will be read.\n"
446 "\n"
Ezio Melotti16d2b472012-09-18 07:20:18 +0300447 "The line terminator is always b'\\n' for binary files; for text\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000448 "files, the newlines argument to open can be used to select the line\n"
449 "terminator(s) recognized.\n");
450
451static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000452iobase_readline(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000453{
454 /* For backwards compatibility, a (slowish) readline(). */
455
456 Py_ssize_t limit = -1;
457 int has_peek = 0;
458 PyObject *buffer, *result;
459 Py_ssize_t old_size = -1;
Martin v. Löwis767046a2011-10-14 15:35:36 +0200460 _Py_IDENTIFIER(peek);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000461
Benjamin Petersonbf5ff762009-12-13 19:25:34 +0000462 if (!PyArg_ParseTuple(args, "|O&:readline", &_PyIO_ConvertSsize_t, &limit)) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000463 return NULL;
464 }
465
Martin v. Löwis767046a2011-10-14 15:35:36 +0200466 if (_PyObject_HasAttrId(self, &PyId_peek))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000467 has_peek = 1;
468
469 buffer = PyByteArray_FromStringAndSize(NULL, 0);
470 if (buffer == NULL)
471 return NULL;
472
473 while (limit < 0 || Py_SIZE(buffer) < limit) {
474 Py_ssize_t nreadahead = 1;
475 PyObject *b;
476
477 if (has_peek) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200478 PyObject *readahead = _PyObject_CallMethodId(self, &PyId_peek, "i", 1);
Gregory P. Smith51359922012-06-23 23:55:39 -0700479 if (readahead == NULL) {
480 /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
481 when EINTR occurs so we needn't do it ourselves. */
482 if (_PyIO_trap_eintr()) {
483 continue;
484 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000485 goto fail;
Gregory P. Smith51359922012-06-23 23:55:39 -0700486 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000487 if (!PyBytes_Check(readahead)) {
488 PyErr_Format(PyExc_IOError,
489 "peek() should have returned a bytes object, "
490 "not '%.200s'", Py_TYPE(readahead)->tp_name);
491 Py_DECREF(readahead);
492 goto fail;
493 }
494 if (PyBytes_GET_SIZE(readahead) > 0) {
495 Py_ssize_t n = 0;
496 const char *buf = PyBytes_AS_STRING(readahead);
497 if (limit >= 0) {
498 do {
499 if (n >= PyBytes_GET_SIZE(readahead) || n >= limit)
500 break;
501 if (buf[n++] == '\n')
502 break;
503 } while (1);
504 }
505 else {
506 do {
507 if (n >= PyBytes_GET_SIZE(readahead))
508 break;
509 if (buf[n++] == '\n')
510 break;
511 } while (1);
512 }
513 nreadahead = n;
514 }
515 Py_DECREF(readahead);
516 }
517
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200518 b = _PyObject_CallMethodId(self, &PyId_read, "n", nreadahead);
Gregory P. Smith51359922012-06-23 23:55:39 -0700519 if (b == NULL) {
520 /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
521 when EINTR occurs so we needn't do it ourselves. */
522 if (_PyIO_trap_eintr()) {
523 continue;
524 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000525 goto fail;
Gregory P. Smith51359922012-06-23 23:55:39 -0700526 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000527 if (!PyBytes_Check(b)) {
528 PyErr_Format(PyExc_IOError,
529 "read() should have returned a bytes object, "
530 "not '%.200s'", Py_TYPE(b)->tp_name);
531 Py_DECREF(b);
532 goto fail;
533 }
534 if (PyBytes_GET_SIZE(b) == 0) {
535 Py_DECREF(b);
536 break;
537 }
538
539 old_size = PyByteArray_GET_SIZE(buffer);
Victor Stinnercc024d12013-10-29 02:23:46 +0100540 if (PyByteArray_Resize(buffer, old_size + PyBytes_GET_SIZE(b)) < 0) {
541 Py_DECREF(b);
542 goto fail;
543 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000544 memcpy(PyByteArray_AS_STRING(buffer) + old_size,
545 PyBytes_AS_STRING(b), PyBytes_GET_SIZE(b));
546
547 Py_DECREF(b);
548
549 if (PyByteArray_AS_STRING(buffer)[PyByteArray_GET_SIZE(buffer) - 1] == '\n')
550 break;
551 }
552
553 result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(buffer),
554 PyByteArray_GET_SIZE(buffer));
555 Py_DECREF(buffer);
556 return result;
557 fail:
558 Py_DECREF(buffer);
559 return NULL;
560}
561
562static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000563iobase_iter(PyObject *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000564{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000565 if (_PyIOBase_check_closed(self, Py_True) == NULL)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000566 return NULL;
567
568 Py_INCREF(self);
569 return self;
570}
571
572static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000573iobase_iternext(PyObject *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000574{
575 PyObject *line = PyObject_CallMethodObjArgs(self, _PyIO_str_readline, NULL);
576
577 if (line == NULL)
578 return NULL;
579
580 if (PyObject_Size(line) == 0) {
581 Py_DECREF(line);
582 return NULL;
583 }
584
585 return line;
586}
587
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000588PyDoc_STRVAR(iobase_readlines_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000589 "Return a list of lines from the stream.\n"
590 "\n"
591 "hint can be specified to control the number of lines read: no more\n"
592 "lines will be read if the total size (in bytes/characters) of all\n"
593 "lines so far exceeds hint.");
594
595static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000596iobase_readlines(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000597{
598 Py_ssize_t hint = -1, length = 0;
Benjamin Peterson05516132009-12-13 19:28:09 +0000599 PyObject *result;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000600
Benjamin Petersonbf5ff762009-12-13 19:25:34 +0000601 if (!PyArg_ParseTuple(args, "|O&:readlines", &_PyIO_ConvertSsize_t, &hint)) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000602 return NULL;
603 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000604
605 result = PyList_New(0);
606 if (result == NULL)
607 return NULL;
608
609 if (hint <= 0) {
610 /* XXX special-casing this made sense in the Python version in order
611 to remove the bytecode interpretation overhead, but it could
612 probably be removed here. */
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200613 _Py_IDENTIFIER(extend);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200614 PyObject *ret = _PyObject_CallMethodId(result, &PyId_extend, "O", self);
615
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000616 if (ret == NULL) {
617 Py_DECREF(result);
618 return NULL;
619 }
620 Py_DECREF(ret);
621 return result;
622 }
623
624 while (1) {
625 PyObject *line = PyIter_Next(self);
626 if (line == NULL) {
627 if (PyErr_Occurred()) {
628 Py_DECREF(result);
629 return NULL;
630 }
631 else
632 break; /* StopIteration raised */
633 }
634
635 if (PyList_Append(result, line) < 0) {
636 Py_DECREF(line);
637 Py_DECREF(result);
638 return NULL;
639 }
640 length += PyObject_Size(line);
641 Py_DECREF(line);
642
643 if (length > hint)
644 break;
645 }
646 return result;
647}
648
649static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000650iobase_writelines(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000651{
652 PyObject *lines, *iter, *res;
653
654 if (!PyArg_ParseTuple(args, "O:writelines", &lines)) {
655 return NULL;
656 }
657
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000658 if (_PyIOBase_check_closed(self, Py_True) == NULL)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000659 return NULL;
660
661 iter = PyObject_GetIter(lines);
662 if (iter == NULL)
663 return NULL;
664
665 while (1) {
666 PyObject *line = PyIter_Next(iter);
667 if (line == NULL) {
668 if (PyErr_Occurred()) {
669 Py_DECREF(iter);
670 return NULL;
671 }
672 else
673 break; /* Stop Iteration */
674 }
675
Gregory P. Smithb9817b02013-02-01 13:03:39 -0800676 res = NULL;
677 do {
678 res = PyObject_CallMethodObjArgs(self, _PyIO_str_write, line, NULL);
679 } while (res == NULL && _PyIO_trap_eintr());
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000680 Py_DECREF(line);
681 if (res == NULL) {
682 Py_DECREF(iter);
683 return NULL;
684 }
685 Py_DECREF(res);
686 }
687 Py_DECREF(iter);
688 Py_RETURN_NONE;
689}
690
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000691static PyMethodDef iobase_methods[] = {
692 {"seek", iobase_seek, METH_VARARGS, iobase_seek_doc},
693 {"tell", iobase_tell, METH_NOARGS, iobase_tell_doc},
694 {"truncate", iobase_truncate, METH_VARARGS, iobase_truncate_doc},
695 {"flush", iobase_flush, METH_NOARGS, iobase_flush_doc},
696 {"close", iobase_close, METH_NOARGS, iobase_close_doc},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000697
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000698 {"seekable", iobase_seekable, METH_NOARGS, iobase_seekable_doc},
699 {"readable", iobase_readable, METH_NOARGS, iobase_readable_doc},
700 {"writable", iobase_writable, METH_NOARGS, iobase_writable_doc},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000701
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000702 {"_checkClosed", _PyIOBase_check_closed, METH_NOARGS},
703 {"_checkSeekable", _PyIOBase_check_seekable, METH_NOARGS},
704 {"_checkReadable", _PyIOBase_check_readable, METH_NOARGS},
705 {"_checkWritable", _PyIOBase_check_writable, METH_NOARGS},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000706
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000707 {"fileno", iobase_fileno, METH_NOARGS, iobase_fileno_doc},
708 {"isatty", iobase_isatty, METH_NOARGS, iobase_isatty_doc},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000709
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000710 {"__enter__", iobase_enter, METH_NOARGS},
711 {"__exit__", iobase_exit, METH_VARARGS},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000712
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000713 {"readline", iobase_readline, METH_VARARGS, iobase_readline_doc},
714 {"readlines", iobase_readlines, METH_VARARGS, iobase_readlines_doc},
715 {"writelines", iobase_writelines, METH_VARARGS},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000716
717 {NULL, NULL}
718};
719
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000720static PyGetSetDef iobase_getset[] = {
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500721 {"__dict__", PyObject_GenericGetDict, NULL, NULL},
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000722 {"closed", (getter)iobase_closed_get, NULL, NULL},
Benjamin Peterson1fea3212009-04-19 03:15:20 +0000723 {NULL}
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000724};
725
726
727PyTypeObject PyIOBase_Type = {
728 PyVarObject_HEAD_INIT(NULL, 0)
729 "_io._IOBase", /*tp_name*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000730 sizeof(iobase), /*tp_basicsize*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000731 0, /*tp_itemsize*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000732 (destructor)iobase_dealloc, /*tp_dealloc*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000733 0, /*tp_print*/
734 0, /*tp_getattr*/
735 0, /*tp_setattr*/
736 0, /*tp_compare */
737 0, /*tp_repr*/
738 0, /*tp_as_number*/
739 0, /*tp_as_sequence*/
740 0, /*tp_as_mapping*/
741 0, /*tp_hash */
742 0, /*tp_call*/
743 0, /*tp_str*/
744 0, /*tp_getattro*/
745 0, /*tp_setattro*/
746 0, /*tp_as_buffer*/
747 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
Antoine Pitrou796564c2013-07-30 19:59:21 +0200748 | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /*tp_flags*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000749 iobase_doc, /* tp_doc */
750 (traverseproc)iobase_traverse, /* tp_traverse */
751 (inquiry)iobase_clear, /* tp_clear */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000752 0, /* tp_richcompare */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000753 offsetof(iobase, weakreflist), /* tp_weaklistoffset */
754 iobase_iter, /* tp_iter */
755 iobase_iternext, /* tp_iternext */
756 iobase_methods, /* tp_methods */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000757 0, /* tp_members */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000758 iobase_getset, /* tp_getset */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000759 0, /* tp_base */
760 0, /* tp_dict */
761 0, /* tp_descr_get */
762 0, /* tp_descr_set */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000763 offsetof(iobase, dict), /* tp_dictoffset */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000764 0, /* tp_init */
765 0, /* tp_alloc */
766 PyType_GenericNew, /* tp_new */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200767 0, /* tp_free */
768 0, /* tp_is_gc */
769 0, /* tp_bases */
770 0, /* tp_mro */
771 0, /* tp_cache */
772 0, /* tp_subclasses */
773 0, /* tp_weaklist */
774 0, /* tp_del */
775 0, /* tp_version_tag */
776 (destructor)iobase_finalize, /* tp_finalize */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000777};
778
779
780/*
781 * RawIOBase class, Inherits from IOBase.
782 */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000783PyDoc_STRVAR(rawiobase_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000784 "Base class for raw binary I/O.");
785
786/*
787 * The read() method is implemented by calling readinto(); derived classes
788 * that want to support read() only need to implement readinto() as a
789 * primitive operation. In general, readinto() can be more efficient than
790 * read().
791 *
792 * (It would be tempting to also provide an implementation of readinto() in
793 * terms of read(), in case the latter is a more suitable primitive operation,
794 * but that would lead to nasty recursion in case a subclass doesn't implement
795 * either.)
796*/
797
798static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000799rawiobase_read(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000800{
801 Py_ssize_t n = -1;
802 PyObject *b, *res;
803
804 if (!PyArg_ParseTuple(args, "|n:read", &n)) {
805 return NULL;
806 }
807
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200808 if (n < 0) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200809 _Py_IDENTIFIER(readall);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200810
811 return _PyObject_CallMethodId(self, &PyId_readall, NULL);
812 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000813
814 /* TODO: allocate a bytes object directly instead and manually construct
815 a writable memoryview pointing to it. */
816 b = PyByteArray_FromStringAndSize(NULL, n);
817 if (b == NULL)
818 return NULL;
819
820 res = PyObject_CallMethodObjArgs(self, _PyIO_str_readinto, b, NULL);
Antoine Pitrou328ec742010-09-14 18:37:24 +0000821 if (res == NULL || res == Py_None) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000822 Py_DECREF(b);
Antoine Pitrou328ec742010-09-14 18:37:24 +0000823 return res;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000824 }
825
826 n = PyNumber_AsSsize_t(res, PyExc_ValueError);
827 Py_DECREF(res);
828 if (n == -1 && PyErr_Occurred()) {
829 Py_DECREF(b);
830 return NULL;
831 }
832
833 res = PyBytes_FromStringAndSize(PyByteArray_AsString(b), n);
834 Py_DECREF(b);
835 return res;
836}
837
838
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000839PyDoc_STRVAR(rawiobase_readall_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000840 "Read until EOF, using multiple read() call.");
841
842static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000843rawiobase_readall(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000844{
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000845 int r;
846 PyObject *chunks = PyList_New(0);
847 PyObject *result;
Victor Stinnercc024d12013-10-29 02:23:46 +0100848
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000849 if (chunks == NULL)
850 return NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000851
852 while (1) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200853 PyObject *data = _PyObject_CallMethodId(self, &PyId_read,
854 "i", DEFAULT_BUFFER_SIZE);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000855 if (!data) {
Gregory P. Smith51359922012-06-23 23:55:39 -0700856 /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
857 when EINTR occurs so we needn't do it ourselves. */
858 if (_PyIO_trap_eintr()) {
859 continue;
860 }
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000861 Py_DECREF(chunks);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000862 return NULL;
863 }
Victor Stinnera80987f2011-05-25 22:47:16 +0200864 if (data == Py_None) {
865 if (PyList_GET_SIZE(chunks) == 0) {
866 Py_DECREF(chunks);
867 return data;
868 }
869 Py_DECREF(data);
870 break;
871 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000872 if (!PyBytes_Check(data)) {
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000873 Py_DECREF(chunks);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000874 Py_DECREF(data);
875 PyErr_SetString(PyExc_TypeError, "read() should return bytes");
876 return NULL;
877 }
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000878 if (PyBytes_GET_SIZE(data) == 0) {
879 /* EOF */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000880 Py_DECREF(data);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000881 break;
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000882 }
883 r = PyList_Append(chunks, data);
884 Py_DECREF(data);
885 if (r < 0) {
886 Py_DECREF(chunks);
887 return NULL;
888 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000889 }
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000890 result = _PyBytes_Join(_PyIO_empty_bytes, chunks);
891 Py_DECREF(chunks);
892 return result;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000893}
894
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000895static PyMethodDef rawiobase_methods[] = {
896 {"read", rawiobase_read, METH_VARARGS},
897 {"readall", rawiobase_readall, METH_NOARGS, rawiobase_readall_doc},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000898 {NULL, NULL}
899};
900
901PyTypeObject PyRawIOBase_Type = {
902 PyVarObject_HEAD_INIT(NULL, 0)
903 "_io._RawIOBase", /*tp_name*/
904 0, /*tp_basicsize*/
905 0, /*tp_itemsize*/
906 0, /*tp_dealloc*/
907 0, /*tp_print*/
908 0, /*tp_getattr*/
909 0, /*tp_setattr*/
910 0, /*tp_compare */
911 0, /*tp_repr*/
912 0, /*tp_as_number*/
913 0, /*tp_as_sequence*/
914 0, /*tp_as_mapping*/
915 0, /*tp_hash */
916 0, /*tp_call*/
917 0, /*tp_str*/
918 0, /*tp_getattro*/
919 0, /*tp_setattro*/
920 0, /*tp_as_buffer*/
Antoine Pitrou796564c2013-07-30 19:59:21 +0200921 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_FINALIZE, /*tp_flags*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000922 rawiobase_doc, /* tp_doc */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000923 0, /* tp_traverse */
924 0, /* tp_clear */
925 0, /* tp_richcompare */
926 0, /* tp_weaklistoffset */
927 0, /* tp_iter */
928 0, /* tp_iternext */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000929 rawiobase_methods, /* tp_methods */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000930 0, /* tp_members */
931 0, /* tp_getset */
932 &PyIOBase_Type, /* tp_base */
933 0, /* tp_dict */
934 0, /* tp_descr_get */
935 0, /* tp_descr_set */
936 0, /* tp_dictoffset */
937 0, /* tp_init */
938 0, /* tp_alloc */
939 0, /* tp_new */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200940 0, /* tp_free */
941 0, /* tp_is_gc */
942 0, /* tp_bases */
943 0, /* tp_mro */
944 0, /* tp_cache */
945 0, /* tp_subclasses */
946 0, /* tp_weaklist */
947 0, /* tp_del */
948 0, /* tp_version_tag */
949 0, /* tp_finalize */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000950};