blob: b58687e3301a47072277060be1fba21af3337766 [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"
45 "Note that calling any method (even inquiries) on a closed stream is\n"
46 "undefined. Implementations may raise IOError in this case.\n"
47 "\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
66/* Internal methods */
67static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000068iobase_unsupported(const char *message)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000069{
70 PyErr_SetString(IO_STATE->unsupported_operation, message);
71 return NULL;
72}
73
74/* Positionning */
75
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000076PyDoc_STRVAR(iobase_seek_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000077 "Change stream position.\n"
78 "\n"
Terry Jan Reedy0158af32013-03-11 17:42:46 -040079 "Change the stream position to the given byte offset. The offset is\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000080 "interpreted relative to the position indicated by whence. Values\n"
81 "for whence are:\n"
82 "\n"
83 "* 0 -- start of stream (the default); offset should be zero or positive\n"
84 "* 1 -- current stream position; offset may be negative\n"
85 "* 2 -- end of stream; offset is usually negative\n"
86 "\n"
87 "Return the new absolute position.");
88
89static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000090iobase_seek(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000091{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000092 return iobase_unsupported("seek");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000093}
94
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000095PyDoc_STRVAR(iobase_tell_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000096 "Return current stream position.");
97
98static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000099iobase_tell(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000100{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200101 _Py_IDENTIFIER(seek);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200102
103 return _PyObject_CallMethodId(self, &PyId_seek, "ii", 0, 1);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000104}
105
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000106PyDoc_STRVAR(iobase_truncate_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000107 "Truncate file to size bytes.\n"
108 "\n"
Antoine Pitrou905a2ff2010-01-31 22:47:27 +0000109 "File pointer is left unchanged. Size defaults to the current IO\n"
110 "position as reported by tell(). Returns the new size.");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000111
112static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000113iobase_truncate(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000114{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000115 return iobase_unsupported("truncate");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000116}
117
118/* Flush and close methods */
119
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000120PyDoc_STRVAR(iobase_flush_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000121 "Flush write buffers, if applicable.\n"
122 "\n"
123 "This is not implemented for read-only and non-blocking streams.\n");
124
125static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000126iobase_flush(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000127{
128 /* XXX Should this return the number of bytes written??? */
129 if (IS_CLOSED(self)) {
130 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file.");
131 return NULL;
132 }
133 Py_RETURN_NONE;
134}
135
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000136PyDoc_STRVAR(iobase_close_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000137 "Flush and close the IO object.\n"
138 "\n"
139 "This method has no effect if the file is already closed.\n");
140
141static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000142iobase_closed(PyObject *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000143{
144 PyObject *res;
145 int closed;
146 /* This gets the derived attribute, which is *not* __IOBase_closed
147 in most cases! */
148 res = PyObject_GetAttr(self, _PyIO_str_closed);
149 if (res == NULL)
150 return 0;
151 closed = PyObject_IsTrue(res);
152 Py_DECREF(res);
153 return closed;
154}
155
156static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000157iobase_closed_get(PyObject *self, void *context)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000158{
159 return PyBool_FromLong(IS_CLOSED(self));
160}
161
162PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000163_PyIOBase_check_closed(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000164{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000165 if (iobase_closed(self)) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000166 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file.");
167 return NULL;
168 }
169 if (args == Py_True)
170 return Py_None;
171 else
172 Py_RETURN_NONE;
173}
174
175/* XXX: IOBase thinks it has to maintain its own internal state in
176 `__IOBase_closed` and call flush() by itself, but it is redundant with
177 whatever behaviour a non-trivial derived class will implement. */
178
179static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000180iobase_close(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000181{
182 PyObject *res;
Martin v. Löwis767046a2011-10-14 15:35:36 +0200183 _Py_IDENTIFIER(__IOBase_closed);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000184
185 if (IS_CLOSED(self))
186 Py_RETURN_NONE;
187
188 res = PyObject_CallMethodObjArgs(self, _PyIO_str_flush, NULL);
Martin v. Löwis767046a2011-10-14 15:35:36 +0200189 _PyObject_SetAttrId(self, &PyId___IOBase_closed, Py_True);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000190 if (res == NULL) {
Antoine Pitrou6be88762010-05-03 16:48:20 +0000191 return NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000192 }
193 Py_XDECREF(res);
194 Py_RETURN_NONE;
195}
196
197/* Finalization and garbage collection support */
198
Antoine Pitrou796564c2013-07-30 19:59:21 +0200199static void
200iobase_finalize(PyObject *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000201{
202 PyObject *res;
Antoine Pitrou796564c2013-07-30 19:59:21 +0200203 PyObject *error_type, *error_value, *error_traceback;
204 int closed;
205 _Py_IDENTIFIER(_finalizing);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000206
Antoine Pitrou796564c2013-07-30 19:59:21 +0200207 /* Save the current exception, if any. */
208 PyErr_Fetch(&error_type, &error_value, &error_traceback);
209
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000210 /* If `closed` doesn't exist or can't be evaluated as bool, then the
211 object is probably in an unusable state, so ignore. */
212 res = PyObject_GetAttr(self, _PyIO_str_closed);
Christian Heimes72f455e2013-07-31 01:33:50 +0200213 if (res == NULL) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000214 PyErr_Clear();
Christian Heimes72f455e2013-07-31 01:33:50 +0200215 closed = -1;
216 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000217 else {
218 closed = PyObject_IsTrue(res);
219 Py_DECREF(res);
220 if (closed == -1)
221 PyErr_Clear();
222 }
223 if (closed == 0) {
Antoine Pitrou796564c2013-07-30 19:59:21 +0200224 /* Signal close() that it was called as part of the object
225 finalization process. */
226 if (_PyObject_SetAttrId(self, &PyId__finalizing, Py_True))
227 PyErr_Clear();
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000228 res = PyObject_CallMethodObjArgs((PyObject *) self, _PyIO_str_close,
229 NULL);
230 /* Silencing I/O errors is bad, but printing spurious tracebacks is
231 equally as bad, and potentially more frequent (because of
232 shutdown issues). */
233 if (res == NULL)
234 PyErr_Clear();
235 else
236 Py_DECREF(res);
237 }
Antoine Pitrou796564c2013-07-30 19:59:21 +0200238
239 /* Restore the saved exception. */
240 PyErr_Restore(error_type, error_value, error_traceback);
241}
242
243int
244_PyIOBase_finalize(PyObject *self)
245{
246 int is_zombie;
247
248 /* If _PyIOBase_finalize() is called from a destructor, we need to
249 resurrect the object as calling close() can invoke arbitrary code. */
250 is_zombie = (Py_REFCNT(self) == 0);
251 if (is_zombie)
252 return PyObject_CallFinalizerFromDealloc(self);
253 else {
254 PyObject_CallFinalizer(self);
255 return 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000256 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000257}
258
259static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000260iobase_traverse(iobase *self, visitproc visit, void *arg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000261{
262 Py_VISIT(self->dict);
263 return 0;
264}
265
266static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000267iobase_clear(iobase *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000268{
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000269 Py_CLEAR(self->dict);
270 return 0;
271}
272
273/* Destructor */
274
275static void
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000276iobase_dealloc(iobase *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000277{
278 /* NOTE: since IOBaseObject has its own dict, Python-defined attributes
279 are still available here for close() to use.
280 However, if the derived class declares a __slots__, those slots are
281 already gone.
282 */
283 if (_PyIOBase_finalize((PyObject *) self) < 0) {
284 /* When called from a heap type's dealloc, the type will be
285 decref'ed on return (see e.g. subtype_dealloc in typeobject.c). */
286 if (PyType_HasFeature(Py_TYPE(self), Py_TPFLAGS_HEAPTYPE))
287 Py_INCREF(Py_TYPE(self));
288 return;
289 }
290 _PyObject_GC_UNTRACK(self);
291 if (self->weakreflist != NULL)
292 PyObject_ClearWeakRefs((PyObject *) self);
293 Py_CLEAR(self->dict);
294 Py_TYPE(self)->tp_free((PyObject *) self);
295}
296
297/* Inquiry methods */
298
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000299PyDoc_STRVAR(iobase_seekable_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000300 "Return whether object supports random access.\n"
301 "\n"
Amaury Forgeot d'Arc616453c2010-09-06 22:31:52 +0000302 "If False, seek(), tell() and truncate() will raise UnsupportedOperation.\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000303 "This method may need to do a test seek().");
304
305static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000306iobase_seekable(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000307{
308 Py_RETURN_FALSE;
309}
310
311PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000312_PyIOBase_check_seekable(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000313{
314 PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_seekable, NULL);
315 if (res == NULL)
316 return NULL;
317 if (res != Py_True) {
318 Py_CLEAR(res);
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000319 iobase_unsupported("File or stream is not seekable.");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000320 return NULL;
321 }
322 if (args == Py_True) {
323 Py_DECREF(res);
324 }
325 return res;
326}
327
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000328PyDoc_STRVAR(iobase_readable_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000329 "Return whether object was opened for reading.\n"
330 "\n"
Amaury Forgeot d'Arc616453c2010-09-06 22:31:52 +0000331 "If False, read() will raise UnsupportedOperation.");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000332
333static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000334iobase_readable(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000335{
336 Py_RETURN_FALSE;
337}
338
339/* May be called with any object */
340PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000341_PyIOBase_check_readable(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000342{
343 PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_readable, NULL);
344 if (res == NULL)
345 return NULL;
346 if (res != Py_True) {
347 Py_CLEAR(res);
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000348 iobase_unsupported("File or stream is not readable.");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000349 return NULL;
350 }
351 if (args == Py_True) {
352 Py_DECREF(res);
353 }
354 return res;
355}
356
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000357PyDoc_STRVAR(iobase_writable_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000358 "Return whether object was opened for writing.\n"
359 "\n"
Amaury Forgeot d'Arc616453c2010-09-06 22:31:52 +0000360 "If False, write() will raise UnsupportedOperation.");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000361
362static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000363iobase_writable(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000364{
365 Py_RETURN_FALSE;
366}
367
368/* May be called with any object */
369PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000370_PyIOBase_check_writable(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000371{
372 PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_writable, NULL);
373 if (res == NULL)
374 return NULL;
375 if (res != Py_True) {
376 Py_CLEAR(res);
Antoine Pitrou0d739d72010-09-05 23:01:12 +0000377 iobase_unsupported("File or stream is not writable.");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000378 return NULL;
379 }
380 if (args == Py_True) {
381 Py_DECREF(res);
382 }
383 return res;
384}
385
386/* Context manager */
387
388static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000389iobase_enter(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000390{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000391 if (_PyIOBase_check_closed(self, Py_True) == NULL)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000392 return NULL;
393
394 Py_INCREF(self);
395 return self;
396}
397
398static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000399iobase_exit(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000400{
401 return PyObject_CallMethodObjArgs(self, _PyIO_str_close, NULL);
402}
403
404/* Lower-level APIs */
405
406/* XXX Should these be present even if unimplemented? */
407
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000408PyDoc_STRVAR(iobase_fileno_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000409 "Returns underlying file descriptor if one exists.\n"
410 "\n"
411 "An IOError is raised if the IO object does not use a file descriptor.\n");
412
413static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000414iobase_fileno(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000415{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000416 return iobase_unsupported("fileno");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000417}
418
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000419PyDoc_STRVAR(iobase_isatty_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000420 "Return whether this is an 'interactive' stream.\n"
421 "\n"
422 "Return False if it can't be determined.\n");
423
424static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000425iobase_isatty(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000426{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000427 if (_PyIOBase_check_closed(self, Py_True) == NULL)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000428 return NULL;
429 Py_RETURN_FALSE;
430}
431
432/* Readline(s) and writelines */
433
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000434PyDoc_STRVAR(iobase_readline_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000435 "Read and return a line from the stream.\n"
436 "\n"
437 "If limit is specified, at most limit bytes will be read.\n"
438 "\n"
Ezio Melotti16d2b472012-09-18 07:20:18 +0300439 "The line terminator is always b'\\n' for binary files; for text\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000440 "files, the newlines argument to open can be used to select the line\n"
441 "terminator(s) recognized.\n");
442
443static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000444iobase_readline(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000445{
446 /* For backwards compatibility, a (slowish) readline(). */
447
448 Py_ssize_t limit = -1;
449 int has_peek = 0;
450 PyObject *buffer, *result;
451 Py_ssize_t old_size = -1;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200452 _Py_IDENTIFIER(read);
Martin v. Löwis767046a2011-10-14 15:35:36 +0200453 _Py_IDENTIFIER(peek);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000454
Benjamin Petersonbf5ff762009-12-13 19:25:34 +0000455 if (!PyArg_ParseTuple(args, "|O&:readline", &_PyIO_ConvertSsize_t, &limit)) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000456 return NULL;
457 }
458
Martin v. Löwis767046a2011-10-14 15:35:36 +0200459 if (_PyObject_HasAttrId(self, &PyId_peek))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000460 has_peek = 1;
461
462 buffer = PyByteArray_FromStringAndSize(NULL, 0);
463 if (buffer == NULL)
464 return NULL;
465
466 while (limit < 0 || Py_SIZE(buffer) < limit) {
467 Py_ssize_t nreadahead = 1;
468 PyObject *b;
469
470 if (has_peek) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200471 PyObject *readahead = _PyObject_CallMethodId(self, &PyId_peek, "i", 1);
Gregory P. Smith51359922012-06-23 23:55:39 -0700472 if (readahead == NULL) {
473 /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
474 when EINTR occurs so we needn't do it ourselves. */
475 if (_PyIO_trap_eintr()) {
476 continue;
477 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000478 goto fail;
Gregory P. Smith51359922012-06-23 23:55:39 -0700479 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000480 if (!PyBytes_Check(readahead)) {
481 PyErr_Format(PyExc_IOError,
482 "peek() should have returned a bytes object, "
483 "not '%.200s'", Py_TYPE(readahead)->tp_name);
484 Py_DECREF(readahead);
485 goto fail;
486 }
487 if (PyBytes_GET_SIZE(readahead) > 0) {
488 Py_ssize_t n = 0;
489 const char *buf = PyBytes_AS_STRING(readahead);
490 if (limit >= 0) {
491 do {
492 if (n >= PyBytes_GET_SIZE(readahead) || n >= limit)
493 break;
494 if (buf[n++] == '\n')
495 break;
496 } while (1);
497 }
498 else {
499 do {
500 if (n >= PyBytes_GET_SIZE(readahead))
501 break;
502 if (buf[n++] == '\n')
503 break;
504 } while (1);
505 }
506 nreadahead = n;
507 }
508 Py_DECREF(readahead);
509 }
510
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200511 b = _PyObject_CallMethodId(self, &PyId_read, "n", nreadahead);
Gregory P. Smith51359922012-06-23 23:55:39 -0700512 if (b == NULL) {
513 /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
514 when EINTR occurs so we needn't do it ourselves. */
515 if (_PyIO_trap_eintr()) {
516 continue;
517 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000518 goto fail;
Gregory P. Smith51359922012-06-23 23:55:39 -0700519 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000520 if (!PyBytes_Check(b)) {
521 PyErr_Format(PyExc_IOError,
522 "read() should have returned a bytes object, "
523 "not '%.200s'", Py_TYPE(b)->tp_name);
524 Py_DECREF(b);
525 goto fail;
526 }
527 if (PyBytes_GET_SIZE(b) == 0) {
528 Py_DECREF(b);
529 break;
530 }
531
532 old_size = PyByteArray_GET_SIZE(buffer);
Victor Stinnercc024d12013-10-29 02:23:46 +0100533 if (PyByteArray_Resize(buffer, old_size + PyBytes_GET_SIZE(b)) < 0) {
534 Py_DECREF(b);
535 goto fail;
536 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000537 memcpy(PyByteArray_AS_STRING(buffer) + old_size,
538 PyBytes_AS_STRING(b), PyBytes_GET_SIZE(b));
539
540 Py_DECREF(b);
541
542 if (PyByteArray_AS_STRING(buffer)[PyByteArray_GET_SIZE(buffer) - 1] == '\n')
543 break;
544 }
545
546 result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(buffer),
547 PyByteArray_GET_SIZE(buffer));
548 Py_DECREF(buffer);
549 return result;
550 fail:
551 Py_DECREF(buffer);
552 return NULL;
553}
554
555static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000556iobase_iter(PyObject *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000557{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000558 if (_PyIOBase_check_closed(self, Py_True) == NULL)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000559 return NULL;
560
561 Py_INCREF(self);
562 return self;
563}
564
565static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000566iobase_iternext(PyObject *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000567{
568 PyObject *line = PyObject_CallMethodObjArgs(self, _PyIO_str_readline, NULL);
569
570 if (line == NULL)
571 return NULL;
572
573 if (PyObject_Size(line) == 0) {
574 Py_DECREF(line);
575 return NULL;
576 }
577
578 return line;
579}
580
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000581PyDoc_STRVAR(iobase_readlines_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000582 "Return a list of lines from the stream.\n"
583 "\n"
584 "hint can be specified to control the number of lines read: no more\n"
585 "lines will be read if the total size (in bytes/characters) of all\n"
586 "lines so far exceeds hint.");
587
588static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000589iobase_readlines(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000590{
591 Py_ssize_t hint = -1, length = 0;
Benjamin Peterson05516132009-12-13 19:28:09 +0000592 PyObject *result;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000593
Benjamin Petersonbf5ff762009-12-13 19:25:34 +0000594 if (!PyArg_ParseTuple(args, "|O&:readlines", &_PyIO_ConvertSsize_t, &hint)) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000595 return NULL;
596 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000597
598 result = PyList_New(0);
599 if (result == NULL)
600 return NULL;
601
602 if (hint <= 0) {
603 /* XXX special-casing this made sense in the Python version in order
604 to remove the bytecode interpretation overhead, but it could
605 probably be removed here. */
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200606 _Py_IDENTIFIER(extend);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200607 PyObject *ret = _PyObject_CallMethodId(result, &PyId_extend, "O", self);
608
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000609 if (ret == NULL) {
610 Py_DECREF(result);
611 return NULL;
612 }
613 Py_DECREF(ret);
614 return result;
615 }
616
617 while (1) {
618 PyObject *line = PyIter_Next(self);
619 if (line == NULL) {
620 if (PyErr_Occurred()) {
621 Py_DECREF(result);
622 return NULL;
623 }
624 else
625 break; /* StopIteration raised */
626 }
627
628 if (PyList_Append(result, line) < 0) {
629 Py_DECREF(line);
630 Py_DECREF(result);
631 return NULL;
632 }
633 length += PyObject_Size(line);
634 Py_DECREF(line);
635
636 if (length > hint)
637 break;
638 }
639 return result;
640}
641
642static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000643iobase_writelines(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000644{
645 PyObject *lines, *iter, *res;
646
647 if (!PyArg_ParseTuple(args, "O:writelines", &lines)) {
648 return NULL;
649 }
650
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000651 if (_PyIOBase_check_closed(self, Py_True) == NULL)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000652 return NULL;
653
654 iter = PyObject_GetIter(lines);
655 if (iter == NULL)
656 return NULL;
657
658 while (1) {
659 PyObject *line = PyIter_Next(iter);
660 if (line == NULL) {
661 if (PyErr_Occurred()) {
662 Py_DECREF(iter);
663 return NULL;
664 }
665 else
666 break; /* Stop Iteration */
667 }
668
Gregory P. Smithb9817b02013-02-01 13:03:39 -0800669 res = NULL;
670 do {
671 res = PyObject_CallMethodObjArgs(self, _PyIO_str_write, line, NULL);
672 } while (res == NULL && _PyIO_trap_eintr());
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000673 Py_DECREF(line);
674 if (res == NULL) {
675 Py_DECREF(iter);
676 return NULL;
677 }
678 Py_DECREF(res);
679 }
680 Py_DECREF(iter);
681 Py_RETURN_NONE;
682}
683
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000684static PyMethodDef iobase_methods[] = {
685 {"seek", iobase_seek, METH_VARARGS, iobase_seek_doc},
686 {"tell", iobase_tell, METH_NOARGS, iobase_tell_doc},
687 {"truncate", iobase_truncate, METH_VARARGS, iobase_truncate_doc},
688 {"flush", iobase_flush, METH_NOARGS, iobase_flush_doc},
689 {"close", iobase_close, METH_NOARGS, iobase_close_doc},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000690
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000691 {"seekable", iobase_seekable, METH_NOARGS, iobase_seekable_doc},
692 {"readable", iobase_readable, METH_NOARGS, iobase_readable_doc},
693 {"writable", iobase_writable, METH_NOARGS, iobase_writable_doc},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000694
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000695 {"_checkClosed", _PyIOBase_check_closed, METH_NOARGS},
696 {"_checkSeekable", _PyIOBase_check_seekable, METH_NOARGS},
697 {"_checkReadable", _PyIOBase_check_readable, METH_NOARGS},
698 {"_checkWritable", _PyIOBase_check_writable, METH_NOARGS},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000699
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000700 {"fileno", iobase_fileno, METH_NOARGS, iobase_fileno_doc},
701 {"isatty", iobase_isatty, METH_NOARGS, iobase_isatty_doc},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000702
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000703 {"__enter__", iobase_enter, METH_NOARGS},
704 {"__exit__", iobase_exit, METH_VARARGS},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000705
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000706 {"readline", iobase_readline, METH_VARARGS, iobase_readline_doc},
707 {"readlines", iobase_readlines, METH_VARARGS, iobase_readlines_doc},
708 {"writelines", iobase_writelines, METH_VARARGS},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000709
710 {NULL, NULL}
711};
712
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000713static PyGetSetDef iobase_getset[] = {
Benjamin Peterson23d7f122012-02-19 20:02:57 -0500714 {"__dict__", PyObject_GenericGetDict, NULL, NULL},
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000715 {"closed", (getter)iobase_closed_get, NULL, NULL},
Benjamin Peterson1fea3212009-04-19 03:15:20 +0000716 {NULL}
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000717};
718
719
720PyTypeObject PyIOBase_Type = {
721 PyVarObject_HEAD_INIT(NULL, 0)
722 "_io._IOBase", /*tp_name*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000723 sizeof(iobase), /*tp_basicsize*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000724 0, /*tp_itemsize*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000725 (destructor)iobase_dealloc, /*tp_dealloc*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000726 0, /*tp_print*/
727 0, /*tp_getattr*/
728 0, /*tp_setattr*/
729 0, /*tp_compare */
730 0, /*tp_repr*/
731 0, /*tp_as_number*/
732 0, /*tp_as_sequence*/
733 0, /*tp_as_mapping*/
734 0, /*tp_hash */
735 0, /*tp_call*/
736 0, /*tp_str*/
737 0, /*tp_getattro*/
738 0, /*tp_setattro*/
739 0, /*tp_as_buffer*/
740 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
Antoine Pitrou796564c2013-07-30 19:59:21 +0200741 | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /*tp_flags*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000742 iobase_doc, /* tp_doc */
743 (traverseproc)iobase_traverse, /* tp_traverse */
744 (inquiry)iobase_clear, /* tp_clear */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000745 0, /* tp_richcompare */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000746 offsetof(iobase, weakreflist), /* tp_weaklistoffset */
747 iobase_iter, /* tp_iter */
748 iobase_iternext, /* tp_iternext */
749 iobase_methods, /* tp_methods */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000750 0, /* tp_members */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000751 iobase_getset, /* tp_getset */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000752 0, /* tp_base */
753 0, /* tp_dict */
754 0, /* tp_descr_get */
755 0, /* tp_descr_set */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000756 offsetof(iobase, dict), /* tp_dictoffset */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000757 0, /* tp_init */
758 0, /* tp_alloc */
759 PyType_GenericNew, /* tp_new */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200760 0, /* tp_free */
761 0, /* tp_is_gc */
762 0, /* tp_bases */
763 0, /* tp_mro */
764 0, /* tp_cache */
765 0, /* tp_subclasses */
766 0, /* tp_weaklist */
767 0, /* tp_del */
768 0, /* tp_version_tag */
769 (destructor)iobase_finalize, /* tp_finalize */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000770};
771
772
773/*
774 * RawIOBase class, Inherits from IOBase.
775 */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000776PyDoc_STRVAR(rawiobase_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000777 "Base class for raw binary I/O.");
778
779/*
780 * The read() method is implemented by calling readinto(); derived classes
781 * that want to support read() only need to implement readinto() as a
782 * primitive operation. In general, readinto() can be more efficient than
783 * read().
784 *
785 * (It would be tempting to also provide an implementation of readinto() in
786 * terms of read(), in case the latter is a more suitable primitive operation,
787 * but that would lead to nasty recursion in case a subclass doesn't implement
788 * either.)
789*/
790
791static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000792rawiobase_read(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000793{
794 Py_ssize_t n = -1;
795 PyObject *b, *res;
796
797 if (!PyArg_ParseTuple(args, "|n:read", &n)) {
798 return NULL;
799 }
800
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200801 if (n < 0) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200802 _Py_IDENTIFIER(readall);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200803
804 return _PyObject_CallMethodId(self, &PyId_readall, NULL);
805 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000806
807 /* TODO: allocate a bytes object directly instead and manually construct
808 a writable memoryview pointing to it. */
809 b = PyByteArray_FromStringAndSize(NULL, n);
810 if (b == NULL)
811 return NULL;
812
813 res = PyObject_CallMethodObjArgs(self, _PyIO_str_readinto, b, NULL);
Antoine Pitrou328ec742010-09-14 18:37:24 +0000814 if (res == NULL || res == Py_None) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000815 Py_DECREF(b);
Antoine Pitrou328ec742010-09-14 18:37:24 +0000816 return res;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000817 }
818
819 n = PyNumber_AsSsize_t(res, PyExc_ValueError);
820 Py_DECREF(res);
821 if (n == -1 && PyErr_Occurred()) {
822 Py_DECREF(b);
823 return NULL;
824 }
825
826 res = PyBytes_FromStringAndSize(PyByteArray_AsString(b), n);
827 Py_DECREF(b);
828 return res;
829}
830
831
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000832PyDoc_STRVAR(rawiobase_readall_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000833 "Read until EOF, using multiple read() call.");
834
835static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000836rawiobase_readall(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000837{
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000838 int r;
839 PyObject *chunks = PyList_New(0);
840 PyObject *result;
Victor Stinnercc024d12013-10-29 02:23:46 +0100841
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000842 if (chunks == NULL)
843 return NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000844
845 while (1) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200846 _Py_IDENTIFIER(read);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200847 PyObject *data = _PyObject_CallMethodId(self, &PyId_read,
848 "i", DEFAULT_BUFFER_SIZE);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000849 if (!data) {
Gregory P. Smith51359922012-06-23 23:55:39 -0700850 /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
851 when EINTR occurs so we needn't do it ourselves. */
852 if (_PyIO_trap_eintr()) {
853 continue;
854 }
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000855 Py_DECREF(chunks);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000856 return NULL;
857 }
Victor Stinnera80987f2011-05-25 22:47:16 +0200858 if (data == Py_None) {
859 if (PyList_GET_SIZE(chunks) == 0) {
860 Py_DECREF(chunks);
861 return data;
862 }
863 Py_DECREF(data);
864 break;
865 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000866 if (!PyBytes_Check(data)) {
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000867 Py_DECREF(chunks);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000868 Py_DECREF(data);
869 PyErr_SetString(PyExc_TypeError, "read() should return bytes");
870 return NULL;
871 }
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000872 if (PyBytes_GET_SIZE(data) == 0) {
873 /* EOF */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000874 Py_DECREF(data);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000875 break;
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000876 }
877 r = PyList_Append(chunks, data);
878 Py_DECREF(data);
879 if (r < 0) {
880 Py_DECREF(chunks);
881 return NULL;
882 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000883 }
Antoine Pitrou00a9b732009-03-29 19:19:49 +0000884 result = _PyBytes_Join(_PyIO_empty_bytes, chunks);
885 Py_DECREF(chunks);
886 return result;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000887}
888
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000889static PyMethodDef rawiobase_methods[] = {
890 {"read", rawiobase_read, METH_VARARGS},
891 {"readall", rawiobase_readall, METH_NOARGS, rawiobase_readall_doc},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000892 {NULL, NULL}
893};
894
895PyTypeObject PyRawIOBase_Type = {
896 PyVarObject_HEAD_INIT(NULL, 0)
897 "_io._RawIOBase", /*tp_name*/
898 0, /*tp_basicsize*/
899 0, /*tp_itemsize*/
900 0, /*tp_dealloc*/
901 0, /*tp_print*/
902 0, /*tp_getattr*/
903 0, /*tp_setattr*/
904 0, /*tp_compare */
905 0, /*tp_repr*/
906 0, /*tp_as_number*/
907 0, /*tp_as_sequence*/
908 0, /*tp_as_mapping*/
909 0, /*tp_hash */
910 0, /*tp_call*/
911 0, /*tp_str*/
912 0, /*tp_getattro*/
913 0, /*tp_setattro*/
914 0, /*tp_as_buffer*/
Antoine Pitrou796564c2013-07-30 19:59:21 +0200915 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_FINALIZE, /*tp_flags*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000916 rawiobase_doc, /* tp_doc */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000917 0, /* tp_traverse */
918 0, /* tp_clear */
919 0, /* tp_richcompare */
920 0, /* tp_weaklistoffset */
921 0, /* tp_iter */
922 0, /* tp_iternext */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000923 rawiobase_methods, /* tp_methods */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000924 0, /* tp_members */
925 0, /* tp_getset */
926 &PyIOBase_Type, /* tp_base */
927 0, /* tp_dict */
928 0, /* tp_descr_get */
929 0, /* tp_descr_set */
930 0, /* tp_dictoffset */
931 0, /* tp_init */
932 0, /* tp_alloc */
933 0, /* tp_new */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200934 0, /* tp_free */
935 0, /* tp_is_gc */
936 0, /* tp_bases */
937 0, /* tp_mro */
938 0, /* tp_cache */
939 0, /* tp_subclasses */
940 0, /* tp_weaklist */
941 0, /* tp_del */
942 0, /* tp_version_tag */
943 0, /* tp_finalize */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000944};