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