Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | An implementation of the I/O abstract base classes hierarchy |
| 3 | as defined by PEP 3116 - "New I/O" |
Victor Stinner | cc024d1 | 2013-10-29 02:23:46 +0100 | [diff] [blame] | 4 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 5 | Classes defined here: IOBase, RawIOBase. |
Victor Stinner | cc024d1 | 2013-10-29 02:23:46 +0100 | [diff] [blame] | 6 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 7 | 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 Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 16 | /*[clinic input] |
| 17 | module _io |
| 18 | class _io._IOBase "PyObject *" "&PyIOBase_Type" |
| 19 | class _io._RawIOBase "PyObject *" "&PyRawIOBase_Type" |
| 20 | [clinic start generated code]*/ |
| 21 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=d29a4d076c2b211c]*/ |
| 22 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 23 | /* |
| 24 | * IOBase class, an abstract class |
| 25 | */ |
| 26 | |
| 27 | typedef struct { |
| 28 | PyObject_HEAD |
Victor Stinner | cc024d1 | 2013-10-29 02:23:46 +0100 | [diff] [blame] | 29 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 30 | PyObject *dict; |
| 31 | PyObject *weakreflist; |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 32 | } iobase; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 33 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 34 | PyDoc_STRVAR(iobase_doc, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 35 | "The abstract base class for all I/O classes, acting on streams of\n" |
| 36 | "bytes. There is no public constructor.\n" |
| 37 | "\n" |
| 38 | "This class provides dummy implementations for many methods that\n" |
| 39 | "derived classes can override selectively; the default implementations\n" |
| 40 | "represent a file that cannot be read, written or seeked.\n" |
| 41 | "\n" |
| 42 | "Even though IOBase does not declare read, readinto, or write because\n" |
| 43 | "their signatures will vary, implementations and clients should\n" |
| 44 | "consider those methods part of the interface. Also, implementations\n" |
Amaury Forgeot d'Arc | 616453c | 2010-09-06 22:31:52 +0000 | [diff] [blame] | 45 | "may raise UnsupportedOperation when operations they do not support are\n" |
| 46 | "called.\n" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 47 | "\n" |
| 48 | "The basic type used for binary data read from or written to a file is\n" |
Martin Panter | 6bb91f3 | 2016-05-28 00:41:57 +0000 | [diff] [blame] | 49 | "bytes. Other bytes-like objects are accepted as method arguments too.\n" |
| 50 | "In some cases (such as readinto), a writable object is required. Text\n" |
| 51 | "I/O classes work with str data.\n" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 52 | "\n" |
Andrew Kuchling | 7646620 | 2014-04-15 21:11:36 -0400 | [diff] [blame] | 53 | "Note that calling any method (except additional calls to close(),\n" |
| 54 | "which are ignored) on a closed stream should raise a ValueError.\n" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 55 | "\n" |
| 56 | "IOBase (and its subclasses) support the iterator protocol, meaning\n" |
| 57 | "that an IOBase object can be iterated over yielding the lines in a\n" |
| 58 | "stream.\n" |
| 59 | "\n" |
| 60 | "IOBase also supports the :keyword:`with` statement. In this example,\n" |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 61 | "fp is closed after the suite of the with statement is complete:\n" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 62 | "\n" |
| 63 | "with open('spam.txt', 'r') as fp:\n" |
| 64 | " fp.write('Spam and eggs!')\n"); |
| 65 | |
| 66 | /* Use this macro whenever you want to check the internal `closed` status |
| 67 | of the IOBase object rather than the virtual `closed` attribute as returned |
| 68 | by whatever subclass. */ |
| 69 | |
Martin v. Löwis | 767046a | 2011-10-14 15:35:36 +0200 | [diff] [blame] | 70 | _Py_IDENTIFIER(__IOBase_closed); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 71 | #define IS_CLOSED(self) \ |
Martin v. Löwis | 767046a | 2011-10-14 15:35:36 +0200 | [diff] [blame] | 72 | _PyObject_HasAttrId(self, &PyId___IOBase_closed) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 73 | |
Victor Stinner | 3f36a57 | 2013-11-12 21:39:02 +0100 | [diff] [blame] | 74 | _Py_IDENTIFIER(read); |
| 75 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 76 | /* Internal methods */ |
| 77 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 78 | iobase_unsupported(const char *message) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 79 | { |
Antoine Pitrou | 712cb73 | 2013-12-21 15:51:54 +0100 | [diff] [blame] | 80 | _PyIO_State *state = IO_STATE(); |
| 81 | if (state != NULL) |
| 82 | PyErr_SetString(state->unsupported_operation, message); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 83 | return NULL; |
| 84 | } |
| 85 | |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 86 | /* Positioning */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 87 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 88 | PyDoc_STRVAR(iobase_seek_doc, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 89 | "Change stream position.\n" |
| 90 | "\n" |
Terry Jan Reedy | 0158af3 | 2013-03-11 17:42:46 -0400 | [diff] [blame] | 91 | "Change the stream position to the given byte offset. The offset is\n" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 92 | "interpreted relative to the position indicated by whence. Values\n" |
| 93 | "for whence are:\n" |
| 94 | "\n" |
| 95 | "* 0 -- start of stream (the default); offset should be zero or positive\n" |
| 96 | "* 1 -- current stream position; offset may be negative\n" |
| 97 | "* 2 -- end of stream; offset is usually negative\n" |
| 98 | "\n" |
| 99 | "Return the new absolute position."); |
| 100 | |
| 101 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 102 | iobase_seek(PyObject *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 103 | { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 104 | return iobase_unsupported("seek"); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 107 | /*[clinic input] |
| 108 | _io._IOBase.tell |
| 109 | |
| 110 | Return current stream position. |
| 111 | [clinic start generated code]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 112 | |
| 113 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 114 | _io__IOBase_tell_impl(PyObject *self) |
| 115 | /*[clinic end generated code: output=89a1c0807935abe2 input=04e615fec128801f]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 116 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 117 | _Py_IDENTIFIER(seek); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 118 | |
| 119 | return _PyObject_CallMethodId(self, &PyId_seek, "ii", 0, 1); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 122 | PyDoc_STRVAR(iobase_truncate_doc, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 123 | "Truncate file to size bytes.\n" |
| 124 | "\n" |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 125 | "File pointer is left unchanged. Size defaults to the current IO\n" |
| 126 | "position as reported by tell(). Returns the new size."); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 127 | |
| 128 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 129 | iobase_truncate(PyObject *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 130 | { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 131 | return iobase_unsupported("truncate"); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /* Flush and close methods */ |
| 135 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 136 | /*[clinic input] |
| 137 | _io._IOBase.flush |
| 138 | |
| 139 | Flush write buffers, if applicable. |
| 140 | |
| 141 | This is not implemented for read-only and non-blocking streams. |
| 142 | [clinic start generated code]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 143 | |
| 144 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 145 | _io__IOBase_flush_impl(PyObject *self) |
| 146 | /*[clinic end generated code: output=7cef4b4d54656a3b input=773be121abe270aa]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 147 | { |
| 148 | /* XXX Should this return the number of bytes written??? */ |
| 149 | if (IS_CLOSED(self)) { |
| 150 | PyErr_SetString(PyExc_ValueError, "I/O operation on closed file."); |
| 151 | return NULL; |
| 152 | } |
| 153 | Py_RETURN_NONE; |
| 154 | } |
| 155 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 156 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 157 | iobase_closed(PyObject *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 158 | { |
| 159 | PyObject *res; |
| 160 | int closed; |
| 161 | /* This gets the derived attribute, which is *not* __IOBase_closed |
| 162 | in most cases! */ |
| 163 | res = PyObject_GetAttr(self, _PyIO_str_closed); |
| 164 | if (res == NULL) |
| 165 | return 0; |
| 166 | closed = PyObject_IsTrue(res); |
| 167 | Py_DECREF(res); |
| 168 | return closed; |
| 169 | } |
| 170 | |
| 171 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 172 | iobase_closed_get(PyObject *self, void *context) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 173 | { |
| 174 | return PyBool_FromLong(IS_CLOSED(self)); |
| 175 | } |
| 176 | |
| 177 | PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 178 | _PyIOBase_check_closed(PyObject *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 179 | { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 180 | if (iobase_closed(self)) { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 181 | PyErr_SetString(PyExc_ValueError, "I/O operation on closed file."); |
| 182 | return NULL; |
| 183 | } |
| 184 | if (args == Py_True) |
| 185 | return Py_None; |
| 186 | else |
| 187 | Py_RETURN_NONE; |
| 188 | } |
| 189 | |
| 190 | /* XXX: IOBase thinks it has to maintain its own internal state in |
| 191 | `__IOBase_closed` and call flush() by itself, but it is redundant with |
| 192 | whatever behaviour a non-trivial derived class will implement. */ |
| 193 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 194 | /*[clinic input] |
| 195 | _io._IOBase.close |
| 196 | |
| 197 | Flush and close the IO object. |
| 198 | |
| 199 | This method has no effect if the file is already closed. |
| 200 | [clinic start generated code]*/ |
| 201 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 202 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 203 | _io__IOBase_close_impl(PyObject *self) |
| 204 | /*[clinic end generated code: output=63c6a6f57d783d6d input=f4494d5c31dbc6b7]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 205 | { |
| 206 | PyObject *res; |
| 207 | |
| 208 | if (IS_CLOSED(self)) |
| 209 | Py_RETURN_NONE; |
| 210 | |
| 211 | res = PyObject_CallMethodObjArgs(self, _PyIO_str_flush, NULL); |
Victor Stinner | aa5bbfa | 2013-11-08 00:29:41 +0100 | [diff] [blame] | 212 | |
| 213 | if (_PyObject_SetAttrId(self, &PyId___IOBase_closed, Py_True) < 0) { |
| 214 | Py_XDECREF(res); |
Antoine Pitrou | 6be8876 | 2010-05-03 16:48:20 +0000 | [diff] [blame] | 215 | return NULL; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 216 | } |
Victor Stinner | aa5bbfa | 2013-11-08 00:29:41 +0100 | [diff] [blame] | 217 | |
| 218 | if (res == NULL) |
| 219 | return NULL; |
| 220 | |
| 221 | Py_DECREF(res); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 222 | Py_RETURN_NONE; |
| 223 | } |
| 224 | |
| 225 | /* Finalization and garbage collection support */ |
| 226 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 227 | static void |
| 228 | iobase_finalize(PyObject *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 229 | { |
| 230 | PyObject *res; |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 231 | PyObject *error_type, *error_value, *error_traceback; |
| 232 | int closed; |
| 233 | _Py_IDENTIFIER(_finalizing); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 234 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 235 | /* Save the current exception, if any. */ |
| 236 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 237 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 238 | /* If `closed` doesn't exist or can't be evaluated as bool, then the |
| 239 | object is probably in an unusable state, so ignore. */ |
| 240 | res = PyObject_GetAttr(self, _PyIO_str_closed); |
Christian Heimes | 72f455e | 2013-07-31 01:33:50 +0200 | [diff] [blame] | 241 | if (res == NULL) { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 242 | PyErr_Clear(); |
Christian Heimes | 72f455e | 2013-07-31 01:33:50 +0200 | [diff] [blame] | 243 | closed = -1; |
| 244 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 245 | else { |
| 246 | closed = PyObject_IsTrue(res); |
| 247 | Py_DECREF(res); |
| 248 | if (closed == -1) |
| 249 | PyErr_Clear(); |
| 250 | } |
| 251 | if (closed == 0) { |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 252 | /* Signal close() that it was called as part of the object |
| 253 | finalization process. */ |
| 254 | if (_PyObject_SetAttrId(self, &PyId__finalizing, Py_True)) |
| 255 | PyErr_Clear(); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 256 | res = PyObject_CallMethodObjArgs((PyObject *) self, _PyIO_str_close, |
| 257 | NULL); |
| 258 | /* Silencing I/O errors is bad, but printing spurious tracebacks is |
| 259 | equally as bad, and potentially more frequent (because of |
| 260 | shutdown issues). */ |
| 261 | if (res == NULL) |
| 262 | PyErr_Clear(); |
| 263 | else |
| 264 | Py_DECREF(res); |
| 265 | } |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 266 | |
| 267 | /* Restore the saved exception. */ |
| 268 | PyErr_Restore(error_type, error_value, error_traceback); |
| 269 | } |
| 270 | |
| 271 | int |
| 272 | _PyIOBase_finalize(PyObject *self) |
| 273 | { |
| 274 | int is_zombie; |
| 275 | |
| 276 | /* If _PyIOBase_finalize() is called from a destructor, we need to |
| 277 | resurrect the object as calling close() can invoke arbitrary code. */ |
| 278 | is_zombie = (Py_REFCNT(self) == 0); |
| 279 | if (is_zombie) |
| 280 | return PyObject_CallFinalizerFromDealloc(self); |
| 281 | else { |
| 282 | PyObject_CallFinalizer(self); |
| 283 | return 0; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 284 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 288 | iobase_traverse(iobase *self, visitproc visit, void *arg) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 289 | { |
| 290 | Py_VISIT(self->dict); |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 295 | iobase_clear(iobase *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 296 | { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 297 | Py_CLEAR(self->dict); |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | /* Destructor */ |
| 302 | |
| 303 | static void |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 304 | iobase_dealloc(iobase *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 305 | { |
| 306 | /* NOTE: since IOBaseObject has its own dict, Python-defined attributes |
| 307 | are still available here for close() to use. |
| 308 | However, if the derived class declares a __slots__, those slots are |
| 309 | already gone. |
| 310 | */ |
| 311 | if (_PyIOBase_finalize((PyObject *) self) < 0) { |
| 312 | /* When called from a heap type's dealloc, the type will be |
| 313 | decref'ed on return (see e.g. subtype_dealloc in typeobject.c). */ |
| 314 | if (PyType_HasFeature(Py_TYPE(self), Py_TPFLAGS_HEAPTYPE)) |
| 315 | Py_INCREF(Py_TYPE(self)); |
| 316 | return; |
| 317 | } |
| 318 | _PyObject_GC_UNTRACK(self); |
| 319 | if (self->weakreflist != NULL) |
| 320 | PyObject_ClearWeakRefs((PyObject *) self); |
| 321 | Py_CLEAR(self->dict); |
| 322 | Py_TYPE(self)->tp_free((PyObject *) self); |
| 323 | } |
| 324 | |
| 325 | /* Inquiry methods */ |
| 326 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 327 | /*[clinic input] |
| 328 | _io._IOBase.seekable |
| 329 | |
| 330 | Return whether object supports random access. |
| 331 | |
Martin Panter | 754aab2 | 2016-03-31 07:21:56 +0000 | [diff] [blame] | 332 | If False, seek(), tell() and truncate() will raise OSError. |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 333 | This method may need to do a test seek(). |
| 334 | [clinic start generated code]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 335 | |
| 336 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 337 | _io__IOBase_seekable_impl(PyObject *self) |
Martin Panter | 754aab2 | 2016-03-31 07:21:56 +0000 | [diff] [blame] | 338 | /*[clinic end generated code: output=4c24c67f5f32a43d input=b976622f7fdf3063]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 339 | { |
| 340 | Py_RETURN_FALSE; |
| 341 | } |
| 342 | |
| 343 | PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 344 | _PyIOBase_check_seekable(PyObject *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 345 | { |
| 346 | PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_seekable, NULL); |
| 347 | if (res == NULL) |
| 348 | return NULL; |
| 349 | if (res != Py_True) { |
| 350 | Py_CLEAR(res); |
Antoine Pitrou | 0d739d7 | 2010-09-05 23:01:12 +0000 | [diff] [blame] | 351 | iobase_unsupported("File or stream is not seekable."); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 352 | return NULL; |
| 353 | } |
| 354 | if (args == Py_True) { |
| 355 | Py_DECREF(res); |
| 356 | } |
| 357 | return res; |
| 358 | } |
| 359 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 360 | /*[clinic input] |
| 361 | _io._IOBase.readable |
| 362 | |
| 363 | Return whether object was opened for reading. |
| 364 | |
Martin Panter | 754aab2 | 2016-03-31 07:21:56 +0000 | [diff] [blame] | 365 | If False, read() will raise OSError. |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 366 | [clinic start generated code]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 367 | |
| 368 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 369 | _io__IOBase_readable_impl(PyObject *self) |
Martin Panter | 754aab2 | 2016-03-31 07:21:56 +0000 | [diff] [blame] | 370 | /*[clinic end generated code: output=e48089250686388b input=285b3b866a0ec35f]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 371 | { |
| 372 | Py_RETURN_FALSE; |
| 373 | } |
| 374 | |
| 375 | /* May be called with any object */ |
| 376 | PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 377 | _PyIOBase_check_readable(PyObject *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 378 | { |
| 379 | PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_readable, NULL); |
| 380 | if (res == NULL) |
| 381 | return NULL; |
| 382 | if (res != Py_True) { |
| 383 | Py_CLEAR(res); |
Antoine Pitrou | 0d739d7 | 2010-09-05 23:01:12 +0000 | [diff] [blame] | 384 | iobase_unsupported("File or stream is not readable."); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 385 | return NULL; |
| 386 | } |
| 387 | if (args == Py_True) { |
| 388 | Py_DECREF(res); |
| 389 | } |
| 390 | return res; |
| 391 | } |
| 392 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 393 | /*[clinic input] |
| 394 | _io._IOBase.writable |
| 395 | |
| 396 | Return whether object was opened for writing. |
| 397 | |
Martin Panter | 754aab2 | 2016-03-31 07:21:56 +0000 | [diff] [blame] | 398 | If False, write() will raise OSError. |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 399 | [clinic start generated code]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 400 | |
| 401 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 402 | _io__IOBase_writable_impl(PyObject *self) |
Martin Panter | 754aab2 | 2016-03-31 07:21:56 +0000 | [diff] [blame] | 403 | /*[clinic end generated code: output=406001d0985be14f input=9dcac18a013a05b5]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 404 | { |
| 405 | Py_RETURN_FALSE; |
| 406 | } |
| 407 | |
| 408 | /* May be called with any object */ |
| 409 | PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 410 | _PyIOBase_check_writable(PyObject *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 411 | { |
| 412 | PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_writable, NULL); |
| 413 | if (res == NULL) |
| 414 | return NULL; |
| 415 | if (res != Py_True) { |
| 416 | Py_CLEAR(res); |
Antoine Pitrou | 0d739d7 | 2010-09-05 23:01:12 +0000 | [diff] [blame] | 417 | iobase_unsupported("File or stream is not writable."); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 418 | return NULL; |
| 419 | } |
| 420 | if (args == Py_True) { |
| 421 | Py_DECREF(res); |
| 422 | } |
| 423 | return res; |
| 424 | } |
| 425 | |
| 426 | /* Context manager */ |
| 427 | |
| 428 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 429 | iobase_enter(PyObject *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 430 | { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 431 | if (_PyIOBase_check_closed(self, Py_True) == NULL) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 432 | return NULL; |
| 433 | |
| 434 | Py_INCREF(self); |
| 435 | return self; |
| 436 | } |
| 437 | |
| 438 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 439 | iobase_exit(PyObject *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 440 | { |
| 441 | return PyObject_CallMethodObjArgs(self, _PyIO_str_close, NULL); |
| 442 | } |
| 443 | |
| 444 | /* Lower-level APIs */ |
| 445 | |
| 446 | /* XXX Should these be present even if unimplemented? */ |
| 447 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 448 | /*[clinic input] |
| 449 | _io._IOBase.fileno |
| 450 | |
| 451 | Returns underlying file descriptor if one exists. |
| 452 | |
Martin Panter | 754aab2 | 2016-03-31 07:21:56 +0000 | [diff] [blame] | 453 | OSError is raised if the IO object does not use a file descriptor. |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 454 | [clinic start generated code]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 455 | |
| 456 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 457 | _io__IOBase_fileno_impl(PyObject *self) |
Martin Panter | 754aab2 | 2016-03-31 07:21:56 +0000 | [diff] [blame] | 458 | /*[clinic end generated code: output=7cc0973f0f5f3b73 input=4e37028947dc1cc8]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 459 | { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 460 | return iobase_unsupported("fileno"); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 463 | /*[clinic input] |
| 464 | _io._IOBase.isatty |
| 465 | |
| 466 | Return whether this is an 'interactive' stream. |
| 467 | |
| 468 | Return False if it can't be determined. |
| 469 | [clinic start generated code]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 470 | |
| 471 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 472 | _io__IOBase_isatty_impl(PyObject *self) |
| 473 | /*[clinic end generated code: output=60cab77cede41cdd input=9ef76530d368458b]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 474 | { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 475 | if (_PyIOBase_check_closed(self, Py_True) == NULL) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 476 | return NULL; |
| 477 | Py_RETURN_FALSE; |
| 478 | } |
| 479 | |
| 480 | /* Readline(s) and writelines */ |
| 481 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 482 | /*[clinic input] |
| 483 | _io._IOBase.readline |
Serhiy Storchaka | 762bf40 | 2017-03-30 09:15:31 +0300 | [diff] [blame] | 484 | size as limit: Py_ssize_t(accept={int, NoneType}) = -1 |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 485 | / |
| 486 | |
| 487 | Read and return a line from the stream. |
| 488 | |
| 489 | If size is specified, at most size bytes will be read. |
| 490 | |
| 491 | The line terminator is always b'\n' for binary files; for text |
| 492 | files, the newlines argument to open can be used to select the line |
| 493 | terminator(s) recognized. |
| 494 | [clinic start generated code]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 495 | |
| 496 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 497 | _io__IOBase_readline_impl(PyObject *self, Py_ssize_t limit) |
Serhiy Storchaka | 762bf40 | 2017-03-30 09:15:31 +0300 | [diff] [blame] | 498 | /*[clinic end generated code: output=4479f79b58187840 input=d0c596794e877bff]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 499 | { |
| 500 | /* For backwards compatibility, a (slowish) readline(). */ |
| 501 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 502 | int has_peek = 0; |
| 503 | PyObject *buffer, *result; |
| 504 | Py_ssize_t old_size = -1; |
Martin v. Löwis | 767046a | 2011-10-14 15:35:36 +0200 | [diff] [blame] | 505 | _Py_IDENTIFIER(peek); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 506 | |
Martin v. Löwis | 767046a | 2011-10-14 15:35:36 +0200 | [diff] [blame] | 507 | if (_PyObject_HasAttrId(self, &PyId_peek)) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 508 | has_peek = 1; |
| 509 | |
| 510 | buffer = PyByteArray_FromStringAndSize(NULL, 0); |
| 511 | if (buffer == NULL) |
| 512 | return NULL; |
| 513 | |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 514 | while (limit < 0 || PyByteArray_GET_SIZE(buffer) < limit) { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 515 | Py_ssize_t nreadahead = 1; |
| 516 | PyObject *b; |
| 517 | |
| 518 | if (has_peek) { |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 519 | PyObject *readahead = _PyObject_CallMethodId(self, &PyId_peek, "i", 1); |
Gregory P. Smith | 5135992 | 2012-06-23 23:55:39 -0700 | [diff] [blame] | 520 | if (readahead == NULL) { |
| 521 | /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() |
| 522 | when EINTR occurs so we needn't do it ourselves. */ |
| 523 | if (_PyIO_trap_eintr()) { |
| 524 | continue; |
| 525 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 526 | goto fail; |
Gregory P. Smith | 5135992 | 2012-06-23 23:55:39 -0700 | [diff] [blame] | 527 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 528 | if (!PyBytes_Check(readahead)) { |
| 529 | PyErr_Format(PyExc_IOError, |
| 530 | "peek() should have returned a bytes object, " |
| 531 | "not '%.200s'", Py_TYPE(readahead)->tp_name); |
| 532 | Py_DECREF(readahead); |
| 533 | goto fail; |
| 534 | } |
| 535 | if (PyBytes_GET_SIZE(readahead) > 0) { |
| 536 | Py_ssize_t n = 0; |
| 537 | const char *buf = PyBytes_AS_STRING(readahead); |
| 538 | if (limit >= 0) { |
| 539 | do { |
| 540 | if (n >= PyBytes_GET_SIZE(readahead) || n >= limit) |
| 541 | break; |
| 542 | if (buf[n++] == '\n') |
| 543 | break; |
| 544 | } while (1); |
| 545 | } |
| 546 | else { |
| 547 | do { |
| 548 | if (n >= PyBytes_GET_SIZE(readahead)) |
| 549 | break; |
| 550 | if (buf[n++] == '\n') |
| 551 | break; |
| 552 | } while (1); |
| 553 | } |
| 554 | nreadahead = n; |
| 555 | } |
| 556 | Py_DECREF(readahead); |
| 557 | } |
| 558 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 559 | b = _PyObject_CallMethodId(self, &PyId_read, "n", nreadahead); |
Gregory P. Smith | 5135992 | 2012-06-23 23:55:39 -0700 | [diff] [blame] | 560 | if (b == NULL) { |
| 561 | /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() |
| 562 | when EINTR occurs so we needn't do it ourselves. */ |
| 563 | if (_PyIO_trap_eintr()) { |
| 564 | continue; |
| 565 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 566 | goto fail; |
Gregory P. Smith | 5135992 | 2012-06-23 23:55:39 -0700 | [diff] [blame] | 567 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 568 | if (!PyBytes_Check(b)) { |
| 569 | PyErr_Format(PyExc_IOError, |
| 570 | "read() should have returned a bytes object, " |
| 571 | "not '%.200s'", Py_TYPE(b)->tp_name); |
| 572 | Py_DECREF(b); |
| 573 | goto fail; |
| 574 | } |
| 575 | if (PyBytes_GET_SIZE(b) == 0) { |
| 576 | Py_DECREF(b); |
| 577 | break; |
| 578 | } |
| 579 | |
| 580 | old_size = PyByteArray_GET_SIZE(buffer); |
Victor Stinner | cc024d1 | 2013-10-29 02:23:46 +0100 | [diff] [blame] | 581 | if (PyByteArray_Resize(buffer, old_size + PyBytes_GET_SIZE(b)) < 0) { |
| 582 | Py_DECREF(b); |
| 583 | goto fail; |
| 584 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 585 | memcpy(PyByteArray_AS_STRING(buffer) + old_size, |
| 586 | PyBytes_AS_STRING(b), PyBytes_GET_SIZE(b)); |
| 587 | |
| 588 | Py_DECREF(b); |
| 589 | |
| 590 | if (PyByteArray_AS_STRING(buffer)[PyByteArray_GET_SIZE(buffer) - 1] == '\n') |
| 591 | break; |
| 592 | } |
| 593 | |
| 594 | result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(buffer), |
| 595 | PyByteArray_GET_SIZE(buffer)); |
| 596 | Py_DECREF(buffer); |
| 597 | return result; |
| 598 | fail: |
| 599 | Py_DECREF(buffer); |
| 600 | return NULL; |
| 601 | } |
| 602 | |
| 603 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 604 | iobase_iter(PyObject *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 605 | { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 606 | if (_PyIOBase_check_closed(self, Py_True) == NULL) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 607 | return NULL; |
| 608 | |
| 609 | Py_INCREF(self); |
| 610 | return self; |
| 611 | } |
| 612 | |
| 613 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 614 | iobase_iternext(PyObject *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 615 | { |
| 616 | PyObject *line = PyObject_CallMethodObjArgs(self, _PyIO_str_readline, NULL); |
| 617 | |
| 618 | if (line == NULL) |
| 619 | return NULL; |
| 620 | |
| 621 | if (PyObject_Size(line) == 0) { |
| 622 | Py_DECREF(line); |
| 623 | return NULL; |
| 624 | } |
| 625 | |
| 626 | return line; |
| 627 | } |
| 628 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 629 | /*[clinic input] |
| 630 | _io._IOBase.readlines |
Serhiy Storchaka | 762bf40 | 2017-03-30 09:15:31 +0300 | [diff] [blame] | 631 | hint: Py_ssize_t(accept={int, NoneType}) = -1 |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 632 | / |
| 633 | |
| 634 | Return a list of lines from the stream. |
| 635 | |
| 636 | hint can be specified to control the number of lines read: no more |
| 637 | lines will be read if the total size (in bytes/characters) of all |
| 638 | lines so far exceeds hint. |
| 639 | [clinic start generated code]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 640 | |
| 641 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 642 | _io__IOBase_readlines_impl(PyObject *self, Py_ssize_t hint) |
Serhiy Storchaka | 762bf40 | 2017-03-30 09:15:31 +0300 | [diff] [blame] | 643 | /*[clinic end generated code: output=2f50421677fa3dea input=9400c786ea9dc416]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 644 | { |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 645 | Py_ssize_t length = 0; |
Xiang Zhang | 026435c | 2017-04-15 12:47:28 +0800 | [diff] [blame^] | 646 | PyObject *result, *it = NULL; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 647 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 648 | result = PyList_New(0); |
| 649 | if (result == NULL) |
| 650 | return NULL; |
| 651 | |
| 652 | if (hint <= 0) { |
| 653 | /* XXX special-casing this made sense in the Python version in order |
| 654 | to remove the bytecode interpretation overhead, but it could |
| 655 | probably be removed here. */ |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 656 | _Py_IDENTIFIER(extend); |
Victor Stinner | 61bdb0d | 2016-12-09 15:39:28 +0100 | [diff] [blame] | 657 | PyObject *ret = _PyObject_CallMethodIdObjArgs(result, &PyId_extend, |
| 658 | self, NULL); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 659 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 660 | if (ret == NULL) { |
Xiang Zhang | 026435c | 2017-04-15 12:47:28 +0800 | [diff] [blame^] | 661 | goto error; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 662 | } |
| 663 | Py_DECREF(ret); |
| 664 | return result; |
| 665 | } |
| 666 | |
Xiang Zhang | 026435c | 2017-04-15 12:47:28 +0800 | [diff] [blame^] | 667 | it = PyObject_GetIter(self); |
| 668 | if (it == NULL) { |
| 669 | goto error; |
| 670 | } |
| 671 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 672 | while (1) { |
Xiang Zhang | 026435c | 2017-04-15 12:47:28 +0800 | [diff] [blame^] | 673 | PyObject *line = PyIter_Next(it); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 674 | if (line == NULL) { |
| 675 | if (PyErr_Occurred()) { |
Xiang Zhang | 026435c | 2017-04-15 12:47:28 +0800 | [diff] [blame^] | 676 | goto error; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 677 | } |
| 678 | else |
| 679 | break; /* StopIteration raised */ |
| 680 | } |
| 681 | |
| 682 | if (PyList_Append(result, line) < 0) { |
| 683 | Py_DECREF(line); |
Xiang Zhang | 026435c | 2017-04-15 12:47:28 +0800 | [diff] [blame^] | 684 | goto error; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 685 | } |
| 686 | length += PyObject_Size(line); |
| 687 | Py_DECREF(line); |
| 688 | |
| 689 | if (length > hint) |
| 690 | break; |
| 691 | } |
Xiang Zhang | 026435c | 2017-04-15 12:47:28 +0800 | [diff] [blame^] | 692 | |
| 693 | Py_DECREF(it); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 694 | return result; |
Xiang Zhang | 026435c | 2017-04-15 12:47:28 +0800 | [diff] [blame^] | 695 | |
| 696 | error: |
| 697 | Py_XDECREF(it); |
| 698 | Py_DECREF(result); |
| 699 | return NULL; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 702 | /*[clinic input] |
| 703 | _io._IOBase.writelines |
| 704 | lines: object |
| 705 | / |
| 706 | [clinic start generated code]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 707 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 708 | static PyObject * |
| 709 | _io__IOBase_writelines(PyObject *self, PyObject *lines) |
| 710 | /*[clinic end generated code: output=976eb0a9b60a6628 input=432e729a8450b3cb]*/ |
| 711 | { |
| 712 | PyObject *iter, *res; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 713 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 714 | if (_PyIOBase_check_closed(self, Py_True) == NULL) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 715 | return NULL; |
| 716 | |
| 717 | iter = PyObject_GetIter(lines); |
| 718 | if (iter == NULL) |
| 719 | return NULL; |
| 720 | |
| 721 | while (1) { |
| 722 | PyObject *line = PyIter_Next(iter); |
| 723 | if (line == NULL) { |
| 724 | if (PyErr_Occurred()) { |
| 725 | Py_DECREF(iter); |
| 726 | return NULL; |
| 727 | } |
| 728 | else |
| 729 | break; /* Stop Iteration */ |
| 730 | } |
| 731 | |
Gregory P. Smith | b9817b0 | 2013-02-01 13:03:39 -0800 | [diff] [blame] | 732 | res = NULL; |
| 733 | do { |
| 734 | res = PyObject_CallMethodObjArgs(self, _PyIO_str_write, line, NULL); |
| 735 | } while (res == NULL && _PyIO_trap_eintr()); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 736 | Py_DECREF(line); |
| 737 | if (res == NULL) { |
| 738 | Py_DECREF(iter); |
| 739 | return NULL; |
| 740 | } |
| 741 | Py_DECREF(res); |
| 742 | } |
| 743 | Py_DECREF(iter); |
| 744 | Py_RETURN_NONE; |
| 745 | } |
| 746 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 747 | #include "clinic/iobase.c.h" |
| 748 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 749 | static PyMethodDef iobase_methods[] = { |
| 750 | {"seek", iobase_seek, METH_VARARGS, iobase_seek_doc}, |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 751 | _IO__IOBASE_TELL_METHODDEF |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 752 | {"truncate", iobase_truncate, METH_VARARGS, iobase_truncate_doc}, |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 753 | _IO__IOBASE_FLUSH_METHODDEF |
| 754 | _IO__IOBASE_CLOSE_METHODDEF |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 755 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 756 | _IO__IOBASE_SEEKABLE_METHODDEF |
| 757 | _IO__IOBASE_READABLE_METHODDEF |
| 758 | _IO__IOBASE_WRITABLE_METHODDEF |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 759 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 760 | {"_checkClosed", _PyIOBase_check_closed, METH_NOARGS}, |
| 761 | {"_checkSeekable", _PyIOBase_check_seekable, METH_NOARGS}, |
| 762 | {"_checkReadable", _PyIOBase_check_readable, METH_NOARGS}, |
| 763 | {"_checkWritable", _PyIOBase_check_writable, METH_NOARGS}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 764 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 765 | _IO__IOBASE_FILENO_METHODDEF |
| 766 | _IO__IOBASE_ISATTY_METHODDEF |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 767 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 768 | {"__enter__", iobase_enter, METH_NOARGS}, |
| 769 | {"__exit__", iobase_exit, METH_VARARGS}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 770 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 771 | _IO__IOBASE_READLINE_METHODDEF |
| 772 | _IO__IOBASE_READLINES_METHODDEF |
| 773 | _IO__IOBASE_WRITELINES_METHODDEF |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 774 | |
| 775 | {NULL, NULL} |
| 776 | }; |
| 777 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 778 | static PyGetSetDef iobase_getset[] = { |
Benjamin Peterson | 23d7f12 | 2012-02-19 20:02:57 -0500 | [diff] [blame] | 779 | {"__dict__", PyObject_GenericGetDict, NULL, NULL}, |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 780 | {"closed", (getter)iobase_closed_get, NULL, NULL}, |
Benjamin Peterson | 1fea321 | 2009-04-19 03:15:20 +0000 | [diff] [blame] | 781 | {NULL} |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 782 | }; |
| 783 | |
| 784 | |
| 785 | PyTypeObject PyIOBase_Type = { |
| 786 | PyVarObject_HEAD_INIT(NULL, 0) |
| 787 | "_io._IOBase", /*tp_name*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 788 | sizeof(iobase), /*tp_basicsize*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 789 | 0, /*tp_itemsize*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 790 | (destructor)iobase_dealloc, /*tp_dealloc*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 791 | 0, /*tp_print*/ |
| 792 | 0, /*tp_getattr*/ |
| 793 | 0, /*tp_setattr*/ |
| 794 | 0, /*tp_compare */ |
| 795 | 0, /*tp_repr*/ |
| 796 | 0, /*tp_as_number*/ |
| 797 | 0, /*tp_as_sequence*/ |
| 798 | 0, /*tp_as_mapping*/ |
| 799 | 0, /*tp_hash */ |
| 800 | 0, /*tp_call*/ |
| 801 | 0, /*tp_str*/ |
| 802 | 0, /*tp_getattro*/ |
| 803 | 0, /*tp_setattro*/ |
| 804 | 0, /*tp_as_buffer*/ |
| 805 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 806 | | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /*tp_flags*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 807 | iobase_doc, /* tp_doc */ |
| 808 | (traverseproc)iobase_traverse, /* tp_traverse */ |
| 809 | (inquiry)iobase_clear, /* tp_clear */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 810 | 0, /* tp_richcompare */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 811 | offsetof(iobase, weakreflist), /* tp_weaklistoffset */ |
| 812 | iobase_iter, /* tp_iter */ |
| 813 | iobase_iternext, /* tp_iternext */ |
| 814 | iobase_methods, /* tp_methods */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 815 | 0, /* tp_members */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 816 | iobase_getset, /* tp_getset */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 817 | 0, /* tp_base */ |
| 818 | 0, /* tp_dict */ |
| 819 | 0, /* tp_descr_get */ |
| 820 | 0, /* tp_descr_set */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 821 | offsetof(iobase, dict), /* tp_dictoffset */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 822 | 0, /* tp_init */ |
| 823 | 0, /* tp_alloc */ |
| 824 | PyType_GenericNew, /* tp_new */ |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 825 | 0, /* tp_free */ |
| 826 | 0, /* tp_is_gc */ |
| 827 | 0, /* tp_bases */ |
| 828 | 0, /* tp_mro */ |
| 829 | 0, /* tp_cache */ |
| 830 | 0, /* tp_subclasses */ |
| 831 | 0, /* tp_weaklist */ |
| 832 | 0, /* tp_del */ |
| 833 | 0, /* tp_version_tag */ |
Victor Stinner | 928bff0 | 2016-03-19 10:36:36 +0100 | [diff] [blame] | 834 | iobase_finalize, /* tp_finalize */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 835 | }; |
| 836 | |
| 837 | |
| 838 | /* |
| 839 | * RawIOBase class, Inherits from IOBase. |
| 840 | */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 841 | PyDoc_STRVAR(rawiobase_doc, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 842 | "Base class for raw binary I/O."); |
| 843 | |
| 844 | /* |
| 845 | * The read() method is implemented by calling readinto(); derived classes |
| 846 | * that want to support read() only need to implement readinto() as a |
| 847 | * primitive operation. In general, readinto() can be more efficient than |
| 848 | * read(). |
| 849 | * |
| 850 | * (It would be tempting to also provide an implementation of readinto() in |
| 851 | * terms of read(), in case the latter is a more suitable primitive operation, |
| 852 | * but that would lead to nasty recursion in case a subclass doesn't implement |
| 853 | * either.) |
| 854 | */ |
| 855 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 856 | /*[clinic input] |
| 857 | _io._RawIOBase.read |
| 858 | size as n: Py_ssize_t = -1 |
| 859 | / |
| 860 | [clinic start generated code]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 861 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 862 | static PyObject * |
| 863 | _io__RawIOBase_read_impl(PyObject *self, Py_ssize_t n) |
| 864 | /*[clinic end generated code: output=6cdeb731e3c9f13c input=b6d0dcf6417d1374]*/ |
| 865 | { |
| 866 | PyObject *b, *res; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 867 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 868 | if (n < 0) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 869 | _Py_IDENTIFIER(readall); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 870 | |
| 871 | return _PyObject_CallMethodId(self, &PyId_readall, NULL); |
| 872 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 873 | |
| 874 | /* TODO: allocate a bytes object directly instead and manually construct |
| 875 | a writable memoryview pointing to it. */ |
| 876 | b = PyByteArray_FromStringAndSize(NULL, n); |
| 877 | if (b == NULL) |
| 878 | return NULL; |
| 879 | |
| 880 | res = PyObject_CallMethodObjArgs(self, _PyIO_str_readinto, b, NULL); |
Antoine Pitrou | 328ec74 | 2010-09-14 18:37:24 +0000 | [diff] [blame] | 881 | if (res == NULL || res == Py_None) { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 882 | Py_DECREF(b); |
Antoine Pitrou | 328ec74 | 2010-09-14 18:37:24 +0000 | [diff] [blame] | 883 | return res; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | n = PyNumber_AsSsize_t(res, PyExc_ValueError); |
| 887 | Py_DECREF(res); |
| 888 | if (n == -1 && PyErr_Occurred()) { |
| 889 | Py_DECREF(b); |
| 890 | return NULL; |
| 891 | } |
| 892 | |
| 893 | res = PyBytes_FromStringAndSize(PyByteArray_AsString(b), n); |
| 894 | Py_DECREF(b); |
| 895 | return res; |
| 896 | } |
| 897 | |
| 898 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 899 | /*[clinic input] |
| 900 | _io._RawIOBase.readall |
| 901 | |
| 902 | Read until EOF, using multiple read() call. |
| 903 | [clinic start generated code]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 904 | |
| 905 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 906 | _io__RawIOBase_readall_impl(PyObject *self) |
| 907 | /*[clinic end generated code: output=1987b9ce929425a0 input=688874141213622a]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 908 | { |
Antoine Pitrou | 00a9b73 | 2009-03-29 19:19:49 +0000 | [diff] [blame] | 909 | int r; |
| 910 | PyObject *chunks = PyList_New(0); |
| 911 | PyObject *result; |
Victor Stinner | cc024d1 | 2013-10-29 02:23:46 +0100 | [diff] [blame] | 912 | |
Antoine Pitrou | 00a9b73 | 2009-03-29 19:19:49 +0000 | [diff] [blame] | 913 | if (chunks == NULL) |
| 914 | return NULL; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 915 | |
| 916 | while (1) { |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 917 | PyObject *data = _PyObject_CallMethodId(self, &PyId_read, |
| 918 | "i", DEFAULT_BUFFER_SIZE); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 919 | if (!data) { |
Gregory P. Smith | 5135992 | 2012-06-23 23:55:39 -0700 | [diff] [blame] | 920 | /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() |
| 921 | when EINTR occurs so we needn't do it ourselves. */ |
| 922 | if (_PyIO_trap_eintr()) { |
| 923 | continue; |
| 924 | } |
Antoine Pitrou | 00a9b73 | 2009-03-29 19:19:49 +0000 | [diff] [blame] | 925 | Py_DECREF(chunks); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 926 | return NULL; |
| 927 | } |
Victor Stinner | a80987f | 2011-05-25 22:47:16 +0200 | [diff] [blame] | 928 | if (data == Py_None) { |
| 929 | if (PyList_GET_SIZE(chunks) == 0) { |
| 930 | Py_DECREF(chunks); |
| 931 | return data; |
| 932 | } |
| 933 | Py_DECREF(data); |
| 934 | break; |
| 935 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 936 | if (!PyBytes_Check(data)) { |
Antoine Pitrou | 00a9b73 | 2009-03-29 19:19:49 +0000 | [diff] [blame] | 937 | Py_DECREF(chunks); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 938 | Py_DECREF(data); |
| 939 | PyErr_SetString(PyExc_TypeError, "read() should return bytes"); |
| 940 | return NULL; |
| 941 | } |
Antoine Pitrou | 00a9b73 | 2009-03-29 19:19:49 +0000 | [diff] [blame] | 942 | if (PyBytes_GET_SIZE(data) == 0) { |
| 943 | /* EOF */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 944 | Py_DECREF(data); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 945 | break; |
Antoine Pitrou | 00a9b73 | 2009-03-29 19:19:49 +0000 | [diff] [blame] | 946 | } |
| 947 | r = PyList_Append(chunks, data); |
| 948 | Py_DECREF(data); |
| 949 | if (r < 0) { |
| 950 | Py_DECREF(chunks); |
| 951 | return NULL; |
| 952 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 953 | } |
Antoine Pitrou | 00a9b73 | 2009-03-29 19:19:49 +0000 | [diff] [blame] | 954 | result = _PyBytes_Join(_PyIO_empty_bytes, chunks); |
| 955 | Py_DECREF(chunks); |
| 956 | return result; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 957 | } |
| 958 | |
Antoine Pitrou | 45d6156 | 2015-05-20 21:50:59 +0200 | [diff] [blame] | 959 | static PyObject * |
| 960 | rawiobase_readinto(PyObject *self, PyObject *args) |
| 961 | { |
| 962 | PyErr_SetNone(PyExc_NotImplementedError); |
| 963 | return NULL; |
| 964 | } |
| 965 | |
| 966 | static PyObject * |
| 967 | rawiobase_write(PyObject *self, PyObject *args) |
| 968 | { |
| 969 | PyErr_SetNone(PyExc_NotImplementedError); |
| 970 | return NULL; |
| 971 | } |
| 972 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 973 | static PyMethodDef rawiobase_methods[] = { |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 974 | _IO__RAWIOBASE_READ_METHODDEF |
| 975 | _IO__RAWIOBASE_READALL_METHODDEF |
Antoine Pitrou | 45d6156 | 2015-05-20 21:50:59 +0200 | [diff] [blame] | 976 | {"readinto", rawiobase_readinto, METH_VARARGS}, |
| 977 | {"write", rawiobase_write, METH_VARARGS}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 978 | {NULL, NULL} |
| 979 | }; |
| 980 | |
| 981 | PyTypeObject PyRawIOBase_Type = { |
| 982 | PyVarObject_HEAD_INIT(NULL, 0) |
| 983 | "_io._RawIOBase", /*tp_name*/ |
| 984 | 0, /*tp_basicsize*/ |
| 985 | 0, /*tp_itemsize*/ |
| 986 | 0, /*tp_dealloc*/ |
| 987 | 0, /*tp_print*/ |
| 988 | 0, /*tp_getattr*/ |
| 989 | 0, /*tp_setattr*/ |
| 990 | 0, /*tp_compare */ |
| 991 | 0, /*tp_repr*/ |
| 992 | 0, /*tp_as_number*/ |
| 993 | 0, /*tp_as_sequence*/ |
| 994 | 0, /*tp_as_mapping*/ |
| 995 | 0, /*tp_hash */ |
| 996 | 0, /*tp_call*/ |
| 997 | 0, /*tp_str*/ |
| 998 | 0, /*tp_getattro*/ |
| 999 | 0, /*tp_setattro*/ |
| 1000 | 0, /*tp_as_buffer*/ |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 1001 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_FINALIZE, /*tp_flags*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1002 | rawiobase_doc, /* tp_doc */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1003 | 0, /* tp_traverse */ |
| 1004 | 0, /* tp_clear */ |
| 1005 | 0, /* tp_richcompare */ |
| 1006 | 0, /* tp_weaklistoffset */ |
| 1007 | 0, /* tp_iter */ |
| 1008 | 0, /* tp_iternext */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1009 | rawiobase_methods, /* tp_methods */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1010 | 0, /* tp_members */ |
| 1011 | 0, /* tp_getset */ |
| 1012 | &PyIOBase_Type, /* tp_base */ |
| 1013 | 0, /* tp_dict */ |
| 1014 | 0, /* tp_descr_get */ |
| 1015 | 0, /* tp_descr_set */ |
| 1016 | 0, /* tp_dictoffset */ |
| 1017 | 0, /* tp_init */ |
| 1018 | 0, /* tp_alloc */ |
| 1019 | 0, /* tp_new */ |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 1020 | 0, /* tp_free */ |
| 1021 | 0, /* tp_is_gc */ |
| 1022 | 0, /* tp_bases */ |
| 1023 | 0, /* tp_mro */ |
| 1024 | 0, /* tp_cache */ |
| 1025 | 0, /* tp_subclasses */ |
| 1026 | 0, /* tp_weaklist */ |
| 1027 | 0, /* tp_del */ |
| 1028 | 0, /* tp_version_tag */ |
| 1029 | 0, /* tp_finalize */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1030 | }; |