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