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