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