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