Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | An implementation of Buffered I/O as defined by PEP 3116 - "New I/O" |
| 3 | |
| 4 | Classes defined here: BufferedIOBase, BufferedReader, BufferedWriter, |
| 5 | BufferedRandom. |
| 6 | |
| 7 | Written by Amaury Forgeot d'Arc and Antoine Pitrou |
| 8 | */ |
| 9 | |
| 10 | #define PY_SSIZE_T_CLEAN |
| 11 | #include "Python.h" |
| 12 | #include "structmember.h" |
| 13 | #include "pythread.h" |
| 14 | #include "_iomodule.h" |
| 15 | |
| 16 | /* |
| 17 | * BufferedIOBase class, inherits from IOBase. |
| 18 | */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 19 | PyDoc_STRVAR(bufferediobase_doc, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 20 | "Base class for buffered IO objects.\n" |
| 21 | "\n" |
| 22 | "The main difference with RawIOBase is that the read() method\n" |
| 23 | "supports omitting the size argument, and does not have a default\n" |
| 24 | "implementation that defers to readinto().\n" |
| 25 | "\n" |
| 26 | "In addition, read(), readinto() and write() may raise\n" |
| 27 | "BlockingIOError if the underlying raw stream is in non-blocking\n" |
| 28 | "mode and not ready; unlike their raw counterparts, they will never\n" |
| 29 | "return None.\n" |
| 30 | "\n" |
| 31 | "A typical implementation should not inherit from a RawIOBase\n" |
| 32 | "implementation, but wrap one.\n" |
| 33 | ); |
| 34 | |
| 35 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 36 | bufferediobase_readinto(PyObject *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 37 | { |
| 38 | Py_buffer buf; |
| 39 | Py_ssize_t len; |
| 40 | PyObject *data; |
| 41 | |
| 42 | if (!PyArg_ParseTuple(args, "w*:readinto", &buf)) { |
| 43 | return NULL; |
| 44 | } |
| 45 | |
| 46 | data = PyObject_CallMethod(self, "read", "n", buf.len); |
| 47 | if (data == NULL) |
| 48 | goto error; |
| 49 | |
| 50 | if (!PyBytes_Check(data)) { |
| 51 | Py_DECREF(data); |
| 52 | PyErr_SetString(PyExc_TypeError, "read() should return bytes"); |
| 53 | goto error; |
| 54 | } |
| 55 | |
| 56 | len = Py_SIZE(data); |
| 57 | memcpy(buf.buf, PyBytes_AS_STRING(data), len); |
| 58 | |
| 59 | PyBuffer_Release(&buf); |
| 60 | Py_DECREF(data); |
| 61 | |
| 62 | return PyLong_FromSsize_t(len); |
| 63 | |
| 64 | error: |
| 65 | PyBuffer_Release(&buf); |
| 66 | return NULL; |
| 67 | } |
| 68 | |
| 69 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 70 | bufferediobase_unsupported(const char *message) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 71 | { |
| 72 | PyErr_SetString(IO_STATE->unsupported_operation, message); |
| 73 | return NULL; |
| 74 | } |
| 75 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 76 | PyDoc_STRVAR(bufferediobase_detach_doc, |
Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 77 | "Disconnect this buffer from its underlying raw stream and return it.\n" |
| 78 | "\n" |
| 79 | "After the raw stream has been detached, the buffer is in an unusable\n" |
| 80 | "state.\n"); |
| 81 | |
| 82 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 83 | bufferediobase_detach(PyObject *self) |
Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 84 | { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 85 | return bufferediobase_unsupported("detach"); |
Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 88 | PyDoc_STRVAR(bufferediobase_read_doc, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 89 | "Read and return up to n bytes.\n" |
| 90 | "\n" |
| 91 | "If the argument is omitted, None, or negative, reads and\n" |
| 92 | "returns all data until EOF.\n" |
| 93 | "\n" |
| 94 | "If the argument is positive, and the underlying raw stream is\n" |
| 95 | "not 'interactive', multiple raw reads may be issued to satisfy\n" |
| 96 | "the byte count (unless EOF is reached first). But for\n" |
| 97 | "interactive raw streams (as well as sockets and pipes), at most\n" |
| 98 | "one raw read will be issued, and a short result does not imply\n" |
| 99 | "that EOF is imminent.\n" |
| 100 | "\n" |
| 101 | "Returns an empty bytes object on EOF.\n" |
| 102 | "\n" |
| 103 | "Returns None if the underlying raw stream was open in non-blocking\n" |
| 104 | "mode and no data is available at the moment.\n"); |
| 105 | |
| 106 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 107 | bufferediobase_read(PyObject *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 108 | { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 109 | return bufferediobase_unsupported("read"); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 112 | PyDoc_STRVAR(bufferediobase_read1_doc, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 113 | "Read and return up to n bytes, with at most one read() call\n" |
| 114 | "to the underlying raw stream. A short result does not imply\n" |
| 115 | "that EOF is imminent.\n" |
| 116 | "\n" |
| 117 | "Returns an empty bytes object on EOF.\n"); |
| 118 | |
| 119 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 120 | bufferediobase_read1(PyObject *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 121 | { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 122 | return bufferediobase_unsupported("read1"); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 125 | PyDoc_STRVAR(bufferediobase_write_doc, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 126 | "Write the given buffer to the IO stream.\n" |
| 127 | "\n" |
| 128 | "Returns the number of bytes written, which is never less than\n" |
| 129 | "len(b).\n" |
| 130 | "\n" |
| 131 | "Raises BlockingIOError if the buffer is full and the\n" |
| 132 | "underlying raw stream cannot accept more data at the moment.\n"); |
| 133 | |
| 134 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 135 | bufferediobase_write(PyObject *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 136 | { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 137 | return bufferediobase_unsupported("write"); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 141 | static PyMethodDef bufferediobase_methods[] = { |
| 142 | {"detach", (PyCFunction)bufferediobase_detach, METH_NOARGS, bufferediobase_detach_doc}, |
| 143 | {"read", bufferediobase_read, METH_VARARGS, bufferediobase_read_doc}, |
| 144 | {"read1", bufferediobase_read1, METH_VARARGS, bufferediobase_read1_doc}, |
| 145 | {"readinto", bufferediobase_readinto, METH_VARARGS, NULL}, |
| 146 | {"write", bufferediobase_write, METH_VARARGS, bufferediobase_write_doc}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 147 | {NULL, NULL} |
| 148 | }; |
| 149 | |
| 150 | PyTypeObject PyBufferedIOBase_Type = { |
| 151 | PyVarObject_HEAD_INIT(NULL, 0) |
| 152 | "_io._BufferedIOBase", /*tp_name*/ |
| 153 | 0, /*tp_basicsize*/ |
| 154 | 0, /*tp_itemsize*/ |
| 155 | 0, /*tp_dealloc*/ |
| 156 | 0, /*tp_print*/ |
| 157 | 0, /*tp_getattr*/ |
| 158 | 0, /*tp_setattr*/ |
| 159 | 0, /*tp_compare */ |
| 160 | 0, /*tp_repr*/ |
| 161 | 0, /*tp_as_number*/ |
| 162 | 0, /*tp_as_sequence*/ |
| 163 | 0, /*tp_as_mapping*/ |
| 164 | 0, /*tp_hash */ |
| 165 | 0, /*tp_call*/ |
| 166 | 0, /*tp_str*/ |
| 167 | 0, /*tp_getattro*/ |
| 168 | 0, /*tp_setattro*/ |
| 169 | 0, /*tp_as_buffer*/ |
| 170 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 171 | bufferediobase_doc, /* tp_doc */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 172 | 0, /* tp_traverse */ |
| 173 | 0, /* tp_clear */ |
| 174 | 0, /* tp_richcompare */ |
| 175 | 0, /* tp_weaklistoffset */ |
| 176 | 0, /* tp_iter */ |
| 177 | 0, /* tp_iternext */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 178 | bufferediobase_methods, /* tp_methods */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 179 | 0, /* tp_members */ |
| 180 | 0, /* tp_getset */ |
| 181 | &PyIOBase_Type, /* tp_base */ |
| 182 | 0, /* tp_dict */ |
| 183 | 0, /* tp_descr_get */ |
| 184 | 0, /* tp_descr_set */ |
| 185 | 0, /* tp_dictoffset */ |
| 186 | 0, /* tp_init */ |
| 187 | 0, /* tp_alloc */ |
| 188 | 0, /* tp_new */ |
| 189 | }; |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 190 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 191 | |
| 192 | typedef struct { |
| 193 | PyObject_HEAD |
| 194 | |
| 195 | PyObject *raw; |
| 196 | int ok; /* Initialized? */ |
Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 197 | int detached; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 198 | int readable; |
| 199 | int writable; |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 200 | |
| 201 | /* True if this is a vanilla Buffered object (rather than a user derived |
| 202 | class) *and* the raw stream is a vanilla FileIO object. */ |
| 203 | int fast_closed_checks; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 204 | |
| 205 | /* Absolute position inside the raw stream (-1 if unknown). */ |
| 206 | Py_off_t abs_pos; |
| 207 | |
| 208 | /* A static buffer of size `buffer_size` */ |
| 209 | char *buffer; |
| 210 | /* Current logical position in the buffer. */ |
| 211 | Py_off_t pos; |
| 212 | /* Position of the raw stream in the buffer. */ |
| 213 | Py_off_t raw_pos; |
| 214 | |
| 215 | /* Just after the last buffered byte in the buffer, or -1 if the buffer |
| 216 | isn't ready for reading. */ |
| 217 | Py_off_t read_end; |
| 218 | |
| 219 | /* Just after the last byte actually written */ |
| 220 | Py_off_t write_pos; |
| 221 | /* Just after the last byte waiting to be written, or -1 if the buffer |
| 222 | isn't ready for writing. */ |
| 223 | Py_off_t write_end; |
| 224 | |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 225 | #ifdef WITH_THREAD |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 226 | PyThread_type_lock lock; |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 227 | #endif |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 228 | |
| 229 | Py_ssize_t buffer_size; |
| 230 | Py_ssize_t buffer_mask; |
| 231 | |
| 232 | PyObject *dict; |
| 233 | PyObject *weakreflist; |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 234 | } buffered; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 235 | |
| 236 | /* |
| 237 | Implementation notes: |
| 238 | |
| 239 | * BufferedReader, BufferedWriter and BufferedRandom try to share most |
| 240 | methods (this is helped by the members `readable` and `writable`, which |
| 241 | are initialized in the respective constructors) |
| 242 | * They also share a single buffer for reading and writing. This enables |
| 243 | interleaved reads and writes without flushing. It also makes the logic |
| 244 | a bit trickier to get right. |
| 245 | * The absolute position of the raw stream is cached, if possible, in the |
| 246 | `abs_pos` member. It must be updated every time an operation is done |
| 247 | on the raw stream. If not sure, it can be reinitialized by calling |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 248 | _buffered_raw_tell(), which queries the raw stream (_buffered_raw_seek() |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 249 | also does it). To read it, use RAW_TELL(). |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 250 | * Three helpers, _bufferedreader_raw_read, _bufferedwriter_raw_write and |
| 251 | _bufferedwriter_flush_unlocked do a lot of useful housekeeping. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 252 | |
| 253 | NOTE: we should try to maintain block alignment of reads and writes to the |
| 254 | raw stream (according to the buffer size), but for now it is only done |
| 255 | in read() and friends. |
| 256 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 257 | */ |
| 258 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 259 | /* These macros protect the buffered object against concurrent operations. */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 260 | |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 261 | #ifdef WITH_THREAD |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 262 | #define ENTER_BUFFERED(self) \ |
| 263 | Py_BEGIN_ALLOW_THREADS \ |
| 264 | PyThread_acquire_lock(self->lock, 1); \ |
| 265 | Py_END_ALLOW_THREADS |
| 266 | |
| 267 | #define LEAVE_BUFFERED(self) \ |
| 268 | PyThread_release_lock(self->lock); |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 269 | #else |
| 270 | #define ENTER_BUFFERED(self) |
| 271 | #define LEAVE_BUFFERED(self) |
| 272 | #endif |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 273 | |
| 274 | #define CHECK_INITIALIZED(self) \ |
| 275 | if (self->ok <= 0) { \ |
Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 276 | if (self->detached) { \ |
| 277 | PyErr_SetString(PyExc_ValueError, \ |
| 278 | "raw stream has been detached"); \ |
| 279 | } else { \ |
| 280 | PyErr_SetString(PyExc_ValueError, \ |
| 281 | "I/O operation on uninitialized object"); \ |
| 282 | } \ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 283 | return NULL; \ |
| 284 | } |
| 285 | |
| 286 | #define CHECK_INITIALIZED_INT(self) \ |
| 287 | if (self->ok <= 0) { \ |
Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 288 | if (self->detached) { \ |
| 289 | PyErr_SetString(PyExc_ValueError, \ |
| 290 | "raw stream has been detached"); \ |
| 291 | } else { \ |
| 292 | PyErr_SetString(PyExc_ValueError, \ |
| 293 | "I/O operation on uninitialized object"); \ |
| 294 | } \ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 295 | return -1; \ |
| 296 | } |
| 297 | |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 298 | #define IS_CLOSED(self) \ |
| 299 | (self->fast_closed_checks \ |
| 300 | ? _PyFileIO_closed(self->raw) \ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 301 | : buffered_closed(self)) |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 302 | |
| 303 | #define CHECK_CLOSED(self, error_msg) \ |
| 304 | if (IS_CLOSED(self)) { \ |
| 305 | PyErr_SetString(PyExc_ValueError, error_msg); \ |
| 306 | return NULL; \ |
| 307 | } |
| 308 | |
| 309 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 310 | #define VALID_READ_BUFFER(self) \ |
| 311 | (self->readable && self->read_end != -1) |
| 312 | |
| 313 | #define VALID_WRITE_BUFFER(self) \ |
| 314 | (self->writable && self->write_end != -1) |
| 315 | |
| 316 | #define ADJUST_POSITION(self, _new_pos) \ |
| 317 | do { \ |
| 318 | self->pos = _new_pos; \ |
| 319 | if (VALID_READ_BUFFER(self) && self->read_end < self->pos) \ |
| 320 | self->read_end = self->pos; \ |
| 321 | } while(0) |
| 322 | |
| 323 | #define READAHEAD(self) \ |
| 324 | ((self->readable && VALID_READ_BUFFER(self)) \ |
| 325 | ? (self->read_end - self->pos) : 0) |
| 326 | |
| 327 | #define RAW_OFFSET(self) \ |
| 328 | (((VALID_READ_BUFFER(self) || VALID_WRITE_BUFFER(self)) \ |
| 329 | && self->raw_pos >= 0) ? self->raw_pos - self->pos : 0) |
| 330 | |
| 331 | #define RAW_TELL(self) \ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 332 | (self->abs_pos != -1 ? self->abs_pos : _buffered_raw_tell(self)) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 333 | |
| 334 | #define MINUS_LAST_BLOCK(self, size) \ |
| 335 | (self->buffer_mask ? \ |
| 336 | (size & ~self->buffer_mask) : \ |
| 337 | (self->buffer_size * (size / self->buffer_size))) |
| 338 | |
| 339 | |
| 340 | static void |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 341 | buffered_dealloc(buffered *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 342 | { |
| 343 | if (self->ok && _PyIOBase_finalize((PyObject *) self) < 0) |
| 344 | return; |
| 345 | _PyObject_GC_UNTRACK(self); |
| 346 | self->ok = 0; |
| 347 | if (self->weakreflist != NULL) |
| 348 | PyObject_ClearWeakRefs((PyObject *)self); |
| 349 | Py_CLEAR(self->raw); |
| 350 | if (self->buffer) { |
| 351 | PyMem_Free(self->buffer); |
| 352 | self->buffer = NULL; |
| 353 | } |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 354 | #ifdef WITH_THREAD |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 355 | if (self->lock) { |
| 356 | PyThread_free_lock(self->lock); |
| 357 | self->lock = NULL; |
| 358 | } |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 359 | #endif |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 360 | Py_CLEAR(self->dict); |
| 361 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 362 | } |
| 363 | |
| 364 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 365 | buffered_traverse(buffered *self, visitproc visit, void *arg) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 366 | { |
| 367 | Py_VISIT(self->raw); |
| 368 | Py_VISIT(self->dict); |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 373 | buffered_clear(buffered *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 374 | { |
| 375 | if (self->ok && _PyIOBase_finalize((PyObject *) self) < 0) |
| 376 | return -1; |
| 377 | self->ok = 0; |
| 378 | Py_CLEAR(self->raw); |
| 379 | Py_CLEAR(self->dict); |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | /* |
| 384 | * _BufferedIOMixin methods |
| 385 | * This is not a class, just a collection of methods that will be reused |
| 386 | * by BufferedReader and BufferedWriter |
| 387 | */ |
| 388 | |
| 389 | /* Flush and close */ |
| 390 | |
| 391 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 392 | buffered_simple_flush(buffered *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 393 | { |
| 394 | CHECK_INITIALIZED(self) |
| 395 | return PyObject_CallMethodObjArgs(self->raw, _PyIO_str_flush, NULL); |
| 396 | } |
| 397 | |
| 398 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 399 | buffered_closed(buffered *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 400 | { |
| 401 | int closed; |
| 402 | PyObject *res; |
| 403 | CHECK_INITIALIZED_INT(self) |
| 404 | res = PyObject_GetAttr(self->raw, _PyIO_str_closed); |
| 405 | if (res == NULL) |
| 406 | return -1; |
| 407 | closed = PyObject_IsTrue(res); |
| 408 | Py_DECREF(res); |
| 409 | return closed; |
| 410 | } |
| 411 | |
| 412 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 413 | buffered_closed_get(buffered *self, void *context) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 414 | { |
| 415 | CHECK_INITIALIZED(self) |
| 416 | return PyObject_GetAttr(self->raw, _PyIO_str_closed); |
| 417 | } |
| 418 | |
| 419 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 420 | buffered_close(buffered *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 421 | { |
| 422 | PyObject *res = NULL; |
| 423 | int r; |
| 424 | |
| 425 | CHECK_INITIALIZED(self) |
| 426 | ENTER_BUFFERED(self) |
| 427 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 428 | r = buffered_closed(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 429 | if (r < 0) |
| 430 | goto end; |
| 431 | if (r > 0) { |
| 432 | res = Py_None; |
| 433 | Py_INCREF(res); |
| 434 | goto end; |
| 435 | } |
| 436 | /* flush() will most probably re-take the lock, so drop it first */ |
| 437 | LEAVE_BUFFERED(self) |
| 438 | res = PyObject_CallMethodObjArgs((PyObject *)self, _PyIO_str_flush, NULL); |
| 439 | ENTER_BUFFERED(self) |
| 440 | if (res == NULL) { |
| 441 | /* If flush() fails, just give up */ |
| 442 | if (PyErr_ExceptionMatches(PyExc_IOError)) |
| 443 | PyErr_Clear(); |
| 444 | else |
| 445 | goto end; |
| 446 | } |
| 447 | Py_XDECREF(res); |
| 448 | |
| 449 | res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_close, NULL); |
| 450 | |
| 451 | end: |
| 452 | LEAVE_BUFFERED(self) |
| 453 | return res; |
| 454 | } |
| 455 | |
Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 456 | /* detach */ |
| 457 | |
| 458 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 459 | buffered_detach(buffered *self, PyObject *args) |
Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 460 | { |
| 461 | PyObject *raw, *res; |
| 462 | CHECK_INITIALIZED(self) |
| 463 | res = PyObject_CallMethodObjArgs((PyObject *)self, _PyIO_str_flush, NULL); |
| 464 | if (res == NULL) |
| 465 | return NULL; |
| 466 | Py_DECREF(res); |
| 467 | raw = self->raw; |
| 468 | self->raw = NULL; |
| 469 | self->detached = 1; |
| 470 | self->ok = 0; |
| 471 | return raw; |
| 472 | } |
| 473 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 474 | /* Inquiries */ |
| 475 | |
| 476 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 477 | buffered_seekable(buffered *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 478 | { |
| 479 | CHECK_INITIALIZED(self) |
| 480 | return PyObject_CallMethodObjArgs(self->raw, _PyIO_str_seekable, NULL); |
| 481 | } |
| 482 | |
| 483 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 484 | buffered_readable(buffered *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 485 | { |
| 486 | CHECK_INITIALIZED(self) |
| 487 | return PyObject_CallMethodObjArgs(self->raw, _PyIO_str_readable, NULL); |
| 488 | } |
| 489 | |
| 490 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 491 | buffered_writable(buffered *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 492 | { |
| 493 | CHECK_INITIALIZED(self) |
| 494 | return PyObject_CallMethodObjArgs(self->raw, _PyIO_str_writable, NULL); |
| 495 | } |
| 496 | |
| 497 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 498 | buffered_name_get(buffered *self, void *context) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 499 | { |
| 500 | CHECK_INITIALIZED(self) |
| 501 | return PyObject_GetAttrString(self->raw, "name"); |
| 502 | } |
| 503 | |
| 504 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 505 | buffered_mode_get(buffered *self, void *context) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 506 | { |
| 507 | CHECK_INITIALIZED(self) |
| 508 | return PyObject_GetAttrString(self->raw, "mode"); |
| 509 | } |
| 510 | |
| 511 | /* Lower-level APIs */ |
| 512 | |
| 513 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 514 | buffered_fileno(buffered *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 515 | { |
| 516 | CHECK_INITIALIZED(self) |
| 517 | return PyObject_CallMethodObjArgs(self->raw, _PyIO_str_fileno, NULL); |
| 518 | } |
| 519 | |
| 520 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 521 | buffered_isatty(buffered *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 522 | { |
| 523 | CHECK_INITIALIZED(self) |
| 524 | return PyObject_CallMethodObjArgs(self->raw, _PyIO_str_isatty, NULL); |
| 525 | } |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 526 | |
| 527 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 528 | /* Forward decls */ |
| 529 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 530 | _bufferedwriter_flush_unlocked(buffered *, int); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 531 | static Py_ssize_t |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 532 | _bufferedreader_fill_buffer(buffered *self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 533 | static void |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 534 | _bufferedreader_reset_buf(buffered *self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 535 | static void |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 536 | _bufferedwriter_reset_buf(buffered *self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 537 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 538 | _bufferedreader_peek_unlocked(buffered *self, Py_ssize_t); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 539 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 540 | _bufferedreader_read_all(buffered *self); |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 541 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 542 | _bufferedreader_read_fast(buffered *self, Py_ssize_t); |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 543 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 544 | _bufferedreader_read_generic(buffered *self, Py_ssize_t); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 545 | |
| 546 | |
| 547 | /* |
| 548 | * Helpers |
| 549 | */ |
| 550 | |
| 551 | /* Returns the address of the `written` member if a BlockingIOError was |
| 552 | raised, NULL otherwise. The error is always re-raised. */ |
| 553 | static Py_ssize_t * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 554 | _buffered_check_blocking_error(void) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 555 | { |
| 556 | PyObject *t, *v, *tb; |
| 557 | PyBlockingIOErrorObject *err; |
| 558 | |
| 559 | PyErr_Fetch(&t, &v, &tb); |
| 560 | if (v == NULL || !PyErr_GivenExceptionMatches(v, PyExc_BlockingIOError)) { |
| 561 | PyErr_Restore(t, v, tb); |
| 562 | return NULL; |
| 563 | } |
| 564 | err = (PyBlockingIOErrorObject *) v; |
| 565 | /* TODO: sanity check (err->written >= 0) */ |
| 566 | PyErr_Restore(t, v, tb); |
| 567 | return &err->written; |
| 568 | } |
| 569 | |
| 570 | static Py_off_t |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 571 | _buffered_raw_tell(buffered *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 572 | { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 573 | Py_off_t n; |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 574 | PyObject *res; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 575 | res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_tell, NULL); |
| 576 | if (res == NULL) |
| 577 | return -1; |
| 578 | n = PyNumber_AsOff_t(res, PyExc_ValueError); |
| 579 | Py_DECREF(res); |
| 580 | if (n < 0) { |
| 581 | if (!PyErr_Occurred()) |
| 582 | PyErr_Format(PyExc_IOError, |
Mark Dickinson | 00de1bd | 2009-10-29 10:01:23 +0000 | [diff] [blame] | 583 | "Raw stream returned invalid position %zd", n); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 584 | return -1; |
| 585 | } |
| 586 | self->abs_pos = n; |
| 587 | return n; |
| 588 | } |
| 589 | |
| 590 | static Py_off_t |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 591 | _buffered_raw_seek(buffered *self, Py_off_t target, int whence) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 592 | { |
| 593 | PyObject *res, *posobj, *whenceobj; |
| 594 | Py_off_t n; |
| 595 | |
| 596 | posobj = PyLong_FromOff_t(target); |
| 597 | if (posobj == NULL) |
| 598 | return -1; |
| 599 | whenceobj = PyLong_FromLong(whence); |
| 600 | if (whenceobj == NULL) { |
| 601 | Py_DECREF(posobj); |
| 602 | return -1; |
| 603 | } |
| 604 | res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_seek, |
| 605 | posobj, whenceobj, NULL); |
| 606 | Py_DECREF(posobj); |
| 607 | Py_DECREF(whenceobj); |
| 608 | if (res == NULL) |
| 609 | return -1; |
| 610 | n = PyNumber_AsOff_t(res, PyExc_ValueError); |
| 611 | Py_DECREF(res); |
| 612 | if (n < 0) { |
| 613 | if (!PyErr_Occurred()) |
| 614 | PyErr_Format(PyExc_IOError, |
Mark Dickinson | 00de1bd | 2009-10-29 10:01:23 +0000 | [diff] [blame] | 615 | "Raw stream returned invalid position %zd", n); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 616 | return -1; |
| 617 | } |
| 618 | self->abs_pos = n; |
| 619 | return n; |
| 620 | } |
| 621 | |
| 622 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 623 | _buffered_init(buffered *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 624 | { |
| 625 | Py_ssize_t n; |
| 626 | if (self->buffer_size <= 0) { |
| 627 | PyErr_SetString(PyExc_ValueError, |
| 628 | "buffer size must be strictly positive"); |
| 629 | return -1; |
| 630 | } |
| 631 | if (self->buffer) |
| 632 | PyMem_Free(self->buffer); |
| 633 | self->buffer = PyMem_Malloc(self->buffer_size); |
| 634 | if (self->buffer == NULL) { |
| 635 | PyErr_NoMemory(); |
| 636 | return -1; |
| 637 | } |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 638 | #ifdef WITH_THREAD |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 639 | self->lock = PyThread_allocate_lock(); |
| 640 | if (self->lock == NULL) { |
| 641 | PyErr_SetString(PyExc_RuntimeError, "can't allocate read lock"); |
| 642 | return -1; |
| 643 | } |
Georg Brandl | dfd7344 | 2009-04-05 11:47:34 +0000 | [diff] [blame] | 644 | #endif |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 645 | /* Find out whether buffer_size is a power of 2 */ |
| 646 | /* XXX is this optimization useful? */ |
| 647 | for (n = self->buffer_size - 1; n & 1; n >>= 1) |
| 648 | ; |
| 649 | if (n == 0) |
| 650 | self->buffer_mask = self->buffer_size - 1; |
| 651 | else |
| 652 | self->buffer_mask = 0; |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 653 | if (_buffered_raw_tell(self) == -1) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 654 | PyErr_Clear(); |
| 655 | return 0; |
| 656 | } |
| 657 | |
| 658 | /* |
| 659 | * Shared methods and wrappers |
| 660 | */ |
| 661 | |
| 662 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 663 | buffered_flush(buffered *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 664 | { |
| 665 | PyObject *res; |
| 666 | |
| 667 | CHECK_INITIALIZED(self) |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 668 | CHECK_CLOSED(self, "flush of closed file") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 669 | |
| 670 | ENTER_BUFFERED(self) |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 671 | res = _bufferedwriter_flush_unlocked(self, 0); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 672 | if (res != NULL && self->readable) { |
| 673 | /* Rewind the raw stream so that its position corresponds to |
| 674 | the current logical position. */ |
| 675 | Py_off_t n; |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 676 | n = _buffered_raw_seek(self, -RAW_OFFSET(self), 1); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 677 | if (n == -1) |
| 678 | Py_CLEAR(res); |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 679 | _bufferedreader_reset_buf(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 680 | } |
| 681 | LEAVE_BUFFERED(self) |
| 682 | |
| 683 | return res; |
| 684 | } |
| 685 | |
| 686 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 687 | buffered_peek(buffered *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 688 | { |
| 689 | Py_ssize_t n = 0; |
| 690 | PyObject *res = NULL; |
| 691 | |
| 692 | CHECK_INITIALIZED(self) |
| 693 | if (!PyArg_ParseTuple(args, "|n:peek", &n)) { |
| 694 | return NULL; |
| 695 | } |
| 696 | |
| 697 | ENTER_BUFFERED(self) |
| 698 | |
| 699 | if (self->writable) { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 700 | res = _bufferedwriter_flush_unlocked(self, 1); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 701 | if (res == NULL) |
| 702 | goto end; |
| 703 | Py_CLEAR(res); |
| 704 | } |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 705 | res = _bufferedreader_peek_unlocked(self, n); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 706 | |
| 707 | end: |
| 708 | LEAVE_BUFFERED(self) |
| 709 | return res; |
| 710 | } |
| 711 | |
| 712 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 713 | buffered_read(buffered *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 714 | { |
| 715 | Py_ssize_t n = -1; |
| 716 | PyObject *res; |
| 717 | |
| 718 | CHECK_INITIALIZED(self) |
Benjamin Peterson | 6b59f77 | 2009-12-13 19:30:15 +0000 | [diff] [blame] | 719 | if (!PyArg_ParseTuple(args, "|O&:read", &_PyIO_ConvertSsize_t, &n)) { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 720 | return NULL; |
| 721 | } |
| 722 | if (n < -1) { |
| 723 | PyErr_SetString(PyExc_ValueError, |
| 724 | "read length must be positive or -1"); |
| 725 | return NULL; |
| 726 | } |
| 727 | |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 728 | CHECK_CLOSED(self, "read of closed file") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 729 | |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 730 | if (n == -1) { |
| 731 | /* The number of bytes is unspecified, read until the end of stream */ |
| 732 | ENTER_BUFFERED(self) |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 733 | res = _bufferedreader_read_all(self); |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 734 | LEAVE_BUFFERED(self) |
| 735 | } |
| 736 | else { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 737 | res = _bufferedreader_read_fast(self, n); |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 738 | if (res == Py_None) { |
| 739 | Py_DECREF(res); |
| 740 | ENTER_BUFFERED(self) |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 741 | res = _bufferedreader_read_generic(self, n); |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 742 | LEAVE_BUFFERED(self) |
| 743 | } |
| 744 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 745 | |
| 746 | return res; |
| 747 | } |
| 748 | |
| 749 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 750 | buffered_read1(buffered *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 751 | { |
| 752 | Py_ssize_t n, have, r; |
| 753 | PyObject *res = NULL; |
| 754 | |
| 755 | CHECK_INITIALIZED(self) |
| 756 | if (!PyArg_ParseTuple(args, "n:read1", &n)) { |
| 757 | return NULL; |
| 758 | } |
| 759 | |
| 760 | if (n < 0) { |
| 761 | PyErr_SetString(PyExc_ValueError, |
| 762 | "read length must be positive"); |
| 763 | return NULL; |
| 764 | } |
| 765 | if (n == 0) |
| 766 | return PyBytes_FromStringAndSize(NULL, 0); |
| 767 | |
| 768 | ENTER_BUFFERED(self) |
| 769 | |
| 770 | if (self->writable) { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 771 | res = _bufferedwriter_flush_unlocked(self, 1); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 772 | if (res == NULL) |
| 773 | goto end; |
| 774 | Py_CLEAR(res); |
| 775 | } |
| 776 | |
| 777 | /* Return up to n bytes. If at least one byte is buffered, we |
| 778 | only return buffered bytes. Otherwise, we do one raw read. */ |
| 779 | |
| 780 | /* XXX: this mimicks the io.py implementation but is probably wrong. |
| 781 | If we need to read from the raw stream, then we could actually read |
| 782 | all `n` bytes asked by the caller (and possibly more, so as to fill |
| 783 | our buffer for the next reads). */ |
| 784 | |
| 785 | have = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t); |
| 786 | if (have > 0) { |
| 787 | if (n > have) |
| 788 | n = have; |
| 789 | res = PyBytes_FromStringAndSize(self->buffer + self->pos, n); |
| 790 | if (res == NULL) |
| 791 | goto end; |
| 792 | self->pos += n; |
| 793 | goto end; |
| 794 | } |
| 795 | |
| 796 | /* Fill the buffer from the raw stream, and copy it to the result. */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 797 | _bufferedreader_reset_buf(self); |
| 798 | r = _bufferedreader_fill_buffer(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 799 | if (r == -1) |
| 800 | goto end; |
| 801 | if (r == -2) |
| 802 | r = 0; |
| 803 | if (n > r) |
| 804 | n = r; |
| 805 | res = PyBytes_FromStringAndSize(self->buffer, n); |
| 806 | if (res == NULL) |
| 807 | goto end; |
| 808 | self->pos = n; |
| 809 | |
| 810 | end: |
| 811 | LEAVE_BUFFERED(self) |
| 812 | return res; |
| 813 | } |
| 814 | |
| 815 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 816 | buffered_readinto(buffered *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 817 | { |
| 818 | PyObject *res = NULL; |
| 819 | |
| 820 | CHECK_INITIALIZED(self) |
| 821 | |
| 822 | /* TODO: use raw.readinto() instead! */ |
| 823 | if (self->writable) { |
| 824 | ENTER_BUFFERED(self) |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 825 | res = _bufferedwriter_flush_unlocked(self, 0); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 826 | LEAVE_BUFFERED(self) |
| 827 | if (res == NULL) |
| 828 | goto end; |
| 829 | Py_DECREF(res); |
| 830 | } |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 831 | res = bufferediobase_readinto((PyObject *)self, args); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 832 | |
| 833 | end: |
| 834 | return res; |
| 835 | } |
| 836 | |
| 837 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 838 | _buffered_readline(buffered *self, Py_ssize_t limit) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 839 | { |
| 840 | PyObject *res = NULL; |
| 841 | PyObject *chunks = NULL; |
| 842 | Py_ssize_t n, written = 0; |
| 843 | const char *start, *s, *end; |
| 844 | |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 845 | CHECK_CLOSED(self, "readline of closed file") |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 846 | |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 847 | /* First, try to find a line in the buffer. This can run unlocked because |
| 848 | the calls to the C API are simple enough that they can't trigger |
| 849 | any thread switch. */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 850 | n = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t); |
| 851 | if (limit >= 0 && n > limit) |
| 852 | n = limit; |
| 853 | start = self->buffer + self->pos; |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 854 | s = memchr(start, '\n', n); |
| 855 | if (s != NULL) { |
| 856 | res = PyBytes_FromStringAndSize(start, s - start + 1); |
| 857 | if (res != NULL) |
| 858 | self->pos += s - start + 1; |
| 859 | goto end_unlocked; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 860 | } |
| 861 | if (n == limit) { |
| 862 | res = PyBytes_FromStringAndSize(start, n); |
| 863 | if (res != NULL) |
| 864 | self->pos += n; |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 865 | goto end_unlocked; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 866 | } |
| 867 | |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 868 | ENTER_BUFFERED(self) |
| 869 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 870 | /* Now we try to get some more from the raw stream */ |
| 871 | if (self->writable) { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 872 | res = _bufferedwriter_flush_unlocked(self, 1); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 873 | if (res == NULL) |
| 874 | goto end; |
| 875 | Py_CLEAR(res); |
| 876 | } |
| 877 | chunks = PyList_New(0); |
| 878 | if (chunks == NULL) |
| 879 | goto end; |
| 880 | if (n > 0) { |
| 881 | res = PyBytes_FromStringAndSize(start, n); |
| 882 | if (res == NULL) |
| 883 | goto end; |
| 884 | if (PyList_Append(chunks, res) < 0) { |
| 885 | Py_CLEAR(res); |
| 886 | goto end; |
| 887 | } |
| 888 | Py_CLEAR(res); |
| 889 | written += n; |
| 890 | if (limit >= 0) |
| 891 | limit -= n; |
| 892 | } |
| 893 | |
| 894 | for (;;) { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 895 | _bufferedreader_reset_buf(self); |
| 896 | n = _bufferedreader_fill_buffer(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 897 | if (n == -1) |
| 898 | goto end; |
| 899 | if (n <= 0) |
| 900 | break; |
| 901 | if (limit >= 0 && n > limit) |
| 902 | n = limit; |
| 903 | start = self->buffer; |
| 904 | end = start + n; |
| 905 | s = start; |
| 906 | while (s < end) { |
| 907 | if (*s++ == '\n') { |
| 908 | res = PyBytes_FromStringAndSize(start, s - start); |
| 909 | if (res == NULL) |
| 910 | goto end; |
| 911 | self->pos = s - start; |
| 912 | goto found; |
| 913 | } |
| 914 | } |
| 915 | res = PyBytes_FromStringAndSize(start, n); |
| 916 | if (res == NULL) |
| 917 | goto end; |
| 918 | if (n == limit) { |
| 919 | self->pos = n; |
| 920 | break; |
| 921 | } |
| 922 | if (PyList_Append(chunks, res) < 0) { |
| 923 | Py_CLEAR(res); |
| 924 | goto end; |
| 925 | } |
| 926 | Py_CLEAR(res); |
| 927 | written += n; |
| 928 | if (limit >= 0) |
| 929 | limit -= n; |
| 930 | } |
| 931 | found: |
| 932 | if (res != NULL && PyList_Append(chunks, res) < 0) { |
| 933 | Py_CLEAR(res); |
| 934 | goto end; |
| 935 | } |
| 936 | Py_CLEAR(res); |
| 937 | res = _PyBytes_Join(_PyIO_empty_bytes, chunks); |
| 938 | |
| 939 | end: |
| 940 | LEAVE_BUFFERED(self) |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 941 | end_unlocked: |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 942 | Py_XDECREF(chunks); |
| 943 | return res; |
| 944 | } |
| 945 | |
| 946 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 947 | buffered_readline(buffered *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 948 | { |
| 949 | Py_ssize_t limit = -1; |
| 950 | |
| 951 | CHECK_INITIALIZED(self) |
Benjamin Peterson | 6b59f77 | 2009-12-13 19:30:15 +0000 | [diff] [blame] | 952 | if (!PyArg_ParseTuple(args, "|O&:readline", &_PyIO_ConvertSsize_t, &limit)) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 953 | return NULL; |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 954 | return _buffered_readline(self, limit); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | |
| 958 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 959 | buffered_tell(buffered *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 960 | { |
| 961 | Py_off_t pos; |
| 962 | |
| 963 | CHECK_INITIALIZED(self) |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 964 | pos = _buffered_raw_tell(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 965 | if (pos == -1) |
| 966 | return NULL; |
| 967 | pos -= RAW_OFFSET(self); |
| 968 | /* TODO: sanity check (pos >= 0) */ |
| 969 | return PyLong_FromOff_t(pos); |
| 970 | } |
| 971 | |
| 972 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 973 | buffered_seek(buffered *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 974 | { |
| 975 | Py_off_t target, n; |
| 976 | int whence = 0; |
| 977 | PyObject *targetobj, *res = NULL; |
| 978 | |
| 979 | CHECK_INITIALIZED(self) |
| 980 | if (!PyArg_ParseTuple(args, "O|i:seek", &targetobj, &whence)) { |
| 981 | return NULL; |
| 982 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 983 | if (whence < 0 || whence > 2) { |
| 984 | PyErr_Format(PyExc_ValueError, |
| 985 | "whence must be between 0 and 2, not %d", whence); |
| 986 | return NULL; |
| 987 | } |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 988 | |
| 989 | CHECK_CLOSED(self, "seek of closed file") |
| 990 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 991 | target = PyNumber_AsOff_t(targetobj, PyExc_ValueError); |
| 992 | if (target == -1 && PyErr_Occurred()) |
| 993 | return NULL; |
| 994 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 995 | if (whence != 2 && self->readable) { |
| 996 | Py_off_t current, avail; |
| 997 | /* Check if seeking leaves us inside the current buffer, |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 998 | so as to return quickly if possible. Also, we needn't take the |
| 999 | lock in this fast path. |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1000 | Don't know how to do that when whence == 2, though. */ |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 1001 | /* NOTE: RAW_TELL() can release the GIL but the object is in a stable |
| 1002 | state at this point. */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1003 | current = RAW_TELL(self); |
| 1004 | avail = READAHEAD(self); |
| 1005 | if (avail > 0) { |
| 1006 | Py_off_t offset; |
| 1007 | if (whence == 0) |
| 1008 | offset = target - (current - RAW_OFFSET(self)); |
| 1009 | else |
| 1010 | offset = target; |
| 1011 | if (offset >= -self->pos && offset <= avail) { |
| 1012 | self->pos += offset; |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 1013 | return PyLong_FromOff_t(current - avail + offset); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1014 | } |
| 1015 | } |
| 1016 | } |
| 1017 | |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 1018 | ENTER_BUFFERED(self) |
| 1019 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1020 | /* Fallback: invoke raw seek() method and clear buffer */ |
| 1021 | if (self->writable) { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1022 | res = _bufferedwriter_flush_unlocked(self, 0); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1023 | if (res == NULL) |
| 1024 | goto end; |
| 1025 | Py_CLEAR(res); |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1026 | _bufferedwriter_reset_buf(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | /* TODO: align on block boundary and read buffer if needed? */ |
| 1030 | if (whence == 1) |
| 1031 | target -= RAW_OFFSET(self); |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1032 | n = _buffered_raw_seek(self, target, whence); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1033 | if (n == -1) |
| 1034 | goto end; |
| 1035 | self->raw_pos = -1; |
| 1036 | res = PyLong_FromOff_t(n); |
| 1037 | if (res != NULL && self->readable) |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1038 | _bufferedreader_reset_buf(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1039 | |
| 1040 | end: |
| 1041 | LEAVE_BUFFERED(self) |
| 1042 | return res; |
| 1043 | } |
| 1044 | |
| 1045 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1046 | buffered_truncate(buffered *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1047 | { |
| 1048 | PyObject *pos = Py_None; |
| 1049 | PyObject *res = NULL; |
| 1050 | |
| 1051 | CHECK_INITIALIZED(self) |
| 1052 | if (!PyArg_ParseTuple(args, "|O:truncate", &pos)) { |
| 1053 | return NULL; |
| 1054 | } |
| 1055 | |
| 1056 | ENTER_BUFFERED(self) |
| 1057 | |
| 1058 | if (self->writable) { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1059 | res = _bufferedwriter_flush_unlocked(self, 0); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1060 | if (res == NULL) |
| 1061 | goto end; |
| 1062 | Py_CLEAR(res); |
| 1063 | } |
| 1064 | if (self->readable) { |
| 1065 | if (pos == Py_None) { |
| 1066 | /* Rewind the raw stream so that its position corresponds to |
| 1067 | the current logical position. */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1068 | if (_buffered_raw_seek(self, -RAW_OFFSET(self), 1) == -1) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1069 | goto end; |
| 1070 | } |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1071 | _bufferedreader_reset_buf(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1072 | } |
| 1073 | res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_truncate, pos, NULL); |
| 1074 | if (res == NULL) |
| 1075 | goto end; |
| 1076 | /* Reset cached position */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1077 | if (_buffered_raw_tell(self) == -1) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1078 | PyErr_Clear(); |
| 1079 | |
| 1080 | end: |
| 1081 | LEAVE_BUFFERED(self) |
| 1082 | return res; |
| 1083 | } |
| 1084 | |
| 1085 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1086 | buffered_iternext(buffered *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1087 | { |
| 1088 | PyObject *line; |
| 1089 | PyTypeObject *tp; |
| 1090 | |
| 1091 | CHECK_INITIALIZED(self); |
| 1092 | |
| 1093 | tp = Py_TYPE(self); |
| 1094 | if (tp == &PyBufferedReader_Type || |
| 1095 | tp == &PyBufferedRandom_Type) { |
| 1096 | /* Skip method call overhead for speed */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1097 | line = _buffered_readline(self, -1); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1098 | } |
| 1099 | else { |
| 1100 | line = PyObject_CallMethodObjArgs((PyObject *)self, |
| 1101 | _PyIO_str_readline, NULL); |
| 1102 | if (line && !PyBytes_Check(line)) { |
| 1103 | PyErr_Format(PyExc_IOError, |
| 1104 | "readline() should have returned a bytes object, " |
| 1105 | "not '%.200s'", Py_TYPE(line)->tp_name); |
| 1106 | Py_DECREF(line); |
| 1107 | return NULL; |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | if (line == NULL) |
| 1112 | return NULL; |
| 1113 | |
| 1114 | if (PyBytes_GET_SIZE(line) == 0) { |
| 1115 | /* Reached EOF or would have blocked */ |
| 1116 | Py_DECREF(line); |
| 1117 | return NULL; |
| 1118 | } |
| 1119 | |
| 1120 | return line; |
| 1121 | } |
| 1122 | |
Antoine Pitrou | 716c444 | 2009-05-23 19:04:03 +0000 | [diff] [blame] | 1123 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1124 | buffered_repr(buffered *self) |
Antoine Pitrou | 716c444 | 2009-05-23 19:04:03 +0000 | [diff] [blame] | 1125 | { |
| 1126 | PyObject *nameobj, *res; |
| 1127 | |
| 1128 | nameobj = PyObject_GetAttrString((PyObject *) self, "name"); |
| 1129 | if (nameobj == NULL) { |
| 1130 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1131 | PyErr_Clear(); |
| 1132 | else |
| 1133 | return NULL; |
| 1134 | res = PyUnicode_FromFormat("<%s>", Py_TYPE(self)->tp_name); |
| 1135 | } |
| 1136 | else { |
| 1137 | res = PyUnicode_FromFormat("<%s name=%R>", |
| 1138 | Py_TYPE(self)->tp_name, nameobj); |
| 1139 | Py_DECREF(nameobj); |
| 1140 | } |
| 1141 | return res; |
| 1142 | } |
| 1143 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1144 | /* |
| 1145 | * class BufferedReader |
| 1146 | */ |
| 1147 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1148 | PyDoc_STRVAR(bufferedreader_doc, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1149 | "Create a new buffered reader using the given readable raw IO object."); |
| 1150 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1151 | static void _bufferedreader_reset_buf(buffered *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1152 | { |
| 1153 | self->read_end = -1; |
| 1154 | } |
| 1155 | |
| 1156 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1157 | bufferedreader_init(buffered *self, PyObject *args, PyObject *kwds) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1158 | { |
| 1159 | char *kwlist[] = {"raw", "buffer_size", NULL}; |
| 1160 | Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE; |
| 1161 | PyObject *raw; |
| 1162 | |
| 1163 | self->ok = 0; |
Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 1164 | self->detached = 0; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1165 | |
| 1166 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|n:BufferedReader", kwlist, |
| 1167 | &raw, &buffer_size)) { |
| 1168 | return -1; |
| 1169 | } |
| 1170 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1171 | if (_PyIOBase_check_readable(raw, Py_True) == NULL) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1172 | return -1; |
| 1173 | |
| 1174 | Py_CLEAR(self->raw); |
| 1175 | Py_INCREF(raw); |
| 1176 | self->raw = raw; |
| 1177 | self->buffer_size = buffer_size; |
| 1178 | self->readable = 1; |
| 1179 | self->writable = 0; |
| 1180 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1181 | if (_buffered_init(self) < 0) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1182 | return -1; |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1183 | _bufferedreader_reset_buf(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1184 | |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 1185 | self->fast_closed_checks = (Py_TYPE(self) == &PyBufferedReader_Type && |
| 1186 | Py_TYPE(raw) == &PyFileIO_Type); |
| 1187 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1188 | self->ok = 1; |
| 1189 | return 0; |
| 1190 | } |
| 1191 | |
| 1192 | static Py_ssize_t |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1193 | _bufferedreader_raw_read(buffered *self, char *start, Py_ssize_t len) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1194 | { |
| 1195 | Py_buffer buf; |
| 1196 | PyObject *memobj, *res; |
| 1197 | Py_ssize_t n; |
| 1198 | /* NOTE: the buffer needn't be released as its object is NULL. */ |
| 1199 | if (PyBuffer_FillInfo(&buf, NULL, start, len, 0, PyBUF_CONTIG) == -1) |
| 1200 | return -1; |
| 1201 | memobj = PyMemoryView_FromBuffer(&buf); |
| 1202 | if (memobj == NULL) |
| 1203 | return -1; |
| 1204 | res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_readinto, memobj, NULL); |
| 1205 | Py_DECREF(memobj); |
| 1206 | if (res == NULL) |
| 1207 | return -1; |
| 1208 | if (res == Py_None) { |
| 1209 | /* Non-blocking stream would have blocked. Special return code! */ |
| 1210 | Py_DECREF(res); |
| 1211 | return -2; |
| 1212 | } |
| 1213 | n = PyNumber_AsSsize_t(res, PyExc_ValueError); |
| 1214 | Py_DECREF(res); |
| 1215 | if (n < 0 || n > len) { |
| 1216 | PyErr_Format(PyExc_IOError, |
| 1217 | "raw readinto() returned invalid length %zd " |
| 1218 | "(should have been between 0 and %zd)", n, len); |
| 1219 | return -1; |
| 1220 | } |
| 1221 | if (n > 0 && self->abs_pos != -1) |
| 1222 | self->abs_pos += n; |
| 1223 | return n; |
| 1224 | } |
| 1225 | |
| 1226 | static Py_ssize_t |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1227 | _bufferedreader_fill_buffer(buffered *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1228 | { |
| 1229 | Py_ssize_t start, len, n; |
| 1230 | if (VALID_READ_BUFFER(self)) |
| 1231 | start = Py_SAFE_DOWNCAST(self->read_end, Py_off_t, Py_ssize_t); |
| 1232 | else |
| 1233 | start = 0; |
| 1234 | len = self->buffer_size - start; |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1235 | n = _bufferedreader_raw_read(self, self->buffer + start, len); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1236 | if (n <= 0) |
| 1237 | return n; |
| 1238 | self->read_end = start + n; |
| 1239 | self->raw_pos = start + n; |
| 1240 | return n; |
| 1241 | } |
| 1242 | |
| 1243 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1244 | _bufferedreader_read_all(buffered *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1245 | { |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 1246 | Py_ssize_t current_size; |
| 1247 | PyObject *res, *data = NULL; |
| 1248 | PyObject *chunks = PyList_New(0); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1249 | |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 1250 | if (chunks == NULL) |
| 1251 | return NULL; |
| 1252 | |
| 1253 | /* First copy what we have in the current buffer. */ |
| 1254 | current_size = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t); |
| 1255 | if (current_size) { |
| 1256 | data = PyBytes_FromStringAndSize( |
| 1257 | self->buffer + self->pos, current_size); |
| 1258 | if (data == NULL) { |
| 1259 | Py_DECREF(chunks); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1260 | return NULL; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1261 | } |
| 1262 | } |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1263 | _bufferedreader_reset_buf(self); |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 1264 | /* We're going past the buffer's bounds, flush it */ |
| 1265 | if (self->writable) { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1266 | res = _bufferedwriter_flush_unlocked(self, 1); |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 1267 | if (res == NULL) { |
| 1268 | Py_DECREF(chunks); |
| 1269 | return NULL; |
| 1270 | } |
| 1271 | Py_CLEAR(res); |
| 1272 | } |
| 1273 | while (1) { |
| 1274 | if (data) { |
| 1275 | if (PyList_Append(chunks, data) < 0) { |
| 1276 | Py_DECREF(data); |
| 1277 | Py_DECREF(chunks); |
| 1278 | return NULL; |
| 1279 | } |
| 1280 | Py_DECREF(data); |
| 1281 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1282 | |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 1283 | /* Read until EOF or until read() would block. */ |
| 1284 | data = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_read, NULL); |
| 1285 | if (data == NULL) { |
| 1286 | Py_DECREF(chunks); |
| 1287 | return NULL; |
| 1288 | } |
| 1289 | if (data != Py_None && !PyBytes_Check(data)) { |
| 1290 | Py_DECREF(data); |
| 1291 | Py_DECREF(chunks); |
| 1292 | PyErr_SetString(PyExc_TypeError, "read() should return bytes"); |
| 1293 | return NULL; |
| 1294 | } |
| 1295 | if (data == Py_None || PyBytes_GET_SIZE(data) == 0) { |
| 1296 | if (current_size == 0) { |
| 1297 | Py_DECREF(chunks); |
| 1298 | return data; |
| 1299 | } |
| 1300 | else { |
| 1301 | res = _PyBytes_Join(_PyIO_empty_bytes, chunks); |
| 1302 | Py_DECREF(data); |
| 1303 | Py_DECREF(chunks); |
| 1304 | return res; |
| 1305 | } |
| 1306 | } |
| 1307 | current_size += PyBytes_GET_SIZE(data); |
| 1308 | if (self->abs_pos != -1) |
| 1309 | self->abs_pos += PyBytes_GET_SIZE(data); |
| 1310 | } |
| 1311 | } |
| 1312 | |
| 1313 | /* Read n bytes from the buffer if it can, otherwise return None. |
| 1314 | This function is simple enough that it can run unlocked. */ |
| 1315 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1316 | _bufferedreader_read_fast(buffered *self, Py_ssize_t n) |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 1317 | { |
| 1318 | Py_ssize_t current_size; |
| 1319 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1320 | current_size = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t); |
| 1321 | if (n <= current_size) { |
| 1322 | /* Fast path: the data to read is fully buffered. */ |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 1323 | PyObject *res = PyBytes_FromStringAndSize(self->buffer + self->pos, n); |
| 1324 | if (res != NULL) |
| 1325 | self->pos += n; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1326 | return res; |
| 1327 | } |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 1328 | Py_RETURN_NONE; |
| 1329 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1330 | |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 1331 | /* Generic read function: read from the stream until enough bytes are read, |
| 1332 | * or until an EOF occurs or until read() would block. |
| 1333 | */ |
| 1334 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1335 | _bufferedreader_read_generic(buffered *self, Py_ssize_t n) |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 1336 | { |
| 1337 | PyObject *res = NULL; |
| 1338 | Py_ssize_t current_size, remaining, written; |
| 1339 | char *out; |
| 1340 | |
| 1341 | current_size = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t); |
| 1342 | if (n <= current_size) |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1343 | return _bufferedreader_read_fast(self, n); |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 1344 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1345 | res = PyBytes_FromStringAndSize(NULL, n); |
| 1346 | if (res == NULL) |
| 1347 | goto error; |
| 1348 | out = PyBytes_AS_STRING(res); |
| 1349 | remaining = n; |
| 1350 | written = 0; |
| 1351 | if (current_size > 0) { |
| 1352 | memcpy(out, self->buffer + self->pos, current_size); |
| 1353 | remaining -= current_size; |
| 1354 | written += current_size; |
| 1355 | } |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1356 | _bufferedreader_reset_buf(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1357 | while (remaining > 0) { |
| 1358 | /* We want to read a whole block at the end into buffer. |
| 1359 | If we had readv() we could do this in one pass. */ |
| 1360 | Py_ssize_t r = MINUS_LAST_BLOCK(self, remaining); |
| 1361 | if (r == 0) |
| 1362 | break; |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1363 | r = _bufferedreader_raw_read(self, out + written, r); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1364 | if (r == -1) |
| 1365 | goto error; |
| 1366 | if (r == 0 || r == -2) { |
| 1367 | /* EOF occurred or read() would block. */ |
| 1368 | if (r == 0 || written > 0) { |
| 1369 | if (_PyBytes_Resize(&res, written)) |
| 1370 | goto error; |
| 1371 | return res; |
| 1372 | } |
| 1373 | Py_DECREF(res); |
| 1374 | Py_INCREF(Py_None); |
| 1375 | return Py_None; |
| 1376 | } |
| 1377 | remaining -= r; |
| 1378 | written += r; |
| 1379 | } |
| 1380 | assert(remaining <= self->buffer_size); |
| 1381 | self->pos = 0; |
| 1382 | self->raw_pos = 0; |
| 1383 | self->read_end = 0; |
| 1384 | while (self->read_end < self->buffer_size) { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1385 | Py_ssize_t r = _bufferedreader_fill_buffer(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1386 | if (r == -1) |
| 1387 | goto error; |
| 1388 | if (r == 0 || r == -2) { |
| 1389 | /* EOF occurred or read() would block. */ |
| 1390 | if (r == 0 || written > 0) { |
| 1391 | if (_PyBytes_Resize(&res, written)) |
| 1392 | goto error; |
| 1393 | return res; |
| 1394 | } |
| 1395 | Py_DECREF(res); |
| 1396 | Py_INCREF(Py_None); |
| 1397 | return Py_None; |
| 1398 | } |
| 1399 | if (remaining > r) { |
| 1400 | memcpy(out + written, self->buffer + self->pos, r); |
| 1401 | written += r; |
| 1402 | self->pos += r; |
| 1403 | remaining -= r; |
| 1404 | } |
| 1405 | else if (remaining > 0) { |
| 1406 | memcpy(out + written, self->buffer + self->pos, remaining); |
| 1407 | written += remaining; |
| 1408 | self->pos += remaining; |
| 1409 | remaining = 0; |
| 1410 | } |
| 1411 | if (remaining == 0) |
| 1412 | break; |
| 1413 | } |
| 1414 | |
| 1415 | return res; |
| 1416 | |
| 1417 | error: |
| 1418 | Py_XDECREF(res); |
| 1419 | return NULL; |
| 1420 | } |
| 1421 | |
| 1422 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1423 | _bufferedreader_peek_unlocked(buffered *self, Py_ssize_t n) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1424 | { |
| 1425 | Py_ssize_t have, r; |
| 1426 | |
| 1427 | have = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t); |
| 1428 | /* Constraints: |
| 1429 | 1. we don't want to advance the file position. |
| 1430 | 2. we don't want to lose block alignment, so we can't shift the buffer |
| 1431 | to make some place. |
| 1432 | Therefore, we either return `have` bytes (if > 0), or a full buffer. |
| 1433 | */ |
| 1434 | if (have > 0) { |
| 1435 | return PyBytes_FromStringAndSize(self->buffer + self->pos, have); |
| 1436 | } |
| 1437 | |
| 1438 | /* Fill the buffer from the raw stream, and copy it to the result. */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1439 | _bufferedreader_reset_buf(self); |
| 1440 | r = _bufferedreader_fill_buffer(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1441 | if (r == -1) |
| 1442 | return NULL; |
| 1443 | if (r == -2) |
| 1444 | r = 0; |
| 1445 | self->pos = 0; |
| 1446 | return PyBytes_FromStringAndSize(self->buffer, r); |
| 1447 | } |
| 1448 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1449 | static PyMethodDef bufferedreader_methods[] = { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1450 | /* BufferedIOMixin methods */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1451 | {"detach", (PyCFunction)buffered_detach, METH_NOARGS}, |
| 1452 | {"flush", (PyCFunction)buffered_simple_flush, METH_NOARGS}, |
| 1453 | {"close", (PyCFunction)buffered_close, METH_NOARGS}, |
| 1454 | {"seekable", (PyCFunction)buffered_seekable, METH_NOARGS}, |
| 1455 | {"readable", (PyCFunction)buffered_readable, METH_NOARGS}, |
| 1456 | {"writable", (PyCFunction)buffered_writable, METH_NOARGS}, |
| 1457 | {"fileno", (PyCFunction)buffered_fileno, METH_NOARGS}, |
| 1458 | {"isatty", (PyCFunction)buffered_isatty, METH_NOARGS}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1459 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1460 | {"read", (PyCFunction)buffered_read, METH_VARARGS}, |
| 1461 | {"peek", (PyCFunction)buffered_peek, METH_VARARGS}, |
| 1462 | {"read1", (PyCFunction)buffered_read1, METH_VARARGS}, |
| 1463 | {"readline", (PyCFunction)buffered_readline, METH_VARARGS}, |
| 1464 | {"seek", (PyCFunction)buffered_seek, METH_VARARGS}, |
| 1465 | {"tell", (PyCFunction)buffered_tell, METH_NOARGS}, |
| 1466 | {"truncate", (PyCFunction)buffered_truncate, METH_VARARGS}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1467 | {NULL, NULL} |
| 1468 | }; |
| 1469 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1470 | static PyMemberDef bufferedreader_members[] = { |
| 1471 | {"raw", T_OBJECT, offsetof(buffered, raw), 0}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1472 | {NULL} |
| 1473 | }; |
| 1474 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1475 | static PyGetSetDef bufferedreader_getset[] = { |
| 1476 | {"closed", (getter)buffered_closed_get, NULL, NULL}, |
| 1477 | {"name", (getter)buffered_name_get, NULL, NULL}, |
| 1478 | {"mode", (getter)buffered_mode_get, NULL, NULL}, |
Benjamin Peterson | 1fea321 | 2009-04-19 03:15:20 +0000 | [diff] [blame] | 1479 | {NULL} |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1480 | }; |
| 1481 | |
| 1482 | |
| 1483 | PyTypeObject PyBufferedReader_Type = { |
| 1484 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1485 | "_io.BufferedReader", /*tp_name*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1486 | sizeof(buffered), /*tp_basicsize*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1487 | 0, /*tp_itemsize*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1488 | (destructor)buffered_dealloc, /*tp_dealloc*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1489 | 0, /*tp_print*/ |
| 1490 | 0, /*tp_getattr*/ |
| 1491 | 0, /*tp_setattr*/ |
| 1492 | 0, /*tp_compare */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1493 | (reprfunc)buffered_repr, /*tp_repr*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1494 | 0, /*tp_as_number*/ |
| 1495 | 0, /*tp_as_sequence*/ |
| 1496 | 0, /*tp_as_mapping*/ |
| 1497 | 0, /*tp_hash */ |
| 1498 | 0, /*tp_call*/ |
| 1499 | 0, /*tp_str*/ |
| 1500 | 0, /*tp_getattro*/ |
| 1501 | 0, /*tp_setattro*/ |
| 1502 | 0, /*tp_as_buffer*/ |
| 1503 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
| 1504 | | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1505 | bufferedreader_doc, /* tp_doc */ |
| 1506 | (traverseproc)buffered_traverse, /* tp_traverse */ |
| 1507 | (inquiry)buffered_clear, /* tp_clear */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1508 | 0, /* tp_richcompare */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1509 | offsetof(buffered, weakreflist), /*tp_weaklistoffset*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1510 | 0, /* tp_iter */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1511 | (iternextfunc)buffered_iternext, /* tp_iternext */ |
| 1512 | bufferedreader_methods, /* tp_methods */ |
| 1513 | bufferedreader_members, /* tp_members */ |
| 1514 | bufferedreader_getset, /* tp_getset */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1515 | 0, /* tp_base */ |
| 1516 | 0, /* tp_dict */ |
| 1517 | 0, /* tp_descr_get */ |
| 1518 | 0, /* tp_descr_set */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1519 | offsetof(buffered, dict), /* tp_dictoffset */ |
| 1520 | (initproc)bufferedreader_init, /* tp_init */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1521 | 0, /* tp_alloc */ |
| 1522 | PyType_GenericNew, /* tp_new */ |
| 1523 | }; |
| 1524 | |
| 1525 | |
Benjamin Peterson | 59406a9 | 2009-03-26 17:10:29 +0000 | [diff] [blame] | 1526 | |
| 1527 | static int |
| 1528 | complain_about_max_buffer_size(void) |
| 1529 | { |
| 1530 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 1531 | "max_buffer_size is deprecated", 1) < 0) |
| 1532 | return 0; |
| 1533 | return 1; |
| 1534 | } |
| 1535 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1536 | /* |
| 1537 | * class BufferedWriter |
| 1538 | */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1539 | PyDoc_STRVAR(bufferedwriter_doc, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1540 | "A buffer for a writeable sequential RawIO object.\n" |
| 1541 | "\n" |
| 1542 | "The constructor creates a BufferedWriter for the given writeable raw\n" |
| 1543 | "stream. If the buffer_size is not given, it defaults to\n" |
| 1544 | "DEFAULT_BUFFER_SIZE. max_buffer_size isn't used anymore.\n" |
| 1545 | ); |
| 1546 | |
| 1547 | static void |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1548 | _bufferedwriter_reset_buf(buffered *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1549 | { |
| 1550 | self->write_pos = 0; |
| 1551 | self->write_end = -1; |
| 1552 | } |
| 1553 | |
| 1554 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1555 | bufferedwriter_init(buffered *self, PyObject *args, PyObject *kwds) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1556 | { |
| 1557 | /* TODO: properly deprecate max_buffer_size */ |
| 1558 | char *kwlist[] = {"raw", "buffer_size", "max_buffer_size", NULL}; |
| 1559 | Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE; |
Benjamin Peterson | 59406a9 | 2009-03-26 17:10:29 +0000 | [diff] [blame] | 1560 | Py_ssize_t max_buffer_size = -234; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1561 | PyObject *raw; |
| 1562 | |
| 1563 | self->ok = 0; |
Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 1564 | self->detached = 0; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1565 | |
| 1566 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|nn:BufferedReader", kwlist, |
| 1567 | &raw, &buffer_size, &max_buffer_size)) { |
| 1568 | return -1; |
| 1569 | } |
| 1570 | |
Benjamin Peterson | 59406a9 | 2009-03-26 17:10:29 +0000 | [diff] [blame] | 1571 | if (max_buffer_size != -234 && !complain_about_max_buffer_size()) |
| 1572 | return -1; |
| 1573 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1574 | if (_PyIOBase_check_writable(raw, Py_True) == NULL) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1575 | return -1; |
| 1576 | |
| 1577 | Py_CLEAR(self->raw); |
| 1578 | Py_INCREF(raw); |
| 1579 | self->raw = raw; |
| 1580 | self->readable = 0; |
| 1581 | self->writable = 1; |
| 1582 | |
| 1583 | self->buffer_size = buffer_size; |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1584 | if (_buffered_init(self) < 0) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1585 | return -1; |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1586 | _bufferedwriter_reset_buf(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1587 | self->pos = 0; |
| 1588 | |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 1589 | self->fast_closed_checks = (Py_TYPE(self) == &PyBufferedWriter_Type && |
| 1590 | Py_TYPE(raw) == &PyFileIO_Type); |
| 1591 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1592 | self->ok = 1; |
| 1593 | return 0; |
| 1594 | } |
| 1595 | |
| 1596 | static Py_ssize_t |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1597 | _bufferedwriter_raw_write(buffered *self, char *start, Py_ssize_t len) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1598 | { |
| 1599 | Py_buffer buf; |
| 1600 | PyObject *memobj, *res; |
| 1601 | Py_ssize_t n; |
| 1602 | /* NOTE: the buffer needn't be released as its object is NULL. */ |
| 1603 | if (PyBuffer_FillInfo(&buf, NULL, start, len, 1, PyBUF_CONTIG_RO) == -1) |
| 1604 | return -1; |
| 1605 | memobj = PyMemoryView_FromBuffer(&buf); |
| 1606 | if (memobj == NULL) |
| 1607 | return -1; |
| 1608 | res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_write, memobj, NULL); |
| 1609 | Py_DECREF(memobj); |
| 1610 | if (res == NULL) |
| 1611 | return -1; |
| 1612 | n = PyNumber_AsSsize_t(res, PyExc_ValueError); |
| 1613 | Py_DECREF(res); |
| 1614 | if (n < 0 || n > len) { |
| 1615 | PyErr_Format(PyExc_IOError, |
| 1616 | "raw write() returned invalid length %zd " |
| 1617 | "(should have been between 0 and %zd)", n, len); |
| 1618 | return -1; |
| 1619 | } |
| 1620 | if (n > 0 && self->abs_pos != -1) |
| 1621 | self->abs_pos += n; |
| 1622 | return n; |
| 1623 | } |
| 1624 | |
| 1625 | /* `restore_pos` is 1 if we need to restore the raw stream position at |
| 1626 | the end, 0 otherwise. */ |
| 1627 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1628 | _bufferedwriter_flush_unlocked(buffered *self, int restore_pos) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1629 | { |
| 1630 | Py_ssize_t written = 0; |
| 1631 | Py_off_t n, rewind; |
| 1632 | |
| 1633 | if (!VALID_WRITE_BUFFER(self) || self->write_pos == self->write_end) |
| 1634 | goto end; |
| 1635 | /* First, rewind */ |
| 1636 | rewind = RAW_OFFSET(self) + (self->pos - self->write_pos); |
| 1637 | if (rewind != 0) { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1638 | n = _buffered_raw_seek(self, -rewind, 1); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1639 | if (n < 0) { |
| 1640 | goto error; |
| 1641 | } |
| 1642 | self->raw_pos -= rewind; |
| 1643 | } |
| 1644 | while (self->write_pos < self->write_end) { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1645 | n = _bufferedwriter_raw_write(self, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1646 | self->buffer + self->write_pos, |
| 1647 | Py_SAFE_DOWNCAST(self->write_end - self->write_pos, |
| 1648 | Py_off_t, Py_ssize_t)); |
| 1649 | if (n == -1) { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1650 | Py_ssize_t *w = _buffered_check_blocking_error(); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1651 | if (w == NULL) |
| 1652 | goto error; |
| 1653 | self->write_pos += *w; |
| 1654 | self->raw_pos = self->write_pos; |
| 1655 | written += *w; |
| 1656 | *w = written; |
| 1657 | /* Already re-raised */ |
| 1658 | goto error; |
| 1659 | } |
| 1660 | self->write_pos += n; |
| 1661 | self->raw_pos = self->write_pos; |
| 1662 | written += Py_SAFE_DOWNCAST(n, Py_off_t, Py_ssize_t); |
| 1663 | } |
| 1664 | |
| 1665 | if (restore_pos) { |
| 1666 | Py_off_t forward = rewind - written; |
| 1667 | if (forward != 0) { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1668 | n = _buffered_raw_seek(self, forward, 1); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1669 | if (n < 0) { |
| 1670 | goto error; |
| 1671 | } |
| 1672 | self->raw_pos += forward; |
| 1673 | } |
| 1674 | } |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1675 | _bufferedwriter_reset_buf(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1676 | |
| 1677 | end: |
| 1678 | Py_RETURN_NONE; |
| 1679 | |
| 1680 | error: |
| 1681 | return NULL; |
| 1682 | } |
| 1683 | |
| 1684 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1685 | bufferedwriter_write(buffered *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1686 | { |
| 1687 | PyObject *res = NULL; |
| 1688 | Py_buffer buf; |
| 1689 | Py_ssize_t written, avail, remaining, n; |
| 1690 | |
| 1691 | CHECK_INITIALIZED(self) |
| 1692 | if (!PyArg_ParseTuple(args, "y*:write", &buf)) { |
| 1693 | return NULL; |
| 1694 | } |
| 1695 | |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 1696 | if (IS_CLOSED(self)) { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1697 | PyErr_SetString(PyExc_ValueError, "write to closed file"); |
| 1698 | PyBuffer_Release(&buf); |
| 1699 | return NULL; |
| 1700 | } |
| 1701 | |
| 1702 | ENTER_BUFFERED(self) |
| 1703 | |
| 1704 | /* Fast path: the data to write can be fully buffered. */ |
| 1705 | if (!VALID_READ_BUFFER(self) && !VALID_WRITE_BUFFER(self)) { |
| 1706 | self->pos = 0; |
| 1707 | self->raw_pos = 0; |
| 1708 | } |
| 1709 | avail = Py_SAFE_DOWNCAST(self->buffer_size - self->pos, Py_off_t, Py_ssize_t); |
| 1710 | if (buf.len <= avail) { |
| 1711 | memcpy(self->buffer + self->pos, buf.buf, buf.len); |
| 1712 | if (!VALID_WRITE_BUFFER(self)) { |
| 1713 | self->write_pos = self->pos; |
| 1714 | } |
| 1715 | ADJUST_POSITION(self, self->pos + buf.len); |
| 1716 | if (self->pos > self->write_end) |
| 1717 | self->write_end = self->pos; |
| 1718 | written = buf.len; |
| 1719 | goto end; |
| 1720 | } |
| 1721 | |
| 1722 | /* First write the current buffer */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1723 | res = _bufferedwriter_flush_unlocked(self, 0); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1724 | if (res == NULL) { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1725 | Py_ssize_t *w = _buffered_check_blocking_error(); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1726 | if (w == NULL) |
| 1727 | goto error; |
| 1728 | if (self->readable) |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1729 | _bufferedreader_reset_buf(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1730 | /* Make some place by shifting the buffer. */ |
| 1731 | assert(VALID_WRITE_BUFFER(self)); |
| 1732 | memmove(self->buffer, self->buffer + self->write_pos, |
| 1733 | Py_SAFE_DOWNCAST(self->write_end - self->write_pos, |
| 1734 | Py_off_t, Py_ssize_t)); |
| 1735 | self->write_end -= self->write_pos; |
| 1736 | self->raw_pos -= self->write_pos; |
| 1737 | self->pos -= self->write_pos; |
| 1738 | self->write_pos = 0; |
| 1739 | avail = Py_SAFE_DOWNCAST(self->buffer_size - self->write_end, |
| 1740 | Py_off_t, Py_ssize_t); |
| 1741 | if (buf.len <= avail) { |
| 1742 | /* Everything can be buffered */ |
| 1743 | PyErr_Clear(); |
| 1744 | memcpy(self->buffer + self->write_end, buf.buf, buf.len); |
| 1745 | self->write_end += buf.len; |
| 1746 | written = buf.len; |
| 1747 | goto end; |
| 1748 | } |
| 1749 | /* Buffer as much as possible. */ |
| 1750 | memcpy(self->buffer + self->write_end, buf.buf, avail); |
| 1751 | self->write_end += avail; |
| 1752 | /* Already re-raised */ |
| 1753 | *w = avail; |
| 1754 | goto error; |
| 1755 | } |
| 1756 | Py_CLEAR(res); |
| 1757 | |
Antoine Pitrou | 0473e56 | 2009-08-06 20:52:43 +0000 | [diff] [blame] | 1758 | /* Adjust the raw stream position if it is away from the logical stream |
| 1759 | position. This happens if the read buffer has been filled but not |
| 1760 | modified (and therefore _bufferedwriter_flush_unlocked() didn't rewind |
| 1761 | the raw stream by itself). |
| 1762 | Fixes issue #6629. |
| 1763 | */ |
| 1764 | n = RAW_OFFSET(self); |
| 1765 | if (n != 0) { |
| 1766 | if (_buffered_raw_seek(self, -n, 1) < 0) |
| 1767 | goto error; |
| 1768 | self->raw_pos -= n; |
| 1769 | } |
| 1770 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1771 | /* Then write buf itself. At this point the buffer has been emptied. */ |
| 1772 | remaining = buf.len; |
| 1773 | written = 0; |
| 1774 | while (remaining > self->buffer_size) { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1775 | n = _bufferedwriter_raw_write( |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1776 | self, (char *) buf.buf + written, buf.len - written); |
| 1777 | if (n == -1) { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1778 | Py_ssize_t *w = _buffered_check_blocking_error(); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1779 | if (w == NULL) |
| 1780 | goto error; |
| 1781 | written += *w; |
| 1782 | remaining -= *w; |
| 1783 | if (remaining > self->buffer_size) { |
| 1784 | /* Can't buffer everything, still buffer as much as possible */ |
| 1785 | memcpy(self->buffer, |
| 1786 | (char *) buf.buf + written, self->buffer_size); |
| 1787 | self->raw_pos = 0; |
| 1788 | ADJUST_POSITION(self, self->buffer_size); |
| 1789 | self->write_end = self->buffer_size; |
| 1790 | *w = written + self->buffer_size; |
| 1791 | /* Already re-raised */ |
| 1792 | goto error; |
| 1793 | } |
| 1794 | PyErr_Clear(); |
| 1795 | break; |
| 1796 | } |
| 1797 | written += n; |
| 1798 | remaining -= n; |
| 1799 | } |
| 1800 | if (self->readable) |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1801 | _bufferedreader_reset_buf(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1802 | if (remaining > 0) { |
| 1803 | memcpy(self->buffer, (char *) buf.buf + written, remaining); |
| 1804 | written += remaining; |
| 1805 | } |
| 1806 | self->write_pos = 0; |
| 1807 | /* TODO: sanity check (remaining >= 0) */ |
| 1808 | self->write_end = remaining; |
| 1809 | ADJUST_POSITION(self, remaining); |
| 1810 | self->raw_pos = 0; |
| 1811 | |
| 1812 | end: |
| 1813 | res = PyLong_FromSsize_t(written); |
| 1814 | |
| 1815 | error: |
| 1816 | LEAVE_BUFFERED(self) |
| 1817 | PyBuffer_Release(&buf); |
| 1818 | return res; |
| 1819 | } |
| 1820 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1821 | static PyMethodDef bufferedwriter_methods[] = { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1822 | /* BufferedIOMixin methods */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1823 | {"close", (PyCFunction)buffered_close, METH_NOARGS}, |
| 1824 | {"detach", (PyCFunction)buffered_detach, METH_NOARGS}, |
| 1825 | {"seekable", (PyCFunction)buffered_seekable, METH_NOARGS}, |
| 1826 | {"readable", (PyCFunction)buffered_readable, METH_NOARGS}, |
| 1827 | {"writable", (PyCFunction)buffered_writable, METH_NOARGS}, |
| 1828 | {"fileno", (PyCFunction)buffered_fileno, METH_NOARGS}, |
| 1829 | {"isatty", (PyCFunction)buffered_isatty, METH_NOARGS}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1830 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1831 | {"write", (PyCFunction)bufferedwriter_write, METH_VARARGS}, |
| 1832 | {"truncate", (PyCFunction)buffered_truncate, METH_VARARGS}, |
| 1833 | {"flush", (PyCFunction)buffered_flush, METH_NOARGS}, |
| 1834 | {"seek", (PyCFunction)buffered_seek, METH_VARARGS}, |
| 1835 | {"tell", (PyCFunction)buffered_tell, METH_NOARGS}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1836 | {NULL, NULL} |
| 1837 | }; |
| 1838 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1839 | static PyMemberDef bufferedwriter_members[] = { |
| 1840 | {"raw", T_OBJECT, offsetof(buffered, raw), 0}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1841 | {NULL} |
| 1842 | }; |
| 1843 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1844 | static PyGetSetDef bufferedwriter_getset[] = { |
| 1845 | {"closed", (getter)buffered_closed_get, NULL, NULL}, |
| 1846 | {"name", (getter)buffered_name_get, NULL, NULL}, |
| 1847 | {"mode", (getter)buffered_mode_get, NULL, NULL}, |
Benjamin Peterson | 1fea321 | 2009-04-19 03:15:20 +0000 | [diff] [blame] | 1848 | {NULL} |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1849 | }; |
| 1850 | |
| 1851 | |
| 1852 | PyTypeObject PyBufferedWriter_Type = { |
| 1853 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1854 | "_io.BufferedWriter", /*tp_name*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1855 | sizeof(buffered), /*tp_basicsize*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1856 | 0, /*tp_itemsize*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1857 | (destructor)buffered_dealloc, /*tp_dealloc*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1858 | 0, /*tp_print*/ |
| 1859 | 0, /*tp_getattr*/ |
| 1860 | 0, /*tp_setattr*/ |
| 1861 | 0, /*tp_compare */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1862 | (reprfunc)buffered_repr, /*tp_repr*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1863 | 0, /*tp_as_number*/ |
| 1864 | 0, /*tp_as_sequence*/ |
| 1865 | 0, /*tp_as_mapping*/ |
| 1866 | 0, /*tp_hash */ |
| 1867 | 0, /*tp_call*/ |
| 1868 | 0, /*tp_str*/ |
| 1869 | 0, /*tp_getattro*/ |
| 1870 | 0, /*tp_setattro*/ |
| 1871 | 0, /*tp_as_buffer*/ |
| 1872 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
| 1873 | | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1874 | bufferedwriter_doc, /* tp_doc */ |
| 1875 | (traverseproc)buffered_traverse, /* tp_traverse */ |
| 1876 | (inquiry)buffered_clear, /* tp_clear */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1877 | 0, /* tp_richcompare */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1878 | offsetof(buffered, weakreflist), /*tp_weaklistoffset*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1879 | 0, /* tp_iter */ |
| 1880 | 0, /* tp_iternext */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1881 | bufferedwriter_methods, /* tp_methods */ |
| 1882 | bufferedwriter_members, /* tp_members */ |
| 1883 | bufferedwriter_getset, /* tp_getset */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1884 | 0, /* tp_base */ |
| 1885 | 0, /* tp_dict */ |
| 1886 | 0, /* tp_descr_get */ |
| 1887 | 0, /* tp_descr_set */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1888 | offsetof(buffered, dict), /* tp_dictoffset */ |
| 1889 | (initproc)bufferedwriter_init, /* tp_init */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1890 | 0, /* tp_alloc */ |
| 1891 | PyType_GenericNew, /* tp_new */ |
| 1892 | }; |
| 1893 | |
| 1894 | |
| 1895 | |
| 1896 | /* |
| 1897 | * BufferedRWPair |
| 1898 | */ |
| 1899 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1900 | PyDoc_STRVAR(bufferedrwpair_doc, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1901 | "A buffered reader and writer object together.\n" |
| 1902 | "\n" |
| 1903 | "A buffered reader object and buffered writer object put together to\n" |
| 1904 | "form a sequential IO object that can read and write. This is typically\n" |
| 1905 | "used with a socket or two-way pipe.\n" |
| 1906 | "\n" |
| 1907 | "reader and writer are RawIOBase objects that are readable and\n" |
| 1908 | "writeable respectively. If the buffer_size is omitted it defaults to\n" |
Benjamin Peterson | 59406a9 | 2009-03-26 17:10:29 +0000 | [diff] [blame] | 1909 | "DEFAULT_BUFFER_SIZE.\n" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1910 | ); |
| 1911 | |
| 1912 | /* XXX The usefulness of this (compared to having two separate IO objects) is |
| 1913 | * questionable. |
| 1914 | */ |
| 1915 | |
| 1916 | typedef struct { |
| 1917 | PyObject_HEAD |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1918 | buffered *reader; |
| 1919 | buffered *writer; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1920 | PyObject *dict; |
| 1921 | PyObject *weakreflist; |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1922 | } rwpair; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1923 | |
| 1924 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1925 | bufferedrwpair_init(rwpair *self, PyObject *args, PyObject *kwds) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1926 | { |
| 1927 | PyObject *reader, *writer; |
| 1928 | Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE; |
Benjamin Peterson | 59406a9 | 2009-03-26 17:10:29 +0000 | [diff] [blame] | 1929 | Py_ssize_t max_buffer_size = -234; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1930 | |
| 1931 | if (!PyArg_ParseTuple(args, "OO|nn:BufferedRWPair", &reader, &writer, |
| 1932 | &buffer_size, &max_buffer_size)) { |
| 1933 | return -1; |
| 1934 | } |
| 1935 | |
Benjamin Peterson | 59406a9 | 2009-03-26 17:10:29 +0000 | [diff] [blame] | 1936 | if (max_buffer_size != -234 && !complain_about_max_buffer_size()) |
| 1937 | return -1; |
| 1938 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1939 | if (_PyIOBase_check_readable(reader, Py_True) == NULL) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1940 | return -1; |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1941 | if (_PyIOBase_check_writable(writer, Py_True) == NULL) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1942 | return -1; |
| 1943 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1944 | self->reader = (buffered *) PyObject_CallFunction( |
Antoine Pitrou | cf4c749 | 2009-04-19 00:09:36 +0000 | [diff] [blame] | 1945 | (PyObject *) &PyBufferedReader_Type, "On", reader, buffer_size); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1946 | if (self->reader == NULL) |
| 1947 | return -1; |
| 1948 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1949 | self->writer = (buffered *) PyObject_CallFunction( |
Antoine Pitrou | cf4c749 | 2009-04-19 00:09:36 +0000 | [diff] [blame] | 1950 | (PyObject *) &PyBufferedWriter_Type, "On", writer, buffer_size); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1951 | if (self->writer == NULL) { |
| 1952 | Py_CLEAR(self->reader); |
| 1953 | return -1; |
| 1954 | } |
Antoine Pitrou | cf4c749 | 2009-04-19 00:09:36 +0000 | [diff] [blame] | 1955 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1956 | return 0; |
| 1957 | } |
| 1958 | |
| 1959 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1960 | bufferedrwpair_traverse(rwpair *self, visitproc visit, void *arg) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1961 | { |
| 1962 | Py_VISIT(self->dict); |
| 1963 | return 0; |
| 1964 | } |
| 1965 | |
| 1966 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1967 | bufferedrwpair_clear(rwpair *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1968 | { |
| 1969 | Py_CLEAR(self->reader); |
| 1970 | Py_CLEAR(self->writer); |
| 1971 | Py_CLEAR(self->dict); |
| 1972 | return 0; |
| 1973 | } |
| 1974 | |
| 1975 | static void |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1976 | bufferedrwpair_dealloc(rwpair *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1977 | { |
| 1978 | _PyObject_GC_UNTRACK(self); |
| 1979 | Py_CLEAR(self->reader); |
| 1980 | Py_CLEAR(self->writer); |
| 1981 | Py_CLEAR(self->dict); |
| 1982 | Py_TYPE(self)->tp_free((PyObject *) self); |
| 1983 | } |
| 1984 | |
| 1985 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1986 | _forward_call(buffered *self, const char *name, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1987 | { |
| 1988 | PyObject *func = PyObject_GetAttrString((PyObject *)self, name); |
| 1989 | PyObject *ret; |
| 1990 | |
| 1991 | if (func == NULL) { |
| 1992 | PyErr_SetString(PyExc_AttributeError, name); |
| 1993 | return NULL; |
| 1994 | } |
| 1995 | |
| 1996 | ret = PyObject_CallObject(func, args); |
| 1997 | Py_DECREF(func); |
| 1998 | return ret; |
| 1999 | } |
| 2000 | |
| 2001 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2002 | bufferedrwpair_read(rwpair *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2003 | { |
| 2004 | return _forward_call(self->reader, "read", args); |
| 2005 | } |
| 2006 | |
| 2007 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2008 | bufferedrwpair_peek(rwpair *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2009 | { |
| 2010 | return _forward_call(self->reader, "peek", args); |
| 2011 | } |
| 2012 | |
| 2013 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2014 | bufferedrwpair_read1(rwpair *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2015 | { |
| 2016 | return _forward_call(self->reader, "read1", args); |
| 2017 | } |
| 2018 | |
| 2019 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2020 | bufferedrwpair_readinto(rwpair *self, PyObject *args) |
Antoine Pitrou | cf4c749 | 2009-04-19 00:09:36 +0000 | [diff] [blame] | 2021 | { |
| 2022 | return _forward_call(self->reader, "readinto", args); |
| 2023 | } |
| 2024 | |
| 2025 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2026 | bufferedrwpair_write(rwpair *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2027 | { |
| 2028 | return _forward_call(self->writer, "write", args); |
| 2029 | } |
| 2030 | |
| 2031 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2032 | bufferedrwpair_flush(rwpair *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2033 | { |
| 2034 | return _forward_call(self->writer, "flush", args); |
| 2035 | } |
| 2036 | |
| 2037 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2038 | bufferedrwpair_readable(rwpair *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2039 | { |
| 2040 | return _forward_call(self->reader, "readable", args); |
| 2041 | } |
| 2042 | |
| 2043 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2044 | bufferedrwpair_writable(rwpair *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2045 | { |
| 2046 | return _forward_call(self->writer, "writable", args); |
| 2047 | } |
| 2048 | |
| 2049 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2050 | bufferedrwpair_close(rwpair *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2051 | { |
| 2052 | PyObject *ret = _forward_call(self->writer, "close", args); |
| 2053 | if (ret == NULL) |
| 2054 | return NULL; |
| 2055 | Py_DECREF(ret); |
| 2056 | |
| 2057 | return _forward_call(self->reader, "close", args); |
| 2058 | } |
| 2059 | |
| 2060 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2061 | bufferedrwpair_isatty(rwpair *self, PyObject *args) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2062 | { |
| 2063 | PyObject *ret = _forward_call(self->writer, "isatty", args); |
| 2064 | |
| 2065 | if (ret != Py_False) { |
| 2066 | /* either True or exception */ |
| 2067 | return ret; |
| 2068 | } |
| 2069 | Py_DECREF(ret); |
| 2070 | |
| 2071 | return _forward_call(self->reader, "isatty", args); |
| 2072 | } |
| 2073 | |
Antoine Pitrou | cf4c749 | 2009-04-19 00:09:36 +0000 | [diff] [blame] | 2074 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2075 | bufferedrwpair_closed_get(rwpair *self, void *context) |
Antoine Pitrou | cf4c749 | 2009-04-19 00:09:36 +0000 | [diff] [blame] | 2076 | { |
| 2077 | return PyObject_GetAttr((PyObject *) self->writer, _PyIO_str_closed); |
| 2078 | } |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2079 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2080 | static PyMethodDef bufferedrwpair_methods[] = { |
| 2081 | {"read", (PyCFunction)bufferedrwpair_read, METH_VARARGS}, |
| 2082 | {"peek", (PyCFunction)bufferedrwpair_peek, METH_VARARGS}, |
| 2083 | {"read1", (PyCFunction)bufferedrwpair_read1, METH_VARARGS}, |
| 2084 | {"readinto", (PyCFunction)bufferedrwpair_readinto, METH_VARARGS}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2085 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2086 | {"write", (PyCFunction)bufferedrwpair_write, METH_VARARGS}, |
| 2087 | {"flush", (PyCFunction)bufferedrwpair_flush, METH_NOARGS}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2088 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2089 | {"readable", (PyCFunction)bufferedrwpair_readable, METH_NOARGS}, |
| 2090 | {"writable", (PyCFunction)bufferedrwpair_writable, METH_NOARGS}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2091 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2092 | {"close", (PyCFunction)bufferedrwpair_close, METH_NOARGS}, |
| 2093 | {"isatty", (PyCFunction)bufferedrwpair_isatty, METH_NOARGS}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2094 | |
| 2095 | {NULL, NULL} |
| 2096 | }; |
| 2097 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2098 | static PyGetSetDef bufferedrwpair_getset[] = { |
| 2099 | {"closed", (getter)bufferedrwpair_closed_get, NULL, NULL}, |
Benjamin Peterson | 1fea321 | 2009-04-19 03:15:20 +0000 | [diff] [blame] | 2100 | {NULL} |
Antoine Pitrou | cf4c749 | 2009-04-19 00:09:36 +0000 | [diff] [blame] | 2101 | }; |
| 2102 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2103 | PyTypeObject PyBufferedRWPair_Type = { |
| 2104 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2105 | "_io.BufferedRWPair", /*tp_name*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2106 | sizeof(rwpair), /*tp_basicsize*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2107 | 0, /*tp_itemsize*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2108 | (destructor)bufferedrwpair_dealloc, /*tp_dealloc*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2109 | 0, /*tp_print*/ |
| 2110 | 0, /*tp_getattr*/ |
| 2111 | 0, /*tp_setattr*/ |
| 2112 | 0, /*tp_compare */ |
| 2113 | 0, /*tp_repr*/ |
| 2114 | 0, /*tp_as_number*/ |
| 2115 | 0, /*tp_as_sequence*/ |
| 2116 | 0, /*tp_as_mapping*/ |
| 2117 | 0, /*tp_hash */ |
| 2118 | 0, /*tp_call*/ |
| 2119 | 0, /*tp_str*/ |
| 2120 | 0, /*tp_getattro*/ |
| 2121 | 0, /*tp_setattro*/ |
| 2122 | 0, /*tp_as_buffer*/ |
| 2123 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
| 2124 | | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2125 | bufferedrwpair_doc, /* tp_doc */ |
| 2126 | (traverseproc)bufferedrwpair_traverse, /* tp_traverse */ |
| 2127 | (inquiry)bufferedrwpair_clear, /* tp_clear */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2128 | 0, /* tp_richcompare */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2129 | offsetof(rwpair, weakreflist), /*tp_weaklistoffset*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2130 | 0, /* tp_iter */ |
| 2131 | 0, /* tp_iternext */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2132 | bufferedrwpair_methods, /* tp_methods */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2133 | 0, /* tp_members */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2134 | bufferedrwpair_getset, /* tp_getset */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2135 | 0, /* tp_base */ |
| 2136 | 0, /* tp_dict */ |
| 2137 | 0, /* tp_descr_get */ |
| 2138 | 0, /* tp_descr_set */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2139 | offsetof(rwpair, dict), /* tp_dictoffset */ |
| 2140 | (initproc)bufferedrwpair_init, /* tp_init */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2141 | 0, /* tp_alloc */ |
| 2142 | PyType_GenericNew, /* tp_new */ |
| 2143 | }; |
| 2144 | |
| 2145 | |
| 2146 | |
| 2147 | /* |
| 2148 | * BufferedRandom |
| 2149 | */ |
| 2150 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2151 | PyDoc_STRVAR(bufferedrandom_doc, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2152 | "A buffered interface to random access streams.\n" |
| 2153 | "\n" |
| 2154 | "The constructor creates a reader and writer for a seekable stream,\n" |
| 2155 | "raw, given in the first argument. If the buffer_size is omitted it\n" |
| 2156 | "defaults to DEFAULT_BUFFER_SIZE. max_buffer_size isn't used anymore.\n" |
| 2157 | ); |
| 2158 | |
| 2159 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2160 | bufferedrandom_init(buffered *self, PyObject *args, PyObject *kwds) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2161 | { |
| 2162 | char *kwlist[] = {"raw", "buffer_size", "max_buffer_size", NULL}; |
| 2163 | Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE; |
Benjamin Peterson | 59406a9 | 2009-03-26 17:10:29 +0000 | [diff] [blame] | 2164 | Py_ssize_t max_buffer_size = -234; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2165 | PyObject *raw; |
| 2166 | |
| 2167 | self->ok = 0; |
Benjamin Peterson | d2e0c79 | 2009-05-01 20:40:59 +0000 | [diff] [blame] | 2168 | self->detached = 0; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2169 | |
| 2170 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|nn:BufferedReader", kwlist, |
| 2171 | &raw, &buffer_size, &max_buffer_size)) { |
| 2172 | return -1; |
| 2173 | } |
| 2174 | |
Benjamin Peterson | 59406a9 | 2009-03-26 17:10:29 +0000 | [diff] [blame] | 2175 | if (max_buffer_size != -234 && !complain_about_max_buffer_size()) |
| 2176 | return -1; |
| 2177 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2178 | if (_PyIOBase_check_seekable(raw, Py_True) == NULL) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2179 | return -1; |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2180 | if (_PyIOBase_check_readable(raw, Py_True) == NULL) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2181 | return -1; |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2182 | if (_PyIOBase_check_writable(raw, Py_True) == NULL) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2183 | return -1; |
| 2184 | |
| 2185 | Py_CLEAR(self->raw); |
| 2186 | Py_INCREF(raw); |
| 2187 | self->raw = raw; |
| 2188 | self->buffer_size = buffer_size; |
| 2189 | self->readable = 1; |
| 2190 | self->writable = 1; |
| 2191 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2192 | if (_buffered_init(self) < 0) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2193 | return -1; |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2194 | _bufferedreader_reset_buf(self); |
| 2195 | _bufferedwriter_reset_buf(self); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2196 | self->pos = 0; |
| 2197 | |
Antoine Pitrou | 711af3a | 2009-04-11 15:39:24 +0000 | [diff] [blame] | 2198 | self->fast_closed_checks = (Py_TYPE(self) == &PyBufferedRandom_Type && |
| 2199 | Py_TYPE(raw) == &PyFileIO_Type); |
| 2200 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2201 | self->ok = 1; |
| 2202 | return 0; |
| 2203 | } |
| 2204 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2205 | static PyMethodDef bufferedrandom_methods[] = { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2206 | /* BufferedIOMixin methods */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2207 | {"close", (PyCFunction)buffered_close, METH_NOARGS}, |
| 2208 | {"detach", (PyCFunction)buffered_detach, METH_NOARGS}, |
| 2209 | {"seekable", (PyCFunction)buffered_seekable, METH_NOARGS}, |
| 2210 | {"readable", (PyCFunction)buffered_readable, METH_NOARGS}, |
| 2211 | {"writable", (PyCFunction)buffered_writable, METH_NOARGS}, |
| 2212 | {"fileno", (PyCFunction)buffered_fileno, METH_NOARGS}, |
| 2213 | {"isatty", (PyCFunction)buffered_isatty, METH_NOARGS}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2214 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2215 | {"flush", (PyCFunction)buffered_flush, METH_NOARGS}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2216 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2217 | {"seek", (PyCFunction)buffered_seek, METH_VARARGS}, |
| 2218 | {"tell", (PyCFunction)buffered_tell, METH_NOARGS}, |
| 2219 | {"truncate", (PyCFunction)buffered_truncate, METH_VARARGS}, |
| 2220 | {"read", (PyCFunction)buffered_read, METH_VARARGS}, |
| 2221 | {"read1", (PyCFunction)buffered_read1, METH_VARARGS}, |
| 2222 | {"readinto", (PyCFunction)buffered_readinto, METH_VARARGS}, |
| 2223 | {"readline", (PyCFunction)buffered_readline, METH_VARARGS}, |
| 2224 | {"peek", (PyCFunction)buffered_peek, METH_VARARGS}, |
| 2225 | {"write", (PyCFunction)bufferedwriter_write, METH_VARARGS}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2226 | {NULL, NULL} |
| 2227 | }; |
| 2228 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2229 | static PyMemberDef bufferedrandom_members[] = { |
| 2230 | {"raw", T_OBJECT, offsetof(buffered, raw), 0}, |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2231 | {NULL} |
| 2232 | }; |
| 2233 | |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2234 | static PyGetSetDef bufferedrandom_getset[] = { |
| 2235 | {"closed", (getter)buffered_closed_get, NULL, NULL}, |
| 2236 | {"name", (getter)buffered_name_get, NULL, NULL}, |
| 2237 | {"mode", (getter)buffered_mode_get, NULL, NULL}, |
Benjamin Peterson | 1fea321 | 2009-04-19 03:15:20 +0000 | [diff] [blame] | 2238 | {NULL} |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2239 | }; |
| 2240 | |
| 2241 | |
| 2242 | PyTypeObject PyBufferedRandom_Type = { |
| 2243 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2244 | "_io.BufferedRandom", /*tp_name*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2245 | sizeof(buffered), /*tp_basicsize*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2246 | 0, /*tp_itemsize*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2247 | (destructor)buffered_dealloc, /*tp_dealloc*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2248 | 0, /*tp_print*/ |
| 2249 | 0, /*tp_getattr*/ |
| 2250 | 0, /*tp_setattr*/ |
| 2251 | 0, /*tp_compare */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2252 | (reprfunc)buffered_repr, /*tp_repr*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2253 | 0, /*tp_as_number*/ |
| 2254 | 0, /*tp_as_sequence*/ |
| 2255 | 0, /*tp_as_mapping*/ |
| 2256 | 0, /*tp_hash */ |
| 2257 | 0, /*tp_call*/ |
| 2258 | 0, /*tp_str*/ |
| 2259 | 0, /*tp_getattro*/ |
| 2260 | 0, /*tp_setattro*/ |
| 2261 | 0, /*tp_as_buffer*/ |
| 2262 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
| 2263 | | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2264 | bufferedrandom_doc, /* tp_doc */ |
| 2265 | (traverseproc)buffered_traverse, /* tp_traverse */ |
| 2266 | (inquiry)buffered_clear, /* tp_clear */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2267 | 0, /* tp_richcompare */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2268 | offsetof(buffered, weakreflist), /*tp_weaklistoffset*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2269 | 0, /* tp_iter */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2270 | (iternextfunc)buffered_iternext, /* tp_iternext */ |
| 2271 | bufferedrandom_methods, /* tp_methods */ |
| 2272 | bufferedrandom_members, /* tp_members */ |
| 2273 | bufferedrandom_getset, /* tp_getset */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2274 | 0, /* tp_base */ |
| 2275 | 0, /*tp_dict*/ |
| 2276 | 0, /* tp_descr_get */ |
| 2277 | 0, /* tp_descr_set */ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 2278 | offsetof(buffered, dict), /*tp_dictoffset*/ |
| 2279 | (initproc)bufferedrandom_init, /* tp_init */ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2280 | 0, /* tp_alloc */ |
| 2281 | PyType_GenericNew, /* tp_new */ |
| 2282 | }; |
| 2283 | |