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); |
| 485 | if (readahead == NULL) |
| 486 | goto fail; |
| 487 | if (!PyBytes_Check(readahead)) { |
| 488 | PyErr_Format(PyExc_IOError, |
| 489 | "peek() should have returned a bytes object, " |
| 490 | "not '%.200s'", Py_TYPE(readahead)->tp_name); |
| 491 | Py_DECREF(readahead); |
| 492 | goto fail; |
| 493 | } |
| 494 | if (PyBytes_GET_SIZE(readahead) > 0) { |
| 495 | Py_ssize_t n = 0; |
| 496 | const char *buf = PyBytes_AS_STRING(readahead); |
| 497 | if (limit >= 0) { |
| 498 | do { |
| 499 | if (n >= PyBytes_GET_SIZE(readahead) || n >= limit) |
| 500 | break; |
| 501 | if (buf[n++] == '\n') |
| 502 | break; |
| 503 | } while (1); |
| 504 | } |
| 505 | else { |
| 506 | do { |
| 507 | if (n >= PyBytes_GET_SIZE(readahead)) |
| 508 | break; |
| 509 | if (buf[n++] == '\n') |
| 510 | break; |
| 511 | } while (1); |
| 512 | } |
| 513 | nreadahead = n; |
| 514 | } |
| 515 | Py_DECREF(readahead); |
| 516 | } |
| 517 | |
| 518 | b = PyObject_CallMethod(self, "read", "n", nreadahead); |
| 519 | if (b == NULL) |
| 520 | goto fail; |
| 521 | if (!PyBytes_Check(b)) { |
| 522 | PyErr_Format(PyExc_IOError, |
| 523 | "read() should have returned a bytes object, " |
| 524 | "not '%.200s'", Py_TYPE(b)->tp_name); |
| 525 | Py_DECREF(b); |
| 526 | goto fail; |
| 527 | } |
| 528 | if (PyBytes_GET_SIZE(b) == 0) { |
| 529 | Py_DECREF(b); |
| 530 | break; |
| 531 | } |
| 532 | |
| 533 | old_size = PyByteArray_GET_SIZE(buffer); |
| 534 | PyByteArray_Resize(buffer, old_size + PyBytes_GET_SIZE(b)); |
| 535 | memcpy(PyByteArray_AS_STRING(buffer) + old_size, |
| 536 | PyBytes_AS_STRING(b), PyBytes_GET_SIZE(b)); |
| 537 | |
| 538 | Py_DECREF(b); |
| 539 | |
| 540 | if (PyByteArray_AS_STRING(buffer)[PyByteArray_GET_SIZE(buffer) - 1] == '\n') |
| 541 | break; |
| 542 | } |
| 543 | |
| 544 | result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(buffer), |
| 545 | PyByteArray_GET_SIZE(buffer)); |
| 546 | Py_DECREF(buffer); |
| 547 | return result; |
| 548 | fail: |
| 549 | Py_DECREF(buffer); |
| 550 | return NULL; |
| 551 | } |
| 552 | |
| 553 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 554 | iobase_iter(PyObject *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 555 | { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 556 | if (_PyIOBase_check_closed(self, Py_True) == NULL) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 557 | return NULL; |
| 558 | |
| 559 | Py_INCREF(self); |
| 560 | return self; |
| 561 | } |
| 562 | |
| 563 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 564 | iobase_iternext(PyObject *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 565 | { |
| 566 | PyObject *line = PyObject_CallMethodObjArgs(self, _PyIO_str_readline, NULL); |
| 567 | |
| 568 | if (line == NULL) |
| 569 | return NULL; |
| 570 | |
| 571 | if (PyObject_Size(line) == 0) { |
| 572 | Py_DECREF(line); |
| 573 | return NULL; |
| 574 | } |
| 575 | |
| 576 | return line; |
| 577 | } |
| 578 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 579 | PyDoc_STRVAR(iobase_readlines_doc, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 580 | "Return a list of lines from the stream.\n" |
| 581 | "\n" |
| 582 | "hint can be specified to control the number of lines read: no more\n" |
| 583 | "lines will be read if the total size (in bytes/characters) of all\n" |
| 584 | "lines so far exceeds hint."); |
| 585 | |
| 586 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 587 | iobase_readlines(PyObject *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 588 | { |
| 589 | Py_ssize_t hint = -1, length = 0; |
Benjamin Peterson | 0551613 | 2009-12-13 19:28:09 +0000 | [diff] [blame] | 590 | PyObject *result; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 591 | |
Benjamin Peterson | bf5ff76 | 2009-12-13 19:25:34 +0000 | [diff] [blame] | 592 | if (!PyArg_ParseTuple(args, "|O&:readlines", &_PyIO_ConvertSsize_t, &hint)) { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 593 | return NULL; |
| 594 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 595 | |
| 596 | result = PyList_New(0); |
| 597 | if (result == NULL) |
| 598 | return NULL; |
| 599 | |
| 600 | if (hint <= 0) { |
| 601 | /* XXX special-casing this made sense in the Python version in order |
| 602 | to remove the bytecode interpretation overhead, but it could |
| 603 | probably be removed here. */ |
| 604 | PyObject *ret = PyObject_CallMethod(result, "extend", "O", self); |
| 605 | if (ret == NULL) { |
| 606 | Py_DECREF(result); |
| 607 | return NULL; |
| 608 | } |
| 609 | Py_DECREF(ret); |
| 610 | return result; |
| 611 | } |
| 612 | |
| 613 | while (1) { |
| 614 | PyObject *line = PyIter_Next(self); |
| 615 | if (line == NULL) { |
| 616 | if (PyErr_Occurred()) { |
| 617 | Py_DECREF(result); |
| 618 | return NULL; |
| 619 | } |
| 620 | else |
| 621 | break; /* StopIteration raised */ |
| 622 | } |
| 623 | |
| 624 | if (PyList_Append(result, line) < 0) { |
| 625 | Py_DECREF(line); |
| 626 | Py_DECREF(result); |
| 627 | return NULL; |
| 628 | } |
| 629 | length += PyObject_Size(line); |
| 630 | Py_DECREF(line); |
| 631 | |
| 632 | if (length > hint) |
| 633 | break; |
| 634 | } |
| 635 | return result; |
| 636 | } |
| 637 | |
| 638 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 639 | iobase_writelines(PyObject *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 640 | { |
| 641 | PyObject *lines, *iter, *res; |
| 642 | |
| 643 | if (!PyArg_ParseTuple(args, "O:writelines", &lines)) { |
| 644 | return NULL; |
| 645 | } |
| 646 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 647 | if (_PyIOBase_check_closed(self, Py_True) == NULL) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 648 | return NULL; |
| 649 | |
| 650 | iter = PyObject_GetIter(lines); |
| 651 | if (iter == NULL) |
| 652 | return NULL; |
| 653 | |
| 654 | while (1) { |
| 655 | PyObject *line = PyIter_Next(iter); |
| 656 | if (line == NULL) { |
| 657 | if (PyErr_Occurred()) { |
| 658 | Py_DECREF(iter); |
| 659 | return NULL; |
| 660 | } |
| 661 | else |
| 662 | break; /* Stop Iteration */ |
| 663 | } |
| 664 | |
| 665 | res = PyObject_CallMethodObjArgs(self, _PyIO_str_write, line, NULL); |
| 666 | Py_DECREF(line); |
| 667 | if (res == NULL) { |
| 668 | Py_DECREF(iter); |
| 669 | return NULL; |
| 670 | } |
| 671 | Py_DECREF(res); |
| 672 | } |
| 673 | Py_DECREF(iter); |
| 674 | Py_RETURN_NONE; |
| 675 | } |
| 676 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 677 | static PyMethodDef iobase_methods[] = { |
| 678 | {"seek", iobase_seek, METH_VARARGS, iobase_seek_doc}, |
| 679 | {"tell", iobase_tell, METH_NOARGS, iobase_tell_doc}, |
| 680 | {"truncate", iobase_truncate, METH_VARARGS, iobase_truncate_doc}, |
| 681 | {"flush", iobase_flush, METH_NOARGS, iobase_flush_doc}, |
| 682 | {"close", iobase_close, METH_NOARGS, iobase_close_doc}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 683 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 684 | {"seekable", iobase_seekable, METH_NOARGS, iobase_seekable_doc}, |
| 685 | {"readable", iobase_readable, METH_NOARGS, iobase_readable_doc}, |
| 686 | {"writable", iobase_writable, METH_NOARGS, iobase_writable_doc}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 687 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 688 | {"_checkClosed", _PyIOBase_check_closed, METH_NOARGS}, |
| 689 | {"_checkSeekable", _PyIOBase_check_seekable, METH_NOARGS}, |
| 690 | {"_checkReadable", _PyIOBase_check_readable, METH_NOARGS}, |
| 691 | {"_checkWritable", _PyIOBase_check_writable, METH_NOARGS}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 692 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 693 | {"fileno", iobase_fileno, METH_NOARGS, iobase_fileno_doc}, |
| 694 | {"isatty", iobase_isatty, METH_NOARGS, iobase_isatty_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 | {"__enter__", iobase_enter, METH_NOARGS}, |
| 697 | {"__exit__", iobase_exit, METH_VARARGS}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 698 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 699 | {"readline", iobase_readline, METH_VARARGS, iobase_readline_doc}, |
| 700 | {"readlines", iobase_readlines, METH_VARARGS, iobase_readlines_doc}, |
| 701 | {"writelines", iobase_writelines, METH_VARARGS}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 702 | |
| 703 | {NULL, NULL} |
| 704 | }; |
| 705 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 706 | static PyGetSetDef iobase_getset[] = { |
Benjamin Peterson | f22913b | 2011-09-06 07:55:34 -0400 | [diff] [blame] | 707 | {"__dict__", (getter)iobase_get_dict, NULL, NULL}, |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 708 | {"closed", (getter)iobase_closed_get, NULL, NULL}, |
Benjamin Peterson | 1fea321 | 2009-04-19 03:15:20 +0000 | [diff] [blame] | 709 | {NULL} |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 710 | }; |
| 711 | |
| 712 | |
| 713 | PyTypeObject PyIOBase_Type = { |
| 714 | PyVarObject_HEAD_INIT(NULL, 0) |
| 715 | "_io._IOBase", /*tp_name*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 716 | sizeof(iobase), /*tp_basicsize*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 717 | 0, /*tp_itemsize*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 718 | (destructor)iobase_dealloc, /*tp_dealloc*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 719 | 0, /*tp_print*/ |
| 720 | 0, /*tp_getattr*/ |
| 721 | 0, /*tp_setattr*/ |
| 722 | 0, /*tp_compare */ |
| 723 | 0, /*tp_repr*/ |
| 724 | 0, /*tp_as_number*/ |
| 725 | 0, /*tp_as_sequence*/ |
| 726 | 0, /*tp_as_mapping*/ |
| 727 | 0, /*tp_hash */ |
| 728 | 0, /*tp_call*/ |
| 729 | 0, /*tp_str*/ |
| 730 | 0, /*tp_getattro*/ |
| 731 | 0, /*tp_setattro*/ |
| 732 | 0, /*tp_as_buffer*/ |
| 733 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
| 734 | | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 735 | iobase_doc, /* tp_doc */ |
| 736 | (traverseproc)iobase_traverse, /* tp_traverse */ |
| 737 | (inquiry)iobase_clear, /* tp_clear */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 738 | 0, /* tp_richcompare */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 739 | offsetof(iobase, weakreflist), /* tp_weaklistoffset */ |
| 740 | iobase_iter, /* tp_iter */ |
| 741 | iobase_iternext, /* tp_iternext */ |
| 742 | iobase_methods, /* tp_methods */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 743 | 0, /* tp_members */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 744 | iobase_getset, /* tp_getset */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 745 | 0, /* tp_base */ |
| 746 | 0, /* tp_dict */ |
| 747 | 0, /* tp_descr_get */ |
| 748 | 0, /* tp_descr_set */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 749 | offsetof(iobase, dict), /* tp_dictoffset */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 750 | 0, /* tp_init */ |
| 751 | 0, /* tp_alloc */ |
| 752 | PyType_GenericNew, /* tp_new */ |
| 753 | }; |
| 754 | |
| 755 | |
| 756 | /* |
| 757 | * RawIOBase class, Inherits from IOBase. |
| 758 | */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 759 | PyDoc_STRVAR(rawiobase_doc, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 760 | "Base class for raw binary I/O."); |
| 761 | |
| 762 | /* |
| 763 | * The read() method is implemented by calling readinto(); derived classes |
| 764 | * that want to support read() only need to implement readinto() as a |
| 765 | * primitive operation. In general, readinto() can be more efficient than |
| 766 | * read(). |
| 767 | * |
| 768 | * (It would be tempting to also provide an implementation of readinto() in |
| 769 | * terms of read(), in case the latter is a more suitable primitive operation, |
| 770 | * but that would lead to nasty recursion in case a subclass doesn't implement |
| 771 | * either.) |
| 772 | */ |
| 773 | |
| 774 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 775 | rawiobase_read(PyObject *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 776 | { |
| 777 | Py_ssize_t n = -1; |
| 778 | PyObject *b, *res; |
| 779 | |
| 780 | if (!PyArg_ParseTuple(args, "|n:read", &n)) { |
| 781 | return NULL; |
| 782 | } |
| 783 | |
| 784 | if (n < 0) |
| 785 | return PyObject_CallMethod(self, "readall", NULL); |
| 786 | |
| 787 | /* TODO: allocate a bytes object directly instead and manually construct |
| 788 | a writable memoryview pointing to it. */ |
| 789 | b = PyByteArray_FromStringAndSize(NULL, n); |
| 790 | if (b == NULL) |
| 791 | return NULL; |
| 792 | |
| 793 | res = PyObject_CallMethodObjArgs(self, _PyIO_str_readinto, b, NULL); |
Antoine Pitrou | 328ec74 | 2010-09-14 18:37:24 +0000 | [diff] [blame] | 794 | if (res == NULL || res == Py_None) { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 795 | Py_DECREF(b); |
Antoine Pitrou | 328ec74 | 2010-09-14 18:37:24 +0000 | [diff] [blame] | 796 | return res; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | n = PyNumber_AsSsize_t(res, PyExc_ValueError); |
| 800 | Py_DECREF(res); |
| 801 | if (n == -1 && PyErr_Occurred()) { |
| 802 | Py_DECREF(b); |
| 803 | return NULL; |
| 804 | } |
| 805 | |
| 806 | res = PyBytes_FromStringAndSize(PyByteArray_AsString(b), n); |
| 807 | Py_DECREF(b); |
| 808 | return res; |
| 809 | } |
| 810 | |
| 811 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 812 | PyDoc_STRVAR(rawiobase_readall_doc, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 813 | "Read until EOF, using multiple read() call."); |
| 814 | |
| 815 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 816 | rawiobase_readall(PyObject *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 817 | { |
Antoine Pitrou | 00a9b73 | 2009-03-29 19:19:49 +0000 | [diff] [blame] | 818 | int r; |
| 819 | PyObject *chunks = PyList_New(0); |
| 820 | PyObject *result; |
| 821 | |
| 822 | if (chunks == NULL) |
| 823 | return NULL; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 824 | |
| 825 | while (1) { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 826 | PyObject *data = PyObject_CallMethod(self, "read", |
| 827 | "i", DEFAULT_BUFFER_SIZE); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 828 | if (!data) { |
Antoine Pitrou | 00a9b73 | 2009-03-29 19:19:49 +0000 | [diff] [blame] | 829 | Py_DECREF(chunks); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 830 | return NULL; |
| 831 | } |
Victor Stinner | a80987f | 2011-05-25 22:47:16 +0200 | [diff] [blame] | 832 | if (data == Py_None) { |
| 833 | if (PyList_GET_SIZE(chunks) == 0) { |
| 834 | Py_DECREF(chunks); |
| 835 | return data; |
| 836 | } |
| 837 | Py_DECREF(data); |
| 838 | break; |
| 839 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 840 | if (!PyBytes_Check(data)) { |
Antoine Pitrou | 00a9b73 | 2009-03-29 19:19:49 +0000 | [diff] [blame] | 841 | Py_DECREF(chunks); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 842 | Py_DECREF(data); |
| 843 | PyErr_SetString(PyExc_TypeError, "read() should return bytes"); |
| 844 | return NULL; |
| 845 | } |
Antoine Pitrou | 00a9b73 | 2009-03-29 19:19:49 +0000 | [diff] [blame] | 846 | if (PyBytes_GET_SIZE(data) == 0) { |
| 847 | /* EOF */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 848 | Py_DECREF(data); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 849 | break; |
Antoine Pitrou | 00a9b73 | 2009-03-29 19:19:49 +0000 | [diff] [blame] | 850 | } |
| 851 | r = PyList_Append(chunks, data); |
| 852 | Py_DECREF(data); |
| 853 | if (r < 0) { |
| 854 | Py_DECREF(chunks); |
| 855 | return NULL; |
| 856 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 857 | } |
Antoine Pitrou | 00a9b73 | 2009-03-29 19:19:49 +0000 | [diff] [blame] | 858 | result = _PyBytes_Join(_PyIO_empty_bytes, chunks); |
| 859 | Py_DECREF(chunks); |
| 860 | return result; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 863 | static PyMethodDef rawiobase_methods[] = { |
| 864 | {"read", rawiobase_read, METH_VARARGS}, |
| 865 | {"readall", rawiobase_readall, METH_NOARGS, rawiobase_readall_doc}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 866 | {NULL, NULL} |
| 867 | }; |
| 868 | |
| 869 | PyTypeObject PyRawIOBase_Type = { |
| 870 | PyVarObject_HEAD_INIT(NULL, 0) |
| 871 | "_io._RawIOBase", /*tp_name*/ |
| 872 | 0, /*tp_basicsize*/ |
| 873 | 0, /*tp_itemsize*/ |
| 874 | 0, /*tp_dealloc*/ |
| 875 | 0, /*tp_print*/ |
| 876 | 0, /*tp_getattr*/ |
| 877 | 0, /*tp_setattr*/ |
| 878 | 0, /*tp_compare */ |
| 879 | 0, /*tp_repr*/ |
| 880 | 0, /*tp_as_number*/ |
| 881 | 0, /*tp_as_sequence*/ |
| 882 | 0, /*tp_as_mapping*/ |
| 883 | 0, /*tp_hash */ |
| 884 | 0, /*tp_call*/ |
| 885 | 0, /*tp_str*/ |
| 886 | 0, /*tp_getattro*/ |
| 887 | 0, /*tp_setattro*/ |
| 888 | 0, /*tp_as_buffer*/ |
| 889 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 890 | rawiobase_doc, /* tp_doc */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 891 | 0, /* tp_traverse */ |
| 892 | 0, /* tp_clear */ |
| 893 | 0, /* tp_richcompare */ |
| 894 | 0, /* tp_weaklistoffset */ |
| 895 | 0, /* tp_iter */ |
| 896 | 0, /* tp_iternext */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 897 | rawiobase_methods, /* tp_methods */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 898 | 0, /* tp_members */ |
| 899 | 0, /* tp_getset */ |
| 900 | &PyIOBase_Type, /* tp_base */ |
| 901 | 0, /* tp_dict */ |
| 902 | 0, /* tp_descr_get */ |
| 903 | 0, /* tp_descr_set */ |
| 904 | 0, /* tp_dictoffset */ |
| 905 | 0, /* tp_init */ |
| 906 | 0, /* tp_alloc */ |
| 907 | 0, /* tp_new */ |
| 908 | }; |