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