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