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