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