/src/cpython3/Modules/_io/bufferedio.c
Line | Count | Source |
1 | | /* |
2 | | An implementation of Buffered I/O as defined by PEP 3116 - "New I/O" |
3 | | |
4 | | Classes defined here: BufferedIOBase, BufferedReader, BufferedWriter, |
5 | | BufferedRandom. |
6 | | |
7 | | Written by Amaury Forgeot d'Arc and Antoine Pitrou |
8 | | */ |
9 | | |
10 | | #include "Python.h" |
11 | | #include "pycore_call.h" // _PyObject_CallNoArgs() |
12 | | #include "pycore_fileutils.h" // _PyFile_Flush |
13 | | #include "pycore_object.h" // _PyObject_GC_UNTRACK() |
14 | | #include "pycore_pyerrors.h" // _Py_FatalErrorFormat() |
15 | | #include "pycore_pylifecycle.h" // _Py_IsInterpreterFinalizing() |
16 | | #include "pycore_weakref.h" // FT_CLEAR_WEAKREFS() |
17 | | |
18 | | #include "_iomodule.h" |
19 | | |
20 | | /*[clinic input] |
21 | | module _io |
22 | | class _io._BufferedIOBase "PyObject *" "clinic_state()->PyBufferedIOBase_Type" |
23 | | class _io._Buffered "buffered *" "clinic_state()->PyBufferedIOBase_Type" |
24 | | class _io.BufferedReader "buffered *" "clinic_state()->PyBufferedReader_Type" |
25 | | class _io.BufferedWriter "buffered *" "clinic_state()->PyBufferedWriter_Type" |
26 | | class _io.BufferedRWPair "rwpair *" "clinic_state()->PyBufferedRWPair_Type" |
27 | | class _io.BufferedRandom "buffered *" "clinic_state()->PyBufferedRandom_Type" |
28 | | [clinic start generated code]*/ |
29 | | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=3b3ef9cbbbad4590]*/ |
30 | | |
31 | | /* |
32 | | * BufferedIOBase class, inherits from IOBase. |
33 | | */ |
34 | | PyDoc_STRVAR(bufferediobase_doc, |
35 | | "Base class for buffered IO objects.\n" |
36 | | "\n" |
37 | | "The main difference with RawIOBase is that the read() method\n" |
38 | | "supports omitting the size argument, and does not have a default\n" |
39 | | "implementation that defers to readinto().\n" |
40 | | "\n" |
41 | | "In addition, read(), readinto() and write() may raise\n" |
42 | | "BlockingIOError if the underlying raw stream is in non-blocking\n" |
43 | | "mode and not ready; unlike their raw counterparts, they will never\n" |
44 | | "return None.\n" |
45 | | "\n" |
46 | | "A typical implementation should not inherit from a RawIOBase\n" |
47 | | "implementation, but wrap one.\n" |
48 | | ); |
49 | | |
50 | | static PyObject * |
51 | | _bufferediobase_readinto_generic(PyObject *self, Py_buffer *buffer, char readinto1) |
52 | 0 | { |
53 | 0 | Py_ssize_t len; |
54 | 0 | PyObject *data; |
55 | |
|
56 | 0 | PyObject *attr = readinto1 |
57 | 0 | ? &_Py_ID(read1) |
58 | 0 | : &_Py_ID(read); |
59 | 0 | data = _PyObject_CallMethod(self, attr, "n", buffer->len); |
60 | 0 | if (data == NULL) |
61 | 0 | return NULL; |
62 | | |
63 | | /* Non-blocking stream with no data available */ |
64 | 0 | if (data == Py_None) { |
65 | 0 | return data; |
66 | 0 | } |
67 | | |
68 | 0 | if (!PyBytes_Check(data)) { |
69 | 0 | Py_DECREF(data); |
70 | 0 | PyErr_SetString(PyExc_TypeError, "read() should return bytes"); |
71 | 0 | return NULL; |
72 | 0 | } |
73 | | |
74 | 0 | len = PyBytes_GET_SIZE(data); |
75 | 0 | if (len > buffer->len) { |
76 | 0 | PyErr_Format(PyExc_ValueError, |
77 | 0 | "read() returned too much data: " |
78 | 0 | "%zd bytes requested, %zd returned", |
79 | 0 | buffer->len, len); |
80 | 0 | Py_DECREF(data); |
81 | 0 | return NULL; |
82 | 0 | } |
83 | 0 | memcpy(buffer->buf, PyBytes_AS_STRING(data), len); |
84 | |
|
85 | 0 | Py_DECREF(data); |
86 | |
|
87 | 0 | return PyLong_FromSsize_t(len); |
88 | 0 | } |
89 | | |
90 | | /*[clinic input] |
91 | | @critical_section |
92 | | _io._BufferedIOBase.readinto |
93 | | buffer: Py_buffer(accept={rwbuffer}) |
94 | | / |
95 | | [clinic start generated code]*/ |
96 | | |
97 | | static PyObject * |
98 | | _io__BufferedIOBase_readinto_impl(PyObject *self, Py_buffer *buffer) |
99 | | /*[clinic end generated code: output=8c8cda6684af8038 input=5273d20db7f56e1a]*/ |
100 | 0 | { |
101 | 0 | return _bufferediobase_readinto_generic(self, buffer, 0); |
102 | 0 | } |
103 | | |
104 | | /*[clinic input] |
105 | | @critical_section |
106 | | _io._BufferedIOBase.readinto1 |
107 | | buffer: Py_buffer(accept={rwbuffer}) |
108 | | / |
109 | | [clinic start generated code]*/ |
110 | | |
111 | | static PyObject * |
112 | | _io__BufferedIOBase_readinto1_impl(PyObject *self, Py_buffer *buffer) |
113 | | /*[clinic end generated code: output=358623e4fd2b69d3 input=d6eb723dedcee654]*/ |
114 | 0 | { |
115 | 0 | return _bufferediobase_readinto_generic(self, buffer, 1); |
116 | 0 | } |
117 | | |
118 | | static PyObject * |
119 | | bufferediobase_unsupported(_PyIO_State *state, const char *message) |
120 | 0 | { |
121 | 0 | PyErr_SetString(state->unsupported_operation, message); |
122 | 0 | return NULL; |
123 | 0 | } |
124 | | |
125 | | /*[clinic input] |
126 | | _io._BufferedIOBase.detach |
127 | | |
128 | | cls: defining_class |
129 | | / |
130 | | |
131 | | Disconnect this buffer from its underlying raw stream and return it. |
132 | | |
133 | | After the raw stream has been detached, the buffer is in an unusable |
134 | | state. |
135 | | [clinic start generated code]*/ |
136 | | |
137 | | static PyObject * |
138 | | _io__BufferedIOBase_detach_impl(PyObject *self, PyTypeObject *cls) |
139 | | /*[clinic end generated code: output=b87b135d67cd4448 input=0b61a7b4357c1ea7]*/ |
140 | 0 | { |
141 | 0 | _PyIO_State *state = get_io_state_by_cls(cls); |
142 | 0 | return bufferediobase_unsupported(state, "detach"); |
143 | 0 | } |
144 | | |
145 | | /*[clinic input] |
146 | | _io._BufferedIOBase.read |
147 | | |
148 | | cls: defining_class |
149 | | size: int(unused=True) = -1 |
150 | | / |
151 | | |
152 | | Read and return up to n bytes. |
153 | | |
154 | | If the size argument is omitted, None, or negative, read and |
155 | | return all data until EOF. |
156 | | |
157 | | If the size argument is positive, and the underlying raw stream is |
158 | | not 'interactive', multiple raw reads may be issued to satisfy |
159 | | the byte count (unless EOF is reached first). |
160 | | However, for interactive raw streams (as well as sockets and pipes), |
161 | | at most one raw read will be issued, and a short result does not |
162 | | imply that EOF is imminent. |
163 | | |
164 | | Return an empty bytes object on EOF. |
165 | | |
166 | | Return None if the underlying raw stream was open in non-blocking |
167 | | mode and no data is available at the moment. |
168 | | [clinic start generated code]*/ |
169 | | |
170 | | static PyObject * |
171 | | _io__BufferedIOBase_read_impl(PyObject *self, PyTypeObject *cls, |
172 | | int Py_UNUSED(size)) |
173 | | /*[clinic end generated code: output=aceb2765587b0a29 input=824f6f910465e61a]*/ |
174 | 0 | { |
175 | 0 | _PyIO_State *state = get_io_state_by_cls(cls); |
176 | 0 | return bufferediobase_unsupported(state, "read"); |
177 | 0 | } |
178 | | |
179 | | /*[clinic input] |
180 | | @permit_long_summary |
181 | | _io._BufferedIOBase.read1 |
182 | | |
183 | | cls: defining_class |
184 | | size: int(unused=True) = -1 |
185 | | / |
186 | | |
187 | | Read and return up to size bytes, with at most one read() call to the underlying raw stream. |
188 | | |
189 | | Return an empty bytes object on EOF. |
190 | | A short result does not imply that EOF is imminent. |
191 | | [clinic start generated code]*/ |
192 | | |
193 | | static PyObject * |
194 | | _io__BufferedIOBase_read1_impl(PyObject *self, PyTypeObject *cls, |
195 | | int Py_UNUSED(size)) |
196 | | /*[clinic end generated code: output=2e7fc62972487eaa input=1e76df255063afd6]*/ |
197 | 0 | { |
198 | 0 | _PyIO_State *state = get_io_state_by_cls(cls); |
199 | 0 | return bufferediobase_unsupported(state, "read1"); |
200 | 0 | } |
201 | | |
202 | | /*[clinic input] |
203 | | _io._BufferedIOBase.write |
204 | | |
205 | | cls: defining_class |
206 | | b: object(unused=True) |
207 | | / |
208 | | |
209 | | Write buffer b to the IO stream. |
210 | | |
211 | | Return the number of bytes written, which is always |
212 | | the length of b in bytes. |
213 | | |
214 | | Raise BlockingIOError if the buffer is full and the |
215 | | underlying raw stream cannot accept more data at the moment. |
216 | | [clinic start generated code]*/ |
217 | | |
218 | | static PyObject * |
219 | | _io__BufferedIOBase_write_impl(PyObject *self, PyTypeObject *cls, |
220 | | PyObject *Py_UNUSED(b)) |
221 | | /*[clinic end generated code: output=712c635246bf2306 input=9793f5c8f71029ad]*/ |
222 | 0 | { |
223 | 0 | _PyIO_State *state = get_io_state_by_cls(cls); |
224 | 0 | return bufferediobase_unsupported(state, "write"); |
225 | 0 | } |
226 | | |
227 | | |
228 | | typedef struct { |
229 | | PyObject_HEAD |
230 | | |
231 | | PyObject *raw; |
232 | | int ok; /* Initialized? */ |
233 | | int detached; |
234 | | int readable; |
235 | | int writable; |
236 | | char finalizing; |
237 | | |
238 | | /* True if this is a vanilla Buffered object (rather than a user derived |
239 | | class) *and* the raw stream is a vanilla FileIO object. */ |
240 | | int fast_closed_checks; |
241 | | |
242 | | /* Absolute position inside the raw stream (-1 if unknown). */ |
243 | | Py_off_t abs_pos; |
244 | | |
245 | | /* A static buffer of size `buffer_size` */ |
246 | | char *buffer; |
247 | | /* Current logical position in the buffer. */ |
248 | | Py_off_t pos; |
249 | | /* Position of the raw stream in the buffer. */ |
250 | | Py_off_t raw_pos; |
251 | | |
252 | | /* Just after the last buffered byte in the buffer, or -1 if the buffer |
253 | | isn't ready for reading. */ |
254 | | Py_off_t read_end; |
255 | | |
256 | | /* Just after the last byte actually written */ |
257 | | Py_off_t write_pos; |
258 | | /* Just after the last byte waiting to be written, or -1 if the buffer |
259 | | isn't ready for writing. */ |
260 | | Py_off_t write_end; |
261 | | |
262 | | PyThread_type_lock lock; |
263 | | volatile unsigned long owner; |
264 | | |
265 | | Py_ssize_t buffer_size; |
266 | | Py_ssize_t buffer_mask; |
267 | | |
268 | | PyObject *dict; |
269 | | PyObject *weakreflist; |
270 | | } buffered; |
271 | | |
272 | 2.26k | #define buffered_CAST(op) ((buffered *)(op)) |
273 | | |
274 | | /* |
275 | | Implementation notes: |
276 | | |
277 | | * BufferedReader, BufferedWriter and BufferedRandom try to share most |
278 | | methods (this is helped by the members `readable` and `writable`, which |
279 | | are initialized in the respective constructors) |
280 | | * They also share a single buffer for reading and writing. This enables |
281 | | interleaved reads and writes without flushing. It also makes the logic |
282 | | a bit trickier to get right. |
283 | | * The absolute position of the raw stream is cached, if possible, in the |
284 | | `abs_pos` member. It must be updated every time an operation is done |
285 | | on the raw stream. If not sure, it can be reinitialized by calling |
286 | | _buffered_raw_tell(), which queries the raw stream (_buffered_raw_seek() |
287 | | also does it). To read it, use RAW_TELL(). |
288 | | * Three helpers, _bufferedreader_raw_read, _bufferedwriter_raw_write and |
289 | | _bufferedwriter_flush_unlocked do a lot of useful housekeeping. |
290 | | |
291 | | NOTE: we should try to maintain block alignment of reads and writes to the |
292 | | raw stream (according to the buffer size), but for now it is only done |
293 | | in read() and friends. |
294 | | |
295 | | */ |
296 | | |
297 | | /* These macros protect the buffered object against concurrent operations. */ |
298 | | |
299 | | static int |
300 | | _enter_buffered_busy(buffered *self) |
301 | 0 | { |
302 | 0 | int relax_locking; |
303 | 0 | PyLockStatus st; |
304 | 0 | if (self->owner == PyThread_get_thread_ident()) { |
305 | 0 | PyErr_Format(PyExc_RuntimeError, |
306 | 0 | "reentrant call inside %R", self); |
307 | 0 | return 0; |
308 | 0 | } |
309 | 0 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
310 | 0 | relax_locking = _Py_IsInterpreterFinalizing(interp); |
311 | 0 | Py_BEGIN_ALLOW_THREADS |
312 | 0 | if (!relax_locking) |
313 | 0 | st = PyThread_acquire_lock(self->lock, 1); |
314 | 0 | else { |
315 | | /* When finalizing, we don't want a deadlock to happen with daemon |
316 | | * threads abruptly shut down while they owned the lock. |
317 | | * Therefore, only wait for a grace period (1 s.). |
318 | | * Note that non-daemon threads have already exited here, so this |
319 | | * shouldn't affect carefully written threaded I/O code. |
320 | | */ |
321 | 0 | st = PyThread_acquire_lock_timed(self->lock, (PY_TIMEOUT_T)1e6, 0); |
322 | 0 | } |
323 | 0 | Py_END_ALLOW_THREADS |
324 | 0 | if (relax_locking && st != PY_LOCK_ACQUIRED) { |
325 | 0 | PyObject *ascii = PyObject_ASCII((PyObject*)self); |
326 | 0 | _Py_FatalErrorFormat(__func__, |
327 | 0 | "could not acquire lock for %s at interpreter " |
328 | 0 | "shutdown, possibly due to daemon threads", |
329 | 0 | ascii ? PyUnicode_AsUTF8(ascii) : "<ascii(self) failed>"); |
330 | 0 | } |
331 | 0 | return 1; |
332 | 0 | } |
333 | | |
334 | | #define ENTER_BUFFERED(self) \ |
335 | 308k | ( (PyThread_acquire_lock(self->lock, 0) ? \ |
336 | 308k | 1 : _enter_buffered_busy(self)) \ |
337 | 308k | && (self->owner = PyThread_get_thread_ident(), 1) ) |
338 | | |
339 | | #define LEAVE_BUFFERED(self) \ |
340 | 308k | do { \ |
341 | 308k | self->owner = 0; \ |
342 | 308k | PyThread_release_lock(self->lock); \ |
343 | 308k | } while(0); |
344 | | |
345 | | #define CHECK_INITIALIZED(self) \ |
346 | 309k | if (self->ok <= 0) { \ |
347 | 0 | if (self->detached) { \ |
348 | 0 | PyErr_SetString(PyExc_ValueError, \ |
349 | 0 | "raw stream has been detached"); \ |
350 | 0 | } else { \ |
351 | 0 | PyErr_SetString(PyExc_ValueError, \ |
352 | 0 | "I/O operation on uninitialized object"); \ |
353 | 0 | } \ |
354 | 0 | return NULL; \ |
355 | 0 | } |
356 | | |
357 | | #define CHECK_INITIALIZED_INT(self) \ |
358 | 0 | if (self->ok <= 0) { \ |
359 | 0 | if (self->detached) { \ |
360 | 0 | PyErr_SetString(PyExc_ValueError, \ |
361 | 0 | "raw stream has been detached"); \ |
362 | 0 | } else { \ |
363 | 0 | PyErr_SetString(PyExc_ValueError, \ |
364 | 0 | "I/O operation on uninitialized object"); \ |
365 | 0 | } \ |
366 | 0 | return -1; \ |
367 | 0 | } |
368 | | |
369 | | #define IS_CLOSED(self) \ |
370 | 308k | (!self->buffer ? 1 : \ |
371 | 308k | (self->fast_closed_checks \ |
372 | 308k | ? _PyFileIO_closed(self->raw) \ |
373 | 308k | : buffered_closed(self))) |
374 | | |
375 | | #define CHECK_CLOSED(self, error_msg) \ |
376 | 154k | do { \ |
377 | 154k | int _closed = IS_CLOSED(self); \ |
378 | 154k | if (_closed < 0) { \ |
379 | 0 | return NULL; \ |
380 | 0 | } \ |
381 | 154k | if (_closed && \ |
382 | 154k | (Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t) == 0)) \ |
383 | 154k | { \ |
384 | 0 | PyErr_SetString(PyExc_ValueError, error_msg); \ |
385 | 0 | return NULL; \ |
386 | 0 | } \ |
387 | 154k | } while (0); |
388 | | |
389 | | #define VALID_READ_BUFFER(self) \ |
390 | 922k | (self->readable && self->read_end != -1) |
391 | | |
392 | | #define VALID_WRITE_BUFFER(self) \ |
393 | 922k | (self->writable && self->write_end != -1) |
394 | | |
395 | | #define ADJUST_POSITION(self, _new_pos) \ |
396 | 153k | do { \ |
397 | 153k | self->pos = _new_pos; \ |
398 | 153k | if (VALID_READ_BUFFER(self) && self->read_end < self->pos) \ |
399 | 153k | self->read_end = self->pos; \ |
400 | 153k | } while(0) |
401 | | |
402 | | #define READAHEAD(self) \ |
403 | 0 | ((self->readable && VALID_READ_BUFFER(self)) \ |
404 | 0 | ? (self->read_end - self->pos) : 0) |
405 | | |
406 | | #define RAW_OFFSET(self) \ |
407 | 153k | (((VALID_READ_BUFFER(self) || VALID_WRITE_BUFFER(self)) \ |
408 | 153k | && self->raw_pos >= 0) ? self->raw_pos - self->pos : 0) |
409 | | |
410 | | #define RAW_TELL(self) \ |
411 | 0 | (self->abs_pos != -1 ? self->abs_pos : _buffered_raw_tell(self)) |
412 | | |
413 | | #define MINUS_LAST_BLOCK(self, size) \ |
414 | 0 | (self->buffer_mask ? \ |
415 | 0 | (size & ~self->buffer_mask) : \ |
416 | 0 | (self->buffer_size * (size / self->buffer_size))) |
417 | | |
418 | | |
419 | | static int |
420 | | buffered_clear(PyObject *op) |
421 | 377 | { |
422 | 377 | buffered *self = buffered_CAST(op); |
423 | 377 | self->ok = 0; |
424 | 377 | Py_CLEAR(self->raw); |
425 | 377 | Py_CLEAR(self->dict); |
426 | 377 | return 0; |
427 | 377 | } |
428 | | |
429 | | static void |
430 | | buffered_dealloc(PyObject *op) |
431 | 377 | { |
432 | 377 | buffered *self = buffered_CAST(op); |
433 | 377 | PyTypeObject *tp = Py_TYPE(self); |
434 | 377 | self->finalizing = 1; |
435 | 377 | if (_PyIOBase_finalize(op) < 0) |
436 | 0 | return; |
437 | 377 | _PyObject_GC_UNTRACK(self); |
438 | 377 | self->ok = 0; |
439 | 377 | FT_CLEAR_WEAKREFS(op, self->weakreflist); |
440 | 377 | if (self->buffer) { |
441 | 0 | PyMem_Free(self->buffer); |
442 | 0 | self->buffer = NULL; |
443 | 0 | } |
444 | 377 | if (self->lock) { |
445 | 377 | PyThread_free_lock(self->lock); |
446 | 377 | self->lock = NULL; |
447 | 377 | } |
448 | 377 | (void)buffered_clear(op); |
449 | 377 | tp->tp_free(self); |
450 | 377 | Py_DECREF(tp); |
451 | 377 | } |
452 | | |
453 | | /*[clinic input] |
454 | | @critical_section |
455 | | _io._Buffered.__sizeof__ |
456 | | [clinic start generated code]*/ |
457 | | |
458 | | static PyObject * |
459 | | _io__Buffered___sizeof___impl(buffered *self) |
460 | | /*[clinic end generated code: output=0231ef7f5053134e input=07a32d578073ea64]*/ |
461 | 0 | { |
462 | 0 | size_t res = _PyObject_SIZE(Py_TYPE(self)); |
463 | 0 | if (self->buffer) { |
464 | 0 | res += (size_t)self->buffer_size; |
465 | 0 | } |
466 | 0 | return PyLong_FromSize_t(res); |
467 | 0 | } |
468 | | |
469 | | static int |
470 | | buffered_traverse(PyObject *op, visitproc visit, void *arg) |
471 | 1.50k | { |
472 | 1.50k | buffered *self = buffered_CAST(op); |
473 | 1.50k | Py_VISIT(Py_TYPE(self)); |
474 | 1.50k | Py_VISIT(self->raw); |
475 | 1.50k | Py_VISIT(self->dict); |
476 | 1.50k | return 0; |
477 | 1.50k | } |
478 | | |
479 | | /* Because this can call arbitrary code, it shouldn't be called when |
480 | | the refcount is 0 (that is, not directly from tp_dealloc unless |
481 | | the refcount has been temporarily re-incremented). */ |
482 | | /*[clinic input] |
483 | | _io._Buffered._dealloc_warn |
484 | | |
485 | | source: object |
486 | | / |
487 | | |
488 | | [clinic start generated code]*/ |
489 | | |
490 | | static PyObject * |
491 | | _io__Buffered__dealloc_warn_impl(buffered *self, PyObject *source) |
492 | | /*[clinic end generated code: output=d8db21c6dec0e614 input=8f845f2a4786391c]*/ |
493 | 0 | { |
494 | 0 | if (self->ok && self->raw) { |
495 | 0 | PyObject *r; |
496 | 0 | r = PyObject_CallMethodOneArg(self->raw, &_Py_ID(_dealloc_warn), source); |
497 | 0 | if (r) |
498 | 0 | Py_DECREF(r); |
499 | 0 | else |
500 | 0 | PyErr_Clear(); |
501 | 0 | } |
502 | 0 | Py_RETURN_NONE; |
503 | 0 | } |
504 | | |
505 | | /* |
506 | | * _BufferedIOMixin methods |
507 | | * This is not a class, just a collection of methods that will be reused |
508 | | * by BufferedReader and BufferedWriter |
509 | | */ |
510 | | |
511 | | /* Flush and close */ |
512 | | /*[clinic input] |
513 | | @critical_section |
514 | | _io._Buffered.flush as _io__Buffered_simple_flush |
515 | | [clinic start generated code]*/ |
516 | | |
517 | | static PyObject * |
518 | | _io__Buffered_simple_flush_impl(buffered *self) |
519 | | /*[clinic end generated code: output=29ebb3820db1bdfd input=5248cb84a65f80bd]*/ |
520 | 377 | { |
521 | 377 | CHECK_INITIALIZED(self) |
522 | 377 | return PyObject_CallMethodNoArgs(self->raw, &_Py_ID(flush)); |
523 | 377 | } |
524 | | |
525 | | static int |
526 | | buffered_closed(buffered *self) |
527 | 0 | { |
528 | 0 | int closed; |
529 | 0 | PyObject *res; |
530 | 0 | CHECK_INITIALIZED_INT(self) |
531 | 0 | res = PyObject_GetAttr(self->raw, &_Py_ID(closed)); |
532 | 0 | if (res == NULL) |
533 | 0 | return -1; |
534 | 0 | closed = PyObject_IsTrue(res); |
535 | 0 | Py_DECREF(res); |
536 | 0 | return closed; |
537 | 0 | } |
538 | | |
539 | | /*[clinic input] |
540 | | @critical_section |
541 | | @getter |
542 | | _io._Buffered.closed |
543 | | [clinic start generated code]*/ |
544 | | |
545 | | static PyObject * |
546 | | _io__Buffered_closed_get_impl(buffered *self) |
547 | | /*[clinic end generated code: output=f08ce57290703a1a input=18eddefdfe4a3d2f]*/ |
548 | 754 | { |
549 | 754 | CHECK_INITIALIZED(self) |
550 | 754 | return PyObject_GetAttr(self->raw, &_Py_ID(closed)); |
551 | 754 | } |
552 | | |
553 | | /*[clinic input] |
554 | | @critical_section |
555 | | _io._Buffered.close |
556 | | [clinic start generated code]*/ |
557 | | |
558 | | static PyObject * |
559 | | _io__Buffered_close_impl(buffered *self) |
560 | | /*[clinic end generated code: output=7280b7b42033be0c input=56d95935b03fd326]*/ |
561 | 377 | { |
562 | 377 | PyObject *res = NULL; |
563 | 377 | int r; |
564 | | |
565 | 377 | CHECK_INITIALIZED(self) |
566 | 377 | if (!ENTER_BUFFERED(self)) { |
567 | 0 | return NULL; |
568 | 0 | } |
569 | | /* gh-138720: Use IS_CLOSED to match flush CHECK_CLOSED. */ |
570 | 377 | r = IS_CLOSED(self); |
571 | 377 | if (r < 0) |
572 | 0 | goto end; |
573 | 377 | if (r > 0) { |
574 | 0 | res = Py_NewRef(Py_None); |
575 | 0 | goto end; |
576 | 0 | } |
577 | | |
578 | 377 | if (self->finalizing) { |
579 | 0 | PyObject *r = _io__Buffered__dealloc_warn_impl(self, (PyObject *)self); |
580 | 0 | if (r) |
581 | 0 | Py_DECREF(r); |
582 | 0 | else |
583 | 0 | PyErr_Clear(); |
584 | 0 | } |
585 | | /* flush() will most probably re-take the lock, so drop it first */ |
586 | 377 | LEAVE_BUFFERED(self) |
587 | 377 | r = _PyFile_Flush((PyObject *)self); |
588 | 377 | if (!ENTER_BUFFERED(self)) { |
589 | 0 | return NULL; |
590 | 0 | } |
591 | 377 | PyObject *exc = NULL; |
592 | 377 | if (r < 0) { |
593 | 0 | exc = PyErr_GetRaisedException(); |
594 | 0 | } |
595 | | |
596 | 377 | res = PyObject_CallMethodNoArgs(self->raw, &_Py_ID(close)); |
597 | | |
598 | 377 | if (self->buffer) { |
599 | 377 | PyMem_Free(self->buffer); |
600 | 377 | self->buffer = NULL; |
601 | 377 | } |
602 | | |
603 | 377 | if (exc != NULL) { |
604 | 0 | _PyErr_ChainExceptions1(exc); |
605 | 0 | Py_CLEAR(res); |
606 | 0 | } |
607 | | |
608 | 377 | self->read_end = 0; |
609 | 377 | self->pos = 0; |
610 | | |
611 | 377 | end: |
612 | 377 | LEAVE_BUFFERED(self) |
613 | 377 | return res; |
614 | 377 | } |
615 | | |
616 | | /*[clinic input] |
617 | | @critical_section |
618 | | _io._Buffered.detach |
619 | | [clinic start generated code]*/ |
620 | | |
621 | | static PyObject * |
622 | | _io__Buffered_detach_impl(buffered *self) |
623 | | /*[clinic end generated code: output=dd0fc057b8b779f7 input=d4ef1828a678be37]*/ |
624 | 0 | { |
625 | 0 | PyObject *raw; |
626 | 0 | CHECK_INITIALIZED(self) |
627 | 0 | if (_PyFile_Flush((PyObject *)self) < 0) { |
628 | 0 | return NULL; |
629 | 0 | } |
630 | 0 | raw = self->raw; |
631 | 0 | self->raw = NULL; |
632 | 0 | self->detached = 1; |
633 | 0 | self->ok = 0; |
634 | 0 | return raw; |
635 | 0 | } |
636 | | |
637 | | /* Inquiries */ |
638 | | |
639 | | /*[clinic input] |
640 | | @critical_section |
641 | | _io._Buffered.seekable |
642 | | [clinic start generated code]*/ |
643 | | |
644 | | static PyObject * |
645 | | _io__Buffered_seekable_impl(buffered *self) |
646 | | /*[clinic end generated code: output=90172abb5ceb6e8f input=e3a4fc1d297b2fd3]*/ |
647 | 66 | { |
648 | 66 | CHECK_INITIALIZED(self) |
649 | 66 | return PyObject_CallMethodNoArgs(self->raw, &_Py_ID(seekable)); |
650 | 66 | } |
651 | | |
652 | | /*[clinic input] |
653 | | @critical_section |
654 | | _io._Buffered.readable |
655 | | [clinic start generated code]*/ |
656 | | |
657 | | static PyObject * |
658 | | _io__Buffered_readable_impl(buffered *self) |
659 | | /*[clinic end generated code: output=92afa07661ecb698 input=abe54107d59bca9a]*/ |
660 | 22 | { |
661 | 22 | CHECK_INITIALIZED(self) |
662 | 22 | return PyObject_CallMethodNoArgs(self->raw, &_Py_ID(readable)); |
663 | 22 | } |
664 | | |
665 | | /*[clinic input] |
666 | | @critical_section |
667 | | _io._Buffered.writable |
668 | | [clinic start generated code]*/ |
669 | | |
670 | | static PyObject * |
671 | | _io__Buffered_writable_impl(buffered *self) |
672 | | /*[clinic end generated code: output=4e3eee8d6f9d8552 input=45eb76bf6a10e6f7]*/ |
673 | 44 | { |
674 | 44 | CHECK_INITIALIZED(self) |
675 | 44 | return PyObject_CallMethodNoArgs(self->raw, &_Py_ID(writable)); |
676 | 44 | } |
677 | | |
678 | | |
679 | | /*[clinic input] |
680 | | @critical_section |
681 | | @getter |
682 | | _io._Buffered.name |
683 | | [clinic start generated code]*/ |
684 | | |
685 | | static PyObject * |
686 | | _io__Buffered_name_get_impl(buffered *self) |
687 | | /*[clinic end generated code: output=d2adf384051d3d10 input=6b84a0e6126f545e]*/ |
688 | 0 | { |
689 | 0 | CHECK_INITIALIZED(self) |
690 | 0 | return PyObject_GetAttr(self->raw, &_Py_ID(name)); |
691 | 0 | } |
692 | | |
693 | | /*[clinic input] |
694 | | @critical_section |
695 | | @getter |
696 | | _io._Buffered.mode |
697 | | [clinic start generated code]*/ |
698 | | |
699 | | static PyObject * |
700 | | _io__Buffered_mode_get_impl(buffered *self) |
701 | | /*[clinic end generated code: output=0feb205748892fa4 input=0762d5e28542fd8c]*/ |
702 | 0 | { |
703 | 0 | CHECK_INITIALIZED(self) |
704 | 0 | return PyObject_GetAttr(self->raw, &_Py_ID(mode)); |
705 | 0 | } |
706 | | |
707 | | /* Lower-level APIs */ |
708 | | |
709 | | /*[clinic input] |
710 | | @critical_section |
711 | | _io._Buffered.fileno |
712 | | [clinic start generated code]*/ |
713 | | |
714 | | static PyObject * |
715 | | _io__Buffered_fileno_impl(buffered *self) |
716 | | /*[clinic end generated code: output=b717648d58a95ee3 input=1c4fead777bae20a]*/ |
717 | 0 | { |
718 | 0 | CHECK_INITIALIZED(self) |
719 | 0 | return PyObject_CallMethodNoArgs(self->raw, &_Py_ID(fileno)); |
720 | 0 | } |
721 | | |
722 | | /*[clinic input] |
723 | | @critical_section |
724 | | _io._Buffered.isatty |
725 | | [clinic start generated code]*/ |
726 | | |
727 | | static PyObject * |
728 | | _io__Buffered_isatty_impl(buffered *self) |
729 | | /*[clinic end generated code: output=c20e55caae67baea input=e53d182d7e490e3a]*/ |
730 | 0 | { |
731 | 0 | CHECK_INITIALIZED(self) |
732 | 0 | return PyObject_CallMethodNoArgs(self->raw, &_Py_ID(isatty)); |
733 | 0 | } |
734 | | |
735 | | /* Forward decls */ |
736 | | static PyObject * |
737 | | _bufferedwriter_flush_unlocked(buffered *); |
738 | | static Py_ssize_t |
739 | | _bufferedreader_fill_buffer(buffered *self); |
740 | | static void |
741 | | _bufferedreader_reset_buf(buffered *self); |
742 | | static void |
743 | | _bufferedwriter_reset_buf(buffered *self); |
744 | | static PyObject * |
745 | | _bufferedreader_peek_unlocked(buffered *self); |
746 | | static PyObject * |
747 | | _bufferedreader_read_all(buffered *self); |
748 | | static PyObject * |
749 | | _bufferedreader_read_fast(buffered *self, Py_ssize_t); |
750 | | static PyObject * |
751 | | _bufferedreader_read_generic(buffered *self, Py_ssize_t); |
752 | | static Py_ssize_t |
753 | | _bufferedreader_raw_read(buffered *self, char *start, Py_ssize_t len); |
754 | | |
755 | | /* |
756 | | * Helpers |
757 | | */ |
758 | | |
759 | | /* Sets the current error to BlockingIOError */ |
760 | | static void |
761 | | _set_BlockingIOError(const char *msg, Py_ssize_t written) |
762 | 0 | { |
763 | 0 | PyObject *err; |
764 | 0 | PyErr_Clear(); |
765 | 0 | err = PyObject_CallFunction(PyExc_BlockingIOError, "isn", |
766 | 0 | errno, msg, written); |
767 | 0 | if (err) |
768 | 0 | PyErr_SetObject(PyExc_BlockingIOError, err); |
769 | 0 | Py_XDECREF(err); |
770 | 0 | } |
771 | | |
772 | | /* Returns the address of the `written` member if a BlockingIOError was |
773 | | raised, NULL otherwise. The error is always re-raised. */ |
774 | | static Py_ssize_t * |
775 | | _buffered_check_blocking_error(void) |
776 | 0 | { |
777 | 0 | PyObject *exc = PyErr_GetRaisedException(); |
778 | 0 | if (exc == NULL || !PyErr_GivenExceptionMatches(exc, PyExc_BlockingIOError)) { |
779 | 0 | PyErr_SetRaisedException(exc); |
780 | 0 | return NULL; |
781 | 0 | } |
782 | 0 | PyOSErrorObject *err = (PyOSErrorObject *)exc; |
783 | | /* TODO: sanity check (err->written >= 0) */ |
784 | 0 | PyErr_SetRaisedException(exc); |
785 | 0 | return &err->written; |
786 | 0 | } |
787 | | |
788 | | static Py_off_t |
789 | | _buffered_raw_tell(buffered *self) |
790 | 487 | { |
791 | 487 | Py_off_t n; |
792 | 487 | PyObject *res; |
793 | 487 | res = PyObject_CallMethodNoArgs(self->raw, &_Py_ID(tell)); |
794 | 487 | if (res == NULL) |
795 | 0 | return -1; |
796 | 487 | n = PyNumber_AsOff_t(res, PyExc_ValueError); |
797 | 487 | Py_DECREF(res); |
798 | 487 | if (n < 0) { |
799 | 0 | if (!PyErr_Occurred()) |
800 | 0 | PyErr_Format(PyExc_OSError, |
801 | 0 | "Raw stream returned invalid position %" PY_PRIdOFF, |
802 | 0 | (PY_OFF_T_COMPAT)n); |
803 | 0 | return -1; |
804 | 0 | } |
805 | 487 | self->abs_pos = n; |
806 | 487 | return n; |
807 | 487 | } |
808 | | |
809 | | static Py_off_t |
810 | | _buffered_raw_seek(buffered *self, Py_off_t target, int whence) |
811 | 0 | { |
812 | 0 | PyObject *res, *posobj, *whenceobj; |
813 | 0 | Py_off_t n; |
814 | |
|
815 | 0 | posobj = PyLong_FromOff_t(target); |
816 | 0 | if (posobj == NULL) |
817 | 0 | return -1; |
818 | 0 | whenceobj = PyLong_FromLong(whence); |
819 | 0 | if (whenceobj == NULL) { |
820 | 0 | Py_DECREF(posobj); |
821 | 0 | return -1; |
822 | 0 | } |
823 | 0 | res = PyObject_CallMethodObjArgs(self->raw, &_Py_ID(seek), |
824 | 0 | posobj, whenceobj, NULL); |
825 | 0 | Py_DECREF(posobj); |
826 | 0 | Py_DECREF(whenceobj); |
827 | 0 | if (res == NULL) |
828 | 0 | return -1; |
829 | 0 | n = PyNumber_AsOff_t(res, PyExc_ValueError); |
830 | 0 | Py_DECREF(res); |
831 | 0 | if (n < 0) { |
832 | 0 | if (!PyErr_Occurred()) |
833 | 0 | PyErr_Format(PyExc_OSError, |
834 | 0 | "Raw stream returned invalid position %" PY_PRIdOFF, |
835 | 0 | (PY_OFF_T_COMPAT)n); |
836 | 0 | return -1; |
837 | 0 | } |
838 | 0 | self->abs_pos = n; |
839 | 0 | return n; |
840 | 0 | } |
841 | | |
842 | | static int |
843 | | _buffered_init(buffered *self) |
844 | 443 | { |
845 | 443 | Py_ssize_t n; |
846 | 443 | if (self->buffer_size <= 0) { |
847 | 0 | PyErr_SetString(PyExc_ValueError, |
848 | 0 | "buffer size must be strictly positive"); |
849 | 0 | return -1; |
850 | 0 | } |
851 | 443 | if (self->buffer) |
852 | 0 | PyMem_Free(self->buffer); |
853 | 443 | self->buffer = PyMem_Malloc(self->buffer_size); |
854 | 443 | if (self->buffer == NULL) { |
855 | 0 | PyErr_NoMemory(); |
856 | 0 | return -1; |
857 | 0 | } |
858 | 443 | if (self->lock) |
859 | 0 | PyThread_free_lock(self->lock); |
860 | 443 | self->lock = PyThread_allocate_lock(); |
861 | 443 | if (self->lock == NULL) { |
862 | 0 | PyErr_SetString(PyExc_RuntimeError, "can't allocate read lock"); |
863 | 0 | return -1; |
864 | 0 | } |
865 | 443 | self->owner = 0; |
866 | | /* Find out whether buffer_size is a power of 2 */ |
867 | | /* XXX is this optimization useful? */ |
868 | 7.97k | for (n = self->buffer_size - 1; n & 1; n >>= 1) |
869 | 7.53k | ; |
870 | 443 | if (n == 0) |
871 | 443 | self->buffer_mask = self->buffer_size - 1; |
872 | 0 | else |
873 | 0 | self->buffer_mask = 0; |
874 | 443 | if (_buffered_raw_tell(self) == -1) |
875 | 0 | PyErr_Clear(); |
876 | 443 | return 0; |
877 | 443 | } |
878 | | |
879 | | /* Return 1 if an OSError with errno == EINTR is set (and then |
880 | | clears the error indicator), 0 otherwise. |
881 | | Should only be called when PyErr_Occurred() is true. |
882 | | */ |
883 | | int |
884 | | _PyIO_trap_eintr(void) |
885 | 0 | { |
886 | 0 | if (!PyErr_ExceptionMatches(PyExc_OSError)) { |
887 | 0 | return 0; |
888 | 0 | } |
889 | 0 | PyObject *exc = PyErr_GetRaisedException(); |
890 | 0 | PyOSErrorObject *env_err = (PyOSErrorObject *)exc; |
891 | 0 | assert(env_err != NULL); |
892 | 0 | if (env_err->myerrno != NULL) { |
893 | 0 | assert(EINTR > 0 && EINTR < INT_MAX); |
894 | 0 | assert(PyLong_CheckExact(env_err->myerrno)); |
895 | 0 | int overflow; |
896 | 0 | int myerrno = PyLong_AsLongAndOverflow(env_err->myerrno, &overflow); |
897 | 0 | PyErr_Clear(); |
898 | 0 | if (myerrno == EINTR) { |
899 | 0 | Py_DECREF(exc); |
900 | 0 | return 1; |
901 | 0 | } |
902 | 0 | } |
903 | | /* This silences any error set by PyObject_RichCompareBool() */ |
904 | 0 | PyErr_SetRaisedException(exc); |
905 | 0 | return 0; |
906 | 0 | } |
907 | | |
908 | | /* |
909 | | * Shared methods and wrappers |
910 | | */ |
911 | | |
912 | | static PyObject * |
913 | | buffered_flush_and_rewind_unlocked(buffered *self) |
914 | 153k | { |
915 | 153k | PyObject *res; |
916 | | |
917 | 153k | res = _bufferedwriter_flush_unlocked(self); |
918 | 153k | if (res == NULL) |
919 | 0 | return NULL; |
920 | 153k | Py_DECREF(res); |
921 | | |
922 | 153k | if (self->readable) { |
923 | | /* Rewind the raw stream so that its position corresponds to |
924 | | the current logical position. */ |
925 | 0 | Py_off_t n; |
926 | 0 | n = _buffered_raw_seek(self, -RAW_OFFSET(self), 1); |
927 | 0 | _bufferedreader_reset_buf(self); |
928 | 0 | if (n == -1) |
929 | 0 | return NULL; |
930 | 0 | } |
931 | 153k | Py_RETURN_NONE; |
932 | 153k | } |
933 | | |
934 | | /*[clinic input] |
935 | | @critical_section |
936 | | _io._Buffered.flush |
937 | | [clinic start generated code]*/ |
938 | | |
939 | | static PyObject * |
940 | | _io__Buffered_flush_impl(buffered *self) |
941 | | /*[clinic end generated code: output=da2674ef1ce71f3a input=6b30de9f083419c2]*/ |
942 | 153k | { |
943 | 153k | PyObject *res; |
944 | | |
945 | 153k | CHECK_INITIALIZED(self) |
946 | 153k | CHECK_CLOSED(self, "flush of closed file") |
947 | | |
948 | 153k | if (!ENTER_BUFFERED(self)) |
949 | 0 | return NULL; |
950 | 153k | res = buffered_flush_and_rewind_unlocked(self); |
951 | 153k | LEAVE_BUFFERED(self) |
952 | | |
953 | 153k | return res; |
954 | 153k | } |
955 | | |
956 | | /*[clinic input] |
957 | | @critical_section |
958 | | _io._Buffered.peek |
959 | | size: Py_ssize_t = 0 |
960 | | / |
961 | | |
962 | | [clinic start generated code]*/ |
963 | | |
964 | | static PyObject * |
965 | | _io__Buffered_peek_impl(buffered *self, Py_ssize_t size) |
966 | | /*[clinic end generated code: output=ba7a097ca230102b input=56733376f926d982]*/ |
967 | 0 | { |
968 | 0 | PyObject *res = NULL; |
969 | |
|
970 | 0 | CHECK_INITIALIZED(self) |
971 | 0 | CHECK_CLOSED(self, "peek of closed file") |
972 | | |
973 | 0 | if (!ENTER_BUFFERED(self)) |
974 | 0 | return NULL; |
975 | | |
976 | 0 | if (self->writable) { |
977 | 0 | res = buffered_flush_and_rewind_unlocked(self); |
978 | 0 | if (res == NULL) |
979 | 0 | goto end; |
980 | 0 | Py_CLEAR(res); |
981 | 0 | } |
982 | 0 | res = _bufferedreader_peek_unlocked(self); |
983 | |
|
984 | 0 | end: |
985 | 0 | LEAVE_BUFFERED(self) |
986 | 0 | return res; |
987 | 0 | } |
988 | | |
989 | | /*[clinic input] |
990 | | @critical_section |
991 | | _io._Buffered.read |
992 | | size as n: Py_ssize_t(accept={int, NoneType}) = -1 |
993 | | / |
994 | | [clinic start generated code]*/ |
995 | | |
996 | | static PyObject * |
997 | | _io__Buffered_read_impl(buffered *self, Py_ssize_t n) |
998 | | /*[clinic end generated code: output=f41c78bb15b9bbe9 input=bdb4b0425b295472]*/ |
999 | 377 | { |
1000 | 377 | PyObject *res; |
1001 | | |
1002 | 377 | CHECK_INITIALIZED(self) |
1003 | 377 | if (n < -1) { |
1004 | 0 | PyErr_SetString(PyExc_ValueError, |
1005 | 0 | "read length must be non-negative or -1"); |
1006 | 0 | return NULL; |
1007 | 0 | } |
1008 | | |
1009 | 377 | CHECK_CLOSED(self, "read of closed file") |
1010 | | |
1011 | 377 | if (n == -1) { |
1012 | | /* The number of bytes is unspecified, read until the end of stream */ |
1013 | 377 | if (!ENTER_BUFFERED(self)) |
1014 | 0 | return NULL; |
1015 | 377 | res = _bufferedreader_read_all(self); |
1016 | 377 | } |
1017 | 0 | else { |
1018 | 0 | res = _bufferedreader_read_fast(self, n); |
1019 | 0 | if (res != Py_None) |
1020 | 0 | return res; |
1021 | 0 | Py_DECREF(res); |
1022 | 0 | if (!ENTER_BUFFERED(self)) |
1023 | 0 | return NULL; |
1024 | 0 | res = _bufferedreader_read_generic(self, n); |
1025 | 0 | } |
1026 | | |
1027 | 377 | LEAVE_BUFFERED(self) |
1028 | 377 | return res; |
1029 | 377 | } |
1030 | | |
1031 | | /*[clinic input] |
1032 | | @critical_section |
1033 | | _io._Buffered.read1 |
1034 | | size as n: Py_ssize_t = -1 |
1035 | | / |
1036 | | [clinic start generated code]*/ |
1037 | | |
1038 | | static PyObject * |
1039 | | _io__Buffered_read1_impl(buffered *self, Py_ssize_t n) |
1040 | | /*[clinic end generated code: output=bcc4fb4e54d103a3 input=3d0ad241aa52b36c]*/ |
1041 | 0 | { |
1042 | 0 | CHECK_INITIALIZED(self) |
1043 | 0 | if (n < 0) { |
1044 | 0 | n = self->buffer_size; |
1045 | 0 | } |
1046 | |
|
1047 | 0 | CHECK_CLOSED(self, "read of closed file") |
1048 | | |
1049 | 0 | if (n == 0) { |
1050 | 0 | return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES); |
1051 | 0 | } |
1052 | | |
1053 | | /* Return up to n bytes. If at least one byte is buffered, we |
1054 | | only return buffered bytes. Otherwise, we do one raw read. */ |
1055 | | |
1056 | 0 | Py_ssize_t have = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t); |
1057 | 0 | if (have > 0) { |
1058 | 0 | n = Py_MIN(have, n); |
1059 | 0 | PyObject *res = _bufferedreader_read_fast(self, n); |
1060 | 0 | assert(res != Py_None); |
1061 | 0 | return res; |
1062 | 0 | } |
1063 | | |
1064 | 0 | if (!ENTER_BUFFERED(self)) { |
1065 | 0 | return NULL; |
1066 | 0 | } |
1067 | | |
1068 | | /* Flush the write buffer if necessary */ |
1069 | 0 | if (self->writable) { |
1070 | 0 | PyObject *res = buffered_flush_and_rewind_unlocked(self); |
1071 | 0 | if (res == NULL) { |
1072 | 0 | LEAVE_BUFFERED(self) |
1073 | 0 | return NULL; |
1074 | 0 | } |
1075 | 0 | Py_DECREF(res); |
1076 | 0 | } |
1077 | 0 | _bufferedreader_reset_buf(self); |
1078 | |
|
1079 | 0 | PyBytesWriter *writer = PyBytesWriter_Create(n); |
1080 | 0 | if (writer == NULL) { |
1081 | 0 | LEAVE_BUFFERED(self) |
1082 | 0 | return NULL; |
1083 | 0 | } |
1084 | | |
1085 | 0 | Py_ssize_t r = _bufferedreader_raw_read(self, |
1086 | 0 | PyBytesWriter_GetData(writer), n); |
1087 | 0 | LEAVE_BUFFERED(self) |
1088 | 0 | if (r == -1) { |
1089 | 0 | PyBytesWriter_Discard(writer); |
1090 | 0 | return NULL; |
1091 | 0 | } |
1092 | 0 | if (r == -2) { |
1093 | 0 | r = 0; |
1094 | 0 | } |
1095 | |
|
1096 | 0 | return PyBytesWriter_FinishWithSize(writer, r); |
1097 | 0 | } |
1098 | | |
1099 | | static PyObject * |
1100 | | _buffered_readinto_generic(buffered *self, Py_buffer *buffer, char readinto1) |
1101 | 0 | { |
1102 | 0 | Py_ssize_t n, written = 0, remaining; |
1103 | 0 | PyObject *res = NULL; |
1104 | |
|
1105 | 0 | CHECK_INITIALIZED(self) |
1106 | 0 | CHECK_CLOSED(self, "readinto of closed file") |
1107 | | |
1108 | 0 | n = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t); |
1109 | 0 | if (n > 0) { |
1110 | 0 | if (n >= buffer->len) { |
1111 | 0 | memcpy(buffer->buf, self->buffer + self->pos, buffer->len); |
1112 | 0 | self->pos += buffer->len; |
1113 | 0 | return PyLong_FromSsize_t(buffer->len); |
1114 | 0 | } |
1115 | 0 | memcpy(buffer->buf, self->buffer + self->pos, n); |
1116 | 0 | self->pos += n; |
1117 | 0 | written = n; |
1118 | 0 | } |
1119 | | |
1120 | 0 | if (!ENTER_BUFFERED(self)) |
1121 | 0 | return NULL; |
1122 | | |
1123 | 0 | if (self->writable) { |
1124 | 0 | res = buffered_flush_and_rewind_unlocked(self); |
1125 | 0 | if (res == NULL) |
1126 | 0 | goto end; |
1127 | 0 | Py_CLEAR(res); |
1128 | 0 | } |
1129 | | |
1130 | 0 | _bufferedreader_reset_buf(self); |
1131 | 0 | self->pos = 0; |
1132 | |
|
1133 | 0 | for (remaining = buffer->len - written; |
1134 | 0 | remaining > 0; |
1135 | 0 | written += n, remaining -= n) { |
1136 | | /* If remaining bytes is larger than internal buffer size, copy |
1137 | | * directly into caller's buffer. */ |
1138 | 0 | if (remaining > self->buffer_size) { |
1139 | 0 | n = _bufferedreader_raw_read(self, (char *) buffer->buf + written, |
1140 | 0 | remaining); |
1141 | 0 | } |
1142 | | |
1143 | | /* In readinto1 mode, we do not want to fill the internal |
1144 | | buffer if we already have some data to return */ |
1145 | 0 | else if (!(readinto1 && written)) { |
1146 | 0 | n = _bufferedreader_fill_buffer(self); |
1147 | 0 | if (n > 0) { |
1148 | 0 | if (n > remaining) |
1149 | 0 | n = remaining; |
1150 | 0 | memcpy((char *) buffer->buf + written, |
1151 | 0 | self->buffer + self->pos, n); |
1152 | 0 | self->pos += n; |
1153 | 0 | continue; /* short circuit */ |
1154 | 0 | } |
1155 | 0 | } |
1156 | 0 | else |
1157 | 0 | n = 0; |
1158 | | |
1159 | 0 | if (n == 0 || (n == -2 && written > 0)) |
1160 | 0 | break; |
1161 | 0 | if (n < 0) { |
1162 | 0 | if (n == -2) { |
1163 | 0 | res = Py_NewRef(Py_None); |
1164 | 0 | } |
1165 | 0 | goto end; |
1166 | 0 | } |
1167 | | |
1168 | | /* At most one read in readinto1 mode */ |
1169 | 0 | if (readinto1) { |
1170 | 0 | written += n; |
1171 | 0 | break; |
1172 | 0 | } |
1173 | 0 | } |
1174 | 0 | res = PyLong_FromSsize_t(written); |
1175 | |
|
1176 | 0 | end: |
1177 | 0 | LEAVE_BUFFERED(self); |
1178 | 0 | return res; |
1179 | 0 | } |
1180 | | |
1181 | | /*[clinic input] |
1182 | | @critical_section |
1183 | | _io._Buffered.readinto |
1184 | | buffer: Py_buffer(accept={rwbuffer}) |
1185 | | / |
1186 | | [clinic start generated code]*/ |
1187 | | |
1188 | | static PyObject * |
1189 | | _io__Buffered_readinto_impl(buffered *self, Py_buffer *buffer) |
1190 | | /*[clinic end generated code: output=bcb376580b1d8170 input=777c33e7adaa2bcd]*/ |
1191 | 0 | { |
1192 | 0 | return _buffered_readinto_generic(self, buffer, 0); |
1193 | 0 | } |
1194 | | |
1195 | | /*[clinic input] |
1196 | | @critical_section |
1197 | | _io._Buffered.readinto1 |
1198 | | buffer: Py_buffer(accept={rwbuffer}) |
1199 | | / |
1200 | | [clinic start generated code]*/ |
1201 | | |
1202 | | static PyObject * |
1203 | | _io__Buffered_readinto1_impl(buffered *self, Py_buffer *buffer) |
1204 | | /*[clinic end generated code: output=6e5c6ac5868205d6 input=ef03cc5fc92a6895]*/ |
1205 | 0 | { |
1206 | 0 | return _buffered_readinto_generic(self, buffer, 1); |
1207 | 0 | } |
1208 | | |
1209 | | |
1210 | | static PyObject * |
1211 | | _buffered_readline(buffered *self, Py_ssize_t limit) |
1212 | 0 | { |
1213 | 0 | _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self); |
1214 | |
|
1215 | 0 | CHECK_CLOSED(self, "readline of closed file") |
1216 | | |
1217 | | /* First, try to find a line in the buffer. This can run unlocked because |
1218 | | the calls to the C API are simple enough that they can't trigger |
1219 | | any thread switch. */ |
1220 | 0 | Py_ssize_t n = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t); |
1221 | 0 | if (limit >= 0 && n > limit) { |
1222 | 0 | n = limit; |
1223 | 0 | } |
1224 | 0 | const char *start = self->buffer + self->pos; |
1225 | 0 | const char *s = memchr(start, '\n', n); |
1226 | 0 | if (s != NULL) { |
1227 | 0 | n = s - start + 1; |
1228 | 0 | PyObject *res = PyBytes_FromStringAndSize(start, n); |
1229 | 0 | if (res == NULL) { |
1230 | 0 | return NULL; |
1231 | 0 | } |
1232 | 0 | self->pos += n; |
1233 | 0 | return res; |
1234 | 0 | } |
1235 | | |
1236 | 0 | if (n == limit) { |
1237 | 0 | PyObject *res = PyBytes_FromStringAndSize(start, n); |
1238 | 0 | if (res == NULL) { |
1239 | 0 | return NULL; |
1240 | 0 | } |
1241 | 0 | self->pos += n; |
1242 | 0 | return res; |
1243 | 0 | } |
1244 | | |
1245 | 0 | PyBytesWriter *writer = NULL; |
1246 | 0 | int locked = 0; |
1247 | 0 | if (!ENTER_BUFFERED(self)) { |
1248 | 0 | goto error; |
1249 | 0 | } |
1250 | 0 | locked = 1; |
1251 | | |
1252 | | /* Now we try to get some more from the raw stream */ |
1253 | 0 | writer = PyBytesWriter_Create(0); |
1254 | 0 | if (writer == NULL) { |
1255 | 0 | goto error; |
1256 | 0 | } |
1257 | | |
1258 | 0 | if (n > 0) { |
1259 | 0 | if (PyBytesWriter_WriteBytes(writer, start, n) < 0) { |
1260 | 0 | goto error; |
1261 | 0 | } |
1262 | 0 | self->pos += n; |
1263 | 0 | if (limit >= 0) { |
1264 | 0 | limit -= n; |
1265 | 0 | } |
1266 | 0 | } |
1267 | 0 | if (self->writable) { |
1268 | 0 | PyObject *res = buffered_flush_and_rewind_unlocked(self); |
1269 | 0 | if (res == NULL) { |
1270 | 0 | goto error; |
1271 | 0 | } |
1272 | 0 | Py_DECREF(res); |
1273 | 0 | } |
1274 | | |
1275 | 0 | for (;;) { |
1276 | 0 | _bufferedreader_reset_buf(self); |
1277 | 0 | n = _bufferedreader_fill_buffer(self); |
1278 | 0 | if (n == -1) { |
1279 | 0 | goto error; |
1280 | 0 | } |
1281 | 0 | if (n <= 0) { |
1282 | 0 | break; |
1283 | 0 | } |
1284 | 0 | if (limit >= 0 && n > limit) { |
1285 | 0 | n = limit; |
1286 | 0 | } |
1287 | 0 | start = self->buffer; |
1288 | 0 | const char *newline = memchr(start, '\n', n); |
1289 | 0 | if (newline != NULL) { |
1290 | 0 | n = newline - start + 1; |
1291 | 0 | if (PyBytesWriter_WriteBytes(writer, start, n) < 0) { |
1292 | 0 | goto error; |
1293 | 0 | } |
1294 | 0 | self->pos = n; |
1295 | 0 | goto found; |
1296 | 0 | } |
1297 | | |
1298 | 0 | if (PyBytesWriter_WriteBytes(writer, start, n) < 0) { |
1299 | 0 | goto error; |
1300 | 0 | } |
1301 | 0 | if (n == limit) { |
1302 | 0 | self->pos = n; |
1303 | 0 | break; |
1304 | 0 | } |
1305 | 0 | if (limit >= 0) { |
1306 | 0 | limit -= n; |
1307 | 0 | } |
1308 | 0 | } |
1309 | | |
1310 | 0 | found: |
1311 | 0 | LEAVE_BUFFERED(self) |
1312 | 0 | return PyBytesWriter_Finish(writer); |
1313 | | |
1314 | 0 | error: |
1315 | 0 | PyBytesWriter_Discard(writer); |
1316 | 0 | if (locked) { |
1317 | 0 | LEAVE_BUFFERED(self) |
1318 | 0 | } |
1319 | 0 | return NULL; |
1320 | 0 | } |
1321 | | |
1322 | | /*[clinic input] |
1323 | | @critical_section |
1324 | | _io._Buffered.readline |
1325 | | size: Py_ssize_t(accept={int, NoneType}) = -1 |
1326 | | / |
1327 | | [clinic start generated code]*/ |
1328 | | |
1329 | | static PyObject * |
1330 | | _io__Buffered_readline_impl(buffered *self, Py_ssize_t size) |
1331 | | /*[clinic end generated code: output=24dd2aa6e33be83c input=e81ca5abd4280776]*/ |
1332 | 0 | { |
1333 | 0 | CHECK_INITIALIZED(self) |
1334 | 0 | return _buffered_readline(self, size); |
1335 | 0 | } |
1336 | | |
1337 | | |
1338 | | /*[clinic input] |
1339 | | @critical_section |
1340 | | _io._Buffered.tell |
1341 | | [clinic start generated code]*/ |
1342 | | |
1343 | | static PyObject * |
1344 | | _io__Buffered_tell_impl(buffered *self) |
1345 | | /*[clinic end generated code: output=386972ae84716c1e input=ab12e67d8abcb42f]*/ |
1346 | 44 | { |
1347 | 44 | Py_off_t pos; |
1348 | | |
1349 | 44 | CHECK_INITIALIZED(self) |
1350 | 44 | pos = _buffered_raw_tell(self); |
1351 | 44 | if (pos == -1) |
1352 | 0 | return NULL; |
1353 | 44 | pos -= RAW_OFFSET(self); |
1354 | | |
1355 | | // GH-95782 |
1356 | 44 | if (pos < 0) |
1357 | 0 | pos = 0; |
1358 | | |
1359 | 44 | return PyLong_FromOff_t(pos); |
1360 | 44 | } |
1361 | | |
1362 | | /*[clinic input] |
1363 | | @critical_section |
1364 | | _io._Buffered.seek |
1365 | | target as targetobj: object |
1366 | | whence: int = 0 |
1367 | | / |
1368 | | [clinic start generated code]*/ |
1369 | | |
1370 | | static PyObject * |
1371 | | _io__Buffered_seek_impl(buffered *self, PyObject *targetobj, int whence) |
1372 | | /*[clinic end generated code: output=7ae0e8dc46efdefb input=b5a12be70e0ad07b]*/ |
1373 | 0 | { |
1374 | 0 | Py_off_t target, n; |
1375 | 0 | PyObject *res = NULL; |
1376 | |
|
1377 | 0 | CHECK_INITIALIZED(self) |
1378 | | |
1379 | | /* Do some error checking instead of trusting OS 'seek()' |
1380 | | ** error detection, just in case. |
1381 | | */ |
1382 | 0 | if ((whence < 0 || whence >2) |
1383 | 0 | #ifdef SEEK_HOLE |
1384 | 0 | && (whence != SEEK_HOLE) |
1385 | 0 | #endif |
1386 | 0 | #ifdef SEEK_DATA |
1387 | 0 | && (whence != SEEK_DATA) |
1388 | 0 | #endif |
1389 | 0 | ) { |
1390 | 0 | PyErr_Format(PyExc_ValueError, |
1391 | 0 | "whence value %d unsupported", whence); |
1392 | 0 | return NULL; |
1393 | 0 | } |
1394 | | |
1395 | 0 | CHECK_CLOSED(self, "seek of closed file") |
1396 | | |
1397 | 0 | _PyIO_State *state = find_io_state_by_def(Py_TYPE(self)); |
1398 | 0 | if (_PyIOBase_check_seekable(state, self->raw, Py_True) == NULL) { |
1399 | 0 | return NULL; |
1400 | 0 | } |
1401 | | |
1402 | 0 | target = PyNumber_AsOff_t(targetobj, PyExc_ValueError); |
1403 | 0 | if (target == -1 && PyErr_Occurred()) |
1404 | 0 | return NULL; |
1405 | | |
1406 | | /* SEEK_SET and SEEK_CUR are special because we could seek inside the |
1407 | | buffer. Other whence values must be managed without this optimization. |
1408 | | Some Operating Systems can provide additional values, like |
1409 | | SEEK_HOLE/SEEK_DATA. */ |
1410 | 0 | if (((whence == 0) || (whence == 1)) && self->readable) { |
1411 | 0 | Py_off_t current, avail; |
1412 | | /* Check if seeking leaves us inside the current buffer, |
1413 | | so as to return quickly if possible. Also, we needn't take the |
1414 | | lock in this fast path. |
1415 | | Don't know how to do that when whence == 2, though. */ |
1416 | | /* NOTE: RAW_TELL() can release the GIL but the object is in a stable |
1417 | | state at this point. */ |
1418 | 0 | current = RAW_TELL(self); |
1419 | 0 | avail = READAHEAD(self); |
1420 | 0 | if (avail > 0) { |
1421 | 0 | Py_off_t offset; |
1422 | 0 | if (whence == 0) |
1423 | 0 | offset = target - (current - RAW_OFFSET(self)); |
1424 | 0 | else |
1425 | 0 | offset = target; |
1426 | 0 | if (offset >= -self->pos && offset <= avail) { |
1427 | 0 | self->pos += offset; |
1428 | | |
1429 | | // GH-95782 |
1430 | 0 | if (current - avail + offset < 0) |
1431 | 0 | return PyLong_FromOff_t(0); |
1432 | | |
1433 | 0 | return PyLong_FromOff_t(current - avail + offset); |
1434 | 0 | } |
1435 | 0 | } |
1436 | 0 | } |
1437 | | |
1438 | 0 | if (!ENTER_BUFFERED(self)) |
1439 | 0 | return NULL; |
1440 | | |
1441 | | /* Fallback: invoke raw seek() method and clear buffer */ |
1442 | 0 | if (self->writable) { |
1443 | 0 | res = _bufferedwriter_flush_unlocked(self); |
1444 | 0 | if (res == NULL) |
1445 | 0 | goto end; |
1446 | 0 | Py_CLEAR(res); |
1447 | 0 | } |
1448 | | |
1449 | | /* TODO: align on block boundary and read buffer if needed? */ |
1450 | 0 | if (whence == 1) |
1451 | 0 | target -= RAW_OFFSET(self); |
1452 | 0 | n = _buffered_raw_seek(self, target, whence); |
1453 | 0 | if (n == -1) |
1454 | 0 | goto end; |
1455 | 0 | self->raw_pos = -1; |
1456 | 0 | res = PyLong_FromOff_t(n); |
1457 | 0 | if (res != NULL && self->readable) |
1458 | 0 | _bufferedreader_reset_buf(self); |
1459 | |
|
1460 | 0 | end: |
1461 | 0 | LEAVE_BUFFERED(self) |
1462 | 0 | return res; |
1463 | 0 | } |
1464 | | |
1465 | | /*[clinic input] |
1466 | | @critical_section |
1467 | | _io._Buffered.truncate |
1468 | | cls: defining_class |
1469 | | pos: object = None |
1470 | | / |
1471 | | [clinic start generated code]*/ |
1472 | | |
1473 | | static PyObject * |
1474 | | _io__Buffered_truncate_impl(buffered *self, PyTypeObject *cls, PyObject *pos) |
1475 | | /*[clinic end generated code: output=fe3882fbffe79f1a input=e3cbf794575bd794]*/ |
1476 | 0 | { |
1477 | 0 | PyObject *res = NULL; |
1478 | |
|
1479 | 0 | CHECK_INITIALIZED(self) |
1480 | 0 | CHECK_CLOSED(self, "truncate of closed file") |
1481 | 0 | if (!self->writable) { |
1482 | 0 | _PyIO_State *state = get_io_state_by_cls(cls); |
1483 | 0 | return bufferediobase_unsupported(state, "truncate"); |
1484 | 0 | } |
1485 | 0 | if (!ENTER_BUFFERED(self)) |
1486 | 0 | return NULL; |
1487 | | |
1488 | 0 | res = buffered_flush_and_rewind_unlocked(self); |
1489 | 0 | if (res == NULL) { |
1490 | 0 | goto end; |
1491 | 0 | } |
1492 | 0 | Py_CLEAR(res); |
1493 | |
|
1494 | 0 | res = PyObject_CallMethodOneArg(self->raw, &_Py_ID(truncate), pos); |
1495 | 0 | if (res == NULL) |
1496 | 0 | goto end; |
1497 | | /* Reset cached position */ |
1498 | 0 | if (_buffered_raw_tell(self) == -1) |
1499 | 0 | PyErr_Clear(); |
1500 | |
|
1501 | 0 | end: |
1502 | 0 | LEAVE_BUFFERED(self) |
1503 | 0 | return res; |
1504 | 0 | } |
1505 | | |
1506 | | static PyObject * |
1507 | | buffered_iternext(PyObject *op) |
1508 | 0 | { |
1509 | 0 | buffered *self = buffered_CAST(op); |
1510 | 0 | PyObject *line; |
1511 | 0 | PyTypeObject *tp; |
1512 | |
|
1513 | 0 | CHECK_INITIALIZED(self); |
1514 | |
|
1515 | 0 | _PyIO_State *state = find_io_state_by_def(Py_TYPE(self)); |
1516 | 0 | tp = Py_TYPE(self); |
1517 | 0 | if (tp == state->PyBufferedReader_Type || |
1518 | 0 | tp == state->PyBufferedRandom_Type) |
1519 | 0 | { |
1520 | | /* Skip method call overhead for speed */ |
1521 | 0 | Py_BEGIN_CRITICAL_SECTION(self); |
1522 | 0 | line = _buffered_readline(self, -1); |
1523 | 0 | Py_END_CRITICAL_SECTION(); |
1524 | 0 | } |
1525 | 0 | else { |
1526 | 0 | line = PyObject_CallMethodNoArgs((PyObject *)self, |
1527 | 0 | &_Py_ID(readline)); |
1528 | 0 | if (line && !PyBytes_Check(line)) { |
1529 | 0 | PyErr_Format(PyExc_OSError, |
1530 | 0 | "readline() should have returned a bytes object, " |
1531 | 0 | "not '%.200s'", Py_TYPE(line)->tp_name); |
1532 | 0 | Py_DECREF(line); |
1533 | 0 | return NULL; |
1534 | 0 | } |
1535 | 0 | } |
1536 | | |
1537 | 0 | if (line == NULL) |
1538 | 0 | return NULL; |
1539 | | |
1540 | 0 | if (PyBytes_GET_SIZE(line) == 0) { |
1541 | | /* Reached EOF or would have blocked */ |
1542 | 0 | Py_DECREF(line); |
1543 | 0 | return NULL; |
1544 | 0 | } |
1545 | | |
1546 | 0 | return line; |
1547 | 0 | } |
1548 | | |
1549 | | static PyObject * |
1550 | | buffered_repr(PyObject *op) |
1551 | 0 | { |
1552 | 0 | buffered *self = buffered_CAST(op); |
1553 | 0 | PyObject *nameobj, *res; |
1554 | |
|
1555 | 0 | if (PyObject_GetOptionalAttr((PyObject *) self, &_Py_ID(name), &nameobj) < 0) { |
1556 | 0 | if (!PyErr_ExceptionMatches(PyExc_ValueError)) { |
1557 | 0 | return NULL; |
1558 | 0 | } |
1559 | | /* Ignore ValueError raised if the underlying stream was detached */ |
1560 | 0 | PyErr_Clear(); |
1561 | 0 | } |
1562 | 0 | if (nameobj == NULL) { |
1563 | 0 | res = PyUnicode_FromFormat("<%s>", Py_TYPE(self)->tp_name); |
1564 | 0 | } |
1565 | 0 | else { |
1566 | 0 | int status = Py_ReprEnter((PyObject *)self); |
1567 | 0 | res = NULL; |
1568 | 0 | if (status == 0) { |
1569 | 0 | res = PyUnicode_FromFormat("<%s name=%R>", |
1570 | 0 | Py_TYPE(self)->tp_name, nameobj); |
1571 | 0 | Py_ReprLeave((PyObject *)self); |
1572 | 0 | } |
1573 | 0 | else if (status > 0) { |
1574 | 0 | PyErr_Format(PyExc_RuntimeError, |
1575 | 0 | "reentrant call inside %s.__repr__", |
1576 | 0 | Py_TYPE(self)->tp_name); |
1577 | 0 | } |
1578 | 0 | Py_DECREF(nameobj); |
1579 | 0 | } |
1580 | 0 | return res; |
1581 | 0 | } |
1582 | | |
1583 | | /* |
1584 | | * class BufferedReader |
1585 | | */ |
1586 | | |
1587 | | static void _bufferedreader_reset_buf(buffered *self) |
1588 | 776 | { |
1589 | 776 | self->read_end = -1; |
1590 | 776 | } |
1591 | | |
1592 | | /*[clinic input] |
1593 | | _io.BufferedReader.__init__ |
1594 | | raw: object |
1595 | | buffer_size: Py_ssize_t(c_default="DEFAULT_BUFFER_SIZE") = DEFAULT_BUFFER_SIZE |
1596 | | |
1597 | | Create a new buffered reader using the given readable raw IO object. |
1598 | | [clinic start generated code]*/ |
1599 | | |
1600 | | static int |
1601 | | _io_BufferedReader___init___impl(buffered *self, PyObject *raw, |
1602 | | Py_ssize_t buffer_size) |
1603 | | /*[clinic end generated code: output=cddcfefa0ed294c4 input=fb887e06f11b4e48]*/ |
1604 | 399 | { |
1605 | 399 | self->ok = 0; |
1606 | 399 | self->detached = 0; |
1607 | | |
1608 | 399 | _PyIO_State *state = find_io_state_by_def(Py_TYPE(self)); |
1609 | 399 | if (_PyIOBase_check_readable(state, raw, Py_True) == NULL) { |
1610 | 0 | return -1; |
1611 | 0 | } |
1612 | | |
1613 | 399 | Py_XSETREF(self->raw, Py_NewRef(raw)); |
1614 | 399 | self->buffer_size = buffer_size; |
1615 | 399 | self->readable = 1; |
1616 | 399 | self->writable = 0; |
1617 | | |
1618 | 399 | if (_buffered_init(self) < 0) |
1619 | 0 | return -1; |
1620 | 399 | _bufferedreader_reset_buf(self); |
1621 | | |
1622 | 399 | self->fast_closed_checks = ( |
1623 | 399 | Py_IS_TYPE(self, state->PyBufferedReader_Type) && |
1624 | 399 | Py_IS_TYPE(raw, state->PyFileIO_Type) |
1625 | 399 | ); |
1626 | | |
1627 | 399 | self->ok = 1; |
1628 | 399 | return 0; |
1629 | 399 | } |
1630 | | |
1631 | | static Py_ssize_t |
1632 | | _bufferedreader_raw_read(buffered *self, char *start, Py_ssize_t len) |
1633 | 0 | { |
1634 | 0 | Py_buffer buf; |
1635 | 0 | PyObject *memobj, *res; |
1636 | 0 | Py_ssize_t n; |
1637 | | /* NOTE: the buffer needn't be released as its object is NULL. */ |
1638 | 0 | if (PyBuffer_FillInfo(&buf, NULL, start, len, 0, PyBUF_CONTIG) == -1) |
1639 | 0 | return -1; |
1640 | 0 | memobj = PyMemoryView_FromBuffer(&buf); |
1641 | 0 | if (memobj == NULL) |
1642 | 0 | return -1; |
1643 | | /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() when EINTR |
1644 | | occurs so we needn't do it ourselves. |
1645 | | We then retry reading, ignoring the signal if no handler has |
1646 | | raised (see issue #10956). |
1647 | | */ |
1648 | 0 | do { |
1649 | 0 | res = PyObject_CallMethodOneArg(self->raw, &_Py_ID(readinto), memobj); |
1650 | 0 | } while (res == NULL && _PyIO_trap_eintr()); |
1651 | 0 | Py_DECREF(memobj); |
1652 | 0 | if (res == NULL) |
1653 | 0 | return -1; |
1654 | 0 | if (res == Py_None) { |
1655 | | /* Non-blocking stream would have blocked. Special return code! */ |
1656 | 0 | Py_DECREF(res); |
1657 | 0 | return -2; |
1658 | 0 | } |
1659 | 0 | n = PyNumber_AsSsize_t(res, PyExc_ValueError); |
1660 | 0 | Py_DECREF(res); |
1661 | |
|
1662 | 0 | if (n == -1 && PyErr_Occurred()) { |
1663 | 0 | _PyErr_FormatFromCause( |
1664 | 0 | PyExc_OSError, |
1665 | 0 | "raw readinto() failed" |
1666 | 0 | ); |
1667 | 0 | return -1; |
1668 | 0 | } |
1669 | | |
1670 | 0 | if (n < 0 || n > len) { |
1671 | 0 | PyErr_Format(PyExc_OSError, |
1672 | 0 | "raw readinto() returned invalid length %zd " |
1673 | 0 | "(should have been between 0 and %zd)", n, len); |
1674 | 0 | return -1; |
1675 | 0 | } |
1676 | 0 | if (n > 0 && self->abs_pos != -1) |
1677 | 0 | self->abs_pos += n; |
1678 | 0 | return n; |
1679 | 0 | } |
1680 | | |
1681 | | static Py_ssize_t |
1682 | | _bufferedreader_fill_buffer(buffered *self) |
1683 | 0 | { |
1684 | 0 | Py_ssize_t start, len, n; |
1685 | 0 | if (VALID_READ_BUFFER(self)) |
1686 | 0 | start = Py_SAFE_DOWNCAST(self->read_end, Py_off_t, Py_ssize_t); |
1687 | 0 | else |
1688 | 0 | start = 0; |
1689 | 0 | len = self->buffer_size - start; |
1690 | 0 | n = _bufferedreader_raw_read(self, self->buffer + start, len); |
1691 | 0 | if (n <= 0) |
1692 | 0 | return n; |
1693 | 0 | self->read_end = start + n; |
1694 | 0 | self->raw_pos = start + n; |
1695 | 0 | return n; |
1696 | 0 | } |
1697 | | |
1698 | | static PyObject * |
1699 | | _bufferedreader_read_all(buffered *self) |
1700 | 377 | { |
1701 | 377 | Py_ssize_t current_size; |
1702 | 377 | PyObject *res = NULL, *data = NULL, *tmp = NULL, *chunks = NULL, *readall; |
1703 | | |
1704 | | /* First copy what we have in the current buffer. */ |
1705 | 377 | current_size = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t); |
1706 | 377 | if (current_size) { |
1707 | 0 | data = PyBytes_FromStringAndSize( |
1708 | 0 | self->buffer + self->pos, current_size); |
1709 | 0 | if (data == NULL) |
1710 | 0 | return NULL; |
1711 | 0 | self->pos += current_size; |
1712 | 0 | } |
1713 | | /* We're going past the buffer's bounds, flush it */ |
1714 | 377 | if (self->writable) { |
1715 | 0 | tmp = buffered_flush_and_rewind_unlocked(self); |
1716 | 0 | if (tmp == NULL) |
1717 | 0 | goto cleanup; |
1718 | 0 | Py_CLEAR(tmp); |
1719 | 0 | } |
1720 | 377 | _bufferedreader_reset_buf(self); |
1721 | | |
1722 | 377 | if (PyObject_GetOptionalAttr(self->raw, &_Py_ID(readall), &readall) < 0) { |
1723 | 0 | goto cleanup; |
1724 | 0 | } |
1725 | 377 | if (readall) { |
1726 | 377 | tmp = _PyObject_CallNoArgs(readall); |
1727 | 377 | Py_DECREF(readall); |
1728 | 377 | if (tmp == NULL) |
1729 | 0 | goto cleanup; |
1730 | 377 | if (tmp != Py_None && !PyBytes_Check(tmp)) { |
1731 | 0 | PyErr_SetString(PyExc_TypeError, "readall() should return bytes"); |
1732 | 0 | goto cleanup; |
1733 | 0 | } |
1734 | 377 | if (current_size == 0) { |
1735 | 377 | res = tmp; |
1736 | 377 | } else { |
1737 | 0 | if (tmp != Py_None) { |
1738 | 0 | PyBytes_Concat(&data, tmp); |
1739 | 0 | } |
1740 | 0 | res = data; |
1741 | 0 | } |
1742 | 377 | goto cleanup; |
1743 | 377 | } |
1744 | | |
1745 | 0 | chunks = PyList_New(0); |
1746 | 0 | if (chunks == NULL) |
1747 | 0 | goto cleanup; |
1748 | | |
1749 | 0 | while (1) { |
1750 | 0 | if (data) { |
1751 | 0 | if (PyList_Append(chunks, data) < 0) |
1752 | 0 | goto cleanup; |
1753 | 0 | Py_CLEAR(data); |
1754 | 0 | } |
1755 | | |
1756 | | /* Read until EOF or until read() would block. */ |
1757 | 0 | data = PyObject_CallMethodNoArgs(self->raw, &_Py_ID(read)); |
1758 | 0 | if (data == NULL) |
1759 | 0 | goto cleanup; |
1760 | 0 | if (data != Py_None && !PyBytes_Check(data)) { |
1761 | 0 | PyErr_SetString(PyExc_TypeError, "read() should return bytes"); |
1762 | 0 | goto cleanup; |
1763 | 0 | } |
1764 | 0 | if (data == Py_None || PyBytes_GET_SIZE(data) == 0) { |
1765 | 0 | if (current_size == 0) { |
1766 | 0 | res = data; |
1767 | 0 | goto cleanup; |
1768 | 0 | } |
1769 | 0 | else { |
1770 | 0 | tmp = PyBytes_Join((PyObject *)&_Py_SINGLETON(bytes_empty), chunks); |
1771 | 0 | res = tmp; |
1772 | 0 | goto cleanup; |
1773 | 0 | } |
1774 | 0 | } |
1775 | 0 | current_size += PyBytes_GET_SIZE(data); |
1776 | 0 | if (self->abs_pos != -1) |
1777 | 0 | self->abs_pos += PyBytes_GET_SIZE(data); |
1778 | 0 | } |
1779 | 377 | cleanup: |
1780 | | /* res is either NULL or a borrowed ref */ |
1781 | 377 | Py_XINCREF(res); |
1782 | 377 | Py_XDECREF(data); |
1783 | 377 | Py_XDECREF(tmp); |
1784 | 377 | Py_XDECREF(chunks); |
1785 | 377 | return res; |
1786 | 0 | } |
1787 | | |
1788 | | /* Read n bytes from the buffer if it can, otherwise return None. |
1789 | | This function is simple enough that it can run unlocked. */ |
1790 | | static PyObject * |
1791 | | _bufferedreader_read_fast(buffered *self, Py_ssize_t n) |
1792 | 0 | { |
1793 | 0 | Py_ssize_t current_size; |
1794 | |
|
1795 | 0 | current_size = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t); |
1796 | 0 | if (n <= current_size) { |
1797 | | /* Fast path: the data to read is fully buffered. */ |
1798 | 0 | PyObject *res = PyBytes_FromStringAndSize(self->buffer + self->pos, n); |
1799 | 0 | if (res != NULL) |
1800 | 0 | self->pos += n; |
1801 | 0 | return res; |
1802 | 0 | } |
1803 | 0 | Py_RETURN_NONE; |
1804 | 0 | } |
1805 | | |
1806 | | /* Generic read function: read from the stream until enough bytes are read, |
1807 | | * or until an EOF occurs or until read() would block. |
1808 | | */ |
1809 | | static PyObject * |
1810 | | _bufferedreader_read_generic(buffered *self, Py_ssize_t n) |
1811 | 0 | { |
1812 | 0 | Py_ssize_t current_size, remaining, written; |
1813 | |
|
1814 | 0 | current_size = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t); |
1815 | 0 | if (n <= current_size) |
1816 | 0 | return _bufferedreader_read_fast(self, n); |
1817 | | |
1818 | 0 | PyBytesWriter *writer = PyBytesWriter_Create(n); |
1819 | 0 | if (writer == NULL) { |
1820 | 0 | goto error; |
1821 | 0 | } |
1822 | 0 | char *out = PyBytesWriter_GetData(writer); |
1823 | |
|
1824 | 0 | remaining = n; |
1825 | 0 | written = 0; |
1826 | 0 | if (current_size > 0) { |
1827 | 0 | memcpy(out, self->buffer + self->pos, current_size); |
1828 | 0 | remaining -= current_size; |
1829 | 0 | written += current_size; |
1830 | 0 | self->pos += current_size; |
1831 | 0 | } |
1832 | | /* Flush the write buffer if necessary */ |
1833 | 0 | if (self->writable) { |
1834 | 0 | PyObject *r = buffered_flush_and_rewind_unlocked(self); |
1835 | 0 | if (r == NULL) |
1836 | 0 | goto error; |
1837 | 0 | Py_DECREF(r); |
1838 | 0 | } |
1839 | 0 | _bufferedreader_reset_buf(self); |
1840 | 0 | while (remaining > 0) { |
1841 | | /* We want to read a whole block at the end into buffer. |
1842 | | If we had readv() we could do this in one pass. */ |
1843 | 0 | Py_ssize_t r = MINUS_LAST_BLOCK(self, remaining); |
1844 | 0 | if (r == 0) |
1845 | 0 | break; |
1846 | 0 | r = _bufferedreader_raw_read(self, out + written, r); |
1847 | 0 | if (r == -1) |
1848 | 0 | goto error; |
1849 | 0 | if (r == 0 || r == -2) { |
1850 | | /* EOF occurred or read() would block. */ |
1851 | 0 | if (r == 0 || written > 0) { |
1852 | 0 | return PyBytesWriter_FinishWithSize(writer, written); |
1853 | 0 | } |
1854 | 0 | PyBytesWriter_Discard(writer); |
1855 | 0 | Py_RETURN_NONE; |
1856 | 0 | } |
1857 | 0 | remaining -= r; |
1858 | 0 | written += r; |
1859 | 0 | } |
1860 | 0 | assert(remaining <= self->buffer_size); |
1861 | 0 | self->pos = 0; |
1862 | 0 | self->raw_pos = 0; |
1863 | 0 | self->read_end = 0; |
1864 | | /* NOTE: when the read is satisfied, we avoid issuing any additional |
1865 | | reads, which could block indefinitely (e.g. on a socket). |
1866 | | See issue #9550. */ |
1867 | 0 | while (remaining > 0 && self->read_end < self->buffer_size) { |
1868 | 0 | Py_ssize_t r = _bufferedreader_fill_buffer(self); |
1869 | 0 | if (r == -1) |
1870 | 0 | goto error; |
1871 | 0 | if (r == 0 || r == -2) { |
1872 | | /* EOF occurred or read() would block. */ |
1873 | 0 | if (r == 0 || written > 0) { |
1874 | 0 | return PyBytesWriter_FinishWithSize(writer, written); |
1875 | 0 | } |
1876 | 0 | PyBytesWriter_Discard(writer); |
1877 | 0 | Py_RETURN_NONE; |
1878 | 0 | } |
1879 | 0 | if (remaining > r) { |
1880 | 0 | memcpy(out + written, self->buffer + self->pos, r); |
1881 | 0 | written += r; |
1882 | 0 | self->pos += r; |
1883 | 0 | remaining -= r; |
1884 | 0 | } |
1885 | 0 | else if (remaining > 0) { |
1886 | 0 | memcpy(out + written, self->buffer + self->pos, remaining); |
1887 | 0 | written += remaining; |
1888 | 0 | self->pos += remaining; |
1889 | 0 | remaining = 0; |
1890 | 0 | } |
1891 | 0 | if (remaining == 0) |
1892 | 0 | break; |
1893 | 0 | } |
1894 | | |
1895 | 0 | return PyBytesWriter_Finish(writer); |
1896 | | |
1897 | 0 | error: |
1898 | 0 | PyBytesWriter_Discard(writer); |
1899 | 0 | return NULL; |
1900 | 0 | } |
1901 | | |
1902 | | static PyObject * |
1903 | | _bufferedreader_peek_unlocked(buffered *self) |
1904 | 0 | { |
1905 | 0 | Py_ssize_t have, r; |
1906 | |
|
1907 | 0 | have = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t); |
1908 | | /* Constraints: |
1909 | | 1. we don't want to advance the file position. |
1910 | | 2. we don't want to lose block alignment, so we can't shift the buffer |
1911 | | to make some place. |
1912 | | Therefore, we either return `have` bytes (if > 0), or a full buffer. |
1913 | | */ |
1914 | 0 | if (have > 0) { |
1915 | 0 | return PyBytes_FromStringAndSize(self->buffer + self->pos, have); |
1916 | 0 | } |
1917 | | |
1918 | | /* Fill the buffer from the raw stream, and copy it to the result. */ |
1919 | 0 | _bufferedreader_reset_buf(self); |
1920 | 0 | r = _bufferedreader_fill_buffer(self); |
1921 | 0 | if (r == -1) |
1922 | 0 | return NULL; |
1923 | 0 | if (r == -2) |
1924 | 0 | r = 0; |
1925 | 0 | self->pos = 0; |
1926 | 0 | return PyBytes_FromStringAndSize(self->buffer, r); |
1927 | 0 | } |
1928 | | |
1929 | | |
1930 | | /* |
1931 | | * class BufferedWriter |
1932 | | */ |
1933 | | static void |
1934 | | _bufferedwriter_reset_buf(buffered *self) |
1935 | 153k | { |
1936 | 153k | self->write_pos = 0; |
1937 | 153k | self->write_end = -1; |
1938 | 153k | } |
1939 | | |
1940 | | /*[clinic input] |
1941 | | _io.BufferedWriter.__init__ |
1942 | | raw: object |
1943 | | buffer_size: Py_ssize_t(c_default="DEFAULT_BUFFER_SIZE") = DEFAULT_BUFFER_SIZE |
1944 | | |
1945 | | A buffer for a writeable sequential RawIO object. |
1946 | | |
1947 | | The constructor creates a BufferedWriter for the given writeable raw |
1948 | | stream. If the buffer_size is not given, it defaults to |
1949 | | DEFAULT_BUFFER_SIZE. |
1950 | | [clinic start generated code]*/ |
1951 | | |
1952 | | static int |
1953 | | _io_BufferedWriter___init___impl(buffered *self, PyObject *raw, |
1954 | | Py_ssize_t buffer_size) |
1955 | | /*[clinic end generated code: output=c8942a020c0dee64 input=914be9b95e16007b]*/ |
1956 | 44 | { |
1957 | 44 | self->ok = 0; |
1958 | 44 | self->detached = 0; |
1959 | | |
1960 | 44 | _PyIO_State *state = find_io_state_by_def(Py_TYPE(self)); |
1961 | 44 | if (_PyIOBase_check_writable(state, raw, Py_True) == NULL) { |
1962 | 0 | return -1; |
1963 | 0 | } |
1964 | | |
1965 | 44 | Py_INCREF(raw); |
1966 | 44 | Py_XSETREF(self->raw, raw); |
1967 | 44 | self->readable = 0; |
1968 | 44 | self->writable = 1; |
1969 | | |
1970 | 44 | self->buffer_size = buffer_size; |
1971 | 44 | if (_buffered_init(self) < 0) |
1972 | 0 | return -1; |
1973 | 44 | _bufferedwriter_reset_buf(self); |
1974 | 44 | self->pos = 0; |
1975 | | |
1976 | 44 | self->fast_closed_checks = ( |
1977 | 44 | Py_IS_TYPE(self, state->PyBufferedWriter_Type) && |
1978 | 44 | Py_IS_TYPE(raw, state->PyFileIO_Type) |
1979 | 44 | ); |
1980 | | |
1981 | 44 | self->ok = 1; |
1982 | 44 | return 0; |
1983 | 44 | } |
1984 | | |
1985 | | static Py_ssize_t |
1986 | | _bufferedwriter_raw_write(buffered *self, char *start, Py_ssize_t len) |
1987 | 153k | { |
1988 | 153k | Py_buffer buf; |
1989 | 153k | PyObject *memobj, *res; |
1990 | 153k | Py_ssize_t n; |
1991 | 153k | int errnum; |
1992 | | /* NOTE: the buffer needn't be released as its object is NULL. */ |
1993 | 153k | if (PyBuffer_FillInfo(&buf, NULL, start, len, 1, PyBUF_CONTIG_RO) == -1) |
1994 | 0 | return -1; |
1995 | 153k | memobj = PyMemoryView_FromBuffer(&buf); |
1996 | 153k | if (memobj == NULL) |
1997 | 0 | return -1; |
1998 | | /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() when EINTR |
1999 | | occurs so we needn't do it ourselves. |
2000 | | We then retry writing, ignoring the signal if no handler has |
2001 | | raised (see issue #10956). |
2002 | | */ |
2003 | 153k | do { |
2004 | 153k | errno = 0; |
2005 | 153k | res = PyObject_CallMethodOneArg(self->raw, &_Py_ID(write), memobj); |
2006 | 153k | errnum = errno; |
2007 | 153k | } while (res == NULL && _PyIO_trap_eintr()); |
2008 | 153k | Py_DECREF(memobj); |
2009 | 153k | if (res == NULL) |
2010 | 0 | return -1; |
2011 | 153k | if (res == Py_None) { |
2012 | | /* Non-blocking stream would have blocked. Special return code! |
2013 | | Being paranoid we reset errno in case it is changed by code |
2014 | | triggered by a decref. errno is used by _set_BlockingIOError(). */ |
2015 | 0 | Py_DECREF(res); |
2016 | 0 | errno = errnum; |
2017 | 0 | return -2; |
2018 | 0 | } |
2019 | 153k | n = PyNumber_AsSsize_t(res, PyExc_ValueError); |
2020 | 153k | Py_DECREF(res); |
2021 | 153k | if (n < 0 || n > len) { |
2022 | 0 | PyErr_Format(PyExc_OSError, |
2023 | 0 | "raw write() returned invalid length %zd " |
2024 | 0 | "(should have been between 0 and %zd)", n, len); |
2025 | 0 | return -1; |
2026 | 0 | } |
2027 | 153k | if (n > 0 && self->abs_pos != -1) |
2028 | 153k | self->abs_pos += n; |
2029 | 153k | return n; |
2030 | 153k | } |
2031 | | |
2032 | | static PyObject * |
2033 | | _bufferedwriter_flush_unlocked(buffered *self) |
2034 | 153k | { |
2035 | 153k | Py_off_t n, rewind; |
2036 | | |
2037 | 153k | if (!VALID_WRITE_BUFFER(self) || self->write_pos == self->write_end) |
2038 | 0 | goto end; |
2039 | | /* First, rewind */ |
2040 | 153k | rewind = RAW_OFFSET(self) + (self->pos - self->write_pos); |
2041 | 153k | if (rewind != 0) { |
2042 | 0 | n = _buffered_raw_seek(self, -rewind, 1); |
2043 | 0 | if (n < 0) { |
2044 | 0 | goto error; |
2045 | 0 | } |
2046 | 0 | self->raw_pos -= rewind; |
2047 | 0 | } |
2048 | 307k | while (self->write_pos < self->write_end) { |
2049 | 153k | n = _bufferedwriter_raw_write(self, |
2050 | 153k | self->buffer + self->write_pos, |
2051 | 153k | Py_SAFE_DOWNCAST(self->write_end - self->write_pos, |
2052 | 153k | Py_off_t, Py_ssize_t)); |
2053 | 153k | if (n == -1) { |
2054 | 0 | goto error; |
2055 | 0 | } |
2056 | 153k | else if (n == -2) { |
2057 | 0 | _set_BlockingIOError("write could not complete without blocking", |
2058 | 0 | 0); |
2059 | 0 | goto error; |
2060 | 0 | } |
2061 | 153k | self->write_pos += n; |
2062 | 153k | self->raw_pos = self->write_pos; |
2063 | | /* Partial writes can return successfully when interrupted by a |
2064 | | signal (see write(2)). We must run signal handlers before |
2065 | | blocking another time, possibly indefinitely. */ |
2066 | 153k | if (PyErr_CheckSignals() < 0) |
2067 | 0 | goto error; |
2068 | 153k | } |
2069 | | |
2070 | | |
2071 | 153k | end: |
2072 | | /* This ensures that after return from this function, |
2073 | | VALID_WRITE_BUFFER(self) returns false. |
2074 | | |
2075 | | This is a required condition because when a tell() is called |
2076 | | after flushing and if VALID_READ_BUFFER(self) is false, we need |
2077 | | VALID_WRITE_BUFFER(self) to be false to have |
2078 | | RAW_OFFSET(self) == 0. |
2079 | | |
2080 | | Issue: https://bugs.python.org/issue32228 */ |
2081 | 153k | _bufferedwriter_reset_buf(self); |
2082 | 153k | Py_RETURN_NONE; |
2083 | | |
2084 | 0 | error: |
2085 | 0 | return NULL; |
2086 | 153k | } |
2087 | | |
2088 | | /*[clinic input] |
2089 | | @critical_section |
2090 | | _io.BufferedWriter.write |
2091 | | buffer: Py_buffer |
2092 | | / |
2093 | | [clinic start generated code]*/ |
2094 | | |
2095 | | static PyObject * |
2096 | | _io_BufferedWriter_write_impl(buffered *self, Py_buffer *buffer) |
2097 | | /*[clinic end generated code: output=7f8d1365759bfc6b input=6a9c041de0c337be]*/ |
2098 | 153k | { |
2099 | 153k | PyObject *res = NULL; |
2100 | 153k | Py_ssize_t written, avail, remaining; |
2101 | 153k | Py_off_t offset; |
2102 | 153k | int r; |
2103 | | |
2104 | 153k | CHECK_INITIALIZED(self) |
2105 | | |
2106 | 153k | if (!ENTER_BUFFERED(self)) |
2107 | 0 | return NULL; |
2108 | | |
2109 | | /* Issue #31976: Check for closed file after acquiring the lock. Another |
2110 | | thread could be holding the lock while closing the file. */ |
2111 | 153k | r = IS_CLOSED(self); |
2112 | 153k | if (r < 0) { |
2113 | 0 | goto error; |
2114 | 0 | } |
2115 | 153k | if (r > 0) { |
2116 | 0 | PyErr_SetString(PyExc_ValueError, "write to closed file"); |
2117 | 0 | goto error; |
2118 | 0 | } |
2119 | | |
2120 | | /* Fast path: the data to write can be fully buffered. */ |
2121 | 153k | if (!VALID_READ_BUFFER(self) && !VALID_WRITE_BUFFER(self)) { |
2122 | 153k | self->pos = 0; |
2123 | 153k | self->raw_pos = 0; |
2124 | 153k | } |
2125 | 153k | avail = Py_SAFE_DOWNCAST(self->buffer_size - self->pos, Py_off_t, Py_ssize_t); |
2126 | 153k | if (buffer->len <= avail && buffer->len < self->buffer_size) { |
2127 | 153k | memcpy(self->buffer + self->pos, buffer->buf, buffer->len); |
2128 | 153k | if (!VALID_WRITE_BUFFER(self) || self->write_pos > self->pos) { |
2129 | 153k | self->write_pos = self->pos; |
2130 | 153k | } |
2131 | 153k | ADJUST_POSITION(self, self->pos + buffer->len); |
2132 | 153k | if (self->pos > self->write_end) |
2133 | 153k | self->write_end = self->pos; |
2134 | 153k | written = buffer->len; |
2135 | 153k | goto end; |
2136 | 153k | } |
2137 | | |
2138 | | /* First write the current buffer */ |
2139 | 0 | res = _bufferedwriter_flush_unlocked(self); |
2140 | 0 | if (res == NULL) { |
2141 | 0 | Py_ssize_t *w = _buffered_check_blocking_error(); |
2142 | 0 | if (w == NULL) |
2143 | 0 | goto error; |
2144 | 0 | if (self->readable) |
2145 | 0 | _bufferedreader_reset_buf(self); |
2146 | | /* Make some place by shifting the buffer. */ |
2147 | 0 | assert(VALID_WRITE_BUFFER(self)); |
2148 | 0 | memmove(self->buffer, self->buffer + self->write_pos, |
2149 | 0 | Py_SAFE_DOWNCAST(self->write_end - self->write_pos, |
2150 | 0 | Py_off_t, Py_ssize_t)); |
2151 | 0 | self->write_end -= self->write_pos; |
2152 | 0 | self->raw_pos -= self->write_pos; |
2153 | 0 | self->pos -= self->write_pos; |
2154 | 0 | self->write_pos = 0; |
2155 | 0 | avail = Py_SAFE_DOWNCAST(self->buffer_size - self->write_end, |
2156 | 0 | Py_off_t, Py_ssize_t); |
2157 | 0 | if (buffer->len <= avail) { |
2158 | | /* Everything can be buffered */ |
2159 | 0 | PyErr_Clear(); |
2160 | 0 | memcpy(self->buffer + self->write_end, buffer->buf, buffer->len); |
2161 | 0 | self->write_end += buffer->len; |
2162 | 0 | self->pos += buffer->len; |
2163 | 0 | written = buffer->len; |
2164 | 0 | goto end; |
2165 | 0 | } |
2166 | | /* Buffer as much as possible. */ |
2167 | 0 | memcpy(self->buffer + self->write_end, buffer->buf, avail); |
2168 | 0 | self->write_end += avail; |
2169 | 0 | self->pos += avail; |
2170 | | /* XXX Modifying the existing exception e using the pointer w |
2171 | | will change e.characters_written but not e.args[2]. |
2172 | | Therefore we just replace with a new error. */ |
2173 | 0 | _set_BlockingIOError("write could not complete without blocking", |
2174 | 0 | avail); |
2175 | 0 | goto error; |
2176 | 0 | } |
2177 | 0 | Py_CLEAR(res); |
2178 | | |
2179 | | /* Adjust the raw stream position if it is away from the logical stream |
2180 | | position. This happens if the read buffer has been filled but not |
2181 | | modified (and therefore _bufferedwriter_flush_unlocked() didn't rewind |
2182 | | the raw stream by itself). |
2183 | | Fixes issue #6629. |
2184 | | */ |
2185 | 0 | offset = RAW_OFFSET(self); |
2186 | 0 | if (offset != 0) { |
2187 | 0 | if (_buffered_raw_seek(self, -offset, 1) < 0) |
2188 | 0 | goto error; |
2189 | 0 | self->raw_pos -= offset; |
2190 | 0 | } |
2191 | | |
2192 | | /* Then write buf itself. At this point the buffer has been emptied. */ |
2193 | 0 | remaining = buffer->len; |
2194 | 0 | written = 0; |
2195 | 0 | while (remaining >= self->buffer_size) { |
2196 | 0 | Py_ssize_t n = _bufferedwriter_raw_write( |
2197 | 0 | self, (char *) buffer->buf + written, buffer->len - written); |
2198 | 0 | if (n == -1) { |
2199 | 0 | goto error; |
2200 | 0 | } else if (n == -2) { |
2201 | | /* Write failed because raw file is non-blocking */ |
2202 | 0 | if (remaining > self->buffer_size) { |
2203 | | /* Can't buffer everything, still buffer as much as possible */ |
2204 | 0 | memcpy(self->buffer, |
2205 | 0 | (char *) buffer->buf + written, self->buffer_size); |
2206 | 0 | self->raw_pos = 0; |
2207 | 0 | ADJUST_POSITION(self, self->buffer_size); |
2208 | 0 | self->write_end = self->buffer_size; |
2209 | 0 | written += self->buffer_size; |
2210 | 0 | _set_BlockingIOError("write could not complete without " |
2211 | 0 | "blocking", written); |
2212 | 0 | goto error; |
2213 | 0 | } |
2214 | 0 | PyErr_Clear(); |
2215 | 0 | break; |
2216 | 0 | } |
2217 | 0 | written += n; |
2218 | 0 | remaining -= n; |
2219 | | /* Partial writes can return successfully when interrupted by a |
2220 | | signal (see write(2)). We must run signal handlers before |
2221 | | blocking another time, possibly indefinitely. */ |
2222 | 0 | if (PyErr_CheckSignals() < 0) |
2223 | 0 | goto error; |
2224 | 0 | } |
2225 | 0 | if (self->readable) |
2226 | 0 | _bufferedreader_reset_buf(self); |
2227 | 0 | if (remaining > 0) { |
2228 | 0 | memcpy(self->buffer, (char *) buffer->buf + written, remaining); |
2229 | 0 | written += remaining; |
2230 | 0 | } |
2231 | 0 | self->write_pos = 0; |
2232 | | /* TODO: sanity check (remaining >= 0) */ |
2233 | 0 | self->write_end = remaining; |
2234 | 0 | ADJUST_POSITION(self, remaining); |
2235 | 0 | self->raw_pos = 0; |
2236 | |
|
2237 | 153k | end: |
2238 | 153k | res = PyLong_FromSsize_t(written); |
2239 | | |
2240 | 153k | error: |
2241 | 153k | LEAVE_BUFFERED(self) |
2242 | 153k | return res; |
2243 | 153k | } |
2244 | | |
2245 | | |
2246 | | /* |
2247 | | * BufferedRWPair |
2248 | | */ |
2249 | | |
2250 | | /* XXX The usefulness of this (compared to having two separate IO objects) is |
2251 | | * questionable. |
2252 | | */ |
2253 | | |
2254 | | typedef struct { |
2255 | | PyObject_HEAD |
2256 | | buffered *reader; |
2257 | | buffered *writer; |
2258 | | PyObject *dict; |
2259 | | PyObject *weakreflist; |
2260 | | } rwpair; |
2261 | | |
2262 | 0 | #define rwpair_CAST(op) ((rwpair *)(op)) |
2263 | | |
2264 | | /*[clinic input] |
2265 | | _io.BufferedRWPair.__init__ |
2266 | | reader: object |
2267 | | writer: object |
2268 | | buffer_size: Py_ssize_t(c_default="DEFAULT_BUFFER_SIZE") = DEFAULT_BUFFER_SIZE |
2269 | | / |
2270 | | |
2271 | | A buffered reader and writer object together. |
2272 | | |
2273 | | A buffered reader object and buffered writer object put together to |
2274 | | form a sequential IO object that can read and write. This is typically |
2275 | | used with a socket or two-way pipe. |
2276 | | |
2277 | | reader and writer are RawIOBase objects that are readable and |
2278 | | writeable respectively. If the buffer_size is omitted it defaults to |
2279 | | DEFAULT_BUFFER_SIZE. |
2280 | | [clinic start generated code]*/ |
2281 | | |
2282 | | static int |
2283 | | _io_BufferedRWPair___init___impl(rwpair *self, PyObject *reader, |
2284 | | PyObject *writer, Py_ssize_t buffer_size) |
2285 | | /*[clinic end generated code: output=327e73d1aee8f984 input=620d42d71f33a031]*/ |
2286 | 0 | { |
2287 | 0 | _PyIO_State *state = find_io_state_by_def(Py_TYPE(self)); |
2288 | 0 | if (_PyIOBase_check_readable(state, reader, Py_True) == NULL) { |
2289 | 0 | return -1; |
2290 | 0 | } |
2291 | 0 | if (_PyIOBase_check_writable(state, writer, Py_True) == NULL) { |
2292 | 0 | return -1; |
2293 | 0 | } |
2294 | | |
2295 | 0 | self->reader = (buffered *) PyObject_CallFunction( |
2296 | 0 | (PyObject *)state->PyBufferedReader_Type, |
2297 | 0 | "On", reader, buffer_size); |
2298 | 0 | if (self->reader == NULL) |
2299 | 0 | return -1; |
2300 | | |
2301 | 0 | self->writer = (buffered *) PyObject_CallFunction( |
2302 | 0 | (PyObject *)state->PyBufferedWriter_Type, |
2303 | 0 | "On", writer, buffer_size); |
2304 | 0 | if (self->writer == NULL) { |
2305 | 0 | Py_CLEAR(self->reader); |
2306 | 0 | return -1; |
2307 | 0 | } |
2308 | | |
2309 | 0 | return 0; |
2310 | 0 | } |
2311 | | |
2312 | | static int |
2313 | | bufferedrwpair_traverse(PyObject *op, visitproc visit, void *arg) |
2314 | 0 | { |
2315 | 0 | rwpair *self = rwpair_CAST(op); |
2316 | 0 | Py_VISIT(Py_TYPE(self)); |
2317 | 0 | Py_VISIT(self->dict); |
2318 | 0 | Py_VISIT(self->reader); |
2319 | 0 | Py_VISIT(self->writer); |
2320 | 0 | return 0; |
2321 | 0 | } |
2322 | | |
2323 | | static int |
2324 | | bufferedrwpair_clear(PyObject *op) |
2325 | 0 | { |
2326 | 0 | rwpair *self = rwpair_CAST(op); |
2327 | 0 | Py_CLEAR(self->reader); |
2328 | 0 | Py_CLEAR(self->writer); |
2329 | 0 | Py_CLEAR(self->dict); |
2330 | 0 | return 0; |
2331 | 0 | } |
2332 | | |
2333 | | static void |
2334 | | bufferedrwpair_dealloc(PyObject *op) |
2335 | 0 | { |
2336 | 0 | rwpair *self = rwpair_CAST(op); |
2337 | 0 | PyTypeObject *tp = Py_TYPE(self); |
2338 | 0 | _PyObject_GC_UNTRACK(self); |
2339 | 0 | FT_CLEAR_WEAKREFS(op, self->weakreflist); |
2340 | 0 | (void)bufferedrwpair_clear(op); |
2341 | 0 | tp->tp_free(self); |
2342 | 0 | Py_DECREF(tp); |
2343 | 0 | } |
2344 | | |
2345 | | /* Call the method of the underlying reader or writer. The argument is |
2346 | | only passed if it is not NULL, so that the default of that method is |
2347 | | used otherwise. */ |
2348 | | static PyObject * |
2349 | | _forward_call(buffered *self, PyObject *name, PyObject *arg) |
2350 | 0 | { |
2351 | 0 | if (self == NULL) { |
2352 | 0 | PyErr_SetString(PyExc_ValueError, |
2353 | 0 | "I/O operation on uninitialized object"); |
2354 | 0 | return NULL; |
2355 | 0 | } |
2356 | | |
2357 | 0 | if (arg == NULL) { |
2358 | 0 | return PyObject_CallMethodNoArgs((PyObject *)self, name); |
2359 | 0 | } |
2360 | 0 | return PyObject_CallMethodOneArg((PyObject *)self, name, arg); |
2361 | 0 | } |
2362 | | |
2363 | | /*[clinic input] |
2364 | | _io.BufferedRWPair.read |
2365 | | size: object(c_default="NULL") = -1 |
2366 | | / |
2367 | | [clinic start generated code]*/ |
2368 | | |
2369 | | static PyObject * |
2370 | | _io_BufferedRWPair_read_impl(rwpair *self, PyObject *size) |
2371 | | /*[clinic end generated code: output=0668e3c5dbd3e93d input=eddb5e52aba9ebe5]*/ |
2372 | 0 | { |
2373 | 0 | return _forward_call(self->reader, &_Py_ID(read), size); |
2374 | 0 | } |
2375 | | |
2376 | | /*[clinic input] |
2377 | | _io.BufferedRWPair.peek |
2378 | | size: object(c_default="NULL") = 0 |
2379 | | / |
2380 | | [clinic start generated code]*/ |
2381 | | |
2382 | | static PyObject * |
2383 | | _io_BufferedRWPair_peek_impl(rwpair *self, PyObject *size) |
2384 | | /*[clinic end generated code: output=190a267bd694efa0 input=36af95964bebe355]*/ |
2385 | 0 | { |
2386 | 0 | return _forward_call(self->reader, &_Py_ID(peek), size); |
2387 | 0 | } |
2388 | | |
2389 | | /*[clinic input] |
2390 | | _io.BufferedRWPair.read1 |
2391 | | size: object(c_default="NULL") = -1 |
2392 | | / |
2393 | | [clinic start generated code]*/ |
2394 | | |
2395 | | static PyObject * |
2396 | | _io_BufferedRWPair_read1_impl(rwpair *self, PyObject *size) |
2397 | | /*[clinic end generated code: output=17ec19608f2bb825 input=9e94db423e490b58]*/ |
2398 | 0 | { |
2399 | 0 | return _forward_call(self->reader, &_Py_ID(read1), size); |
2400 | 0 | } |
2401 | | |
2402 | | /*[clinic input] |
2403 | | _io.BufferedRWPair.readline |
2404 | | size: object(c_default="NULL") = -1 |
2405 | | / |
2406 | | |
2407 | | Read and return a line from the stream. |
2408 | | |
2409 | | If size is specified, at most size bytes will be read. |
2410 | | |
2411 | | The line terminator is always b'\n'. |
2412 | | [clinic start generated code]*/ |
2413 | | |
2414 | | static PyObject * |
2415 | | _io_BufferedRWPair_readline_impl(rwpair *self, PyObject *size) |
2416 | | /*[clinic end generated code: output=ee5bb2364231e6e1 input=1c552272a69db1ad]*/ |
2417 | 0 | { |
2418 | 0 | return _forward_call(self->reader, &_Py_ID(readline), size); |
2419 | 0 | } |
2420 | | |
2421 | | /*[clinic input] |
2422 | | _io.BufferedRWPair.readinto |
2423 | | buffer: object |
2424 | | / |
2425 | | [clinic start generated code]*/ |
2426 | | |
2427 | | static PyObject * |
2428 | | _io_BufferedRWPair_readinto_impl(rwpair *self, PyObject *buffer) |
2429 | | /*[clinic end generated code: output=16c86b071015f7a4 input=ccd86ce2666261f7]*/ |
2430 | 0 | { |
2431 | 0 | return _forward_call(self->reader, &_Py_ID(readinto), buffer); |
2432 | 0 | } |
2433 | | |
2434 | | /*[clinic input] |
2435 | | _io.BufferedRWPair.readinto1 |
2436 | | buffer: object |
2437 | | / |
2438 | | [clinic start generated code]*/ |
2439 | | |
2440 | | static PyObject * |
2441 | | _io_BufferedRWPair_readinto1_impl(rwpair *self, PyObject *buffer) |
2442 | | /*[clinic end generated code: output=f1577b6f54c2b02a input=613d9bf127f88a4a]*/ |
2443 | 0 | { |
2444 | 0 | return _forward_call(self->reader, &_Py_ID(readinto1), buffer); |
2445 | 0 | } |
2446 | | |
2447 | | /*[clinic input] |
2448 | | _io.BufferedRWPair.write |
2449 | | buffer: object |
2450 | | / |
2451 | | [clinic start generated code]*/ |
2452 | | |
2453 | | static PyObject * |
2454 | | _io_BufferedRWPair_write_impl(rwpair *self, PyObject *buffer) |
2455 | | /*[clinic end generated code: output=6f7509a747410c68 input=66c602422e3ec36f]*/ |
2456 | 0 | { |
2457 | 0 | return _forward_call(self->writer, &_Py_ID(write), buffer); |
2458 | 0 | } |
2459 | | |
2460 | | /*[clinic input] |
2461 | | _io.BufferedRWPair.flush |
2462 | | [clinic start generated code]*/ |
2463 | | |
2464 | | static PyObject * |
2465 | | _io_BufferedRWPair_flush_impl(rwpair *self) |
2466 | | /*[clinic end generated code: output=0b2dcbe828718d6b input=e853da796ee61df1]*/ |
2467 | 0 | { |
2468 | 0 | return _forward_call(self->writer, &_Py_ID(flush), NULL); |
2469 | 0 | } |
2470 | | |
2471 | | /*[clinic input] |
2472 | | _io.BufferedRWPair.readable |
2473 | | [clinic start generated code]*/ |
2474 | | |
2475 | | static PyObject * |
2476 | | _io_BufferedRWPair_readable_impl(rwpair *self) |
2477 | | /*[clinic end generated code: output=615967d4aa58f122 input=0475ed73d0a3167f]*/ |
2478 | 0 | { |
2479 | 0 | return _forward_call(self->reader, &_Py_ID(readable), NULL); |
2480 | 0 | } |
2481 | | |
2482 | | /*[clinic input] |
2483 | | _io.BufferedRWPair.writable |
2484 | | [clinic start generated code]*/ |
2485 | | |
2486 | | static PyObject * |
2487 | | _io_BufferedRWPair_writable_impl(rwpair *self) |
2488 | | /*[clinic end generated code: output=c5a43c84e0195c11 input=3cfd44fb4757082f]*/ |
2489 | 0 | { |
2490 | 0 | return _forward_call(self->writer, &_Py_ID(writable), NULL); |
2491 | 0 | } |
2492 | | |
2493 | | /*[clinic input] |
2494 | | _io.BufferedRWPair.close |
2495 | | [clinic start generated code]*/ |
2496 | | |
2497 | | static PyObject * |
2498 | | _io_BufferedRWPair_close_impl(rwpair *self) |
2499 | | /*[clinic end generated code: output=5924ba5ecc78752a input=4087d69f2d8fc368]*/ |
2500 | 0 | { |
2501 | 0 | PyObject *exc = NULL; |
2502 | 0 | PyObject *ret = _forward_call(self->writer, &_Py_ID(close), NULL); |
2503 | 0 | if (ret == NULL) { |
2504 | 0 | exc = PyErr_GetRaisedException(); |
2505 | 0 | } |
2506 | 0 | else { |
2507 | 0 | Py_DECREF(ret); |
2508 | 0 | } |
2509 | 0 | ret = _forward_call(self->reader, &_Py_ID(close), NULL); |
2510 | 0 | if (exc != NULL) { |
2511 | 0 | _PyErr_ChainExceptions1(exc); |
2512 | 0 | Py_CLEAR(ret); |
2513 | 0 | } |
2514 | 0 | return ret; |
2515 | 0 | } |
2516 | | |
2517 | | /*[clinic input] |
2518 | | _io.BufferedRWPair.isatty |
2519 | | [clinic start generated code]*/ |
2520 | | |
2521 | | static PyObject * |
2522 | | _io_BufferedRWPair_isatty_impl(rwpair *self) |
2523 | | /*[clinic end generated code: output=d017c621ed879cb7 input=92833e3d60586e14]*/ |
2524 | 0 | { |
2525 | 0 | PyObject *ret = _forward_call(self->writer, &_Py_ID(isatty), NULL); |
2526 | |
|
2527 | 0 | if (ret != Py_False) { |
2528 | | /* either True or exception */ |
2529 | 0 | return ret; |
2530 | 0 | } |
2531 | 0 | Py_DECREF(ret); |
2532 | |
|
2533 | 0 | return _forward_call(self->reader, &_Py_ID(isatty), NULL); |
2534 | 0 | } |
2535 | | |
2536 | | /*[clinic input] |
2537 | | @getter |
2538 | | _io.BufferedRWPair.closed |
2539 | | [clinic start generated code]*/ |
2540 | | |
2541 | | static PyObject * |
2542 | | _io_BufferedRWPair_closed_get_impl(rwpair *self) |
2543 | | /*[clinic end generated code: output=4117400c74766f21 input=8248430ac54e5b25]*/ |
2544 | 0 | { |
2545 | 0 | if (self->writer == NULL) { |
2546 | 0 | PyErr_SetString(PyExc_RuntimeError, |
2547 | 0 | "the BufferedRWPair object is being garbage-collected"); |
2548 | 0 | return NULL; |
2549 | 0 | } |
2550 | 0 | return PyObject_GetAttr((PyObject *) self->writer, &_Py_ID(closed)); |
2551 | 0 | } |
2552 | | |
2553 | | |
2554 | | /* |
2555 | | * BufferedRandom |
2556 | | */ |
2557 | | |
2558 | | /*[clinic input] |
2559 | | _io.BufferedRandom.__init__ |
2560 | | raw: object |
2561 | | buffer_size: Py_ssize_t(c_default="DEFAULT_BUFFER_SIZE") = DEFAULT_BUFFER_SIZE |
2562 | | |
2563 | | A buffered interface to random access streams. |
2564 | | |
2565 | | The constructor creates a reader and writer for a seekable stream, |
2566 | | raw, given in the first argument. If the buffer_size is omitted it |
2567 | | defaults to DEFAULT_BUFFER_SIZE. |
2568 | | [clinic start generated code]*/ |
2569 | | |
2570 | | static int |
2571 | | _io_BufferedRandom___init___impl(buffered *self, PyObject *raw, |
2572 | | Py_ssize_t buffer_size) |
2573 | | /*[clinic end generated code: output=d3d64eb0f64e64a3 input=a4e818fb86d0e50c]*/ |
2574 | 0 | { |
2575 | 0 | self->ok = 0; |
2576 | 0 | self->detached = 0; |
2577 | |
|
2578 | 0 | _PyIO_State *state = find_io_state_by_def(Py_TYPE(self)); |
2579 | 0 | if (_PyIOBase_check_seekable(state, raw, Py_True) == NULL) { |
2580 | 0 | return -1; |
2581 | 0 | } |
2582 | 0 | if (_PyIOBase_check_readable(state, raw, Py_True) == NULL) { |
2583 | 0 | return -1; |
2584 | 0 | } |
2585 | 0 | if (_PyIOBase_check_writable(state, raw, Py_True) == NULL) { |
2586 | 0 | return -1; |
2587 | 0 | } |
2588 | | |
2589 | 0 | Py_INCREF(raw); |
2590 | 0 | Py_XSETREF(self->raw, raw); |
2591 | 0 | self->buffer_size = buffer_size; |
2592 | 0 | self->readable = 1; |
2593 | 0 | self->writable = 1; |
2594 | |
|
2595 | 0 | if (_buffered_init(self) < 0) |
2596 | 0 | return -1; |
2597 | 0 | _bufferedreader_reset_buf(self); |
2598 | 0 | _bufferedwriter_reset_buf(self); |
2599 | 0 | self->pos = 0; |
2600 | |
|
2601 | 0 | self->fast_closed_checks = (Py_IS_TYPE(self, state->PyBufferedRandom_Type) && |
2602 | 0 | Py_IS_TYPE(raw, state->PyFileIO_Type)); |
2603 | |
|
2604 | 0 | self->ok = 1; |
2605 | 0 | return 0; |
2606 | 0 | } |
2607 | | |
2608 | 0 | #define clinic_state() (find_io_state_by_def(Py_TYPE(self))) |
2609 | | #include "clinic/bufferedio.c.h" |
2610 | | #undef clinic_state |
2611 | | |
2612 | | static PyMethodDef bufferediobase_methods[] = { |
2613 | | _IO__BUFFEREDIOBASE_DETACH_METHODDEF |
2614 | | _IO__BUFFEREDIOBASE_READ_METHODDEF |
2615 | | _IO__BUFFEREDIOBASE_READ1_METHODDEF |
2616 | | _IO__BUFFEREDIOBASE_READINTO_METHODDEF |
2617 | | _IO__BUFFEREDIOBASE_READINTO1_METHODDEF |
2618 | | _IO__BUFFEREDIOBASE_WRITE_METHODDEF |
2619 | | {NULL, NULL} |
2620 | | }; |
2621 | | |
2622 | | static PyType_Slot bufferediobase_slots[] = { |
2623 | | {Py_tp_doc, (void *)bufferediobase_doc}, |
2624 | | {Py_tp_methods, bufferediobase_methods}, |
2625 | | {0, NULL}, |
2626 | | }; |
2627 | | |
2628 | | /* Do not set Py_TPFLAGS_HAVE_GC so that tp_traverse and tp_clear are inherited */ |
2629 | | PyType_Spec _Py_bufferediobase_spec = { |
2630 | | .name = "_io._BufferedIOBase", |
2631 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
2632 | | Py_TPFLAGS_IMMUTABLETYPE), |
2633 | | .slots = bufferediobase_slots, |
2634 | | }; |
2635 | | |
2636 | | static PyMethodDef bufferedreader_methods[] = { |
2637 | | /* BufferedIOMixin methods */ |
2638 | | _IO__BUFFERED_DETACH_METHODDEF |
2639 | | _IO__BUFFERED_SIMPLE_FLUSH_METHODDEF |
2640 | | _IO__BUFFERED_CLOSE_METHODDEF |
2641 | | _IO__BUFFERED_SEEKABLE_METHODDEF |
2642 | | _IO__BUFFERED_READABLE_METHODDEF |
2643 | | _IO__BUFFERED_FILENO_METHODDEF |
2644 | | _IO__BUFFERED_ISATTY_METHODDEF |
2645 | | _IO__BUFFERED__DEALLOC_WARN_METHODDEF |
2646 | | |
2647 | | _IO__BUFFERED_READ_METHODDEF |
2648 | | _IO__BUFFERED_PEEK_METHODDEF |
2649 | | _IO__BUFFERED_READ1_METHODDEF |
2650 | | _IO__BUFFERED_READINTO_METHODDEF |
2651 | | _IO__BUFFERED_READINTO1_METHODDEF |
2652 | | _IO__BUFFERED_READLINE_METHODDEF |
2653 | | _IO__BUFFERED_SEEK_METHODDEF |
2654 | | _IO__BUFFERED_TELL_METHODDEF |
2655 | | _IO__BUFFERED_TRUNCATE_METHODDEF |
2656 | | _IO__BUFFERED___SIZEOF___METHODDEF |
2657 | | |
2658 | | {"__getstate__", _PyIOBase_cannot_pickle, METH_NOARGS}, |
2659 | | {NULL, NULL} |
2660 | | }; |
2661 | | |
2662 | | static PyMemberDef bufferedreader_members[] = { |
2663 | | {"raw", _Py_T_OBJECT, offsetof(buffered, raw), Py_READONLY}, |
2664 | | {"_finalizing", Py_T_BOOL, offsetof(buffered, finalizing), 0}, |
2665 | | {"__weaklistoffset__", Py_T_PYSSIZET, offsetof(buffered, weakreflist), Py_READONLY}, |
2666 | | {"__dictoffset__", Py_T_PYSSIZET, offsetof(buffered, dict), Py_READONLY}, |
2667 | | {NULL} |
2668 | | }; |
2669 | | |
2670 | | static PyGetSetDef bufferedreader_getset[] = { |
2671 | | _IO__BUFFERED_CLOSED_GETSETDEF |
2672 | | _IO__BUFFERED_NAME_GETSETDEF |
2673 | | _IO__BUFFERED_MODE_GETSETDEF |
2674 | | {NULL} |
2675 | | }; |
2676 | | |
2677 | | |
2678 | | static PyType_Slot bufferedreader_slots[] = { |
2679 | | {Py_tp_dealloc, buffered_dealloc}, |
2680 | | {Py_tp_repr, buffered_repr}, |
2681 | | {Py_tp_doc, (void *)_io_BufferedReader___init____doc__}, |
2682 | | {Py_tp_traverse, buffered_traverse}, |
2683 | | {Py_tp_clear, buffered_clear}, |
2684 | | {Py_tp_iternext, buffered_iternext}, |
2685 | | {Py_tp_methods, bufferedreader_methods}, |
2686 | | {Py_tp_members, bufferedreader_members}, |
2687 | | {Py_tp_getset, bufferedreader_getset}, |
2688 | | {Py_tp_init, _io_BufferedReader___init__}, |
2689 | | {0, NULL}, |
2690 | | }; |
2691 | | |
2692 | | PyType_Spec _Py_bufferedreader_spec = { |
2693 | | .name = "_io.BufferedReader", |
2694 | | .basicsize = sizeof(buffered), |
2695 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | |
2696 | | Py_TPFLAGS_IMMUTABLETYPE), |
2697 | | .slots = bufferedreader_slots, |
2698 | | }; |
2699 | | |
2700 | | static PyMethodDef bufferedwriter_methods[] = { |
2701 | | /* BufferedIOMixin methods */ |
2702 | | _IO__BUFFERED_CLOSE_METHODDEF |
2703 | | _IO__BUFFERED_DETACH_METHODDEF |
2704 | | _IO__BUFFERED_SEEKABLE_METHODDEF |
2705 | | _IO__BUFFERED_WRITABLE_METHODDEF |
2706 | | _IO__BUFFERED_FILENO_METHODDEF |
2707 | | _IO__BUFFERED_ISATTY_METHODDEF |
2708 | | _IO__BUFFERED__DEALLOC_WARN_METHODDEF |
2709 | | |
2710 | | _IO_BUFFEREDWRITER_WRITE_METHODDEF |
2711 | | _IO__BUFFERED_TRUNCATE_METHODDEF |
2712 | | _IO__BUFFERED_FLUSH_METHODDEF |
2713 | | _IO__BUFFERED_SEEK_METHODDEF |
2714 | | _IO__BUFFERED_TELL_METHODDEF |
2715 | | _IO__BUFFERED___SIZEOF___METHODDEF |
2716 | | |
2717 | | {"__getstate__", _PyIOBase_cannot_pickle, METH_NOARGS}, |
2718 | | {NULL, NULL} |
2719 | | }; |
2720 | | |
2721 | | static PyMemberDef bufferedwriter_members[] = { |
2722 | | {"raw", _Py_T_OBJECT, offsetof(buffered, raw), Py_READONLY}, |
2723 | | {"_finalizing", Py_T_BOOL, offsetof(buffered, finalizing), 0}, |
2724 | | {"__weaklistoffset__", Py_T_PYSSIZET, offsetof(buffered, weakreflist), Py_READONLY}, |
2725 | | {"__dictoffset__", Py_T_PYSSIZET, offsetof(buffered, dict), Py_READONLY}, |
2726 | | {NULL} |
2727 | | }; |
2728 | | |
2729 | | static PyGetSetDef bufferedwriter_getset[] = { |
2730 | | _IO__BUFFERED_CLOSED_GETSETDEF |
2731 | | _IO__BUFFERED_NAME_GETSETDEF |
2732 | | _IO__BUFFERED_MODE_GETSETDEF |
2733 | | {NULL} |
2734 | | }; |
2735 | | |
2736 | | |
2737 | | static PyType_Slot bufferedwriter_slots[] = { |
2738 | | {Py_tp_dealloc, buffered_dealloc}, |
2739 | | {Py_tp_repr, buffered_repr}, |
2740 | | {Py_tp_doc, (void *)_io_BufferedWriter___init____doc__}, |
2741 | | {Py_tp_traverse, buffered_traverse}, |
2742 | | {Py_tp_clear, buffered_clear}, |
2743 | | {Py_tp_methods, bufferedwriter_methods}, |
2744 | | {Py_tp_members, bufferedwriter_members}, |
2745 | | {Py_tp_getset, bufferedwriter_getset}, |
2746 | | {Py_tp_init, _io_BufferedWriter___init__}, |
2747 | | {0, NULL}, |
2748 | | }; |
2749 | | |
2750 | | PyType_Spec _Py_bufferedwriter_spec = { |
2751 | | .name = "_io.BufferedWriter", |
2752 | | .basicsize = sizeof(buffered), |
2753 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | |
2754 | | Py_TPFLAGS_IMMUTABLETYPE), |
2755 | | .slots = bufferedwriter_slots, |
2756 | | }; |
2757 | | |
2758 | | static PyMethodDef bufferedrwpair_methods[] = { |
2759 | | _IO_BUFFEREDRWPAIR_READ_METHODDEF |
2760 | | _IO_BUFFEREDRWPAIR_PEEK_METHODDEF |
2761 | | _IO_BUFFEREDRWPAIR_READ1_METHODDEF |
2762 | | _IO_BUFFEREDRWPAIR_READLINE_METHODDEF |
2763 | | _IO_BUFFEREDRWPAIR_READINTO_METHODDEF |
2764 | | _IO_BUFFEREDRWPAIR_READINTO1_METHODDEF |
2765 | | |
2766 | | _IO_BUFFEREDRWPAIR_WRITE_METHODDEF |
2767 | | _IO_BUFFEREDRWPAIR_FLUSH_METHODDEF |
2768 | | |
2769 | | _IO_BUFFEREDRWPAIR_READABLE_METHODDEF |
2770 | | _IO_BUFFEREDRWPAIR_WRITABLE_METHODDEF |
2771 | | |
2772 | | _IO_BUFFEREDRWPAIR_CLOSE_METHODDEF |
2773 | | _IO_BUFFEREDRWPAIR_ISATTY_METHODDEF |
2774 | | |
2775 | | {NULL, NULL} |
2776 | | }; |
2777 | | |
2778 | | static PyMemberDef bufferedrwpair_members[] = { |
2779 | | {"__weaklistoffset__", Py_T_PYSSIZET, offsetof(rwpair, weakreflist), Py_READONLY}, |
2780 | | {"__dictoffset__", Py_T_PYSSIZET, offsetof(rwpair, dict), Py_READONLY}, |
2781 | | {NULL} |
2782 | | }; |
2783 | | |
2784 | | static PyGetSetDef bufferedrwpair_getset[] = { |
2785 | | _IO_BUFFEREDRWPAIR_CLOSED_GETSETDEF |
2786 | | {NULL} |
2787 | | }; |
2788 | | |
2789 | | static PyType_Slot bufferedrwpair_slots[] = { |
2790 | | {Py_tp_dealloc, bufferedrwpair_dealloc}, |
2791 | | {Py_tp_doc, (void *)_io_BufferedRWPair___init____doc__}, |
2792 | | {Py_tp_traverse, bufferedrwpair_traverse}, |
2793 | | {Py_tp_clear, bufferedrwpair_clear}, |
2794 | | {Py_tp_methods, bufferedrwpair_methods}, |
2795 | | {Py_tp_members, bufferedrwpair_members}, |
2796 | | {Py_tp_getset, bufferedrwpair_getset}, |
2797 | | {Py_tp_init, _io_BufferedRWPair___init__}, |
2798 | | {0, NULL}, |
2799 | | }; |
2800 | | |
2801 | | PyType_Spec _Py_bufferedrwpair_spec = { |
2802 | | .name = "_io.BufferedRWPair", |
2803 | | .basicsize = sizeof(rwpair), |
2804 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | |
2805 | | Py_TPFLAGS_IMMUTABLETYPE), |
2806 | | .slots = bufferedrwpair_slots, |
2807 | | }; |
2808 | | |
2809 | | |
2810 | | static PyMethodDef bufferedrandom_methods[] = { |
2811 | | /* BufferedIOMixin methods */ |
2812 | | _IO__BUFFERED_CLOSE_METHODDEF |
2813 | | _IO__BUFFERED_DETACH_METHODDEF |
2814 | | _IO__BUFFERED_SEEKABLE_METHODDEF |
2815 | | _IO__BUFFERED_READABLE_METHODDEF |
2816 | | _IO__BUFFERED_WRITABLE_METHODDEF |
2817 | | _IO__BUFFERED_FILENO_METHODDEF |
2818 | | _IO__BUFFERED_ISATTY_METHODDEF |
2819 | | _IO__BUFFERED__DEALLOC_WARN_METHODDEF |
2820 | | |
2821 | | _IO__BUFFERED_FLUSH_METHODDEF |
2822 | | |
2823 | | _IO__BUFFERED_SEEK_METHODDEF |
2824 | | _IO__BUFFERED_TELL_METHODDEF |
2825 | | _IO__BUFFERED_TRUNCATE_METHODDEF |
2826 | | _IO__BUFFERED_READ_METHODDEF |
2827 | | _IO__BUFFERED_READ1_METHODDEF |
2828 | | _IO__BUFFERED_READINTO_METHODDEF |
2829 | | _IO__BUFFERED_READINTO1_METHODDEF |
2830 | | _IO__BUFFERED_READLINE_METHODDEF |
2831 | | _IO__BUFFERED_PEEK_METHODDEF |
2832 | | _IO_BUFFEREDWRITER_WRITE_METHODDEF |
2833 | | _IO__BUFFERED___SIZEOF___METHODDEF |
2834 | | |
2835 | | {"__getstate__", _PyIOBase_cannot_pickle, METH_NOARGS}, |
2836 | | {NULL, NULL} |
2837 | | }; |
2838 | | |
2839 | | static PyMemberDef bufferedrandom_members[] = { |
2840 | | {"raw", _Py_T_OBJECT, offsetof(buffered, raw), Py_READONLY}, |
2841 | | {"_finalizing", Py_T_BOOL, offsetof(buffered, finalizing), 0}, |
2842 | | {"__weaklistoffset__", Py_T_PYSSIZET, offsetof(buffered, weakreflist), Py_READONLY}, |
2843 | | {"__dictoffset__", Py_T_PYSSIZET, offsetof(buffered, dict), Py_READONLY}, |
2844 | | {NULL} |
2845 | | }; |
2846 | | |
2847 | | static PyGetSetDef bufferedrandom_getset[] = { |
2848 | | _IO__BUFFERED_CLOSED_GETSETDEF |
2849 | | _IO__BUFFERED_NAME_GETSETDEF |
2850 | | _IO__BUFFERED_MODE_GETSETDEF |
2851 | | {NULL} |
2852 | | }; |
2853 | | |
2854 | | |
2855 | | static PyType_Slot bufferedrandom_slots[] = { |
2856 | | {Py_tp_dealloc, buffered_dealloc}, |
2857 | | {Py_tp_repr, buffered_repr}, |
2858 | | {Py_tp_doc, (void *)_io_BufferedRandom___init____doc__}, |
2859 | | {Py_tp_traverse, buffered_traverse}, |
2860 | | {Py_tp_clear, buffered_clear}, |
2861 | | {Py_tp_iternext, buffered_iternext}, |
2862 | | {Py_tp_methods, bufferedrandom_methods}, |
2863 | | {Py_tp_members, bufferedrandom_members}, |
2864 | | {Py_tp_getset, bufferedrandom_getset}, |
2865 | | {Py_tp_init, _io_BufferedRandom___init__}, |
2866 | | {0, NULL}, |
2867 | | }; |
2868 | | |
2869 | | PyType_Spec _Py_bufferedrandom_spec = { |
2870 | | .name = "_io.BufferedRandom", |
2871 | | .basicsize = sizeof(buffered), |
2872 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | |
2873 | | Py_TPFLAGS_IMMUTABLETYPE), |
2874 | | .slots = bufferedrandom_slots, |
2875 | | }; |