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 | |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 238 | res = PyObject_CallMethodNoArgs(self, _PyIO_str_flush); |
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(); |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 284 | res = PyObject_CallMethodNoArgs((PyObject *)self, _PyIO_str_close); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 285 | /* Silencing I/O errors is bad, but printing spurious tracebacks is |
| 286 | equally as bad, and potentially more frequent (because of |
| 287 | shutdown issues). */ |
Victor Stinner | 4423504 | 2019-04-12 17:06:47 +0200 | [diff] [blame] | 288 | if (res == NULL) { |
| 289 | #ifndef Py_DEBUG |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 290 | const PyConfig *config = &_PyInterpreterState_GET_UNSAFE()->config; |
Victor Stinner | 4423504 | 2019-04-12 17:06:47 +0200 | [diff] [blame] | 291 | if (config->dev_mode) { |
| 292 | PyErr_WriteUnraisable(self); |
| 293 | } |
| 294 | else { |
| 295 | PyErr_Clear(); |
| 296 | } |
| 297 | #else |
| 298 | PyErr_WriteUnraisable(self); |
| 299 | #endif |
| 300 | } |
| 301 | else { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 302 | Py_DECREF(res); |
Victor Stinner | 4423504 | 2019-04-12 17:06:47 +0200 | [diff] [blame] | 303 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 304 | } |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 305 | |
| 306 | /* Restore the saved exception. */ |
| 307 | PyErr_Restore(error_type, error_value, error_traceback); |
| 308 | } |
| 309 | |
| 310 | int |
| 311 | _PyIOBase_finalize(PyObject *self) |
| 312 | { |
| 313 | int is_zombie; |
| 314 | |
| 315 | /* If _PyIOBase_finalize() is called from a destructor, we need to |
| 316 | resurrect the object as calling close() can invoke arbitrary code. */ |
| 317 | is_zombie = (Py_REFCNT(self) == 0); |
| 318 | if (is_zombie) |
| 319 | return PyObject_CallFinalizerFromDealloc(self); |
| 320 | else { |
| 321 | PyObject_CallFinalizer(self); |
| 322 | return 0; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 323 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 327 | iobase_traverse(iobase *self, visitproc visit, void *arg) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 328 | { |
| 329 | Py_VISIT(self->dict); |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 334 | iobase_clear(iobase *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 335 | { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 336 | Py_CLEAR(self->dict); |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | /* Destructor */ |
| 341 | |
| 342 | static void |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 343 | iobase_dealloc(iobase *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 344 | { |
| 345 | /* NOTE: since IOBaseObject has its own dict, Python-defined attributes |
| 346 | are still available here for close() to use. |
| 347 | However, if the derived class declares a __slots__, those slots are |
| 348 | already gone. |
| 349 | */ |
| 350 | if (_PyIOBase_finalize((PyObject *) self) < 0) { |
| 351 | /* When called from a heap type's dealloc, the type will be |
| 352 | decref'ed on return (see e.g. subtype_dealloc in typeobject.c). */ |
| 353 | if (PyType_HasFeature(Py_TYPE(self), Py_TPFLAGS_HEAPTYPE)) |
| 354 | Py_INCREF(Py_TYPE(self)); |
| 355 | return; |
| 356 | } |
| 357 | _PyObject_GC_UNTRACK(self); |
| 358 | if (self->weakreflist != NULL) |
| 359 | PyObject_ClearWeakRefs((PyObject *) self); |
| 360 | Py_CLEAR(self->dict); |
| 361 | Py_TYPE(self)->tp_free((PyObject *) self); |
| 362 | } |
| 363 | |
| 364 | /* Inquiry methods */ |
| 365 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 366 | /*[clinic input] |
| 367 | _io._IOBase.seekable |
| 368 | |
| 369 | Return whether object supports random access. |
| 370 | |
Martin Panter | 754aab2 | 2016-03-31 07:21:56 +0000 | [diff] [blame] | 371 | If False, seek(), tell() and truncate() will raise OSError. |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 372 | This method may need to do a test seek(). |
| 373 | [clinic start generated code]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 374 | |
| 375 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 376 | _io__IOBase_seekable_impl(PyObject *self) |
Martin Panter | 754aab2 | 2016-03-31 07:21:56 +0000 | [diff] [blame] | 377 | /*[clinic end generated code: output=4c24c67f5f32a43d input=b976622f7fdf3063]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 378 | { |
| 379 | Py_RETURN_FALSE; |
| 380 | } |
| 381 | |
| 382 | PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 383 | _PyIOBase_check_seekable(PyObject *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 384 | { |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 385 | PyObject *res = PyObject_CallMethodNoArgs(self, _PyIO_str_seekable); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 386 | if (res == NULL) |
| 387 | return NULL; |
| 388 | if (res != Py_True) { |
| 389 | Py_CLEAR(res); |
Antoine Pitrou | 0d739d7 | 2010-09-05 23:01:12 +0000 | [diff] [blame] | 390 | iobase_unsupported("File or stream is not seekable."); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 391 | return NULL; |
| 392 | } |
| 393 | if (args == Py_True) { |
| 394 | Py_DECREF(res); |
| 395 | } |
| 396 | return res; |
| 397 | } |
| 398 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 399 | /*[clinic input] |
| 400 | _io._IOBase.readable |
| 401 | |
| 402 | Return whether object was opened for reading. |
| 403 | |
Martin Panter | 754aab2 | 2016-03-31 07:21:56 +0000 | [diff] [blame] | 404 | If False, read() will raise OSError. |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 405 | [clinic start generated code]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 406 | |
| 407 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 408 | _io__IOBase_readable_impl(PyObject *self) |
Martin Panter | 754aab2 | 2016-03-31 07:21:56 +0000 | [diff] [blame] | 409 | /*[clinic end generated code: output=e48089250686388b input=285b3b866a0ec35f]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 410 | { |
| 411 | Py_RETURN_FALSE; |
| 412 | } |
| 413 | |
| 414 | /* May be called with any object */ |
| 415 | PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 416 | _PyIOBase_check_readable(PyObject *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 417 | { |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 418 | PyObject *res = PyObject_CallMethodNoArgs(self, _PyIO_str_readable); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 419 | if (res == NULL) |
| 420 | return NULL; |
| 421 | if (res != Py_True) { |
| 422 | Py_CLEAR(res); |
Antoine Pitrou | 0d739d7 | 2010-09-05 23:01:12 +0000 | [diff] [blame] | 423 | iobase_unsupported("File or stream is not readable."); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 424 | return NULL; |
| 425 | } |
| 426 | if (args == Py_True) { |
| 427 | Py_DECREF(res); |
| 428 | } |
| 429 | return res; |
| 430 | } |
| 431 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 432 | /*[clinic input] |
| 433 | _io._IOBase.writable |
| 434 | |
| 435 | Return whether object was opened for writing. |
| 436 | |
Martin Panter | 754aab2 | 2016-03-31 07:21:56 +0000 | [diff] [blame] | 437 | If False, write() will raise OSError. |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 438 | [clinic start generated code]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 439 | |
| 440 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 441 | _io__IOBase_writable_impl(PyObject *self) |
Martin Panter | 754aab2 | 2016-03-31 07:21:56 +0000 | [diff] [blame] | 442 | /*[clinic end generated code: output=406001d0985be14f input=9dcac18a013a05b5]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 443 | { |
| 444 | Py_RETURN_FALSE; |
| 445 | } |
| 446 | |
| 447 | /* May be called with any object */ |
| 448 | PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 449 | _PyIOBase_check_writable(PyObject *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 450 | { |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 451 | PyObject *res = PyObject_CallMethodNoArgs(self, _PyIO_str_writable); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 452 | if (res == NULL) |
| 453 | return NULL; |
| 454 | if (res != Py_True) { |
| 455 | Py_CLEAR(res); |
Antoine Pitrou | 0d739d7 | 2010-09-05 23:01:12 +0000 | [diff] [blame] | 456 | iobase_unsupported("File or stream is not writable."); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 457 | return NULL; |
| 458 | } |
| 459 | if (args == Py_True) { |
| 460 | Py_DECREF(res); |
| 461 | } |
| 462 | return res; |
| 463 | } |
| 464 | |
| 465 | /* Context manager */ |
| 466 | |
| 467 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 468 | iobase_enter(PyObject *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 469 | { |
Serhiy Storchaka | 4d9aec0 | 2018-01-16 18:34:21 +0200 | [diff] [blame] | 470 | if (iobase_check_closed(self)) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 471 | return NULL; |
| 472 | |
| 473 | Py_INCREF(self); |
| 474 | return self; |
| 475 | } |
| 476 | |
| 477 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 478 | iobase_exit(PyObject *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 479 | { |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 480 | return PyObject_CallMethodNoArgs(self, _PyIO_str_close); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | /* Lower-level APIs */ |
| 484 | |
| 485 | /* XXX Should these be present even if unimplemented? */ |
| 486 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 487 | /*[clinic input] |
| 488 | _io._IOBase.fileno |
| 489 | |
| 490 | Returns underlying file descriptor if one exists. |
| 491 | |
Martin Panter | 754aab2 | 2016-03-31 07:21:56 +0000 | [diff] [blame] | 492 | 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] | 493 | [clinic start generated code]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 494 | |
| 495 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 496 | _io__IOBase_fileno_impl(PyObject *self) |
Martin Panter | 754aab2 | 2016-03-31 07:21:56 +0000 | [diff] [blame] | 497 | /*[clinic end generated code: output=7cc0973f0f5f3b73 input=4e37028947dc1cc8]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 498 | { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 499 | return iobase_unsupported("fileno"); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 502 | /*[clinic input] |
| 503 | _io._IOBase.isatty |
| 504 | |
| 505 | Return whether this is an 'interactive' stream. |
| 506 | |
| 507 | Return False if it can't be determined. |
| 508 | [clinic start generated code]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 509 | |
| 510 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 511 | _io__IOBase_isatty_impl(PyObject *self) |
| 512 | /*[clinic end generated code: output=60cab77cede41cdd input=9ef76530d368458b]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 513 | { |
Serhiy Storchaka | 4d9aec0 | 2018-01-16 18:34:21 +0200 | [diff] [blame] | 514 | if (iobase_check_closed(self)) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 515 | return NULL; |
| 516 | Py_RETURN_FALSE; |
| 517 | } |
| 518 | |
| 519 | /* Readline(s) and writelines */ |
| 520 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 521 | /*[clinic input] |
| 522 | _io._IOBase.readline |
Serhiy Storchaka | 762bf40 | 2017-03-30 09:15:31 +0300 | [diff] [blame] | 523 | size as limit: Py_ssize_t(accept={int, NoneType}) = -1 |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 524 | / |
| 525 | |
| 526 | Read and return a line from the stream. |
| 527 | |
| 528 | If size is specified, at most size bytes will be read. |
| 529 | |
| 530 | The line terminator is always b'\n' for binary files; for text |
| 531 | files, the newlines argument to open can be used to select the line |
| 532 | terminator(s) recognized. |
| 533 | [clinic start generated code]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 534 | |
| 535 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 536 | _io__IOBase_readline_impl(PyObject *self, Py_ssize_t limit) |
Serhiy Storchaka | 762bf40 | 2017-03-30 09:15:31 +0300 | [diff] [blame] | 537 | /*[clinic end generated code: output=4479f79b58187840 input=d0c596794e877bff]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 538 | { |
| 539 | /* For backwards compatibility, a (slowish) readline(). */ |
| 540 | |
Serhiy Storchaka | 4d9aec0 | 2018-01-16 18:34:21 +0200 | [diff] [blame] | 541 | PyObject *peek, *buffer, *result; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 542 | Py_ssize_t old_size = -1; |
| 543 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 544 | if (_PyObject_LookupAttr(self, _PyIO_str_peek, &peek) < 0) { |
Serhiy Storchaka | 4d9aec0 | 2018-01-16 18:34:21 +0200 | [diff] [blame] | 545 | return NULL; |
| 546 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 547 | |
| 548 | buffer = PyByteArray_FromStringAndSize(NULL, 0); |
Serhiy Storchaka | 4d9aec0 | 2018-01-16 18:34:21 +0200 | [diff] [blame] | 549 | if (buffer == NULL) { |
| 550 | Py_XDECREF(peek); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 551 | return NULL; |
Serhiy Storchaka | 4d9aec0 | 2018-01-16 18:34:21 +0200 | [diff] [blame] | 552 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 553 | |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 554 | while (limit < 0 || PyByteArray_GET_SIZE(buffer) < limit) { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 555 | Py_ssize_t nreadahead = 1; |
| 556 | PyObject *b; |
| 557 | |
Serhiy Storchaka | 4d9aec0 | 2018-01-16 18:34:21 +0200 | [diff] [blame] | 558 | if (peek != NULL) { |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 559 | PyObject *readahead = PyObject_CallOneArg(peek, _PyLong_One); |
Gregory P. Smith | 5135992 | 2012-06-23 23:55:39 -0700 | [diff] [blame] | 560 | if (readahead == NULL) { |
| 561 | /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() |
| 562 | when EINTR occurs so we needn't do it ourselves. */ |
| 563 | if (_PyIO_trap_eintr()) { |
| 564 | continue; |
| 565 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 566 | goto fail; |
Gregory P. Smith | 5135992 | 2012-06-23 23:55:39 -0700 | [diff] [blame] | 567 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 568 | if (!PyBytes_Check(readahead)) { |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 569 | PyErr_Format(PyExc_OSError, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 570 | "peek() should have returned a bytes object, " |
| 571 | "not '%.200s'", Py_TYPE(readahead)->tp_name); |
| 572 | Py_DECREF(readahead); |
| 573 | goto fail; |
| 574 | } |
| 575 | if (PyBytes_GET_SIZE(readahead) > 0) { |
| 576 | Py_ssize_t n = 0; |
| 577 | const char *buf = PyBytes_AS_STRING(readahead); |
| 578 | if (limit >= 0) { |
| 579 | do { |
| 580 | if (n >= PyBytes_GET_SIZE(readahead) || n >= limit) |
| 581 | break; |
| 582 | if (buf[n++] == '\n') |
| 583 | break; |
| 584 | } while (1); |
| 585 | } |
| 586 | else { |
| 587 | do { |
| 588 | if (n >= PyBytes_GET_SIZE(readahead)) |
| 589 | break; |
| 590 | if (buf[n++] == '\n') |
| 591 | break; |
| 592 | } while (1); |
| 593 | } |
| 594 | nreadahead = n; |
| 595 | } |
| 596 | Py_DECREF(readahead); |
| 597 | } |
| 598 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 599 | b = _PyObject_CallMethodId(self, &PyId_read, "n", nreadahead); |
Gregory P. Smith | 5135992 | 2012-06-23 23:55:39 -0700 | [diff] [blame] | 600 | if (b == NULL) { |
| 601 | /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() |
| 602 | when EINTR occurs so we needn't do it ourselves. */ |
| 603 | if (_PyIO_trap_eintr()) { |
| 604 | continue; |
| 605 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 606 | goto fail; |
Gregory P. Smith | 5135992 | 2012-06-23 23:55:39 -0700 | [diff] [blame] | 607 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 608 | if (!PyBytes_Check(b)) { |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 609 | PyErr_Format(PyExc_OSError, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 610 | "read() should have returned a bytes object, " |
| 611 | "not '%.200s'", Py_TYPE(b)->tp_name); |
| 612 | Py_DECREF(b); |
| 613 | goto fail; |
| 614 | } |
| 615 | if (PyBytes_GET_SIZE(b) == 0) { |
| 616 | Py_DECREF(b); |
| 617 | break; |
| 618 | } |
| 619 | |
| 620 | old_size = PyByteArray_GET_SIZE(buffer); |
Victor Stinner | cc024d1 | 2013-10-29 02:23:46 +0100 | [diff] [blame] | 621 | if (PyByteArray_Resize(buffer, old_size + PyBytes_GET_SIZE(b)) < 0) { |
| 622 | Py_DECREF(b); |
| 623 | goto fail; |
| 624 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 625 | memcpy(PyByteArray_AS_STRING(buffer) + old_size, |
| 626 | PyBytes_AS_STRING(b), PyBytes_GET_SIZE(b)); |
| 627 | |
| 628 | Py_DECREF(b); |
| 629 | |
| 630 | if (PyByteArray_AS_STRING(buffer)[PyByteArray_GET_SIZE(buffer) - 1] == '\n') |
| 631 | break; |
| 632 | } |
| 633 | |
| 634 | result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(buffer), |
| 635 | PyByteArray_GET_SIZE(buffer)); |
Serhiy Storchaka | 4d9aec0 | 2018-01-16 18:34:21 +0200 | [diff] [blame] | 636 | Py_XDECREF(peek); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 637 | Py_DECREF(buffer); |
| 638 | return result; |
| 639 | fail: |
Serhiy Storchaka | 4d9aec0 | 2018-01-16 18:34:21 +0200 | [diff] [blame] | 640 | Py_XDECREF(peek); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 641 | Py_DECREF(buffer); |
| 642 | return NULL; |
| 643 | } |
| 644 | |
| 645 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 646 | iobase_iter(PyObject *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 647 | { |
Serhiy Storchaka | 4d9aec0 | 2018-01-16 18:34:21 +0200 | [diff] [blame] | 648 | if (iobase_check_closed(self)) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 649 | return NULL; |
| 650 | |
| 651 | Py_INCREF(self); |
| 652 | return self; |
| 653 | } |
| 654 | |
| 655 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 656 | iobase_iternext(PyObject *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 657 | { |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 658 | PyObject *line = PyObject_CallMethodNoArgs(self, _PyIO_str_readline); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 659 | |
| 660 | if (line == NULL) |
| 661 | return NULL; |
| 662 | |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 663 | if (PyObject_Size(line) <= 0) { |
| 664 | /* Error or empty */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 665 | Py_DECREF(line); |
| 666 | return NULL; |
| 667 | } |
| 668 | |
| 669 | return line; |
| 670 | } |
| 671 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 672 | /*[clinic input] |
| 673 | _io._IOBase.readlines |
Serhiy Storchaka | 762bf40 | 2017-03-30 09:15:31 +0300 | [diff] [blame] | 674 | hint: Py_ssize_t(accept={int, NoneType}) = -1 |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 675 | / |
| 676 | |
| 677 | Return a list of lines from the stream. |
| 678 | |
| 679 | hint can be specified to control the number of lines read: no more |
| 680 | lines will be read if the total size (in bytes/characters) of all |
| 681 | lines so far exceeds hint. |
| 682 | [clinic start generated code]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 683 | |
| 684 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 685 | _io__IOBase_readlines_impl(PyObject *self, Py_ssize_t hint) |
Serhiy Storchaka | 762bf40 | 2017-03-30 09:15:31 +0300 | [diff] [blame] | 686 | /*[clinic end generated code: output=2f50421677fa3dea input=9400c786ea9dc416]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 687 | { |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 688 | Py_ssize_t length = 0; |
Xiang Zhang | 026435c | 2017-04-15 12:47:28 +0800 | [diff] [blame] | 689 | PyObject *result, *it = NULL; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 690 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 691 | result = PyList_New(0); |
| 692 | if (result == NULL) |
| 693 | return NULL; |
| 694 | |
| 695 | if (hint <= 0) { |
| 696 | /* XXX special-casing this made sense in the Python version in order |
| 697 | to remove the bytecode interpretation overhead, but it could |
| 698 | probably be removed here. */ |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 699 | _Py_IDENTIFIER(extend); |
Victor Stinner | 61bdb0d | 2016-12-09 15:39:28 +0100 | [diff] [blame] | 700 | PyObject *ret = _PyObject_CallMethodIdObjArgs(result, &PyId_extend, |
| 701 | self, NULL); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 702 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 703 | if (ret == NULL) { |
Xiang Zhang | 026435c | 2017-04-15 12:47:28 +0800 | [diff] [blame] | 704 | goto error; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 705 | } |
| 706 | Py_DECREF(ret); |
| 707 | return result; |
| 708 | } |
| 709 | |
Xiang Zhang | 026435c | 2017-04-15 12:47:28 +0800 | [diff] [blame] | 710 | it = PyObject_GetIter(self); |
| 711 | if (it == NULL) { |
| 712 | goto error; |
| 713 | } |
| 714 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 715 | while (1) { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 716 | Py_ssize_t line_length; |
Xiang Zhang | 026435c | 2017-04-15 12:47:28 +0800 | [diff] [blame] | 717 | PyObject *line = PyIter_Next(it); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 718 | if (line == NULL) { |
| 719 | if (PyErr_Occurred()) { |
Xiang Zhang | 026435c | 2017-04-15 12:47:28 +0800 | [diff] [blame] | 720 | goto error; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 721 | } |
| 722 | else |
| 723 | break; /* StopIteration raised */ |
| 724 | } |
| 725 | |
| 726 | if (PyList_Append(result, line) < 0) { |
| 727 | Py_DECREF(line); |
Xiang Zhang | 026435c | 2017-04-15 12:47:28 +0800 | [diff] [blame] | 728 | goto error; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 729 | } |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 730 | line_length = PyObject_Size(line); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 731 | Py_DECREF(line); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 732 | if (line_length < 0) { |
| 733 | goto error; |
| 734 | } |
| 735 | if (line_length > hint - length) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 736 | break; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 737 | length += line_length; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 738 | } |
Xiang Zhang | 026435c | 2017-04-15 12:47:28 +0800 | [diff] [blame] | 739 | |
| 740 | Py_DECREF(it); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 741 | return result; |
Xiang Zhang | 026435c | 2017-04-15 12:47:28 +0800 | [diff] [blame] | 742 | |
| 743 | error: |
| 744 | Py_XDECREF(it); |
| 745 | Py_DECREF(result); |
| 746 | return NULL; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 749 | /*[clinic input] |
| 750 | _io._IOBase.writelines |
| 751 | lines: object |
| 752 | / |
Marcin Niemira | ab86521 | 2019-04-22 21:13:51 +1000 | [diff] [blame] | 753 | |
| 754 | Write a list of lines to stream. |
| 755 | |
| 756 | Line separators are not added, so it is usual for each of the |
| 757 | lines provided to have a line separator at the end. |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 758 | [clinic start generated code]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 759 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 760 | static PyObject * |
| 761 | _io__IOBase_writelines(PyObject *self, PyObject *lines) |
Marcin Niemira | ab86521 | 2019-04-22 21:13:51 +1000 | [diff] [blame] | 762 | /*[clinic end generated code: output=976eb0a9b60a6628 input=cac3fc8864183359]*/ |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 763 | { |
| 764 | PyObject *iter, *res; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 765 | |
Serhiy Storchaka | 4d9aec0 | 2018-01-16 18:34:21 +0200 | [diff] [blame] | 766 | if (iobase_check_closed(self)) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 767 | return NULL; |
| 768 | |
| 769 | iter = PyObject_GetIter(lines); |
| 770 | if (iter == NULL) |
| 771 | return NULL; |
| 772 | |
| 773 | while (1) { |
| 774 | PyObject *line = PyIter_Next(iter); |
| 775 | if (line == NULL) { |
| 776 | if (PyErr_Occurred()) { |
| 777 | Py_DECREF(iter); |
| 778 | return NULL; |
| 779 | } |
| 780 | else |
| 781 | break; /* Stop Iteration */ |
| 782 | } |
| 783 | |
Gregory P. Smith | b9817b0 | 2013-02-01 13:03:39 -0800 | [diff] [blame] | 784 | res = NULL; |
| 785 | do { |
| 786 | res = PyObject_CallMethodObjArgs(self, _PyIO_str_write, line, NULL); |
| 787 | } while (res == NULL && _PyIO_trap_eintr()); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 788 | Py_DECREF(line); |
| 789 | if (res == NULL) { |
| 790 | Py_DECREF(iter); |
| 791 | return NULL; |
| 792 | } |
| 793 | Py_DECREF(res); |
| 794 | } |
| 795 | Py_DECREF(iter); |
| 796 | Py_RETURN_NONE; |
| 797 | } |
| 798 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 799 | #include "clinic/iobase.c.h" |
| 800 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 801 | static PyMethodDef iobase_methods[] = { |
| 802 | {"seek", iobase_seek, METH_VARARGS, iobase_seek_doc}, |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 803 | _IO__IOBASE_TELL_METHODDEF |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 804 | {"truncate", iobase_truncate, METH_VARARGS, iobase_truncate_doc}, |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 805 | _IO__IOBASE_FLUSH_METHODDEF |
| 806 | _IO__IOBASE_CLOSE_METHODDEF |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 807 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 808 | _IO__IOBASE_SEEKABLE_METHODDEF |
| 809 | _IO__IOBASE_READABLE_METHODDEF |
| 810 | _IO__IOBASE_WRITABLE_METHODDEF |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 811 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 812 | {"_checkClosed", _PyIOBase_check_closed, METH_NOARGS}, |
| 813 | {"_checkSeekable", _PyIOBase_check_seekable, METH_NOARGS}, |
| 814 | {"_checkReadable", _PyIOBase_check_readable, METH_NOARGS}, |
| 815 | {"_checkWritable", _PyIOBase_check_writable, METH_NOARGS}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 816 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 817 | _IO__IOBASE_FILENO_METHODDEF |
| 818 | _IO__IOBASE_ISATTY_METHODDEF |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 819 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 820 | {"__enter__", iobase_enter, METH_NOARGS}, |
| 821 | {"__exit__", iobase_exit, METH_VARARGS}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 822 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 823 | _IO__IOBASE_READLINE_METHODDEF |
| 824 | _IO__IOBASE_READLINES_METHODDEF |
| 825 | _IO__IOBASE_WRITELINES_METHODDEF |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 826 | |
| 827 | {NULL, NULL} |
| 828 | }; |
| 829 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 830 | static PyGetSetDef iobase_getset[] = { |
Benjamin Peterson | 23d7f12 | 2012-02-19 20:02:57 -0500 | [diff] [blame] | 831 | {"__dict__", PyObject_GenericGetDict, NULL, NULL}, |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 832 | {"closed", (getter)iobase_closed_get, NULL, NULL}, |
Benjamin Peterson | 1fea321 | 2009-04-19 03:15:20 +0000 | [diff] [blame] | 833 | {NULL} |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 834 | }; |
| 835 | |
| 836 | |
| 837 | PyTypeObject PyIOBase_Type = { |
| 838 | PyVarObject_HEAD_INIT(NULL, 0) |
| 839 | "_io._IOBase", /*tp_name*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 840 | sizeof(iobase), /*tp_basicsize*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 841 | 0, /*tp_itemsize*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 842 | (destructor)iobase_dealloc, /*tp_dealloc*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 843 | 0, /*tp_vectorcall_offset*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 844 | 0, /*tp_getattr*/ |
| 845 | 0, /*tp_setattr*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 846 | 0, /*tp_as_async*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 847 | 0, /*tp_repr*/ |
| 848 | 0, /*tp_as_number*/ |
| 849 | 0, /*tp_as_sequence*/ |
| 850 | 0, /*tp_as_mapping*/ |
| 851 | 0, /*tp_hash */ |
| 852 | 0, /*tp_call*/ |
| 853 | 0, /*tp_str*/ |
| 854 | 0, /*tp_getattro*/ |
| 855 | 0, /*tp_setattro*/ |
| 856 | 0, /*tp_as_buffer*/ |
| 857 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
Antoine Pitrou | ada319b | 2019-05-29 22:12:38 +0200 | [diff] [blame] | 858 | | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 859 | iobase_doc, /* tp_doc */ |
| 860 | (traverseproc)iobase_traverse, /* tp_traverse */ |
| 861 | (inquiry)iobase_clear, /* tp_clear */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 862 | 0, /* tp_richcompare */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 863 | offsetof(iobase, weakreflist), /* tp_weaklistoffset */ |
| 864 | iobase_iter, /* tp_iter */ |
| 865 | iobase_iternext, /* tp_iternext */ |
| 866 | iobase_methods, /* tp_methods */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 867 | 0, /* tp_members */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 868 | iobase_getset, /* tp_getset */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 869 | 0, /* tp_base */ |
| 870 | 0, /* tp_dict */ |
| 871 | 0, /* tp_descr_get */ |
| 872 | 0, /* tp_descr_set */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 873 | offsetof(iobase, dict), /* tp_dictoffset */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 874 | 0, /* tp_init */ |
| 875 | 0, /* tp_alloc */ |
| 876 | PyType_GenericNew, /* tp_new */ |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 877 | 0, /* tp_free */ |
| 878 | 0, /* tp_is_gc */ |
| 879 | 0, /* tp_bases */ |
| 880 | 0, /* tp_mro */ |
| 881 | 0, /* tp_cache */ |
| 882 | 0, /* tp_subclasses */ |
| 883 | 0, /* tp_weaklist */ |
| 884 | 0, /* tp_del */ |
| 885 | 0, /* tp_version_tag */ |
Victor Stinner | 928bff0 | 2016-03-19 10:36:36 +0100 | [diff] [blame] | 886 | iobase_finalize, /* tp_finalize */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 887 | }; |
| 888 | |
| 889 | |
| 890 | /* |
| 891 | * RawIOBase class, Inherits from IOBase. |
| 892 | */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 893 | PyDoc_STRVAR(rawiobase_doc, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 894 | "Base class for raw binary I/O."); |
| 895 | |
| 896 | /* |
| 897 | * The read() method is implemented by calling readinto(); derived classes |
| 898 | * that want to support read() only need to implement readinto() as a |
| 899 | * primitive operation. In general, readinto() can be more efficient than |
| 900 | * read(). |
| 901 | * |
| 902 | * (It would be tempting to also provide an implementation of readinto() in |
| 903 | * terms of read(), in case the latter is a more suitable primitive operation, |
| 904 | * but that would lead to nasty recursion in case a subclass doesn't implement |
| 905 | * either.) |
| 906 | */ |
| 907 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 908 | /*[clinic input] |
| 909 | _io._RawIOBase.read |
| 910 | size as n: Py_ssize_t = -1 |
| 911 | / |
| 912 | [clinic start generated code]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 913 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 914 | static PyObject * |
| 915 | _io__RawIOBase_read_impl(PyObject *self, Py_ssize_t n) |
| 916 | /*[clinic end generated code: output=6cdeb731e3c9f13c input=b6d0dcf6417d1374]*/ |
| 917 | { |
| 918 | PyObject *b, *res; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 919 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 920 | if (n < 0) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 921 | _Py_IDENTIFIER(readall); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 922 | |
Jeroen Demeyer | 762f93f | 2019-07-08 10:19:25 +0200 | [diff] [blame] | 923 | return _PyObject_CallMethodIdNoArgs(self, &PyId_readall); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 924 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 925 | |
| 926 | /* TODO: allocate a bytes object directly instead and manually construct |
| 927 | a writable memoryview pointing to it. */ |
| 928 | b = PyByteArray_FromStringAndSize(NULL, n); |
| 929 | if (b == NULL) |
| 930 | return NULL; |
| 931 | |
| 932 | res = PyObject_CallMethodObjArgs(self, _PyIO_str_readinto, b, NULL); |
Antoine Pitrou | 328ec74 | 2010-09-14 18:37:24 +0000 | [diff] [blame] | 933 | if (res == NULL || res == Py_None) { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 934 | Py_DECREF(b); |
Antoine Pitrou | 328ec74 | 2010-09-14 18:37:24 +0000 | [diff] [blame] | 935 | return res; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | n = PyNumber_AsSsize_t(res, PyExc_ValueError); |
| 939 | Py_DECREF(res); |
| 940 | if (n == -1 && PyErr_Occurred()) { |
| 941 | Py_DECREF(b); |
| 942 | return NULL; |
| 943 | } |
| 944 | |
| 945 | res = PyBytes_FromStringAndSize(PyByteArray_AsString(b), n); |
| 946 | Py_DECREF(b); |
| 947 | return res; |
| 948 | } |
| 949 | |
| 950 | |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 951 | /*[clinic input] |
| 952 | _io._RawIOBase.readall |
| 953 | |
| 954 | Read until EOF, using multiple read() call. |
| 955 | [clinic start generated code]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 956 | |
| 957 | static PyObject * |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 958 | _io__RawIOBase_readall_impl(PyObject *self) |
| 959 | /*[clinic end generated code: output=1987b9ce929425a0 input=688874141213622a]*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 960 | { |
Antoine Pitrou | 00a9b73 | 2009-03-29 19:19:49 +0000 | [diff] [blame] | 961 | int r; |
| 962 | PyObject *chunks = PyList_New(0); |
| 963 | PyObject *result; |
Victor Stinner | cc024d1 | 2013-10-29 02:23:46 +0100 | [diff] [blame] | 964 | |
Antoine Pitrou | 00a9b73 | 2009-03-29 19:19:49 +0000 | [diff] [blame] | 965 | if (chunks == NULL) |
| 966 | return NULL; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 967 | |
| 968 | while (1) { |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 969 | PyObject *data = _PyObject_CallMethodId(self, &PyId_read, |
| 970 | "i", DEFAULT_BUFFER_SIZE); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 971 | if (!data) { |
Gregory P. Smith | 5135992 | 2012-06-23 23:55:39 -0700 | [diff] [blame] | 972 | /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() |
| 973 | when EINTR occurs so we needn't do it ourselves. */ |
| 974 | if (_PyIO_trap_eintr()) { |
| 975 | continue; |
| 976 | } |
Antoine Pitrou | 00a9b73 | 2009-03-29 19:19:49 +0000 | [diff] [blame] | 977 | Py_DECREF(chunks); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 978 | return NULL; |
| 979 | } |
Victor Stinner | a80987f | 2011-05-25 22:47:16 +0200 | [diff] [blame] | 980 | if (data == Py_None) { |
| 981 | if (PyList_GET_SIZE(chunks) == 0) { |
| 982 | Py_DECREF(chunks); |
| 983 | return data; |
| 984 | } |
| 985 | Py_DECREF(data); |
| 986 | break; |
| 987 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 988 | if (!PyBytes_Check(data)) { |
Antoine Pitrou | 00a9b73 | 2009-03-29 19:19:49 +0000 | [diff] [blame] | 989 | Py_DECREF(chunks); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 990 | Py_DECREF(data); |
| 991 | PyErr_SetString(PyExc_TypeError, "read() should return bytes"); |
| 992 | return NULL; |
| 993 | } |
Antoine Pitrou | 00a9b73 | 2009-03-29 19:19:49 +0000 | [diff] [blame] | 994 | if (PyBytes_GET_SIZE(data) == 0) { |
| 995 | /* EOF */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 996 | Py_DECREF(data); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 997 | break; |
Antoine Pitrou | 00a9b73 | 2009-03-29 19:19:49 +0000 | [diff] [blame] | 998 | } |
| 999 | r = PyList_Append(chunks, data); |
| 1000 | Py_DECREF(data); |
| 1001 | if (r < 0) { |
| 1002 | Py_DECREF(chunks); |
| 1003 | return NULL; |
| 1004 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1005 | } |
Antoine Pitrou | 00a9b73 | 2009-03-29 19:19:49 +0000 | [diff] [blame] | 1006 | result = _PyBytes_Join(_PyIO_empty_bytes, chunks); |
| 1007 | Py_DECREF(chunks); |
| 1008 | return result; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
Antoine Pitrou | 45d6156 | 2015-05-20 21:50:59 +0200 | [diff] [blame] | 1011 | static PyObject * |
| 1012 | rawiobase_readinto(PyObject *self, PyObject *args) |
| 1013 | { |
| 1014 | PyErr_SetNone(PyExc_NotImplementedError); |
| 1015 | return NULL; |
| 1016 | } |
| 1017 | |
| 1018 | static PyObject * |
| 1019 | rawiobase_write(PyObject *self, PyObject *args) |
| 1020 | { |
| 1021 | PyErr_SetNone(PyExc_NotImplementedError); |
| 1022 | return NULL; |
| 1023 | } |
| 1024 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1025 | static PyMethodDef rawiobase_methods[] = { |
Serhiy Storchaka | f24131f | 2015-04-16 11:19:43 +0300 | [diff] [blame] | 1026 | _IO__RAWIOBASE_READ_METHODDEF |
| 1027 | _IO__RAWIOBASE_READALL_METHODDEF |
Antoine Pitrou | 45d6156 | 2015-05-20 21:50:59 +0200 | [diff] [blame] | 1028 | {"readinto", rawiobase_readinto, METH_VARARGS}, |
| 1029 | {"write", rawiobase_write, METH_VARARGS}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1030 | {NULL, NULL} |
| 1031 | }; |
| 1032 | |
| 1033 | PyTypeObject PyRawIOBase_Type = { |
| 1034 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1035 | "_io._RawIOBase", /*tp_name*/ |
| 1036 | 0, /*tp_basicsize*/ |
| 1037 | 0, /*tp_itemsize*/ |
| 1038 | 0, /*tp_dealloc*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 1039 | 0, /*tp_vectorcall_offset*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1040 | 0, /*tp_getattr*/ |
| 1041 | 0, /*tp_setattr*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 1042 | 0, /*tp_as_async*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1043 | 0, /*tp_repr*/ |
| 1044 | 0, /*tp_as_number*/ |
| 1045 | 0, /*tp_as_sequence*/ |
| 1046 | 0, /*tp_as_mapping*/ |
| 1047 | 0, /*tp_hash */ |
| 1048 | 0, /*tp_call*/ |
| 1049 | 0, /*tp_str*/ |
| 1050 | 0, /*tp_getattro*/ |
| 1051 | 0, /*tp_setattro*/ |
| 1052 | 0, /*tp_as_buffer*/ |
Antoine Pitrou | ada319b | 2019-05-29 22:12:38 +0200 | [diff] [blame] | 1053 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1054 | rawiobase_doc, /* tp_doc */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1055 | 0, /* tp_traverse */ |
| 1056 | 0, /* tp_clear */ |
| 1057 | 0, /* tp_richcompare */ |
| 1058 | 0, /* tp_weaklistoffset */ |
| 1059 | 0, /* tp_iter */ |
| 1060 | 0, /* tp_iternext */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1061 | rawiobase_methods, /* tp_methods */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1062 | 0, /* tp_members */ |
| 1063 | 0, /* tp_getset */ |
| 1064 | &PyIOBase_Type, /* tp_base */ |
| 1065 | 0, /* tp_dict */ |
| 1066 | 0, /* tp_descr_get */ |
| 1067 | 0, /* tp_descr_set */ |
| 1068 | 0, /* tp_dictoffset */ |
| 1069 | 0, /* tp_init */ |
| 1070 | 0, /* tp_alloc */ |
| 1071 | 0, /* tp_new */ |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 1072 | 0, /* tp_free */ |
| 1073 | 0, /* tp_is_gc */ |
| 1074 | 0, /* tp_bases */ |
| 1075 | 0, /* tp_mro */ |
| 1076 | 0, /* tp_cache */ |
| 1077 | 0, /* tp_subclasses */ |
| 1078 | 0, /* tp_weaklist */ |
| 1079 | 0, /* tp_del */ |
| 1080 | 0, /* tp_version_tag */ |
| 1081 | 0, /* tp_finalize */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1082 | }; |