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