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