Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 1 | #include "Python.h" |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 2 | #include "structmember.h" /* for offsetof() */ |
| 3 | #include "_iomodule.h" |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 4 | |
| 5 | typedef struct { |
| 6 | PyObject_HEAD |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 7 | PyObject *buf; |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 8 | Py_ssize_t pos; |
| 9 | Py_ssize_t string_size; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 10 | PyObject *dict; |
| 11 | PyObject *weakreflist; |
Antoine Pitrou | 972ee13 | 2010-09-06 18:48:21 +0000 | [diff] [blame] | 12 | Py_ssize_t exports; |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 13 | } bytesio; |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 14 | |
Antoine Pitrou | 972ee13 | 2010-09-06 18:48:21 +0000 | [diff] [blame] | 15 | typedef struct { |
| 16 | PyObject_HEAD |
| 17 | bytesio *source; |
| 18 | } bytesiobuf; |
| 19 | |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 20 | /* The bytesio object can be in three states: |
| 21 | * Py_REFCNT(buf) == 1, exports == 0. |
Serhiy Storchaka | 38c30e6 | 2015-02-03 18:51:58 +0200 | [diff] [blame] | 22 | * Py_REFCNT(buf) > 1. exports == 0, |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 23 | first modification or export causes the internal buffer copying. |
| 24 | * exports > 0. Py_REFCNT(buf) == 1, any modifications are forbidden. |
| 25 | */ |
Antoine Pitrou | 972ee13 | 2010-09-06 18:48:21 +0000 | [diff] [blame] | 26 | |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 27 | #define CHECK_CLOSED(self) \ |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 28 | if ((self)->buf == NULL) { \ |
| 29 | PyErr_SetString(PyExc_ValueError, \ |
| 30 | "I/O operation on closed file."); \ |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 31 | return NULL; \ |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Antoine Pitrou | 972ee13 | 2010-09-06 18:48:21 +0000 | [diff] [blame] | 34 | #define CHECK_EXPORTS(self) \ |
| 35 | if ((self)->exports > 0) { \ |
| 36 | PyErr_SetString(PyExc_BufferError, \ |
| 37 | "Existing exports of data: object cannot be re-sized"); \ |
| 38 | return NULL; \ |
| 39 | } |
| 40 | |
Serhiy Storchaka | 38c30e6 | 2015-02-03 18:51:58 +0200 | [diff] [blame] | 41 | #define SHARED_BUF(self) (Py_REFCNT((self)->buf) > 1) |
Antoine Pitrou | cc66a73 | 2014-07-29 19:41:11 -0400 | [diff] [blame] | 42 | |
Antoine Pitrou | 972ee13 | 2010-09-06 18:48:21 +0000 | [diff] [blame] | 43 | |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 44 | /* Internal routine to get a line from the buffer of a BytesIO |
| 45 | object. Returns the length between the current position to the |
| 46 | next newline character. */ |
| 47 | static Py_ssize_t |
Serhiy Storchaka | d7728ca | 2014-08-14 22:26:38 +0300 | [diff] [blame] | 48 | scan_eol(bytesio *self, Py_ssize_t len) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 49 | { |
Serhiy Storchaka | d7728ca | 2014-08-14 22:26:38 +0300 | [diff] [blame] | 50 | const char *start, *n; |
| 51 | Py_ssize_t maxlen; |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 52 | |
| 53 | assert(self->buf != NULL); |
| 54 | |
| 55 | /* Move to the end of the line, up to the end of the string, s. */ |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 56 | start = PyBytes_AS_STRING(self->buf) + self->pos; |
Serhiy Storchaka | d7728ca | 2014-08-14 22:26:38 +0300 | [diff] [blame] | 57 | maxlen = self->string_size - self->pos; |
| 58 | if (len < 0 || len > maxlen) |
| 59 | len = maxlen; |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 60 | |
Serhiy Storchaka | d7728ca | 2014-08-14 22:26:38 +0300 | [diff] [blame] | 61 | if (len) { |
| 62 | n = memchr(start, '\n', len); |
| 63 | if (n) |
| 64 | /* Get the length from the current position to the end of |
| 65 | the line. */ |
| 66 | len = n - start + 1; |
| 67 | } |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 68 | assert(len >= 0); |
| 69 | assert(self->pos < PY_SSIZE_T_MAX - len); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 70 | |
| 71 | return len; |
| 72 | } |
| 73 | |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 74 | /* Internal routine for detaching the shared buffer of BytesIO objects. |
| 75 | The caller should ensure that the 'size' argument is non-negative and |
| 76 | not lesser than self->string_size. Returns 0 on success, -1 otherwise. */ |
| 77 | static int |
| 78 | unshare_buffer(bytesio *self, size_t size) |
| 79 | { |
| 80 | PyObject *new_buf, *old_buf; |
| 81 | assert(SHARED_BUF(self)); |
| 82 | assert(self->exports == 0); |
| 83 | assert(size >= (size_t)self->string_size); |
| 84 | new_buf = PyBytes_FromStringAndSize(NULL, size); |
| 85 | if (new_buf == NULL) |
| 86 | return -1; |
| 87 | memcpy(PyBytes_AS_STRING(new_buf), PyBytes_AS_STRING(self->buf), |
| 88 | self->string_size); |
| 89 | old_buf = self->buf; |
| 90 | self->buf = new_buf; |
| 91 | Py_DECREF(old_buf); |
| 92 | return 0; |
| 93 | } |
| 94 | |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 95 | /* Internal routine for changing the size of the buffer of BytesIO objects. |
| 96 | The caller should ensure that the 'size' argument is non-negative. Returns |
| 97 | 0 on success, -1 otherwise. */ |
| 98 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 99 | resize_buffer(bytesio *self, size_t size) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 100 | { |
| 101 | /* Here, unsigned types are used to avoid dealing with signed integer |
| 102 | overflow, which is undefined in C. */ |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 103 | size_t alloc = PyBytes_GET_SIZE(self->buf); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 104 | |
| 105 | assert(self->buf != NULL); |
| 106 | |
| 107 | /* For simplicity, stay in the range of the signed type. Anyway, Python |
| 108 | doesn't allow strings to be longer than this. */ |
| 109 | if (size > PY_SSIZE_T_MAX) |
| 110 | goto overflow; |
| 111 | |
| 112 | if (size < alloc / 2) { |
| 113 | /* Major downsize; resize down to exact size. */ |
| 114 | alloc = size + 1; |
| 115 | } |
| 116 | else if (size < alloc) { |
| 117 | /* Within allocated size; quick exit */ |
| 118 | return 0; |
| 119 | } |
| 120 | else if (size <= alloc * 1.125) { |
| 121 | /* Moderate upsize; overallocate similar to list_resize() */ |
| 122 | alloc = size + (size >> 3) + (size < 9 ? 3 : 6); |
| 123 | } |
| 124 | else { |
| 125 | /* Major upsize; resize up to exact size */ |
| 126 | alloc = size + 1; |
| 127 | } |
| 128 | |
| 129 | if (alloc > ((size_t)-1) / sizeof(char)) |
| 130 | goto overflow; |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 131 | |
| 132 | if (SHARED_BUF(self)) { |
| 133 | if (unshare_buffer(self, alloc) < 0) |
| 134 | return -1; |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 135 | } |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 136 | else { |
| 137 | if (_PyBytes_Resize(&self->buf, alloc) < 0) |
| 138 | return -1; |
| 139 | } |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 140 | |
| 141 | return 0; |
| 142 | |
| 143 | overflow: |
| 144 | PyErr_SetString(PyExc_OverflowError, |
| 145 | "new buffer size too large"); |
| 146 | return -1; |
| 147 | } |
| 148 | |
| 149 | /* Internal routine for writing a string of bytes to the buffer of a BytesIO |
Antoine Pitrou | 1d85745 | 2012-09-05 20:11:49 +0200 | [diff] [blame] | 150 | object. Returns the number of bytes written, or -1 on error. */ |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 151 | static Py_ssize_t |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 152 | write_bytes(bytesio *self, const char *bytes, Py_ssize_t len) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 153 | { |
Serhiy Storchaka | 38c30e6 | 2015-02-03 18:51:58 +0200 | [diff] [blame] | 154 | size_t endpos; |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 155 | assert(self->buf != NULL); |
| 156 | assert(self->pos >= 0); |
| 157 | assert(len >= 0); |
| 158 | |
Serhiy Storchaka | 38c30e6 | 2015-02-03 18:51:58 +0200 | [diff] [blame] | 159 | endpos = (size_t)self->pos + len; |
| 160 | if (endpos > (size_t)PyBytes_GET_SIZE(self->buf)) { |
| 161 | if (resize_buffer(self, endpos) < 0) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 162 | return -1; |
| 163 | } |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 164 | else if (SHARED_BUF(self)) { |
Serhiy Storchaka | 38c30e6 | 2015-02-03 18:51:58 +0200 | [diff] [blame] | 165 | if (unshare_buffer(self, Py_MAX(endpos, (size_t)self->string_size)) < 0) |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 166 | return -1; |
| 167 | } |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 168 | |
| 169 | if (self->pos > self->string_size) { |
| 170 | /* In case of overseek, pad with null bytes the buffer region between |
| 171 | the end of stream and the current position. |
| 172 | |
| 173 | 0 lo string_size hi |
| 174 | | |<---used--->|<----------available----------->| |
| 175 | | | <--to pad-->|<---to write---> | |
| 176 | 0 buf position |
| 177 | */ |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 178 | memset(PyBytes_AS_STRING(self->buf) + self->string_size, '\0', |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 179 | (self->pos - self->string_size) * sizeof(char)); |
| 180 | } |
| 181 | |
| 182 | /* Copy the data to the internal buffer, overwriting some of the existing |
| 183 | data if self->pos < self->string_size. */ |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 184 | memcpy(PyBytes_AS_STRING(self->buf) + self->pos, bytes, len); |
Serhiy Storchaka | 38c30e6 | 2015-02-03 18:51:58 +0200 | [diff] [blame] | 185 | self->pos = endpos; |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 186 | |
| 187 | /* Set the new length of the internal string if it has changed. */ |
Serhiy Storchaka | 38c30e6 | 2015-02-03 18:51:58 +0200 | [diff] [blame] | 188 | if ((size_t)self->string_size < endpos) { |
| 189 | self->string_size = endpos; |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | return len; |
| 193 | } |
| 194 | |
| 195 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 196 | bytesio_get_closed(bytesio *self) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 197 | { |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 198 | if (self->buf == NULL) { |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 199 | Py_RETURN_TRUE; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 200 | } |
| 201 | else { |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 202 | Py_RETURN_FALSE; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 203 | } |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Antoine Pitrou | 1d85745 | 2012-09-05 20:11:49 +0200 | [diff] [blame] | 206 | PyDoc_STRVAR(readable_doc, |
| 207 | "readable() -> bool. Returns True if the IO object can be read."); |
| 208 | |
| 209 | PyDoc_STRVAR(writable_doc, |
| 210 | "writable() -> bool. Returns True if the IO object can be written."); |
| 211 | |
| 212 | PyDoc_STRVAR(seekable_doc, |
| 213 | "seekable() -> bool. Returns True if the IO object can be seeked."); |
| 214 | |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 215 | /* Generic getter for the writable, readable and seekable properties */ |
| 216 | static PyObject * |
Antoine Pitrou | 1d85745 | 2012-09-05 20:11:49 +0200 | [diff] [blame] | 217 | return_not_closed(bytesio *self) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 218 | { |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 219 | CHECK_CLOSED(self); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 220 | Py_RETURN_TRUE; |
| 221 | } |
| 222 | |
| 223 | PyDoc_STRVAR(flush_doc, |
| 224 | "flush() -> None. Does nothing."); |
| 225 | |
| 226 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 227 | bytesio_flush(bytesio *self) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 228 | { |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 229 | CHECK_CLOSED(self); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 230 | Py_RETURN_NONE; |
| 231 | } |
| 232 | |
Antoine Pitrou | 972ee13 | 2010-09-06 18:48:21 +0000 | [diff] [blame] | 233 | PyDoc_STRVAR(getbuffer_doc, |
| 234 | "getbuffer() -> bytes.\n" |
| 235 | "\n" |
| 236 | "Get a read-write view over the contents of the BytesIO object."); |
| 237 | |
| 238 | static PyObject * |
| 239 | bytesio_getbuffer(bytesio *self) |
| 240 | { |
| 241 | PyTypeObject *type = &_PyBytesIOBuffer_Type; |
| 242 | bytesiobuf *buf; |
| 243 | PyObject *view; |
| 244 | |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 245 | CHECK_CLOSED(self); |
Antoine Pitrou | 972ee13 | 2010-09-06 18:48:21 +0000 | [diff] [blame] | 246 | |
| 247 | buf = (bytesiobuf *) type->tp_alloc(type, 0); |
| 248 | if (buf == NULL) |
| 249 | return NULL; |
| 250 | Py_INCREF(self); |
| 251 | buf->source = self; |
| 252 | view = PyMemoryView_FromObject((PyObject *) buf); |
| 253 | Py_DECREF(buf); |
| 254 | return view; |
| 255 | } |
| 256 | |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 257 | PyDoc_STRVAR(getval_doc, |
Alexandre Vassalotti | 10dfc1e | 2008-05-08 01:34:41 +0000 | [diff] [blame] | 258 | "getvalue() -> bytes.\n" |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 259 | "\n" |
| 260 | "Retrieve the entire contents of the BytesIO object."); |
| 261 | |
| 262 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 263 | bytesio_getvalue(bytesio *self) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 264 | { |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 265 | CHECK_CLOSED(self); |
| 266 | if (self->string_size <= 1 || self->exports > 0) |
| 267 | return PyBytes_FromStringAndSize(PyBytes_AS_STRING(self->buf), |
| 268 | self->string_size); |
| 269 | |
| 270 | if (self->string_size != PyBytes_GET_SIZE(self->buf)) { |
| 271 | if (SHARED_BUF(self)) { |
| 272 | if (unshare_buffer(self, self->string_size) < 0) |
| 273 | return NULL; |
| 274 | } |
| 275 | else { |
| 276 | if (_PyBytes_Resize(&self->buf, self->string_size) < 0) |
| 277 | return NULL; |
| 278 | } |
| 279 | } |
| 280 | Py_INCREF(self->buf); |
| 281 | return self->buf; |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | PyDoc_STRVAR(isatty_doc, |
| 285 | "isatty() -> False.\n" |
| 286 | "\n" |
| 287 | "Always returns False since BytesIO objects are not connected\n" |
| 288 | "to a tty-like device."); |
| 289 | |
| 290 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 291 | bytesio_isatty(bytesio *self) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 292 | { |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 293 | CHECK_CLOSED(self); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 294 | Py_RETURN_FALSE; |
| 295 | } |
| 296 | |
| 297 | PyDoc_STRVAR(tell_doc, |
| 298 | "tell() -> current file position, an integer\n"); |
| 299 | |
| 300 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 301 | bytesio_tell(bytesio *self) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 302 | { |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 303 | CHECK_CLOSED(self); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 304 | return PyLong_FromSsize_t(self->pos); |
| 305 | } |
| 306 | |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 307 | static PyObject * |
| 308 | read_bytes(bytesio *self, Py_ssize_t size) |
| 309 | { |
| 310 | char *output; |
| 311 | |
| 312 | assert(self->buf != NULL); |
Serhiy Storchaka | b9765ee | 2015-02-03 14:57:49 +0200 | [diff] [blame] | 313 | assert(size <= self->string_size); |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 314 | if (size > 1 && |
| 315 | self->pos == 0 && size == PyBytes_GET_SIZE(self->buf) && |
| 316 | self->exports == 0) { |
| 317 | self->pos += size; |
| 318 | Py_INCREF(self->buf); |
| 319 | return self->buf; |
| 320 | } |
| 321 | |
| 322 | output = PyBytes_AS_STRING(self->buf) + self->pos; |
| 323 | self->pos += size; |
| 324 | return PyBytes_FromStringAndSize(output, size); |
| 325 | } |
| 326 | |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 327 | PyDoc_STRVAR(read_doc, |
| 328 | "read([size]) -> read at most size bytes, returned as a string.\n" |
| 329 | "\n" |
| 330 | "If the size argument is negative, read until EOF is reached.\n" |
| 331 | "Return an empty string at EOF."); |
| 332 | |
| 333 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 334 | bytesio_read(bytesio *self, PyObject *args) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 335 | { |
| 336 | Py_ssize_t size, n; |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 337 | PyObject *arg = Py_None; |
| 338 | |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 339 | CHECK_CLOSED(self); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 340 | |
| 341 | if (!PyArg_ParseTuple(args, "|O:read", &arg)) |
| 342 | return NULL; |
| 343 | |
| 344 | if (PyLong_Check(arg)) { |
| 345 | size = PyLong_AsSsize_t(arg); |
Benjamin Peterson | a8a9304 | 2008-09-30 02:18:09 +0000 | [diff] [blame] | 346 | if (size == -1 && PyErr_Occurred()) |
| 347 | return NULL; |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 348 | } |
| 349 | else if (arg == Py_None) { |
| 350 | /* Read until EOF is reached, by default. */ |
| 351 | size = -1; |
| 352 | } |
| 353 | else { |
| 354 | PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'", |
| 355 | Py_TYPE(arg)->tp_name); |
| 356 | return NULL; |
| 357 | } |
| 358 | |
| 359 | /* adjust invalid sizes */ |
| 360 | n = self->string_size - self->pos; |
| 361 | if (size < 0 || size > n) { |
| 362 | size = n; |
| 363 | if (size < 0) |
| 364 | size = 0; |
| 365 | } |
| 366 | |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 367 | return read_bytes(self, size); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | |
| 371 | PyDoc_STRVAR(read1_doc, |
| 372 | "read1(size) -> read at most size bytes, returned as a string.\n" |
| 373 | "\n" |
| 374 | "If the size argument is negative or omitted, read until EOF is reached.\n" |
| 375 | "Return an empty string at EOF."); |
| 376 | |
| 377 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 378 | bytesio_read1(bytesio *self, PyObject *n) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 379 | { |
| 380 | PyObject *arg, *res; |
| 381 | |
| 382 | arg = PyTuple_Pack(1, n); |
| 383 | if (arg == NULL) |
| 384 | return NULL; |
| 385 | res = bytesio_read(self, arg); |
| 386 | Py_DECREF(arg); |
| 387 | return res; |
| 388 | } |
| 389 | |
| 390 | PyDoc_STRVAR(readline_doc, |
| 391 | "readline([size]) -> next line from the file, as a string.\n" |
| 392 | "\n" |
| 393 | "Retain newline. A non-negative size argument limits the maximum\n" |
| 394 | "number of bytes to return (an incomplete line may be returned then).\n" |
| 395 | "Return an empty string at EOF.\n"); |
| 396 | |
| 397 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 398 | bytesio_readline(bytesio *self, PyObject *args) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 399 | { |
| 400 | Py_ssize_t size, n; |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 401 | PyObject *arg = Py_None; |
| 402 | |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 403 | CHECK_CLOSED(self); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 404 | |
| 405 | if (!PyArg_ParseTuple(args, "|O:readline", &arg)) |
| 406 | return NULL; |
| 407 | |
| 408 | if (PyLong_Check(arg)) { |
| 409 | size = PyLong_AsSsize_t(arg); |
Benjamin Peterson | a8a9304 | 2008-09-30 02:18:09 +0000 | [diff] [blame] | 410 | if (size == -1 && PyErr_Occurred()) |
| 411 | return NULL; |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 412 | } |
| 413 | else if (arg == Py_None) { |
| 414 | /* No size limit, by default. */ |
| 415 | size = -1; |
| 416 | } |
| 417 | else { |
| 418 | PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'", |
| 419 | Py_TYPE(arg)->tp_name); |
| 420 | return NULL; |
| 421 | } |
| 422 | |
Serhiy Storchaka | d7728ca | 2014-08-14 22:26:38 +0300 | [diff] [blame] | 423 | n = scan_eol(self, size); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 424 | |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 425 | return read_bytes(self, n); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | PyDoc_STRVAR(readlines_doc, |
| 429 | "readlines([size]) -> list of strings, each a line from the file.\n" |
| 430 | "\n" |
| 431 | "Call readline() repeatedly and return a list of the lines so read.\n" |
| 432 | "The optional size argument, if given, is an approximate bound on the\n" |
| 433 | "total number of bytes in the lines returned.\n"); |
| 434 | |
| 435 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 436 | bytesio_readlines(bytesio *self, PyObject *args) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 437 | { |
| 438 | Py_ssize_t maxsize, size, n; |
| 439 | PyObject *result, *line; |
| 440 | char *output; |
| 441 | PyObject *arg = Py_None; |
| 442 | |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 443 | CHECK_CLOSED(self); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 444 | |
| 445 | if (!PyArg_ParseTuple(args, "|O:readlines", &arg)) |
| 446 | return NULL; |
| 447 | |
| 448 | if (PyLong_Check(arg)) { |
| 449 | maxsize = PyLong_AsSsize_t(arg); |
Benjamin Peterson | a8a9304 | 2008-09-30 02:18:09 +0000 | [diff] [blame] | 450 | if (maxsize == -1 && PyErr_Occurred()) |
| 451 | return NULL; |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 452 | } |
| 453 | else if (arg == Py_None) { |
| 454 | /* No size limit, by default. */ |
| 455 | maxsize = -1; |
| 456 | } |
| 457 | else { |
| 458 | PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'", |
| 459 | Py_TYPE(arg)->tp_name); |
| 460 | return NULL; |
| 461 | } |
| 462 | |
| 463 | size = 0; |
| 464 | result = PyList_New(0); |
| 465 | if (!result) |
| 466 | return NULL; |
| 467 | |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 468 | output = PyBytes_AS_STRING(self->buf) + self->pos; |
Serhiy Storchaka | d7728ca | 2014-08-14 22:26:38 +0300 | [diff] [blame] | 469 | while ((n = scan_eol(self, -1)) != 0) { |
| 470 | self->pos += n; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 471 | line = PyBytes_FromStringAndSize(output, n); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 472 | if (!line) |
| 473 | goto on_error; |
| 474 | if (PyList_Append(result, line) == -1) { |
| 475 | Py_DECREF(line); |
| 476 | goto on_error; |
| 477 | } |
| 478 | Py_DECREF(line); |
| 479 | size += n; |
| 480 | if (maxsize > 0 && size >= maxsize) |
| 481 | break; |
Serhiy Storchaka | d7728ca | 2014-08-14 22:26:38 +0300 | [diff] [blame] | 482 | output += n; |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 483 | } |
| 484 | return result; |
| 485 | |
| 486 | on_error: |
| 487 | Py_DECREF(result); |
| 488 | return NULL; |
| 489 | } |
| 490 | |
| 491 | PyDoc_STRVAR(readinto_doc, |
Alexandre Vassalotti | 10dfc1e | 2008-05-08 01:34:41 +0000 | [diff] [blame] | 492 | "readinto(bytearray) -> int. Read up to len(b) bytes into b.\n" |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 493 | "\n" |
| 494 | "Returns number of bytes read (0 for EOF), or None if the object\n" |
| 495 | "is set not to block as has no data to read."); |
| 496 | |
| 497 | static PyObject * |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 498 | bytesio_readinto(bytesio *self, PyObject *arg) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 499 | { |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 500 | Py_buffer buffer; |
Benjamin Peterson | fa73555 | 2010-11-20 17:24:04 +0000 | [diff] [blame] | 501 | Py_ssize_t len, n; |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 502 | |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 503 | CHECK_CLOSED(self); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 504 | |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 505 | if (!PyArg_Parse(arg, "w*", &buffer)) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 506 | return NULL; |
| 507 | |
Benjamin Peterson | fa73555 | 2010-11-20 17:24:04 +0000 | [diff] [blame] | 508 | /* adjust invalid sizes */ |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 509 | len = buffer.len; |
Benjamin Peterson | fa73555 | 2010-11-20 17:24:04 +0000 | [diff] [blame] | 510 | n = self->string_size - self->pos; |
| 511 | if (len > n) { |
| 512 | len = n; |
| 513 | if (len < 0) |
| 514 | len = 0; |
| 515 | } |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 516 | |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 517 | memcpy(buffer.buf, PyBytes_AS_STRING(self->buf) + self->pos, len); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 518 | assert(self->pos + len < PY_SSIZE_T_MAX); |
| 519 | assert(len >= 0); |
| 520 | self->pos += len; |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 521 | PyBuffer_Release(&buffer); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 522 | |
| 523 | return PyLong_FromSsize_t(len); |
| 524 | } |
| 525 | |
| 526 | PyDoc_STRVAR(truncate_doc, |
| 527 | "truncate([size]) -> int. Truncate the file to at most size bytes.\n" |
| 528 | "\n" |
| 529 | "Size defaults to the current file position, as returned by tell().\n" |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 530 | "The current file position is unchanged. Returns the new size.\n"); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 531 | |
| 532 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 533 | bytesio_truncate(bytesio *self, PyObject *args) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 534 | { |
| 535 | Py_ssize_t size; |
| 536 | PyObject *arg = Py_None; |
| 537 | |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 538 | CHECK_CLOSED(self); |
Antoine Pitrou | 972ee13 | 2010-09-06 18:48:21 +0000 | [diff] [blame] | 539 | CHECK_EXPORTS(self); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 540 | |
| 541 | if (!PyArg_ParseTuple(args, "|O:truncate", &arg)) |
| 542 | return NULL; |
| 543 | |
| 544 | if (PyLong_Check(arg)) { |
| 545 | size = PyLong_AsSsize_t(arg); |
Benjamin Peterson | a8a9304 | 2008-09-30 02:18:09 +0000 | [diff] [blame] | 546 | if (size == -1 && PyErr_Occurred()) |
| 547 | return NULL; |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 548 | } |
| 549 | else if (arg == Py_None) { |
| 550 | /* Truncate to current position if no argument is passed. */ |
| 551 | size = self->pos; |
| 552 | } |
| 553 | else { |
| 554 | PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'", |
| 555 | Py_TYPE(arg)->tp_name); |
| 556 | return NULL; |
| 557 | } |
| 558 | |
| 559 | if (size < 0) { |
| 560 | PyErr_Format(PyExc_ValueError, |
| 561 | "negative size value %zd", size); |
| 562 | return NULL; |
| 563 | } |
| 564 | |
| 565 | if (size < self->string_size) { |
| 566 | self->string_size = size; |
| 567 | if (resize_buffer(self, size) < 0) |
| 568 | return NULL; |
| 569 | } |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 570 | |
| 571 | return PyLong_FromSsize_t(size); |
| 572 | } |
| 573 | |
| 574 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 575 | bytesio_iternext(bytesio *self) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 576 | { |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 577 | Py_ssize_t n; |
| 578 | |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 579 | CHECK_CLOSED(self); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 580 | |
Serhiy Storchaka | d7728ca | 2014-08-14 22:26:38 +0300 | [diff] [blame] | 581 | n = scan_eol(self, -1); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 582 | |
Serhiy Storchaka | d7728ca | 2014-08-14 22:26:38 +0300 | [diff] [blame] | 583 | if (n == 0) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 584 | return NULL; |
| 585 | |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 586 | return read_bytes(self, n); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | PyDoc_STRVAR(seek_doc, |
| 590 | "seek(pos, whence=0) -> int. Change stream position.\n" |
| 591 | "\n" |
| 592 | "Seek to byte offset pos relative to position indicated by whence:\n" |
| 593 | " 0 Start of stream (the default). pos should be >= 0;\n" |
| 594 | " 1 Current position - pos may be negative;\n" |
| 595 | " 2 End of stream - pos usually negative.\n" |
| 596 | "Returns the new absolute position."); |
| 597 | |
| 598 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 599 | bytesio_seek(bytesio *self, PyObject *args) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 600 | { |
| 601 | Py_ssize_t pos; |
| 602 | int mode = 0; |
| 603 | |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 604 | CHECK_CLOSED(self); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 605 | |
| 606 | if (!PyArg_ParseTuple(args, "n|i:seek", &pos, &mode)) |
| 607 | return NULL; |
| 608 | |
| 609 | if (pos < 0 && mode == 0) { |
| 610 | PyErr_Format(PyExc_ValueError, |
| 611 | "negative seek value %zd", pos); |
| 612 | return NULL; |
| 613 | } |
| 614 | |
| 615 | /* mode 0: offset relative to beginning of the string. |
| 616 | mode 1: offset relative to current position. |
| 617 | mode 2: offset relative the end of the string. */ |
| 618 | if (mode == 1) { |
| 619 | if (pos > PY_SSIZE_T_MAX - self->pos) { |
| 620 | PyErr_SetString(PyExc_OverflowError, |
| 621 | "new position too large"); |
| 622 | return NULL; |
| 623 | } |
| 624 | pos += self->pos; |
| 625 | } |
| 626 | else if (mode == 2) { |
| 627 | if (pos > PY_SSIZE_T_MAX - self->string_size) { |
| 628 | PyErr_SetString(PyExc_OverflowError, |
| 629 | "new position too large"); |
| 630 | return NULL; |
| 631 | } |
| 632 | pos += self->string_size; |
| 633 | } |
| 634 | else if (mode != 0) { |
| 635 | PyErr_Format(PyExc_ValueError, |
| 636 | "invalid whence (%i, should be 0, 1 or 2)", mode); |
| 637 | return NULL; |
| 638 | } |
| 639 | |
| 640 | if (pos < 0) |
| 641 | pos = 0; |
| 642 | self->pos = pos; |
| 643 | |
| 644 | return PyLong_FromSsize_t(self->pos); |
| 645 | } |
| 646 | |
| 647 | PyDoc_STRVAR(write_doc, |
Alexandre Vassalotti | 10dfc1e | 2008-05-08 01:34:41 +0000 | [diff] [blame] | 648 | "write(bytes) -> int. Write bytes to file.\n" |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 649 | "\n" |
| 650 | "Return the number of bytes written."); |
| 651 | |
| 652 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 653 | bytesio_write(bytesio *self, PyObject *obj) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 654 | { |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 655 | Py_ssize_t n = 0; |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 656 | Py_buffer buf; |
| 657 | PyObject *result = NULL; |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 658 | |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 659 | CHECK_CLOSED(self); |
Antoine Pitrou | 972ee13 | 2010-09-06 18:48:21 +0000 | [diff] [blame] | 660 | CHECK_EXPORTS(self); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 661 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 662 | if (PyObject_GetBuffer(obj, &buf, PyBUF_CONTIG_RO) < 0) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 663 | return NULL; |
| 664 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 665 | if (buf.len != 0) |
| 666 | n = write_bytes(self, buf.buf, buf.len); |
| 667 | if (n >= 0) |
| 668 | result = PyLong_FromSsize_t(n); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 669 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 670 | PyBuffer_Release(&buf); |
| 671 | return result; |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | PyDoc_STRVAR(writelines_doc, |
Alexandre Vassalotti | 7d06089 | 2008-05-07 01:47:37 +0000 | [diff] [blame] | 675 | "writelines(sequence_of_strings) -> None. Write strings to the file.\n" |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 676 | "\n" |
Alexandre Vassalotti | 7d06089 | 2008-05-07 01:47:37 +0000 | [diff] [blame] | 677 | "Note that newlines are not added. The sequence can be any iterable\n" |
| 678 | "object producing strings. This is equivalent to calling write() for\n" |
| 679 | "each string."); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 680 | |
| 681 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 682 | bytesio_writelines(bytesio *self, PyObject *v) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 683 | { |
| 684 | PyObject *it, *item; |
| 685 | PyObject *ret; |
| 686 | |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 687 | CHECK_CLOSED(self); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 688 | |
| 689 | it = PyObject_GetIter(v); |
| 690 | if (it == NULL) |
| 691 | return NULL; |
| 692 | |
| 693 | while ((item = PyIter_Next(it)) != NULL) { |
| 694 | ret = bytesio_write(self, item); |
| 695 | Py_DECREF(item); |
| 696 | if (ret == NULL) { |
| 697 | Py_DECREF(it); |
| 698 | return NULL; |
| 699 | } |
| 700 | Py_DECREF(ret); |
| 701 | } |
| 702 | Py_DECREF(it); |
| 703 | |
| 704 | /* See if PyIter_Next failed */ |
| 705 | if (PyErr_Occurred()) |
| 706 | return NULL; |
| 707 | |
| 708 | Py_RETURN_NONE; |
| 709 | } |
| 710 | |
| 711 | PyDoc_STRVAR(close_doc, |
| 712 | "close() -> None. Disable all I/O operations."); |
| 713 | |
| 714 | static PyObject * |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 715 | bytesio_close(bytesio *self) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 716 | { |
Serhiy Storchaka | c057c38 | 2015-02-03 02:00:18 +0200 | [diff] [blame] | 717 | CHECK_EXPORTS(self); |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 718 | Py_CLEAR(self->buf); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 719 | Py_RETURN_NONE; |
| 720 | } |
| 721 | |
Alexandre Vassalotti | cf76e1a | 2009-07-22 03:24:36 +0000 | [diff] [blame] | 722 | /* Pickling support. |
| 723 | |
| 724 | Note that only pickle protocol 2 and onward are supported since we use |
| 725 | extended __reduce__ API of PEP 307 to make BytesIO instances picklable. |
| 726 | |
| 727 | Providing support for protocol < 2 would require the __reduce_ex__ method |
| 728 | which is notably long-winded when defined properly. |
| 729 | |
| 730 | For BytesIO, the implementation would similar to one coded for |
| 731 | object.__reduce_ex__, but slightly less general. To be more specific, we |
| 732 | could call bytesio_getstate directly and avoid checking for the presence of |
| 733 | a fallback __reduce__ method. However, we would still need a __newobj__ |
| 734 | function to use the efficient instance representation of PEP 307. |
| 735 | */ |
| 736 | |
| 737 | static PyObject * |
| 738 | bytesio_getstate(bytesio *self) |
| 739 | { |
| 740 | PyObject *initvalue = bytesio_getvalue(self); |
| 741 | PyObject *dict; |
| 742 | PyObject *state; |
| 743 | |
| 744 | if (initvalue == NULL) |
| 745 | return NULL; |
| 746 | if (self->dict == NULL) { |
| 747 | Py_INCREF(Py_None); |
| 748 | dict = Py_None; |
| 749 | } |
| 750 | else { |
| 751 | dict = PyDict_Copy(self->dict); |
Stefan Krah | 96efdd4 | 2012-09-08 11:12:33 +0200 | [diff] [blame] | 752 | if (dict == NULL) { |
| 753 | Py_DECREF(initvalue); |
Alexandre Vassalotti | cf76e1a | 2009-07-22 03:24:36 +0000 | [diff] [blame] | 754 | return NULL; |
Stefan Krah | 96efdd4 | 2012-09-08 11:12:33 +0200 | [diff] [blame] | 755 | } |
Alexandre Vassalotti | cf76e1a | 2009-07-22 03:24:36 +0000 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | state = Py_BuildValue("(OnN)", initvalue, self->pos, dict); |
| 759 | Py_DECREF(initvalue); |
| 760 | return state; |
| 761 | } |
| 762 | |
| 763 | static PyObject * |
| 764 | bytesio_setstate(bytesio *self, PyObject *state) |
| 765 | { |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 766 | PyObject *result; |
Alexandre Vassalotti | cf76e1a | 2009-07-22 03:24:36 +0000 | [diff] [blame] | 767 | PyObject *position_obj; |
| 768 | PyObject *dict; |
| 769 | Py_ssize_t pos; |
| 770 | |
| 771 | assert(state != NULL); |
| 772 | |
| 773 | /* We allow the state tuple to be longer than 3, because we may need |
| 774 | someday to extend the object's state without breaking |
| 775 | backward-compatibility. */ |
| 776 | if (!PyTuple_Check(state) || Py_SIZE(state) < 3) { |
| 777 | PyErr_Format(PyExc_TypeError, |
| 778 | "%.200s.__setstate__ argument should be 3-tuple, got %.200s", |
| 779 | Py_TYPE(self)->tp_name, Py_TYPE(state)->tp_name); |
| 780 | return NULL; |
| 781 | } |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 782 | CHECK_EXPORTS(self); |
| 783 | /* Reset the object to its default state. This is only needed to handle |
| 784 | the case of repeated calls to __setstate__. */ |
| 785 | self->string_size = 0; |
| 786 | self->pos = 0; |
Alexandre Vassalotti | cf76e1a | 2009-07-22 03:24:36 +0000 | [diff] [blame] | 787 | |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 788 | /* Set the value of the internal buffer. If state[0] does not support the |
| 789 | buffer protocol, bytesio_write will raise the appropriate TypeError. */ |
| 790 | result = bytesio_write(self, PyTuple_GET_ITEM(state, 0)); |
| 791 | if (result == NULL) |
Alexandre Vassalotti | cf76e1a | 2009-07-22 03:24:36 +0000 | [diff] [blame] | 792 | return NULL; |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 793 | Py_DECREF(result); |
Alexandre Vassalotti | cf76e1a | 2009-07-22 03:24:36 +0000 | [diff] [blame] | 794 | |
| 795 | /* Set carefully the position value. Alternatively, we could use the seek |
| 796 | method instead of modifying self->pos directly to better protect the |
| 797 | object internal state against errneous (or malicious) inputs. */ |
| 798 | position_obj = PyTuple_GET_ITEM(state, 1); |
| 799 | if (!PyLong_Check(position_obj)) { |
| 800 | PyErr_Format(PyExc_TypeError, |
| 801 | "second item of state must be an integer, not %.200s", |
| 802 | Py_TYPE(position_obj)->tp_name); |
| 803 | return NULL; |
| 804 | } |
| 805 | pos = PyLong_AsSsize_t(position_obj); |
| 806 | if (pos == -1 && PyErr_Occurred()) |
| 807 | return NULL; |
| 808 | if (pos < 0) { |
| 809 | PyErr_SetString(PyExc_ValueError, |
| 810 | "position value cannot be negative"); |
| 811 | return NULL; |
| 812 | } |
| 813 | self->pos = pos; |
| 814 | |
| 815 | /* Set the dictionary of the instance variables. */ |
| 816 | dict = PyTuple_GET_ITEM(state, 2); |
| 817 | if (dict != Py_None) { |
| 818 | if (!PyDict_Check(dict)) { |
| 819 | PyErr_Format(PyExc_TypeError, |
| 820 | "third item of state should be a dict, got a %.200s", |
| 821 | Py_TYPE(dict)->tp_name); |
| 822 | return NULL; |
| 823 | } |
| 824 | if (self->dict) { |
| 825 | /* Alternatively, we could replace the internal dictionary |
| 826 | completely. However, it seems more practical to just update it. */ |
| 827 | if (PyDict_Update(self->dict, dict) < 0) |
| 828 | return NULL; |
| 829 | } |
| 830 | else { |
| 831 | Py_INCREF(dict); |
| 832 | self->dict = dict; |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | Py_RETURN_NONE; |
| 837 | } |
| 838 | |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 839 | static void |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 840 | bytesio_dealloc(bytesio *self) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 841 | { |
Alexandre Vassalotti | fc47704 | 2009-07-22 02:24:49 +0000 | [diff] [blame] | 842 | _PyObject_GC_UNTRACK(self); |
Antoine Pitrou | 972ee13 | 2010-09-06 18:48:21 +0000 | [diff] [blame] | 843 | if (self->exports > 0) { |
| 844 | PyErr_SetString(PyExc_SystemError, |
| 845 | "deallocated BytesIO object has exported buffers"); |
| 846 | PyErr_Print(); |
| 847 | } |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 848 | Py_CLEAR(self->buf); |
Alexandre Vassalotti | fc47704 | 2009-07-22 02:24:49 +0000 | [diff] [blame] | 849 | Py_CLEAR(self->dict); |
| 850 | if (self->weakreflist != NULL) |
| 851 | PyObject_ClearWeakRefs((PyObject *) self); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 852 | Py_TYPE(self)->tp_free(self); |
| 853 | } |
| 854 | |
| 855 | static PyObject * |
| 856 | bytesio_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 857 | { |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 858 | bytesio *self; |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 859 | |
| 860 | assert(type != NULL && type->tp_alloc != NULL); |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 861 | self = (bytesio *)type->tp_alloc(type, 0); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 862 | if (self == NULL) |
| 863 | return NULL; |
| 864 | |
Alexandre Vassalotti | cf76e1a | 2009-07-22 03:24:36 +0000 | [diff] [blame] | 865 | /* tp_alloc initializes all the fields to zero. So we don't have to |
| 866 | initialize them here. */ |
| 867 | |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 868 | self->buf = PyBytes_FromStringAndSize(NULL, 0); |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 869 | if (self->buf == NULL) { |
| 870 | Py_DECREF(self); |
| 871 | return PyErr_NoMemory(); |
| 872 | } |
| 873 | |
| 874 | return (PyObject *)self; |
| 875 | } |
| 876 | |
| 877 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 878 | bytesio_init(bytesio *self, PyObject *args, PyObject *kwds) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 879 | { |
Alexandre Vassalotti | ba5c743 | 2009-08-04 23:19:13 +0000 | [diff] [blame] | 880 | char *kwlist[] = {"initial_bytes", NULL}; |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 881 | PyObject *initvalue = NULL; |
| 882 | |
Alexandre Vassalotti | ba5c743 | 2009-08-04 23:19:13 +0000 | [diff] [blame] | 883 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:BytesIO", kwlist, |
| 884 | &initvalue)) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 885 | return -1; |
| 886 | |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 887 | /* In case, __init__ is called multiple times. */ |
| 888 | self->string_size = 0; |
| 889 | self->pos = 0; |
| 890 | |
| 891 | if (self->exports > 0) { |
| 892 | PyErr_SetString(PyExc_BufferError, |
| 893 | "Existing exports of data: object cannot be re-sized"); |
| 894 | return -1; |
| 895 | } |
| 896 | if (initvalue && initvalue != Py_None) { |
| 897 | if (PyBytes_CheckExact(initvalue)) { |
| 898 | Py_INCREF(initvalue); |
| 899 | Py_XDECREF(self->buf); |
| 900 | self->buf = initvalue; |
| 901 | self->string_size = PyBytes_GET_SIZE(initvalue); |
| 902 | } |
| 903 | else { |
| 904 | PyObject *res; |
| 905 | res = bytesio_write(self, initvalue); |
| 906 | if (res == NULL) |
| 907 | return -1; |
| 908 | Py_DECREF(res); |
| 909 | self->pos = 0; |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | return 0; |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 914 | } |
| 915 | |
Antoine Pitrou | 8f328d0 | 2012-07-30 00:01:06 +0200 | [diff] [blame] | 916 | static PyObject * |
| 917 | bytesio_sizeof(bytesio *self, void *unused) |
| 918 | { |
| 919 | Py_ssize_t res; |
| 920 | |
| 921 | res = sizeof(bytesio); |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 922 | if (self->buf && !SHARED_BUF(self)) |
| 923 | res += _PySys_GetSizeOf(self->buf); |
Antoine Pitrou | 8f328d0 | 2012-07-30 00:01:06 +0200 | [diff] [blame] | 924 | return PyLong_FromSsize_t(res); |
| 925 | } |
| 926 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 927 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 928 | bytesio_traverse(bytesio *self, visitproc visit, void *arg) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 929 | { |
| 930 | Py_VISIT(self->dict); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 931 | return 0; |
| 932 | } |
| 933 | |
| 934 | static int |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 935 | bytesio_clear(bytesio *self) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 936 | { |
| 937 | Py_CLEAR(self->dict); |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 938 | return 0; |
| 939 | } |
| 940 | |
| 941 | |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 942 | static PyGetSetDef bytesio_getsetlist[] = { |
| 943 | {"closed", (getter)bytesio_get_closed, NULL, |
| 944 | "True if the file is closed."}, |
Benjamin Peterson | 1fea321 | 2009-04-19 03:15:20 +0000 | [diff] [blame] | 945 | {NULL}, /* sentinel */ |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 946 | }; |
| 947 | |
| 948 | static struct PyMethodDef bytesio_methods[] = { |
Antoine Pitrou | 1d85745 | 2012-09-05 20:11:49 +0200 | [diff] [blame] | 949 | {"readable", (PyCFunction)return_not_closed, METH_NOARGS, readable_doc}, |
| 950 | {"seekable", (PyCFunction)return_not_closed, METH_NOARGS, seekable_doc}, |
| 951 | {"writable", (PyCFunction)return_not_closed, METH_NOARGS, writable_doc}, |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 952 | {"close", (PyCFunction)bytesio_close, METH_NOARGS, close_doc}, |
| 953 | {"flush", (PyCFunction)bytesio_flush, METH_NOARGS, flush_doc}, |
| 954 | {"isatty", (PyCFunction)bytesio_isatty, METH_NOARGS, isatty_doc}, |
| 955 | {"tell", (PyCFunction)bytesio_tell, METH_NOARGS, tell_doc}, |
| 956 | {"write", (PyCFunction)bytesio_write, METH_O, write_doc}, |
| 957 | {"writelines", (PyCFunction)bytesio_writelines, METH_O, writelines_doc}, |
| 958 | {"read1", (PyCFunction)bytesio_read1, METH_O, read1_doc}, |
| 959 | {"readinto", (PyCFunction)bytesio_readinto, METH_O, readinto_doc}, |
| 960 | {"readline", (PyCFunction)bytesio_readline, METH_VARARGS, readline_doc}, |
| 961 | {"readlines", (PyCFunction)bytesio_readlines, METH_VARARGS, readlines_doc}, |
| 962 | {"read", (PyCFunction)bytesio_read, METH_VARARGS, read_doc}, |
Antoine Pitrou | 972ee13 | 2010-09-06 18:48:21 +0000 | [diff] [blame] | 963 | {"getbuffer", (PyCFunction)bytesio_getbuffer, METH_NOARGS, getbuffer_doc}, |
Antoine Pitrou | d5c3f6c | 2010-09-02 19:48:07 +0000 | [diff] [blame] | 964 | {"getvalue", (PyCFunction)bytesio_getvalue, METH_NOARGS, getval_doc}, |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 965 | {"seek", (PyCFunction)bytesio_seek, METH_VARARGS, seek_doc}, |
| 966 | {"truncate", (PyCFunction)bytesio_truncate, METH_VARARGS, truncate_doc}, |
Alexandre Vassalotti | cf76e1a | 2009-07-22 03:24:36 +0000 | [diff] [blame] | 967 | {"__getstate__", (PyCFunction)bytesio_getstate, METH_NOARGS, NULL}, |
| 968 | {"__setstate__", (PyCFunction)bytesio_setstate, METH_O, NULL}, |
Antoine Pitrou | 8f328d0 | 2012-07-30 00:01:06 +0200 | [diff] [blame] | 969 | {"__sizeof__", (PyCFunction)bytesio_sizeof, METH_NOARGS, NULL}, |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 970 | {NULL, NULL} /* sentinel */ |
| 971 | }; |
| 972 | |
| 973 | PyDoc_STRVAR(bytesio_doc, |
| 974 | "BytesIO([buffer]) -> object\n" |
| 975 | "\n" |
| 976 | "Create a buffered I/O implementation using an in-memory bytes\n" |
| 977 | "buffer, ready for reading and writing."); |
| 978 | |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 979 | PyTypeObject PyBytesIO_Type = { |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 980 | PyVarObject_HEAD_INIT(NULL, 0) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 981 | "_io.BytesIO", /*tp_name*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 982 | sizeof(bytesio), /*tp_basicsize*/ |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 983 | 0, /*tp_itemsize*/ |
| 984 | (destructor)bytesio_dealloc, /*tp_dealloc*/ |
| 985 | 0, /*tp_print*/ |
| 986 | 0, /*tp_getattr*/ |
| 987 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 988 | 0, /*tp_reserved*/ |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 989 | 0, /*tp_repr*/ |
| 990 | 0, /*tp_as_number*/ |
| 991 | 0, /*tp_as_sequence*/ |
| 992 | 0, /*tp_as_mapping*/ |
| 993 | 0, /*tp_hash*/ |
| 994 | 0, /*tp_call*/ |
| 995 | 0, /*tp_str*/ |
| 996 | 0, /*tp_getattro*/ |
| 997 | 0, /*tp_setattro*/ |
| 998 | 0, /*tp_as_buffer*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 999 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 1000 | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 1001 | bytesio_doc, /*tp_doc*/ |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 1002 | (traverseproc)bytesio_traverse, /*tp_traverse*/ |
| 1003 | (inquiry)bytesio_clear, /*tp_clear*/ |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 1004 | 0, /*tp_richcompare*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1005 | offsetof(bytesio, weakreflist), /*tp_weaklistoffset*/ |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 1006 | PyObject_SelfIter, /*tp_iter*/ |
| 1007 | (iternextfunc)bytesio_iternext, /*tp_iternext*/ |
| 1008 | bytesio_methods, /*tp_methods*/ |
| 1009 | 0, /*tp_members*/ |
| 1010 | bytesio_getsetlist, /*tp_getset*/ |
| 1011 | 0, /*tp_base*/ |
| 1012 | 0, /*tp_dict*/ |
| 1013 | 0, /*tp_descr_get*/ |
| 1014 | 0, /*tp_descr_set*/ |
Benjamin Peterson | 680bf1a | 2009-06-12 02:07:12 +0000 | [diff] [blame] | 1015 | offsetof(bytesio, dict), /*tp_dictoffset*/ |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 1016 | (initproc)bytesio_init, /*tp_init*/ |
| 1017 | 0, /*tp_alloc*/ |
| 1018 | bytesio_new, /*tp_new*/ |
| 1019 | }; |
Antoine Pitrou | 972ee13 | 2010-09-06 18:48:21 +0000 | [diff] [blame] | 1020 | |
| 1021 | |
| 1022 | /* |
| 1023 | * Implementation of the small intermediate object used by getbuffer(). |
| 1024 | * getbuffer() returns a memoryview over this object, which should make it |
| 1025 | * invisible from Python code. |
| 1026 | */ |
| 1027 | |
| 1028 | static int |
| 1029 | bytesiobuf_getbuffer(bytesiobuf *obj, Py_buffer *view, int flags) |
| 1030 | { |
Antoine Pitrou | 972ee13 | 2010-09-06 18:48:21 +0000 | [diff] [blame] | 1031 | bytesio *b = (bytesio *) obj->source; |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 1032 | |
| 1033 | if (view == NULL) { |
| 1034 | PyErr_SetString(PyExc_BufferError, |
| 1035 | "bytesiobuf_getbuffer: view==NULL argument is obsolete"); |
| 1036 | return -1; |
| 1037 | } |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 1038 | if (SHARED_BUF(b)) { |
| 1039 | if (unshare_buffer(b, b->string_size) < 0) |
| 1040 | return -1; |
| 1041 | } |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 1042 | |
| 1043 | /* cannot fail if view != NULL and readonly == 0 */ |
| 1044 | (void)PyBuffer_FillInfo(view, (PyObject*)obj, |
Serhiy Storchaka | 87d0b45 | 2015-02-03 11:30:10 +0200 | [diff] [blame] | 1045 | PyBytes_AS_STRING(b->buf), b->string_size, |
Antoine Pitrou | 972ee13 | 2010-09-06 18:48:21 +0000 | [diff] [blame] | 1046 | 0, flags); |
Stefan Krah | 650c1e8 | 2015-02-03 21:43:23 +0100 | [diff] [blame] | 1047 | b->exports++; |
| 1048 | return 0; |
Antoine Pitrou | 972ee13 | 2010-09-06 18:48:21 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | static void |
| 1052 | bytesiobuf_releasebuffer(bytesiobuf *obj, Py_buffer *view) |
| 1053 | { |
| 1054 | bytesio *b = (bytesio *) obj->source; |
| 1055 | b->exports--; |
| 1056 | } |
| 1057 | |
| 1058 | static int |
| 1059 | bytesiobuf_traverse(bytesiobuf *self, visitproc visit, void *arg) |
| 1060 | { |
| 1061 | Py_VISIT(self->source); |
| 1062 | return 0; |
| 1063 | } |
| 1064 | |
| 1065 | static void |
| 1066 | bytesiobuf_dealloc(bytesiobuf *self) |
| 1067 | { |
| 1068 | Py_CLEAR(self->source); |
| 1069 | Py_TYPE(self)->tp_free(self); |
| 1070 | } |
| 1071 | |
| 1072 | static PyBufferProcs bytesiobuf_as_buffer = { |
| 1073 | (getbufferproc) bytesiobuf_getbuffer, |
| 1074 | (releasebufferproc) bytesiobuf_releasebuffer, |
| 1075 | }; |
| 1076 | |
| 1077 | PyTypeObject _PyBytesIOBuffer_Type = { |
| 1078 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1079 | "_io._BytesIOBuffer", /*tp_name*/ |
| 1080 | sizeof(bytesiobuf), /*tp_basicsize*/ |
| 1081 | 0, /*tp_itemsize*/ |
| 1082 | (destructor)bytesiobuf_dealloc, /*tp_dealloc*/ |
| 1083 | 0, /*tp_print*/ |
| 1084 | 0, /*tp_getattr*/ |
| 1085 | 0, /*tp_setattr*/ |
| 1086 | 0, /*tp_reserved*/ |
| 1087 | 0, /*tp_repr*/ |
| 1088 | 0, /*tp_as_number*/ |
| 1089 | 0, /*tp_as_sequence*/ |
| 1090 | 0, /*tp_as_mapping*/ |
| 1091 | 0, /*tp_hash*/ |
| 1092 | 0, /*tp_call*/ |
| 1093 | 0, /*tp_str*/ |
| 1094 | 0, /*tp_getattro*/ |
| 1095 | 0, /*tp_setattro*/ |
| 1096 | &bytesiobuf_as_buffer, /*tp_as_buffer*/ |
| 1097 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
| 1098 | 0, /*tp_doc*/ |
| 1099 | (traverseproc)bytesiobuf_traverse, /*tp_traverse*/ |
| 1100 | 0, /*tp_clear*/ |
| 1101 | 0, /*tp_richcompare*/ |
| 1102 | 0, /*tp_weaklistoffset*/ |
| 1103 | 0, /*tp_iter*/ |
| 1104 | 0, /*tp_iternext*/ |
| 1105 | 0, /*tp_methods*/ |
| 1106 | 0, /*tp_members*/ |
| 1107 | 0, /*tp_getset*/ |
| 1108 | 0, /*tp_base*/ |
| 1109 | 0, /*tp_dict*/ |
| 1110 | 0, /*tp_descr_get*/ |
| 1111 | 0, /*tp_descr_set*/ |
| 1112 | 0, /*tp_dictoffset*/ |
| 1113 | 0, /*tp_init*/ |
| 1114 | 0, /*tp_alloc*/ |
| 1115 | 0, /*tp_new*/ |
| 1116 | }; |