/src/cpython/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_critical_section.h" // _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED() |
13 | | #include "pycore_fileutils.h" // _PyFile_Flush |
14 | | #include "pycore_object.h" // _PyObject_GC_UNTRACK() |
15 | | #include "pycore_pyerrors.h" // _Py_FatalErrorFormat() |
16 | | #include "pycore_pylifecycle.h" // _Py_IsInterpreterFinalizing() |
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 | | } buffered; |
269 | | |
270 | 62.4k | #define buffered_CAST(op) ((buffered *)(op)) |
271 | | |
272 | | /* |
273 | | Implementation notes: |
274 | | |
275 | | * BufferedReader, BufferedWriter and BufferedRandom try to share most |
276 | | methods (this is helped by the members `readable` and `writable`, which |
277 | | are initialized in the respective constructors) |
278 | | * They also share a single buffer for reading and writing. This enables |
279 | | interleaved reads and writes without flushing. It also makes the logic |
280 | | a bit trickier to get right. |
281 | | * The absolute position of the raw stream is cached, if possible, in the |
282 | | `abs_pos` member. It must be updated every time an operation is done |
283 | | on the raw stream. If not sure, it can be reinitialized by calling |
284 | | _buffered_raw_tell(), which queries the raw stream (_buffered_raw_seek() |
285 | | also does it). To read it, use RAW_TELL(). |
286 | | * Three helpers, _bufferedreader_raw_read, _bufferedwriter_raw_write and |
287 | | _bufferedwriter_flush_unlocked do a lot of useful housekeeping. |
288 | | |
289 | | NOTE: we should try to maintain block alignment of reads and writes to the |
290 | | raw stream (according to the buffer size), but for now it is only done |
291 | | in read() and friends. |
292 | | |
293 | | */ |
294 | | |
295 | | /* These macros protect the buffered object against concurrent operations. */ |
296 | | |
297 | | static int |
298 | | _enter_buffered_busy(buffered *self) |
299 | 0 | { |
300 | 0 | int relax_locking; |
301 | 0 | PyLockStatus st; |
302 | 0 | if (self->owner == PyThread_get_thread_ident()) { |
303 | 0 | PyErr_Format(PyExc_RuntimeError, |
304 | 0 | "reentrant call inside %R", self); |
305 | 0 | return 0; |
306 | 0 | } |
307 | 0 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
308 | 0 | relax_locking = _Py_IsInterpreterFinalizing(interp); |
309 | 0 | Py_BEGIN_ALLOW_THREADS |
310 | 0 | if (!relax_locking) |
311 | 0 | st = PyThread_acquire_lock(self->lock, 1); |
312 | 0 | else { |
313 | | /* When finalizing, we don't want a deadlock to happen with daemon |
314 | | * threads abruptly shut down while they owned the lock. |
315 | | * Therefore, only wait for a grace period (1 s.). |
316 | | * Note that non-daemon threads have already exited here, so this |
317 | | * shouldn't affect carefully written threaded I/O code. |
318 | | */ |
319 | 0 | st = PyThread_acquire_lock_timed(self->lock, (PY_TIMEOUT_T)1e6, 0); |
320 | 0 | } |
321 | 0 | Py_END_ALLOW_THREADS |
322 | 0 | if (relax_locking && st != PY_LOCK_ACQUIRED) { |
323 | 0 | PyObject *ascii = PyObject_ASCII((PyObject*)self); |
324 | 0 | _Py_FatalErrorFormat(__func__, |
325 | 0 | "could not acquire lock for %s at interpreter " |
326 | 0 | "shutdown, possibly due to daemon threads", |
327 | 0 | ascii ? PyUnicode_AsUTF8(ascii) : "<ascii(self) failed>"); |
328 | 0 | } |
329 | 0 | return 1; |
330 | 0 | } |
331 | | |
332 | | #define ENTER_BUFFERED(self) \ |
333 | 492k | ( (PyThread_acquire_lock(self->lock, 0) ? \ |
334 | 492k | 1 : _enter_buffered_busy(self)) \ |
335 | 492k | && (self->owner = PyThread_get_thread_ident(), 1) ) |
336 | | |
337 | | #define LEAVE_BUFFERED(self) \ |
338 | 492k | do { \ |
339 | 492k | self->owner = 0; \ |
340 | 492k | PyThread_release_lock(self->lock); \ |
341 | 492k | } while(0); |
342 | | |
343 | | #define CHECK_INITIALIZED(self) \ |
344 | 8.80M | if (self->ok <= 0) { \ |
345 | 0 | if (self->detached) { \ |
346 | 0 | PyErr_SetString(PyExc_ValueError, \ |
347 | 0 | "raw stream has been detached"); \ |
348 | 0 | } else { \ |
349 | 0 | PyErr_SetString(PyExc_ValueError, \ |
350 | 0 | "I/O operation on uninitialized object"); \ |
351 | 0 | } \ |
352 | 0 | return NULL; \ |
353 | 0 | } |
354 | | |
355 | | #define CHECK_INITIALIZED_INT(self) \ |
356 | 6.90M | if (self->ok <= 0) { \ |
357 | 0 | if (self->detached) { \ |
358 | 0 | PyErr_SetString(PyExc_ValueError, \ |
359 | 0 | "raw stream has been detached"); \ |
360 | 0 | } else { \ |
361 | 0 | PyErr_SetString(PyExc_ValueError, \ |
362 | 0 | "I/O operation on uninitialized object"); \ |
363 | 0 | } \ |
364 | 0 | return -1; \ |
365 | 0 | } |
366 | | |
367 | | #define IS_CLOSED(self) \ |
368 | 7.32M | (!self->buffer ? 1 : \ |
369 | 7.32M | (self->fast_closed_checks \ |
370 | 7.32M | ? _PyFileIO_closed(self->raw) \ |
371 | 7.32M | : buffered_closed(self))) |
372 | | |
373 | | #define CHECK_CLOSED(self, error_msg) \ |
374 | 7.09M | do { \ |
375 | 7.09M | int _closed = IS_CLOSED(self); \ |
376 | 7.09M | if (_closed < 0) { \ |
377 | 0 | return NULL; \ |
378 | 0 | } \ |
379 | 7.09M | if (_closed && \ |
380 | 7.09M | (Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t) == 0)) \ |
381 | 7.09M | { \ |
382 | 0 | PyErr_SetString(PyExc_ValueError, error_msg); \ |
383 | 0 | return NULL; \ |
384 | 0 | } \ |
385 | 7.09M | } while (0); |
386 | | |
387 | | #define VALID_READ_BUFFER(self) \ |
388 | 2.67M | (self->readable && self->read_end != -1) |
389 | | |
390 | | #define VALID_WRITE_BUFFER(self) \ |
391 | 1.18M | (self->writable && self->write_end != -1) |
392 | | |
393 | | #define ADJUST_POSITION(self, _new_pos) \ |
394 | 195k | do { \ |
395 | 195k | self->pos = _new_pos; \ |
396 | 195k | if (VALID_READ_BUFFER(self) && self->read_end < self->pos) \ |
397 | 195k | self->read_end = self->pos; \ |
398 | 195k | } while(0) |
399 | | |
400 | | #define READAHEAD(self) \ |
401 | 1.41M | ((self->readable && VALID_READ_BUFFER(self)) \ |
402 | 1.41M | ? (self->read_end - self->pos) : 0) |
403 | | |
404 | | #define RAW_OFFSET(self) \ |
405 | 209k | (((VALID_READ_BUFFER(self) || VALID_WRITE_BUFFER(self)) \ |
406 | 209k | && self->raw_pos >= 0) ? self->raw_pos - self->pos : 0) |
407 | | |
408 | | #define RAW_TELL(self) \ |
409 | 1.41M | (self->abs_pos != -1 ? self->abs_pos : _buffered_raw_tell(self)) |
410 | | |
411 | | #define MINUS_LAST_BLOCK(self, size) \ |
412 | 34.9k | (self->buffer_mask ? \ |
413 | 34.9k | (size & ~self->buffer_mask) : \ |
414 | 34.9k | (self->buffer_size * (size / self->buffer_size))) |
415 | | |
416 | | |
417 | | static int |
418 | | buffered_clear(PyObject *op) |
419 | 28.0k | { |
420 | 28.0k | buffered *self = buffered_CAST(op); |
421 | 28.0k | self->ok = 0; |
422 | 28.0k | Py_CLEAR(self->raw); |
423 | 28.0k | PyObject_ClearManagedDict(op); |
424 | 28.0k | return 0; |
425 | 28.0k | } |
426 | | |
427 | | static void |
428 | | buffered_dealloc(PyObject *op) |
429 | 28.0k | { |
430 | 28.0k | buffered *self = buffered_CAST(op); |
431 | 28.0k | PyTypeObject *tp = Py_TYPE(self); |
432 | 28.0k | self->finalizing = 1; |
433 | 28.0k | if (_PyIOBase_finalize(op) < 0) |
434 | 0 | return; |
435 | 28.0k | _PyObject_GC_UNTRACK(self); |
436 | 28.0k | self->ok = 0; |
437 | 28.0k | PyObject_ClearWeakRefs(op); |
438 | 28.0k | if (self->buffer) { |
439 | 0 | PyMem_Free(self->buffer); |
440 | 0 | self->buffer = NULL; |
441 | 0 | } |
442 | 28.0k | if (self->lock) { |
443 | 28.0k | PyThread_free_lock(self->lock); |
444 | 28.0k | self->lock = NULL; |
445 | 28.0k | } |
446 | 28.0k | (void)buffered_clear(op); |
447 | 28.0k | tp->tp_free(self); |
448 | 28.0k | Py_DECREF(tp); |
449 | 28.0k | } |
450 | | |
451 | | /*[clinic input] |
452 | | @critical_section |
453 | | _io._Buffered.__sizeof__ |
454 | | [clinic start generated code]*/ |
455 | | |
456 | | static PyObject * |
457 | | _io__Buffered___sizeof___impl(buffered *self) |
458 | | /*[clinic end generated code: output=0231ef7f5053134e input=07a32d578073ea64]*/ |
459 | 0 | { |
460 | 0 | size_t res = _PyObject_SIZE(Py_TYPE(self)); |
461 | 0 | if (self->buffer) { |
462 | 0 | res += (size_t)self->buffer_size; |
463 | 0 | } |
464 | 0 | return PyLong_FromSize_t(res); |
465 | 0 | } |
466 | | |
467 | | static int |
468 | | buffered_traverse(PyObject *op, visitproc visit, void *arg) |
469 | 6.37k | { |
470 | 6.37k | buffered *self = buffered_CAST(op); |
471 | 6.37k | Py_VISIT(Py_TYPE(self)); |
472 | 6.37k | Py_VISIT(self->raw); |
473 | 6.37k | return PyObject_VisitManagedDict(op, visit, arg); |
474 | 6.37k | } |
475 | | |
476 | | /* Because this can call arbitrary code, it shouldn't be called when |
477 | | the refcount is 0 (that is, not directly from tp_dealloc unless |
478 | | the refcount has been temporarily re-incremented). */ |
479 | | /*[clinic input] |
480 | | _io._Buffered._dealloc_warn |
481 | | |
482 | | source: object |
483 | | / |
484 | | |
485 | | [clinic start generated code]*/ |
486 | | |
487 | | static PyObject * |
488 | | _io__Buffered__dealloc_warn_impl(buffered *self, PyObject *source) |
489 | | /*[clinic end generated code: output=d8db21c6dec0e614 input=8f845f2a4786391c]*/ |
490 | 5 | { |
491 | 5 | if (self->ok && self->raw) { |
492 | 5 | PyObject *r; |
493 | 5 | r = PyObject_CallMethodOneArg(self->raw, &_Py_ID(_dealloc_warn), source); |
494 | 5 | if (r) |
495 | 0 | Py_DECREF(r); |
496 | 5 | else |
497 | 5 | PyErr_Clear(); |
498 | 5 | } |
499 | 5 | Py_RETURN_NONE; |
500 | 5 | } |
501 | | |
502 | | /* |
503 | | * _BufferedIOMixin methods |
504 | | * This is not a class, just a collection of methods that will be reused |
505 | | * by BufferedReader and BufferedWriter |
506 | | */ |
507 | | |
508 | | /* Flush and close */ |
509 | | /*[clinic input] |
510 | | @critical_section |
511 | | _io._Buffered.flush as _io__Buffered_simple_flush |
512 | | [clinic start generated code]*/ |
513 | | |
514 | | static PyObject * |
515 | | _io__Buffered_simple_flush_impl(buffered *self) |
516 | | /*[clinic end generated code: output=29ebb3820db1bdfd input=5248cb84a65f80bd]*/ |
517 | 1.42M | { |
518 | 1.42M | CHECK_INITIALIZED(self) |
519 | 1.42M | return PyObject_CallMethodNoArgs(self->raw, &_Py_ID(flush)); |
520 | 1.42M | } |
521 | | |
522 | | static int |
523 | | buffered_closed(buffered *self) |
524 | 6.90M | { |
525 | 6.90M | int closed; |
526 | 6.90M | PyObject *res; |
527 | 6.90M | CHECK_INITIALIZED_INT(self) |
528 | 6.90M | res = PyObject_GetAttr(self->raw, &_Py_ID(closed)); |
529 | 6.90M | if (res == NULL) |
530 | 0 | return -1; |
531 | 6.90M | closed = PyObject_IsTrue(res); |
532 | 6.90M | Py_DECREF(res); |
533 | 6.90M | return closed; |
534 | 6.90M | } |
535 | | |
536 | | /*[clinic input] |
537 | | @critical_section |
538 | | @getter |
539 | | _io._Buffered.closed |
540 | | [clinic start generated code]*/ |
541 | | |
542 | | static PyObject * |
543 | | _io__Buffered_closed_get_impl(buffered *self) |
544 | | /*[clinic end generated code: output=f08ce57290703a1a input=18eddefdfe4a3d2f]*/ |
545 | 52.9k | { |
546 | 52.9k | CHECK_INITIALIZED(self) |
547 | 52.9k | return PyObject_GetAttr(self->raw, &_Py_ID(closed)); |
548 | 52.9k | } |
549 | | |
550 | | /*[clinic input] |
551 | | @critical_section |
552 | | _io._Buffered.close |
553 | | [clinic start generated code]*/ |
554 | | |
555 | | static PyObject * |
556 | | _io__Buffered_close_impl(buffered *self) |
557 | | /*[clinic end generated code: output=7280b7b42033be0c input=56d95935b03fd326]*/ |
558 | 28.0k | { |
559 | 28.0k | PyObject *res = NULL; |
560 | 28.0k | int r; |
561 | | |
562 | 28.0k | CHECK_INITIALIZED(self) |
563 | 28.0k | if (!ENTER_BUFFERED(self)) { |
564 | 0 | return NULL; |
565 | 0 | } |
566 | | /* gh-138720: Use IS_CLOSED to match flush CHECK_CLOSED. */ |
567 | 28.0k | r = IS_CLOSED(self); |
568 | 28.0k | if (r < 0) |
569 | 0 | goto end; |
570 | 28.0k | if (r > 0) { |
571 | 0 | res = Py_NewRef(Py_None); |
572 | 0 | goto end; |
573 | 0 | } |
574 | | |
575 | 28.0k | if (self->finalizing) { |
576 | 5 | PyObject *r = _io__Buffered__dealloc_warn_impl(self, (PyObject *)self); |
577 | 5 | if (r) |
578 | 5 | Py_DECREF(r); |
579 | 0 | else |
580 | 0 | PyErr_Clear(); |
581 | 5 | } |
582 | | /* flush() will most probably re-take the lock, so drop it first */ |
583 | 28.0k | LEAVE_BUFFERED(self) |
584 | 28.0k | r = _PyFile_Flush((PyObject *)self); |
585 | 28.0k | if (!ENTER_BUFFERED(self)) { |
586 | 0 | return NULL; |
587 | 0 | } |
588 | 28.0k | PyObject *exc = NULL; |
589 | 28.0k | if (r < 0) { |
590 | 0 | exc = PyErr_GetRaisedException(); |
591 | 0 | } |
592 | | |
593 | 28.0k | res = PyObject_CallMethodNoArgs(self->raw, &_Py_ID(close)); |
594 | | |
595 | 28.0k | if (self->buffer) { |
596 | 28.0k | PyMem_Free(self->buffer); |
597 | 28.0k | self->buffer = NULL; |
598 | 28.0k | } |
599 | | |
600 | 28.0k | if (exc != NULL) { |
601 | 0 | _PyErr_ChainExceptions1(exc); |
602 | 0 | Py_CLEAR(res); |
603 | 0 | } |
604 | | |
605 | 28.0k | self->read_end = 0; |
606 | 28.0k | self->pos = 0; |
607 | | |
608 | 28.0k | end: |
609 | 28.0k | LEAVE_BUFFERED(self) |
610 | 28.0k | return res; |
611 | 28.0k | } |
612 | | |
613 | | /*[clinic input] |
614 | | @critical_section |
615 | | _io._Buffered.detach |
616 | | [clinic start generated code]*/ |
617 | | |
618 | | static PyObject * |
619 | | _io__Buffered_detach_impl(buffered *self) |
620 | | /*[clinic end generated code: output=dd0fc057b8b779f7 input=d4ef1828a678be37]*/ |
621 | 0 | { |
622 | 0 | PyObject *raw; |
623 | 0 | CHECK_INITIALIZED(self) |
624 | 0 | if (_PyFile_Flush((PyObject *)self) < 0) { |
625 | 0 | return NULL; |
626 | 0 | } |
627 | 0 | raw = self->raw; |
628 | 0 | self->raw = NULL; |
629 | 0 | self->detached = 1; |
630 | 0 | self->ok = 0; |
631 | 0 | return raw; |
632 | 0 | } |
633 | | |
634 | | /* Inquiries */ |
635 | | |
636 | | /*[clinic input] |
637 | | @critical_section |
638 | | _io._Buffered.seekable |
639 | | [clinic start generated code]*/ |
640 | | |
641 | | static PyObject * |
642 | | _io__Buffered_seekable_impl(buffered *self) |
643 | | /*[clinic end generated code: output=90172abb5ceb6e8f input=e3a4fc1d297b2fd3]*/ |
644 | 131 | { |
645 | 131 | CHECK_INITIALIZED(self) |
646 | 131 | return PyObject_CallMethodNoArgs(self->raw, &_Py_ID(seekable)); |
647 | 131 | } |
648 | | |
649 | | /*[clinic input] |
650 | | @critical_section |
651 | | _io._Buffered.readable |
652 | | [clinic start generated code]*/ |
653 | | |
654 | | static PyObject * |
655 | | _io__Buffered_readable_impl(buffered *self) |
656 | | /*[clinic end generated code: output=92afa07661ecb698 input=abe54107d59bca9a]*/ |
657 | 55 | { |
658 | 55 | CHECK_INITIALIZED(self) |
659 | 55 | return PyObject_CallMethodNoArgs(self->raw, &_Py_ID(readable)); |
660 | 55 | } |
661 | | |
662 | | /*[clinic input] |
663 | | @critical_section |
664 | | _io._Buffered.writable |
665 | | [clinic start generated code]*/ |
666 | | |
667 | | static PyObject * |
668 | | _io__Buffered_writable_impl(buffered *self) |
669 | | /*[clinic end generated code: output=4e3eee8d6f9d8552 input=45eb76bf6a10e6f7]*/ |
670 | 75 | { |
671 | 75 | CHECK_INITIALIZED(self) |
672 | 75 | return PyObject_CallMethodNoArgs(self->raw, &_Py_ID(writable)); |
673 | 75 | } |
674 | | |
675 | | |
676 | | /*[clinic input] |
677 | | @critical_section |
678 | | @getter |
679 | | _io._Buffered.name |
680 | | [clinic start generated code]*/ |
681 | | |
682 | | static PyObject * |
683 | | _io__Buffered_name_get_impl(buffered *self) |
684 | | /*[clinic end generated code: output=d2adf384051d3d10 input=6b84a0e6126f545e]*/ |
685 | 10 | { |
686 | 10 | CHECK_INITIALIZED(self) |
687 | 10 | return PyObject_GetAttr(self->raw, &_Py_ID(name)); |
688 | 10 | } |
689 | | |
690 | | /*[clinic input] |
691 | | @critical_section |
692 | | @getter |
693 | | _io._Buffered.mode |
694 | | [clinic start generated code]*/ |
695 | | |
696 | | static PyObject * |
697 | | _io__Buffered_mode_get_impl(buffered *self) |
698 | | /*[clinic end generated code: output=0feb205748892fa4 input=0762d5e28542fd8c]*/ |
699 | 3 | { |
700 | 3 | CHECK_INITIALIZED(self) |
701 | 3 | return PyObject_GetAttr(self->raw, &_Py_ID(mode)); |
702 | 3 | } |
703 | | |
704 | | /* Lower-level APIs */ |
705 | | |
706 | | /*[clinic input] |
707 | | @critical_section |
708 | | _io._Buffered.fileno |
709 | | [clinic start generated code]*/ |
710 | | |
711 | | static PyObject * |
712 | | _io__Buffered_fileno_impl(buffered *self) |
713 | | /*[clinic end generated code: output=b717648d58a95ee3 input=1c4fead777bae20a]*/ |
714 | 0 | { |
715 | 0 | CHECK_INITIALIZED(self) |
716 | 0 | return PyObject_CallMethodNoArgs(self->raw, &_Py_ID(fileno)); |
717 | 0 | } |
718 | | |
719 | | /*[clinic input] |
720 | | @critical_section |
721 | | _io._Buffered.isatty |
722 | | [clinic start generated code]*/ |
723 | | |
724 | | static PyObject * |
725 | | _io__Buffered_isatty_impl(buffered *self) |
726 | | /*[clinic end generated code: output=c20e55caae67baea input=e53d182d7e490e3a]*/ |
727 | 0 | { |
728 | 0 | CHECK_INITIALIZED(self) |
729 | 0 | return PyObject_CallMethodNoArgs(self->raw, &_Py_ID(isatty)); |
730 | 0 | } |
731 | | |
732 | | /* Forward decls */ |
733 | | static PyObject * |
734 | | _bufferedwriter_flush_unlocked(buffered *); |
735 | | static Py_ssize_t |
736 | | _bufferedreader_fill_buffer(buffered *self); |
737 | | static void |
738 | | _bufferedreader_reset_buf(buffered *self); |
739 | | static void |
740 | | _bufferedwriter_reset_buf(buffered *self); |
741 | | static PyObject * |
742 | | _bufferedreader_peek_unlocked(buffered *self); |
743 | | static PyObject * |
744 | | _bufferedreader_read_all(buffered *self); |
745 | | static PyObject * |
746 | | _bufferedreader_read_fast(buffered *self, Py_ssize_t); |
747 | | static PyObject * |
748 | | _bufferedreader_read_generic(buffered *self, Py_ssize_t); |
749 | | static Py_ssize_t |
750 | | _bufferedreader_raw_read(buffered *self, char *start, Py_ssize_t len); |
751 | | |
752 | | /* |
753 | | * Helpers |
754 | | */ |
755 | | |
756 | | /* Sets the current error to BlockingIOError */ |
757 | | static void |
758 | | _set_BlockingIOError(const char *msg, Py_ssize_t written) |
759 | 0 | { |
760 | 0 | PyObject *err; |
761 | 0 | PyErr_Clear(); |
762 | 0 | err = PyObject_CallFunction(PyExc_BlockingIOError, "isn", |
763 | 0 | errno, msg, written); |
764 | 0 | if (err) |
765 | 0 | PyErr_SetObject(PyExc_BlockingIOError, err); |
766 | 0 | Py_XDECREF(err); |
767 | 0 | } |
768 | | |
769 | | /* Returns the address of the `written` member if a BlockingIOError was |
770 | | raised, NULL otherwise. The error is always re-raised. */ |
771 | | static Py_ssize_t * |
772 | | _buffered_check_blocking_error(void) |
773 | 0 | { |
774 | 0 | PyObject *exc = PyErr_GetRaisedException(); |
775 | 0 | if (exc == NULL || !PyErr_GivenExceptionMatches(exc, PyExc_BlockingIOError)) { |
776 | 0 | PyErr_SetRaisedException(exc); |
777 | 0 | return NULL; |
778 | 0 | } |
779 | 0 | PyOSErrorObject *err = (PyOSErrorObject *)exc; |
780 | | /* TODO: sanity check (err->written >= 0) */ |
781 | 0 | PyErr_SetRaisedException(exc); |
782 | 0 | return &err->written; |
783 | 0 | } |
784 | | |
785 | | static Py_off_t |
786 | | _buffered_raw_tell(buffered *self) |
787 | 34.6k | { |
788 | 34.6k | Py_off_t n; |
789 | 34.6k | PyObject *res; |
790 | 34.6k | res = PyObject_CallMethodNoArgs(self->raw, &_Py_ID(tell)); |
791 | 34.6k | if (res == NULL) |
792 | 5 | return -1; |
793 | 34.6k | n = PyNumber_AsOff_t(res, PyExc_ValueError); |
794 | 34.6k | Py_DECREF(res); |
795 | 34.6k | if (n < 0) { |
796 | 0 | if (!PyErr_Occurred()) |
797 | 0 | PyErr_Format(PyExc_OSError, |
798 | 0 | "Raw stream returned invalid position %" PY_PRIdOFF, |
799 | 0 | (PY_OFF_T_COMPAT)n); |
800 | 0 | return -1; |
801 | 0 | } |
802 | 34.6k | self->abs_pos = n; |
803 | 34.6k | return n; |
804 | 34.6k | } |
805 | | |
806 | | static Py_off_t |
807 | | _buffered_raw_seek(buffered *self, Py_off_t target, int whence) |
808 | 7.32k | { |
809 | 7.32k | PyObject *res, *posobj, *whenceobj; |
810 | 7.32k | Py_off_t n; |
811 | | |
812 | 7.32k | posobj = PyLong_FromOff_t(target); |
813 | 7.32k | if (posobj == NULL) |
814 | 0 | return -1; |
815 | 7.32k | whenceobj = PyLong_FromLong(whence); |
816 | 7.32k | if (whenceobj == NULL) { |
817 | 0 | Py_DECREF(posobj); |
818 | 0 | return -1; |
819 | 0 | } |
820 | 7.32k | res = PyObject_CallMethodObjArgs(self->raw, &_Py_ID(seek), |
821 | 7.32k | posobj, whenceobj, NULL); |
822 | 7.32k | Py_DECREF(posobj); |
823 | 7.32k | Py_DECREF(whenceobj); |
824 | 7.32k | if (res == NULL) |
825 | 22 | return -1; |
826 | 7.30k | n = PyNumber_AsOff_t(res, PyExc_ValueError); |
827 | 7.30k | Py_DECREF(res); |
828 | 7.30k | if (n < 0) { |
829 | 0 | if (!PyErr_Occurred()) |
830 | 0 | PyErr_Format(PyExc_OSError, |
831 | 0 | "Raw stream returned invalid position %" PY_PRIdOFF, |
832 | 0 | (PY_OFF_T_COMPAT)n); |
833 | 0 | return -1; |
834 | 0 | } |
835 | 7.30k | self->abs_pos = n; |
836 | 7.30k | return n; |
837 | 7.30k | } |
838 | | |
839 | | static int |
840 | | _buffered_init(buffered *self) |
841 | 28.1k | { |
842 | 28.1k | Py_ssize_t n; |
843 | 28.1k | if (self->buffer_size <= 0) { |
844 | 0 | PyErr_SetString(PyExc_ValueError, |
845 | 0 | "buffer size must be strictly positive"); |
846 | 0 | return -1; |
847 | 0 | } |
848 | 28.1k | if (self->buffer) |
849 | 0 | PyMem_Free(self->buffer); |
850 | 28.1k | self->buffer = PyMem_Malloc(self->buffer_size); |
851 | 28.1k | if (self->buffer == NULL) { |
852 | 0 | PyErr_NoMemory(); |
853 | 0 | return -1; |
854 | 0 | } |
855 | 28.1k | if (self->lock) |
856 | 0 | PyThread_free_lock(self->lock); |
857 | 28.1k | self->lock = PyThread_allocate_lock(); |
858 | 28.1k | if (self->lock == NULL) { |
859 | 0 | PyErr_SetString(PyExc_RuntimeError, "can't allocate read lock"); |
860 | 0 | return -1; |
861 | 0 | } |
862 | 28.1k | self->owner = 0; |
863 | | /* Find out whether buffer_size is a power of 2 */ |
864 | | /* XXX is this optimization useful? */ |
865 | 507k | for (n = self->buffer_size - 1; n & 1; n >>= 1) |
866 | 478k | ; |
867 | 28.1k | if (n == 0) |
868 | 28.1k | self->buffer_mask = self->buffer_size - 1; |
869 | 0 | else |
870 | 0 | self->buffer_mask = 0; |
871 | 28.1k | if (_buffered_raw_tell(self) == -1) |
872 | 5 | PyErr_Clear(); |
873 | 28.1k | return 0; |
874 | 28.1k | } |
875 | | |
876 | | /* Return 1 if an OSError with errno == EINTR is set (and then |
877 | | clears the error indicator), 0 otherwise. |
878 | | Should only be called when PyErr_Occurred() is true. |
879 | | */ |
880 | | int |
881 | | _PyIO_trap_eintr(void) |
882 | 5.50k | { |
883 | 5.50k | if (!PyErr_ExceptionMatches(PyExc_OSError)) { |
884 | 3.36k | return 0; |
885 | 3.36k | } |
886 | 2.13k | PyObject *exc = PyErr_GetRaisedException(); |
887 | 2.13k | PyOSErrorObject *env_err = (PyOSErrorObject *)exc; |
888 | 2.13k | assert(env_err != NULL); |
889 | 2.13k | if (env_err->myerrno != NULL) { |
890 | 0 | assert(EINTR > 0 && EINTR < INT_MAX); |
891 | 0 | assert(PyLong_CheckExact(env_err->myerrno)); |
892 | 0 | int overflow; |
893 | 0 | int myerrno = PyLong_AsLongAndOverflow(env_err->myerrno, &overflow); |
894 | 0 | PyErr_Clear(); |
895 | 0 | if (myerrno == EINTR) { |
896 | 0 | Py_DECREF(exc); |
897 | 0 | return 1; |
898 | 0 | } |
899 | 0 | } |
900 | | /* This silences any error set by PyObject_RichCompareBool() */ |
901 | 2.13k | PyErr_SetRaisedException(exc); |
902 | 2.13k | return 0; |
903 | 2.13k | } |
904 | | |
905 | | /* |
906 | | * Shared methods and wrappers |
907 | | */ |
908 | | |
909 | | static PyObject * |
910 | | buffered_flush_and_rewind_unlocked(buffered *self) |
911 | 195k | { |
912 | 195k | PyObject *res; |
913 | | |
914 | 195k | res = _bufferedwriter_flush_unlocked(self); |
915 | 195k | if (res == NULL) |
916 | 0 | return NULL; |
917 | 195k | Py_DECREF(res); |
918 | | |
919 | 195k | if (self->readable) { |
920 | | /* Rewind the raw stream so that its position corresponds to |
921 | | the current logical position. */ |
922 | 0 | Py_off_t n; |
923 | 0 | n = _buffered_raw_seek(self, -RAW_OFFSET(self), 1); |
924 | 0 | _bufferedreader_reset_buf(self); |
925 | 0 | if (n == -1) |
926 | 0 | return NULL; |
927 | 0 | } |
928 | 195k | Py_RETURN_NONE; |
929 | 195k | } |
930 | | |
931 | | /*[clinic input] |
932 | | @critical_section |
933 | | _io._Buffered.flush |
934 | | [clinic start generated code]*/ |
935 | | |
936 | | static PyObject * |
937 | | _io__Buffered_flush_impl(buffered *self) |
938 | | /*[clinic end generated code: output=da2674ef1ce71f3a input=6b30de9f083419c2]*/ |
939 | 195k | { |
940 | 195k | PyObject *res; |
941 | | |
942 | 195k | CHECK_INITIALIZED(self) |
943 | 195k | CHECK_CLOSED(self, "flush of closed file") |
944 | | |
945 | 195k | if (!ENTER_BUFFERED(self)) |
946 | 0 | return NULL; |
947 | 195k | res = buffered_flush_and_rewind_unlocked(self); |
948 | 195k | LEAVE_BUFFERED(self) |
949 | | |
950 | 195k | return res; |
951 | 195k | } |
952 | | |
953 | | /*[clinic input] |
954 | | @critical_section |
955 | | _io._Buffered.peek |
956 | | size: Py_ssize_t = 0 |
957 | | / |
958 | | |
959 | | [clinic start generated code]*/ |
960 | | |
961 | | static PyObject * |
962 | | _io__Buffered_peek_impl(buffered *self, Py_ssize_t size) |
963 | | /*[clinic end generated code: output=ba7a097ca230102b input=56733376f926d982]*/ |
964 | 0 | { |
965 | 0 | PyObject *res = NULL; |
966 | |
|
967 | 0 | CHECK_INITIALIZED(self) |
968 | 0 | CHECK_CLOSED(self, "peek of closed file") |
969 | | |
970 | 0 | if (!ENTER_BUFFERED(self)) |
971 | 0 | return NULL; |
972 | | |
973 | 0 | if (self->writable) { |
974 | 0 | res = buffered_flush_and_rewind_unlocked(self); |
975 | 0 | if (res == NULL) |
976 | 0 | goto end; |
977 | 0 | Py_CLEAR(res); |
978 | 0 | } |
979 | 0 | res = _bufferedreader_peek_unlocked(self); |
980 | |
|
981 | 0 | end: |
982 | 0 | LEAVE_BUFFERED(self) |
983 | 0 | return res; |
984 | 0 | } |
985 | | |
986 | | /*[clinic input] |
987 | | @critical_section |
988 | | _io._Buffered.read |
989 | | size as n: Py_ssize_t(accept={int, NoneType}) = -1 |
990 | | / |
991 | | [clinic start generated code]*/ |
992 | | |
993 | | static PyObject * |
994 | | _io__Buffered_read_impl(buffered *self, Py_ssize_t n) |
995 | | /*[clinic end generated code: output=f41c78bb15b9bbe9 input=bdb4b0425b295472]*/ |
996 | 5.48M | { |
997 | 5.48M | PyObject *res; |
998 | | |
999 | 5.48M | CHECK_INITIALIZED(self) |
1000 | 5.48M | if (n < -1) { |
1001 | 0 | PyErr_SetString(PyExc_ValueError, |
1002 | 0 | "read length must be non-negative or -1"); |
1003 | 0 | return NULL; |
1004 | 0 | } |
1005 | | |
1006 | 5.48M | CHECK_CLOSED(self, "read of closed file") |
1007 | | |
1008 | 5.48M | if (n == -1) { |
1009 | | /* The number of bytes is unspecified, read until the end of stream */ |
1010 | 2.08k | if (!ENTER_BUFFERED(self)) |
1011 | 0 | return NULL; |
1012 | 2.08k | res = _bufferedreader_read_all(self); |
1013 | 2.08k | } |
1014 | 5.48M | else { |
1015 | 5.48M | res = _bufferedreader_read_fast(self, n); |
1016 | 5.48M | if (res != Py_None) |
1017 | 5.44M | return res; |
1018 | 34.9k | Py_DECREF(res); |
1019 | 34.9k | if (!ENTER_BUFFERED(self)) |
1020 | 0 | return NULL; |
1021 | 34.9k | res = _bufferedreader_read_generic(self, n); |
1022 | 34.9k | } |
1023 | | |
1024 | 37.0k | LEAVE_BUFFERED(self) |
1025 | 37.0k | return res; |
1026 | 5.48M | } |
1027 | | |
1028 | | /*[clinic input] |
1029 | | @critical_section |
1030 | | _io._Buffered.read1 |
1031 | | size as n: Py_ssize_t = -1 |
1032 | | / |
1033 | | [clinic start generated code]*/ |
1034 | | |
1035 | | static PyObject * |
1036 | | _io__Buffered_read1_impl(buffered *self, Py_ssize_t n) |
1037 | | /*[clinic end generated code: output=bcc4fb4e54d103a3 input=3d0ad241aa52b36c]*/ |
1038 | 130 | { |
1039 | 130 | CHECK_INITIALIZED(self) |
1040 | 130 | if (n < 0) { |
1041 | 0 | n = self->buffer_size; |
1042 | 0 | } |
1043 | | |
1044 | 130 | CHECK_CLOSED(self, "read of closed file") |
1045 | | |
1046 | 130 | if (n == 0) { |
1047 | 0 | return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES); |
1048 | 0 | } |
1049 | | |
1050 | | /* Return up to n bytes. If at least one byte is buffered, we |
1051 | | only return buffered bytes. Otherwise, we do one raw read. */ |
1052 | | |
1053 | 130 | Py_ssize_t have = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t); |
1054 | 130 | if (have > 0) { |
1055 | 120 | n = Py_MIN(have, n); |
1056 | 120 | PyObject *res = _bufferedreader_read_fast(self, n); |
1057 | 120 | assert(res != Py_None); |
1058 | 120 | return res; |
1059 | 120 | } |
1060 | | |
1061 | 10 | if (!ENTER_BUFFERED(self)) { |
1062 | 0 | return NULL; |
1063 | 0 | } |
1064 | | |
1065 | | /* Flush the write buffer if necessary */ |
1066 | 10 | if (self->writable) { |
1067 | 0 | PyObject *res = buffered_flush_and_rewind_unlocked(self); |
1068 | 0 | if (res == NULL) { |
1069 | 0 | LEAVE_BUFFERED(self) |
1070 | 0 | return NULL; |
1071 | 0 | } |
1072 | 0 | Py_DECREF(res); |
1073 | 0 | } |
1074 | 10 | _bufferedreader_reset_buf(self); |
1075 | | |
1076 | 10 | PyBytesWriter *writer = PyBytesWriter_Create(n); |
1077 | 10 | if (writer == NULL) { |
1078 | 0 | LEAVE_BUFFERED(self) |
1079 | 0 | return NULL; |
1080 | 0 | } |
1081 | | |
1082 | 10 | Py_ssize_t r = _bufferedreader_raw_read(self, |
1083 | 10 | PyBytesWriter_GetData(writer), n); |
1084 | 10 | LEAVE_BUFFERED(self) |
1085 | 10 | if (r == -1) { |
1086 | 0 | PyBytesWriter_Discard(writer); |
1087 | 0 | return NULL; |
1088 | 0 | } |
1089 | 10 | if (r == -2) { |
1090 | 0 | r = 0; |
1091 | 0 | } |
1092 | | |
1093 | 10 | return PyBytesWriter_FinishWithSize(writer, r); |
1094 | 10 | } |
1095 | | |
1096 | | static PyObject * |
1097 | | _buffered_readinto_generic(buffered *self, Py_buffer *buffer, char readinto1) |
1098 | 0 | { |
1099 | 0 | Py_ssize_t n, written = 0, remaining; |
1100 | 0 | PyObject *res = NULL; |
1101 | |
|
1102 | 0 | CHECK_INITIALIZED(self) |
1103 | 0 | CHECK_CLOSED(self, "readinto of closed file") |
1104 | | |
1105 | 0 | n = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t); |
1106 | 0 | if (n > 0) { |
1107 | 0 | if (n >= buffer->len) { |
1108 | 0 | memcpy(buffer->buf, self->buffer + self->pos, buffer->len); |
1109 | 0 | self->pos += buffer->len; |
1110 | 0 | return PyLong_FromSsize_t(buffer->len); |
1111 | 0 | } |
1112 | 0 | memcpy(buffer->buf, self->buffer + self->pos, n); |
1113 | 0 | self->pos += n; |
1114 | 0 | written = n; |
1115 | 0 | } |
1116 | | |
1117 | 0 | if (!ENTER_BUFFERED(self)) |
1118 | 0 | return NULL; |
1119 | | |
1120 | 0 | if (self->writable) { |
1121 | 0 | res = buffered_flush_and_rewind_unlocked(self); |
1122 | 0 | if (res == NULL) |
1123 | 0 | goto end; |
1124 | 0 | Py_CLEAR(res); |
1125 | 0 | } |
1126 | | |
1127 | 0 | _bufferedreader_reset_buf(self); |
1128 | 0 | self->pos = 0; |
1129 | |
|
1130 | 0 | for (remaining = buffer->len - written; |
1131 | 0 | remaining > 0; |
1132 | 0 | written += n, remaining -= n) { |
1133 | | /* If remaining bytes is larger than internal buffer size, copy |
1134 | | * directly into caller's buffer. */ |
1135 | 0 | if (remaining > self->buffer_size) { |
1136 | 0 | n = _bufferedreader_raw_read(self, (char *) buffer->buf + written, |
1137 | 0 | remaining); |
1138 | 0 | } |
1139 | | |
1140 | | /* In readinto1 mode, we do not want to fill the internal |
1141 | | buffer if we already have some data to return */ |
1142 | 0 | else if (!(readinto1 && written)) { |
1143 | 0 | n = _bufferedreader_fill_buffer(self); |
1144 | 0 | if (n > 0) { |
1145 | 0 | if (n > remaining) |
1146 | 0 | n = remaining; |
1147 | 0 | memcpy((char *) buffer->buf + written, |
1148 | 0 | self->buffer + self->pos, n); |
1149 | 0 | self->pos += n; |
1150 | 0 | continue; /* short circuit */ |
1151 | 0 | } |
1152 | 0 | } |
1153 | 0 | else |
1154 | 0 | n = 0; |
1155 | | |
1156 | 0 | if (n == 0 || (n == -2 && written > 0)) |
1157 | 0 | break; |
1158 | 0 | if (n < 0) { |
1159 | 0 | if (n == -2) { |
1160 | 0 | res = Py_NewRef(Py_None); |
1161 | 0 | } |
1162 | 0 | goto end; |
1163 | 0 | } |
1164 | | |
1165 | | /* At most one read in readinto1 mode */ |
1166 | 0 | if (readinto1) { |
1167 | 0 | written += n; |
1168 | 0 | break; |
1169 | 0 | } |
1170 | 0 | } |
1171 | 0 | res = PyLong_FromSsize_t(written); |
1172 | |
|
1173 | 0 | end: |
1174 | 0 | LEAVE_BUFFERED(self); |
1175 | 0 | return res; |
1176 | 0 | } |
1177 | | |
1178 | | /*[clinic input] |
1179 | | @critical_section |
1180 | | _io._Buffered.readinto |
1181 | | buffer: Py_buffer(accept={rwbuffer}) |
1182 | | / |
1183 | | [clinic start generated code]*/ |
1184 | | |
1185 | | static PyObject * |
1186 | | _io__Buffered_readinto_impl(buffered *self, Py_buffer *buffer) |
1187 | | /*[clinic end generated code: output=bcb376580b1d8170 input=777c33e7adaa2bcd]*/ |
1188 | 0 | { |
1189 | 0 | return _buffered_readinto_generic(self, buffer, 0); |
1190 | 0 | } |
1191 | | |
1192 | | /*[clinic input] |
1193 | | @critical_section |
1194 | | _io._Buffered.readinto1 |
1195 | | buffer: Py_buffer(accept={rwbuffer}) |
1196 | | / |
1197 | | [clinic start generated code]*/ |
1198 | | |
1199 | | static PyObject * |
1200 | | _io__Buffered_readinto1_impl(buffered *self, Py_buffer *buffer) |
1201 | | /*[clinic end generated code: output=6e5c6ac5868205d6 input=ef03cc5fc92a6895]*/ |
1202 | 0 | { |
1203 | 0 | return _buffered_readinto_generic(self, buffer, 1); |
1204 | 0 | } |
1205 | | |
1206 | | |
1207 | | static PyObject * |
1208 | | _buffered_readline(buffered *self, Py_ssize_t limit) |
1209 | 18 | { |
1210 | 18 | _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self); |
1211 | | |
1212 | 18 | CHECK_CLOSED(self, "readline of closed file") |
1213 | | |
1214 | | /* First, try to find a line in the buffer. This can run unlocked because |
1215 | | the calls to the C API are simple enough that they can't trigger |
1216 | | any thread switch. */ |
1217 | 18 | Py_ssize_t n = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t); |
1218 | 18 | if (limit >= 0 && n > limit) { |
1219 | 0 | n = limit; |
1220 | 0 | } |
1221 | 18 | const char *start = self->buffer + self->pos; |
1222 | 18 | const char *s = memchr(start, '\n', n); |
1223 | 18 | if (s != NULL) { |
1224 | 8 | n = s - start + 1; |
1225 | 8 | PyObject *res = PyBytes_FromStringAndSize(start, n); |
1226 | 8 | if (res == NULL) { |
1227 | 0 | return NULL; |
1228 | 0 | } |
1229 | 8 | self->pos += n; |
1230 | 8 | return res; |
1231 | 8 | } |
1232 | | |
1233 | 10 | if (n == limit) { |
1234 | 0 | PyObject *res = PyBytes_FromStringAndSize(start, n); |
1235 | 0 | if (res == NULL) { |
1236 | 0 | return NULL; |
1237 | 0 | } |
1238 | 0 | self->pos += n; |
1239 | 0 | return res; |
1240 | 0 | } |
1241 | | |
1242 | 10 | PyBytesWriter *writer = NULL; |
1243 | 10 | int locked = 0; |
1244 | 10 | if (!ENTER_BUFFERED(self)) { |
1245 | 0 | goto error; |
1246 | 0 | } |
1247 | 10 | locked = 1; |
1248 | | |
1249 | | /* Now we try to get some more from the raw stream */ |
1250 | 10 | writer = PyBytesWriter_Create(0); |
1251 | 10 | if (writer == NULL) { |
1252 | 0 | goto error; |
1253 | 0 | } |
1254 | | |
1255 | 10 | if (n > 0) { |
1256 | 0 | if (PyBytesWriter_WriteBytes(writer, start, n) < 0) { |
1257 | 0 | goto error; |
1258 | 0 | } |
1259 | 0 | self->pos += n; |
1260 | 0 | if (limit >= 0) { |
1261 | 0 | limit -= n; |
1262 | 0 | } |
1263 | 0 | } |
1264 | 10 | if (self->writable) { |
1265 | 0 | PyObject *res = buffered_flush_and_rewind_unlocked(self); |
1266 | 0 | if (res == NULL) { |
1267 | 0 | goto error; |
1268 | 0 | } |
1269 | 0 | Py_DECREF(res); |
1270 | 0 | } |
1271 | | |
1272 | 10 | for (;;) { |
1273 | 10 | _bufferedreader_reset_buf(self); |
1274 | 10 | n = _bufferedreader_fill_buffer(self); |
1275 | 10 | if (n == -1) { |
1276 | 0 | goto error; |
1277 | 0 | } |
1278 | 10 | if (n <= 0) { |
1279 | 0 | break; |
1280 | 0 | } |
1281 | 10 | if (limit >= 0 && n > limit) { |
1282 | 0 | n = limit; |
1283 | 0 | } |
1284 | 10 | start = self->buffer; |
1285 | 10 | const char *newline = memchr(start, '\n', n); |
1286 | 10 | if (newline != NULL) { |
1287 | 10 | n = newline - start + 1; |
1288 | 10 | if (PyBytesWriter_WriteBytes(writer, start, n) < 0) { |
1289 | 0 | goto error; |
1290 | 0 | } |
1291 | 10 | self->pos = n; |
1292 | 10 | goto found; |
1293 | 10 | } |
1294 | | |
1295 | 0 | if (PyBytesWriter_WriteBytes(writer, start, n) < 0) { |
1296 | 0 | goto error; |
1297 | 0 | } |
1298 | 0 | if (n == limit) { |
1299 | 0 | self->pos = n; |
1300 | 0 | break; |
1301 | 0 | } |
1302 | 0 | if (limit >= 0) { |
1303 | 0 | limit -= n; |
1304 | 0 | } |
1305 | 0 | } |
1306 | | |
1307 | 10 | found: |
1308 | 10 | LEAVE_BUFFERED(self) |
1309 | 10 | return PyBytesWriter_Finish(writer); |
1310 | | |
1311 | 0 | error: |
1312 | 0 | PyBytesWriter_Discard(writer); |
1313 | 0 | if (locked) { |
1314 | 0 | LEAVE_BUFFERED(self) |
1315 | 0 | } |
1316 | 0 | return NULL; |
1317 | 10 | } |
1318 | | |
1319 | | /*[clinic input] |
1320 | | @critical_section |
1321 | | _io._Buffered.readline |
1322 | | size: Py_ssize_t(accept={int, NoneType}) = -1 |
1323 | | / |
1324 | | [clinic start generated code]*/ |
1325 | | |
1326 | | static PyObject * |
1327 | | _io__Buffered_readline_impl(buffered *self, Py_ssize_t size) |
1328 | | /*[clinic end generated code: output=24dd2aa6e33be83c input=e81ca5abd4280776]*/ |
1329 | 18 | { |
1330 | 18 | CHECK_INITIALIZED(self) |
1331 | 18 | return _buffered_readline(self, size); |
1332 | 18 | } |
1333 | | |
1334 | | |
1335 | | /*[clinic input] |
1336 | | @critical_section |
1337 | | _io._Buffered.tell |
1338 | | [clinic start generated code]*/ |
1339 | | |
1340 | | static PyObject * |
1341 | | _io__Buffered_tell_impl(buffered *self) |
1342 | | /*[clinic end generated code: output=386972ae84716c1e input=ab12e67d8abcb42f]*/ |
1343 | 6.49k | { |
1344 | 6.49k | Py_off_t pos; |
1345 | | |
1346 | 6.49k | CHECK_INITIALIZED(self) |
1347 | 6.49k | pos = _buffered_raw_tell(self); |
1348 | 6.49k | if (pos == -1) |
1349 | 0 | return NULL; |
1350 | 6.49k | pos -= RAW_OFFSET(self); |
1351 | | |
1352 | | // GH-95782 |
1353 | 6.49k | if (pos < 0) |
1354 | 0 | pos = 0; |
1355 | | |
1356 | 6.49k | return PyLong_FromOff_t(pos); |
1357 | 6.49k | } |
1358 | | |
1359 | | /*[clinic input] |
1360 | | @critical_section |
1361 | | _io._Buffered.seek |
1362 | | target as targetobj: object |
1363 | | whence: int = 0 |
1364 | | / |
1365 | | [clinic start generated code]*/ |
1366 | | |
1367 | | static PyObject * |
1368 | | _io__Buffered_seek_impl(buffered *self, PyObject *targetobj, int whence) |
1369 | | /*[clinic end generated code: output=7ae0e8dc46efdefb input=b5a12be70e0ad07b]*/ |
1370 | 1.41M | { |
1371 | 1.41M | Py_off_t target, n; |
1372 | 1.41M | PyObject *res = NULL; |
1373 | | |
1374 | 1.41M | CHECK_INITIALIZED(self) |
1375 | | |
1376 | | /* Do some error checking instead of trusting OS 'seek()' |
1377 | | ** error detection, just in case. |
1378 | | */ |
1379 | 1.41M | if ((whence < 0 || whence >2) |
1380 | 0 | #ifdef SEEK_HOLE |
1381 | 0 | && (whence != SEEK_HOLE) |
1382 | 0 | #endif |
1383 | 0 | #ifdef SEEK_DATA |
1384 | 0 | && (whence != SEEK_DATA) |
1385 | 1.41M | #endif |
1386 | 1.41M | ) { |
1387 | 0 | PyErr_Format(PyExc_ValueError, |
1388 | 0 | "whence value %d unsupported", whence); |
1389 | 0 | return NULL; |
1390 | 0 | } |
1391 | | |
1392 | 1.41M | CHECK_CLOSED(self, "seek of closed file") |
1393 | | |
1394 | 1.41M | _PyIO_State *state = find_io_state_by_def(Py_TYPE(self)); |
1395 | 1.41M | if (_PyIOBase_check_seekable(state, self->raw, Py_True) == NULL) { |
1396 | 0 | return NULL; |
1397 | 0 | } |
1398 | | |
1399 | 1.41M | target = PyNumber_AsOff_t(targetobj, PyExc_ValueError); |
1400 | 1.41M | if (target == -1 && PyErr_Occurred()) |
1401 | 0 | return NULL; |
1402 | | |
1403 | | /* SEEK_SET and SEEK_CUR are special because we could seek inside the |
1404 | | buffer. Other whence values must be managed without this optimization. |
1405 | | Some Operating Systems can provide additional values, like |
1406 | | SEEK_HOLE/SEEK_DATA. */ |
1407 | 1.41M | if (((whence == 0) || (whence == 1)) && self->readable) { |
1408 | 1.41M | Py_off_t current, avail; |
1409 | | /* Check if seeking leaves us inside the current buffer, |
1410 | | so as to return quickly if possible. Also, we needn't take the |
1411 | | lock in this fast path. |
1412 | | Don't know how to do that when whence == 2, though. */ |
1413 | | /* NOTE: RAW_TELL() can release the GIL but the object is in a stable |
1414 | | state at this point. */ |
1415 | 1.41M | current = RAW_TELL(self); |
1416 | 1.41M | avail = READAHEAD(self); |
1417 | 1.41M | if (avail > 0) { |
1418 | 1.40M | Py_off_t offset; |
1419 | 1.40M | if (whence == 0) |
1420 | 33 | offset = target - (current - RAW_OFFSET(self)); |
1421 | 1.40M | else |
1422 | 1.40M | offset = target; |
1423 | 1.40M | if (offset >= -self->pos && offset <= avail) { |
1424 | 1.40M | self->pos += offset; |
1425 | | |
1426 | | // GH-95782 |
1427 | 1.40M | if (current - avail + offset < 0) |
1428 | 0 | return PyLong_FromOff_t(0); |
1429 | | |
1430 | 1.40M | return PyLong_FromOff_t(current - avail + offset); |
1431 | 1.40M | } |
1432 | 1.40M | } |
1433 | 1.41M | } |
1434 | | |
1435 | 7.32k | if (!ENTER_BUFFERED(self)) |
1436 | 0 | return NULL; |
1437 | | |
1438 | | /* Fallback: invoke raw seek() method and clear buffer */ |
1439 | 7.32k | if (self->writable) { |
1440 | 0 | res = _bufferedwriter_flush_unlocked(self); |
1441 | 0 | if (res == NULL) |
1442 | 0 | goto end; |
1443 | 0 | Py_CLEAR(res); |
1444 | 0 | } |
1445 | | |
1446 | | /* TODO: align on block boundary and read buffer if needed? */ |
1447 | 7.32k | if (whence == 1) |
1448 | 7.30k | target -= RAW_OFFSET(self); |
1449 | 7.32k | n = _buffered_raw_seek(self, target, whence); |
1450 | 7.32k | if (n == -1) |
1451 | 22 | goto end; |
1452 | 7.30k | self->raw_pos = -1; |
1453 | 7.30k | res = PyLong_FromOff_t(n); |
1454 | 7.30k | if (res != NULL && self->readable) |
1455 | 7.30k | _bufferedreader_reset_buf(self); |
1456 | | |
1457 | 7.32k | end: |
1458 | 7.32k | LEAVE_BUFFERED(self) |
1459 | 7.32k | return res; |
1460 | 7.30k | } |
1461 | | |
1462 | | /*[clinic input] |
1463 | | @critical_section |
1464 | | _io._Buffered.truncate |
1465 | | cls: defining_class |
1466 | | pos: object = None |
1467 | | / |
1468 | | [clinic start generated code]*/ |
1469 | | |
1470 | | static PyObject * |
1471 | | _io__Buffered_truncate_impl(buffered *self, PyTypeObject *cls, PyObject *pos) |
1472 | | /*[clinic end generated code: output=fe3882fbffe79f1a input=e3cbf794575bd794]*/ |
1473 | 0 | { |
1474 | 0 | PyObject *res = NULL; |
1475 | |
|
1476 | 0 | CHECK_INITIALIZED(self) |
1477 | 0 | CHECK_CLOSED(self, "truncate of closed file") |
1478 | 0 | if (!self->writable) { |
1479 | 0 | _PyIO_State *state = get_io_state_by_cls(cls); |
1480 | 0 | return bufferediobase_unsupported(state, "truncate"); |
1481 | 0 | } |
1482 | 0 | if (!ENTER_BUFFERED(self)) |
1483 | 0 | return NULL; |
1484 | | |
1485 | 0 | res = buffered_flush_and_rewind_unlocked(self); |
1486 | 0 | if (res == NULL) { |
1487 | 0 | goto end; |
1488 | 0 | } |
1489 | 0 | Py_CLEAR(res); |
1490 | |
|
1491 | 0 | res = PyObject_CallMethodOneArg(self->raw, &_Py_ID(truncate), pos); |
1492 | 0 | if (res == NULL) |
1493 | 0 | goto end; |
1494 | | /* Reset cached position */ |
1495 | 0 | if (_buffered_raw_tell(self) == -1) |
1496 | 0 | PyErr_Clear(); |
1497 | |
|
1498 | 0 | end: |
1499 | 0 | LEAVE_BUFFERED(self) |
1500 | 0 | return res; |
1501 | 0 | } |
1502 | | |
1503 | | static PyObject * |
1504 | | buffered_iternext(PyObject *op) |
1505 | 0 | { |
1506 | 0 | buffered *self = buffered_CAST(op); |
1507 | 0 | PyObject *line; |
1508 | 0 | PyTypeObject *tp; |
1509 | |
|
1510 | 0 | CHECK_INITIALIZED(self); |
1511 | |
|
1512 | 0 | _PyIO_State *state = find_io_state_by_def(Py_TYPE(self)); |
1513 | 0 | tp = Py_TYPE(self); |
1514 | 0 | if (tp == state->PyBufferedReader_Type || |
1515 | 0 | tp == state->PyBufferedRandom_Type) |
1516 | 0 | { |
1517 | | /* Skip method call overhead for speed */ |
1518 | 0 | Py_BEGIN_CRITICAL_SECTION(self); |
1519 | 0 | line = _buffered_readline(self, -1); |
1520 | 0 | Py_END_CRITICAL_SECTION(); |
1521 | 0 | } |
1522 | 0 | else { |
1523 | 0 | line = PyObject_CallMethodNoArgs((PyObject *)self, |
1524 | 0 | &_Py_ID(readline)); |
1525 | 0 | if (line && !PyBytes_Check(line)) { |
1526 | 0 | PyErr_Format(PyExc_OSError, |
1527 | 0 | "readline() should have returned a bytes object, " |
1528 | 0 | "not '%.200s'", Py_TYPE(line)->tp_name); |
1529 | 0 | Py_DECREF(line); |
1530 | 0 | return NULL; |
1531 | 0 | } |
1532 | 0 | } |
1533 | | |
1534 | 0 | if (line == NULL) |
1535 | 0 | return NULL; |
1536 | | |
1537 | 0 | if (PyBytes_GET_SIZE(line) == 0) { |
1538 | | /* Reached EOF or would have blocked */ |
1539 | 0 | Py_DECREF(line); |
1540 | 0 | return NULL; |
1541 | 0 | } |
1542 | | |
1543 | 0 | return line; |
1544 | 0 | } |
1545 | | |
1546 | | static PyObject * |
1547 | | buffered_repr(PyObject *op) |
1548 | 0 | { |
1549 | 0 | buffered *self = buffered_CAST(op); |
1550 | 0 | PyObject *nameobj, *res; |
1551 | |
|
1552 | 0 | if (PyObject_GetOptionalAttr((PyObject *) self, &_Py_ID(name), &nameobj) < 0) { |
1553 | 0 | if (!PyErr_ExceptionMatches(PyExc_ValueError)) { |
1554 | 0 | return NULL; |
1555 | 0 | } |
1556 | | /* Ignore ValueError raised if the underlying stream was detached */ |
1557 | 0 | PyErr_Clear(); |
1558 | 0 | } |
1559 | 0 | if (nameobj == NULL) { |
1560 | 0 | res = PyUnicode_FromFormat("<%s>", Py_TYPE(self)->tp_name); |
1561 | 0 | } |
1562 | 0 | else { |
1563 | 0 | int status = Py_ReprEnter((PyObject *)self); |
1564 | 0 | res = NULL; |
1565 | 0 | if (status == 0) { |
1566 | 0 | res = PyUnicode_FromFormat("<%s name=%R>", |
1567 | 0 | Py_TYPE(self)->tp_name, nameobj); |
1568 | 0 | Py_ReprLeave((PyObject *)self); |
1569 | 0 | } |
1570 | 0 | else if (status > 0) { |
1571 | 0 | PyErr_Format(PyExc_RuntimeError, |
1572 | 0 | "reentrant call inside %s.__repr__", |
1573 | 0 | Py_TYPE(self)->tp_name); |
1574 | 0 | } |
1575 | 0 | Py_DECREF(nameobj); |
1576 | 0 | } |
1577 | 0 | return res; |
1578 | 0 | } |
1579 | | |
1580 | | /* |
1581 | | * class BufferedReader |
1582 | | */ |
1583 | | |
1584 | | static void _bufferedreader_reset_buf(buffered *self) |
1585 | 52.3k | { |
1586 | 52.3k | self->read_end = -1; |
1587 | 52.3k | } |
1588 | | |
1589 | | /*[clinic input] |
1590 | | _io.BufferedReader.__init__ |
1591 | | raw: object |
1592 | | buffer_size: Py_ssize_t(c_default="DEFAULT_BUFFER_SIZE") = DEFAULT_BUFFER_SIZE |
1593 | | |
1594 | | Create a new buffered reader using the given readable raw IO object. |
1595 | | [clinic start generated code]*/ |
1596 | | |
1597 | | static int |
1598 | | _io_BufferedReader___init___impl(buffered *self, PyObject *raw, |
1599 | | Py_ssize_t buffer_size) |
1600 | | /*[clinic end generated code: output=cddcfefa0ed294c4 input=fb887e06f11b4e48]*/ |
1601 | 7.99k | { |
1602 | 7.99k | self->ok = 0; |
1603 | 7.99k | self->detached = 0; |
1604 | | |
1605 | 7.99k | _PyIO_State *state = find_io_state_by_def(Py_TYPE(self)); |
1606 | 7.99k | if (_PyIOBase_check_readable(state, raw, Py_True) == NULL) { |
1607 | 0 | return -1; |
1608 | 0 | } |
1609 | | |
1610 | 7.99k | Py_XSETREF(self->raw, Py_NewRef(raw)); |
1611 | 7.99k | self->buffer_size = buffer_size; |
1612 | 7.99k | self->readable = 1; |
1613 | 7.99k | self->writable = 0; |
1614 | | |
1615 | 7.99k | if (_buffered_init(self) < 0) |
1616 | 0 | return -1; |
1617 | 7.99k | _bufferedreader_reset_buf(self); |
1618 | | |
1619 | 7.99k | self->fast_closed_checks = ( |
1620 | 7.99k | Py_IS_TYPE(self, state->PyBufferedReader_Type) && |
1621 | 7.99k | Py_IS_TYPE(raw, state->PyFileIO_Type) |
1622 | 7.99k | ); |
1623 | | |
1624 | 7.99k | self->ok = 1; |
1625 | 7.99k | return 0; |
1626 | 7.99k | } |
1627 | | |
1628 | | static Py_ssize_t |
1629 | | _bufferedreader_raw_read(buffered *self, char *start, Py_ssize_t len) |
1630 | 53.7k | { |
1631 | 53.7k | Py_buffer buf; |
1632 | 53.7k | PyObject *memobj, *res; |
1633 | 53.7k | Py_ssize_t n; |
1634 | | /* NOTE: the buffer needn't be released as its object is NULL. */ |
1635 | 53.7k | if (PyBuffer_FillInfo(&buf, NULL, start, len, 0, PyBUF_CONTIG) == -1) |
1636 | 0 | return -1; |
1637 | 53.7k | memobj = PyMemoryView_FromBuffer(&buf); |
1638 | 53.7k | if (memobj == NULL) |
1639 | 0 | return -1; |
1640 | | /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() when EINTR |
1641 | | occurs so we needn't do it ourselves. |
1642 | | We then retry reading, ignoring the signal if no handler has |
1643 | | raised (see issue #10956). |
1644 | | */ |
1645 | 53.7k | do { |
1646 | 53.7k | res = PyObject_CallMethodOneArg(self->raw, &_Py_ID(readinto), memobj); |
1647 | 53.7k | } while (res == NULL && _PyIO_trap_eintr()); |
1648 | 53.7k | Py_DECREF(memobj); |
1649 | 53.7k | if (res == NULL) |
1650 | 5.50k | return -1; |
1651 | 48.2k | if (res == Py_None) { |
1652 | | /* Non-blocking stream would have blocked. Special return code! */ |
1653 | 0 | Py_DECREF(res); |
1654 | 0 | return -2; |
1655 | 0 | } |
1656 | 48.2k | n = PyNumber_AsSsize_t(res, PyExc_ValueError); |
1657 | 48.2k | Py_DECREF(res); |
1658 | | |
1659 | 48.2k | if (n == -1 && PyErr_Occurred()) { |
1660 | 0 | _PyErr_FormatFromCause( |
1661 | 0 | PyExc_OSError, |
1662 | 0 | "raw readinto() failed" |
1663 | 0 | ); |
1664 | 0 | return -1; |
1665 | 0 | } |
1666 | | |
1667 | 48.2k | if (n < 0 || n > len) { |
1668 | 0 | PyErr_Format(PyExc_OSError, |
1669 | 0 | "raw readinto() returned invalid length %zd " |
1670 | 0 | "(should have been between 0 and %zd)", n, len); |
1671 | 0 | return -1; |
1672 | 0 | } |
1673 | 48.2k | if (n > 0 && self->abs_pos != -1) |
1674 | 47.9k | self->abs_pos += n; |
1675 | 48.2k | return n; |
1676 | 48.2k | } |
1677 | | |
1678 | | static Py_ssize_t |
1679 | | _bufferedreader_fill_buffer(buffered *self) |
1680 | 53.7k | { |
1681 | 53.7k | Py_ssize_t start, len, n; |
1682 | 53.7k | if (VALID_READ_BUFFER(self)) |
1683 | 53.7k | start = Py_SAFE_DOWNCAST(self->read_end, Py_off_t, Py_ssize_t); |
1684 | 10 | else |
1685 | 10 | start = 0; |
1686 | 53.7k | len = self->buffer_size - start; |
1687 | 53.7k | n = _bufferedreader_raw_read(self, self->buffer + start, len); |
1688 | 53.7k | if (n <= 0) |
1689 | 5.81k | return n; |
1690 | 47.9k | self->read_end = start + n; |
1691 | 47.9k | self->raw_pos = start + n; |
1692 | 47.9k | return n; |
1693 | 53.7k | } |
1694 | | |
1695 | | static PyObject * |
1696 | | _bufferedreader_read_all(buffered *self) |
1697 | 2.08k | { |
1698 | 2.08k | Py_ssize_t current_size; |
1699 | 2.08k | PyObject *res = NULL, *data = NULL, *tmp = NULL, *chunks = NULL, *readall; |
1700 | | |
1701 | | /* First copy what we have in the current buffer. */ |
1702 | 2.08k | current_size = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t); |
1703 | 2.08k | if (current_size) { |
1704 | 0 | data = PyBytes_FromStringAndSize( |
1705 | 0 | self->buffer + self->pos, current_size); |
1706 | 0 | if (data == NULL) |
1707 | 0 | return NULL; |
1708 | 0 | self->pos += current_size; |
1709 | 0 | } |
1710 | | /* We're going past the buffer's bounds, flush it */ |
1711 | 2.08k | if (self->writable) { |
1712 | 0 | tmp = buffered_flush_and_rewind_unlocked(self); |
1713 | 0 | if (tmp == NULL) |
1714 | 0 | goto cleanup; |
1715 | 0 | Py_CLEAR(tmp); |
1716 | 0 | } |
1717 | 2.08k | _bufferedreader_reset_buf(self); |
1718 | | |
1719 | 2.08k | if (PyObject_GetOptionalAttr(self->raw, &_Py_ID(readall), &readall) < 0) { |
1720 | 0 | goto cleanup; |
1721 | 0 | } |
1722 | 2.08k | if (readall) { |
1723 | 2.08k | tmp = _PyObject_CallNoArgs(readall); |
1724 | 2.08k | Py_DECREF(readall); |
1725 | 2.08k | if (tmp == NULL) |
1726 | 0 | goto cleanup; |
1727 | 2.08k | if (tmp != Py_None && !PyBytes_Check(tmp)) { |
1728 | 0 | PyErr_SetString(PyExc_TypeError, "readall() should return bytes"); |
1729 | 0 | goto cleanup; |
1730 | 0 | } |
1731 | 2.08k | if (current_size == 0) { |
1732 | 2.08k | res = tmp; |
1733 | 2.08k | } else { |
1734 | 0 | if (tmp != Py_None) { |
1735 | 0 | PyBytes_Concat(&data, tmp); |
1736 | 0 | } |
1737 | 0 | res = data; |
1738 | 0 | } |
1739 | 2.08k | goto cleanup; |
1740 | 2.08k | } |
1741 | | |
1742 | 0 | chunks = PyList_New(0); |
1743 | 0 | if (chunks == NULL) |
1744 | 0 | goto cleanup; |
1745 | | |
1746 | 0 | while (1) { |
1747 | 0 | if (data) { |
1748 | 0 | if (PyList_Append(chunks, data) < 0) |
1749 | 0 | goto cleanup; |
1750 | 0 | Py_CLEAR(data); |
1751 | 0 | } |
1752 | | |
1753 | | /* Read until EOF or until read() would block. */ |
1754 | 0 | data = PyObject_CallMethodNoArgs(self->raw, &_Py_ID(read)); |
1755 | 0 | if (data == NULL) |
1756 | 0 | goto cleanup; |
1757 | 0 | if (data != Py_None && !PyBytes_Check(data)) { |
1758 | 0 | PyErr_SetString(PyExc_TypeError, "read() should return bytes"); |
1759 | 0 | goto cleanup; |
1760 | 0 | } |
1761 | 0 | if (data == Py_None || PyBytes_GET_SIZE(data) == 0) { |
1762 | 0 | if (current_size == 0) { |
1763 | 0 | res = data; |
1764 | 0 | goto cleanup; |
1765 | 0 | } |
1766 | 0 | else { |
1767 | 0 | tmp = PyBytes_Join((PyObject *)&_Py_SINGLETON(bytes_empty), chunks); |
1768 | 0 | res = tmp; |
1769 | 0 | goto cleanup; |
1770 | 0 | } |
1771 | 0 | } |
1772 | 0 | current_size += PyBytes_GET_SIZE(data); |
1773 | 0 | if (self->abs_pos != -1) |
1774 | 0 | self->abs_pos += PyBytes_GET_SIZE(data); |
1775 | 0 | } |
1776 | 2.08k | cleanup: |
1777 | | /* res is either NULL or a borrowed ref */ |
1778 | 2.08k | Py_XINCREF(res); |
1779 | 2.08k | Py_XDECREF(data); |
1780 | 2.08k | Py_XDECREF(tmp); |
1781 | 2.08k | Py_XDECREF(chunks); |
1782 | 2.08k | return res; |
1783 | 0 | } |
1784 | | |
1785 | | /* Read n bytes from the buffer if it can, otherwise return None. |
1786 | | This function is simple enough that it can run unlocked. */ |
1787 | | static PyObject * |
1788 | | _bufferedreader_read_fast(buffered *self, Py_ssize_t n) |
1789 | 5.48M | { |
1790 | 5.48M | Py_ssize_t current_size; |
1791 | | |
1792 | 5.48M | current_size = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t); |
1793 | 5.48M | if (n <= current_size) { |
1794 | | /* Fast path: the data to read is fully buffered. */ |
1795 | 5.44M | PyObject *res = PyBytes_FromStringAndSize(self->buffer + self->pos, n); |
1796 | 5.44M | if (res != NULL) |
1797 | 5.44M | self->pos += n; |
1798 | 5.44M | return res; |
1799 | 5.44M | } |
1800 | 5.48M | Py_RETURN_NONE; |
1801 | 5.48M | } |
1802 | | |
1803 | | /* Generic read function: read from the stream until enough bytes are read, |
1804 | | * or until an EOF occurs or until read() would block. |
1805 | | */ |
1806 | | static PyObject * |
1807 | | _bufferedreader_read_generic(buffered *self, Py_ssize_t n) |
1808 | 34.9k | { |
1809 | 34.9k | Py_ssize_t current_size, remaining, written; |
1810 | | |
1811 | 34.9k | current_size = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t); |
1812 | 34.9k | if (n <= current_size) |
1813 | 0 | return _bufferedreader_read_fast(self, n); |
1814 | | |
1815 | 34.9k | PyBytesWriter *writer = PyBytesWriter_Create(n); |
1816 | 34.9k | if (writer == NULL) { |
1817 | 0 | goto error; |
1818 | 0 | } |
1819 | 34.9k | char *out = PyBytesWriter_GetData(writer); |
1820 | | |
1821 | 34.9k | remaining = n; |
1822 | 34.9k | written = 0; |
1823 | 34.9k | if (current_size > 0) { |
1824 | 9.93k | memcpy(out, self->buffer + self->pos, current_size); |
1825 | 9.93k | remaining -= current_size; |
1826 | 9.93k | written += current_size; |
1827 | 9.93k | self->pos += current_size; |
1828 | 9.93k | } |
1829 | | /* Flush the write buffer if necessary */ |
1830 | 34.9k | if (self->writable) { |
1831 | 0 | PyObject *r = buffered_flush_and_rewind_unlocked(self); |
1832 | 0 | if (r == NULL) |
1833 | 0 | goto error; |
1834 | 0 | Py_DECREF(r); |
1835 | 0 | } |
1836 | 34.9k | _bufferedreader_reset_buf(self); |
1837 | 34.9k | while (remaining > 0) { |
1838 | | /* We want to read a whole block at the end into buffer. |
1839 | | If we had readv() we could do this in one pass. */ |
1840 | 34.9k | Py_ssize_t r = MINUS_LAST_BLOCK(self, remaining); |
1841 | 34.9k | if (r == 0) |
1842 | 34.9k | break; |
1843 | 0 | r = _bufferedreader_raw_read(self, out + written, r); |
1844 | 0 | if (r == -1) |
1845 | 0 | goto error; |
1846 | 0 | if (r == 0 || r == -2) { |
1847 | | /* EOF occurred or read() would block. */ |
1848 | 0 | if (r == 0 || written > 0) { |
1849 | 0 | return PyBytesWriter_FinishWithSize(writer, written); |
1850 | 0 | } |
1851 | 0 | PyBytesWriter_Discard(writer); |
1852 | 0 | Py_RETURN_NONE; |
1853 | 0 | } |
1854 | 0 | remaining -= r; |
1855 | 0 | written += r; |
1856 | 0 | } |
1857 | 34.9k | assert(remaining <= self->buffer_size); |
1858 | 34.9k | self->pos = 0; |
1859 | 34.9k | self->raw_pos = 0; |
1860 | 34.9k | self->read_end = 0; |
1861 | | /* NOTE: when the read is satisfied, we avoid issuing any additional |
1862 | | reads, which could block indefinitely (e.g. on a socket). |
1863 | | See issue #9550. */ |
1864 | 53.7k | while (remaining > 0 && self->read_end < self->buffer_size) { |
1865 | 53.7k | Py_ssize_t r = _bufferedreader_fill_buffer(self); |
1866 | 53.7k | if (r == -1) |
1867 | 5.50k | goto error; |
1868 | 48.2k | if (r == 0 || r == -2) { |
1869 | | /* EOF occurred or read() would block. */ |
1870 | 315 | if (r == 0 || written > 0) { |
1871 | 315 | return PyBytesWriter_FinishWithSize(writer, written); |
1872 | 315 | } |
1873 | 0 | PyBytesWriter_Discard(writer); |
1874 | 0 | Py_RETURN_NONE; |
1875 | 315 | } |
1876 | 47.9k | if (remaining > r) { |
1877 | 18.7k | memcpy(out + written, self->buffer + self->pos, r); |
1878 | 18.7k | written += r; |
1879 | 18.7k | self->pos += r; |
1880 | 18.7k | remaining -= r; |
1881 | 18.7k | } |
1882 | 29.1k | else if (remaining > 0) { |
1883 | 29.1k | memcpy(out + written, self->buffer + self->pos, remaining); |
1884 | 29.1k | written += remaining; |
1885 | 29.1k | self->pos += remaining; |
1886 | 29.1k | remaining = 0; |
1887 | 29.1k | } |
1888 | 47.9k | if (remaining == 0) |
1889 | 29.1k | break; |
1890 | 47.9k | } |
1891 | | |
1892 | 29.1k | return PyBytesWriter_Finish(writer); |
1893 | | |
1894 | 5.50k | error: |
1895 | 5.50k | PyBytesWriter_Discard(writer); |
1896 | 5.50k | return NULL; |
1897 | 34.9k | } |
1898 | | |
1899 | | static PyObject * |
1900 | | _bufferedreader_peek_unlocked(buffered *self) |
1901 | 0 | { |
1902 | 0 | Py_ssize_t have, r; |
1903 | |
|
1904 | 0 | have = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t); |
1905 | | /* Constraints: |
1906 | | 1. we don't want to advance the file position. |
1907 | | 2. we don't want to lose block alignment, so we can't shift the buffer |
1908 | | to make some place. |
1909 | | Therefore, we either return `have` bytes (if > 0), or a full buffer. |
1910 | | */ |
1911 | 0 | if (have > 0) { |
1912 | 0 | return PyBytes_FromStringAndSize(self->buffer + self->pos, have); |
1913 | 0 | } |
1914 | | |
1915 | | /* Fill the buffer from the raw stream, and copy it to the result. */ |
1916 | 0 | _bufferedreader_reset_buf(self); |
1917 | 0 | r = _bufferedreader_fill_buffer(self); |
1918 | 0 | if (r == -1) |
1919 | 0 | return NULL; |
1920 | 0 | if (r == -2) |
1921 | 0 | r = 0; |
1922 | 0 | self->pos = 0; |
1923 | 0 | return PyBytes_FromStringAndSize(self->buffer, r); |
1924 | 0 | } |
1925 | | |
1926 | | |
1927 | | /* |
1928 | | * class BufferedWriter |
1929 | | */ |
1930 | | static void |
1931 | | _bufferedwriter_reset_buf(buffered *self) |
1932 | 216k | { |
1933 | 216k | self->write_pos = 0; |
1934 | 216k | self->write_end = -1; |
1935 | 216k | } |
1936 | | |
1937 | | /*[clinic input] |
1938 | | _io.BufferedWriter.__init__ |
1939 | | raw: object |
1940 | | buffer_size: Py_ssize_t(c_default="DEFAULT_BUFFER_SIZE") = DEFAULT_BUFFER_SIZE |
1941 | | |
1942 | | A buffer for a writeable sequential RawIO object. |
1943 | | |
1944 | | The constructor creates a BufferedWriter for the given writeable raw |
1945 | | stream. If the buffer_size is not given, it defaults to |
1946 | | DEFAULT_BUFFER_SIZE. |
1947 | | [clinic start generated code]*/ |
1948 | | |
1949 | | static int |
1950 | | _io_BufferedWriter___init___impl(buffered *self, PyObject *raw, |
1951 | | Py_ssize_t buffer_size) |
1952 | | /*[clinic end generated code: output=c8942a020c0dee64 input=914be9b95e16007b]*/ |
1953 | 20.1k | { |
1954 | 20.1k | self->ok = 0; |
1955 | 20.1k | self->detached = 0; |
1956 | | |
1957 | 20.1k | _PyIO_State *state = find_io_state_by_def(Py_TYPE(self)); |
1958 | 20.1k | if (_PyIOBase_check_writable(state, raw, Py_True) == NULL) { |
1959 | 0 | return -1; |
1960 | 0 | } |
1961 | | |
1962 | 20.1k | Py_INCREF(raw); |
1963 | 20.1k | Py_XSETREF(self->raw, raw); |
1964 | 20.1k | self->readable = 0; |
1965 | 20.1k | self->writable = 1; |
1966 | | |
1967 | 20.1k | self->buffer_size = buffer_size; |
1968 | 20.1k | if (_buffered_init(self) < 0) |
1969 | 0 | return -1; |
1970 | 20.1k | _bufferedwriter_reset_buf(self); |
1971 | 20.1k | self->pos = 0; |
1972 | | |
1973 | 20.1k | self->fast_closed_checks = ( |
1974 | 20.1k | Py_IS_TYPE(self, state->PyBufferedWriter_Type) && |
1975 | 20.1k | Py_IS_TYPE(raw, state->PyFileIO_Type) |
1976 | 20.1k | ); |
1977 | | |
1978 | 20.1k | self->ok = 1; |
1979 | 20.1k | return 0; |
1980 | 20.1k | } |
1981 | | |
1982 | | static Py_ssize_t |
1983 | | _bufferedwriter_raw_write(buffered *self, char *start, Py_ssize_t len) |
1984 | 195k | { |
1985 | 195k | Py_buffer buf; |
1986 | 195k | PyObject *memobj, *res; |
1987 | 195k | Py_ssize_t n; |
1988 | 195k | int errnum; |
1989 | | /* NOTE: the buffer needn't be released as its object is NULL. */ |
1990 | 195k | if (PyBuffer_FillInfo(&buf, NULL, start, len, 1, PyBUF_CONTIG_RO) == -1) |
1991 | 0 | return -1; |
1992 | 195k | memobj = PyMemoryView_FromBuffer(&buf); |
1993 | 195k | if (memobj == NULL) |
1994 | 0 | return -1; |
1995 | | /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() when EINTR |
1996 | | occurs so we needn't do it ourselves. |
1997 | | We then retry writing, ignoring the signal if no handler has |
1998 | | raised (see issue #10956). |
1999 | | */ |
2000 | 195k | do { |
2001 | 195k | errno = 0; |
2002 | 195k | res = PyObject_CallMethodOneArg(self->raw, &_Py_ID(write), memobj); |
2003 | 195k | errnum = errno; |
2004 | 195k | } while (res == NULL && _PyIO_trap_eintr()); |
2005 | 195k | Py_DECREF(memobj); |
2006 | 195k | if (res == NULL) |
2007 | 0 | return -1; |
2008 | 195k | if (res == Py_None) { |
2009 | | /* Non-blocking stream would have blocked. Special return code! |
2010 | | Being paranoid we reset errno in case it is changed by code |
2011 | | triggered by a decref. errno is used by _set_BlockingIOError(). */ |
2012 | 0 | Py_DECREF(res); |
2013 | 0 | errno = errnum; |
2014 | 0 | return -2; |
2015 | 0 | } |
2016 | 195k | n = PyNumber_AsSsize_t(res, PyExc_ValueError); |
2017 | 195k | Py_DECREF(res); |
2018 | 195k | if (n < 0 || n > len) { |
2019 | 0 | PyErr_Format(PyExc_OSError, |
2020 | 0 | "raw write() returned invalid length %zd " |
2021 | 0 | "(should have been between 0 and %zd)", n, len); |
2022 | 0 | return -1; |
2023 | 0 | } |
2024 | 195k | if (n > 0 && self->abs_pos != -1) |
2025 | 195k | self->abs_pos += n; |
2026 | 195k | return n; |
2027 | 195k | } |
2028 | | |
2029 | | static PyObject * |
2030 | | _bufferedwriter_flush_unlocked(buffered *self) |
2031 | 196k | { |
2032 | 196k | Py_off_t n, rewind; |
2033 | | |
2034 | 196k | if (!VALID_WRITE_BUFFER(self) || self->write_pos == self->write_end) |
2035 | 1.14k | goto end; |
2036 | | /* First, rewind */ |
2037 | 195k | rewind = RAW_OFFSET(self) + (self->pos - self->write_pos); |
2038 | 195k | if (rewind != 0) { |
2039 | 0 | n = _buffered_raw_seek(self, -rewind, 1); |
2040 | 0 | if (n < 0) { |
2041 | 0 | goto error; |
2042 | 0 | } |
2043 | 0 | self->raw_pos -= rewind; |
2044 | 0 | } |
2045 | 390k | while (self->write_pos < self->write_end) { |
2046 | 195k | n = _bufferedwriter_raw_write(self, |
2047 | 195k | self->buffer + self->write_pos, |
2048 | 195k | Py_SAFE_DOWNCAST(self->write_end - self->write_pos, |
2049 | 195k | Py_off_t, Py_ssize_t)); |
2050 | 195k | if (n == -1) { |
2051 | 0 | goto error; |
2052 | 0 | } |
2053 | 195k | else if (n == -2) { |
2054 | 0 | _set_BlockingIOError("write could not complete without blocking", |
2055 | 0 | 0); |
2056 | 0 | goto error; |
2057 | 0 | } |
2058 | 195k | self->write_pos += n; |
2059 | 195k | self->raw_pos = self->write_pos; |
2060 | | /* Partial writes can return successfully when interrupted by a |
2061 | | signal (see write(2)). We must run signal handlers before |
2062 | | blocking another time, possibly indefinitely. */ |
2063 | 195k | if (PyErr_CheckSignals() < 0) |
2064 | 0 | goto error; |
2065 | 195k | } |
2066 | | |
2067 | | |
2068 | 196k | end: |
2069 | | /* This ensures that after return from this function, |
2070 | | VALID_WRITE_BUFFER(self) returns false. |
2071 | | |
2072 | | This is a required condition because when a tell() is called |
2073 | | after flushing and if VALID_READ_BUFFER(self) is false, we need |
2074 | | VALID_WRITE_BUFFER(self) to be false to have |
2075 | | RAW_OFFSET(self) == 0. |
2076 | | |
2077 | | Issue: https://bugs.python.org/issue32228 */ |
2078 | 196k | _bufferedwriter_reset_buf(self); |
2079 | 196k | Py_RETURN_NONE; |
2080 | | |
2081 | 0 | error: |
2082 | 0 | return NULL; |
2083 | 195k | } |
2084 | | |
2085 | | /*[clinic input] |
2086 | | @critical_section |
2087 | | _io.BufferedWriter.write |
2088 | | buffer: Py_buffer |
2089 | | / |
2090 | | [clinic start generated code]*/ |
2091 | | |
2092 | | static PyObject * |
2093 | | _io_BufferedWriter_write_impl(buffered *self, Py_buffer *buffer) |
2094 | | /*[clinic end generated code: output=7f8d1365759bfc6b input=6a9c041de0c337be]*/ |
2095 | 195k | { |
2096 | 195k | PyObject *res = NULL; |
2097 | 195k | Py_ssize_t written, avail, remaining; |
2098 | 195k | Py_off_t offset; |
2099 | 195k | int r; |
2100 | | |
2101 | 195k | CHECK_INITIALIZED(self) |
2102 | | |
2103 | 195k | if (!ENTER_BUFFERED(self)) |
2104 | 0 | return NULL; |
2105 | | |
2106 | | /* Issue #31976: Check for closed file after acquiring the lock. Another |
2107 | | thread could be holding the lock while closing the file. */ |
2108 | 195k | r = IS_CLOSED(self); |
2109 | 195k | if (r < 0) { |
2110 | 0 | goto error; |
2111 | 0 | } |
2112 | 195k | if (r > 0) { |
2113 | 0 | PyErr_SetString(PyExc_ValueError, "write to closed file"); |
2114 | 0 | goto error; |
2115 | 0 | } |
2116 | | |
2117 | | /* Fast path: the data to write can be fully buffered. */ |
2118 | 195k | if (!VALID_READ_BUFFER(self) && !VALID_WRITE_BUFFER(self)) { |
2119 | 195k | self->pos = 0; |
2120 | 195k | self->raw_pos = 0; |
2121 | 195k | } |
2122 | 195k | avail = Py_SAFE_DOWNCAST(self->buffer_size - self->pos, Py_off_t, Py_ssize_t); |
2123 | 195k | if (buffer->len <= avail && buffer->len < self->buffer_size) { |
2124 | 195k | memcpy(self->buffer + self->pos, buffer->buf, buffer->len); |
2125 | 195k | if (!VALID_WRITE_BUFFER(self) || self->write_pos > self->pos) { |
2126 | 195k | self->write_pos = self->pos; |
2127 | 195k | } |
2128 | 195k | ADJUST_POSITION(self, self->pos + buffer->len); |
2129 | 195k | if (self->pos > self->write_end) |
2130 | 195k | self->write_end = self->pos; |
2131 | 195k | written = buffer->len; |
2132 | 195k | goto end; |
2133 | 195k | } |
2134 | | |
2135 | | /* First write the current buffer */ |
2136 | 568 | res = _bufferedwriter_flush_unlocked(self); |
2137 | 568 | if (res == NULL) { |
2138 | 0 | Py_ssize_t *w = _buffered_check_blocking_error(); |
2139 | 0 | if (w == NULL) |
2140 | 0 | goto error; |
2141 | 0 | if (self->readable) |
2142 | 0 | _bufferedreader_reset_buf(self); |
2143 | | /* Make some place by shifting the buffer. */ |
2144 | 0 | assert(VALID_WRITE_BUFFER(self)); |
2145 | 0 | memmove(self->buffer, self->buffer + self->write_pos, |
2146 | 0 | Py_SAFE_DOWNCAST(self->write_end - self->write_pos, |
2147 | 0 | Py_off_t, Py_ssize_t)); |
2148 | 0 | self->write_end -= self->write_pos; |
2149 | 0 | self->raw_pos -= self->write_pos; |
2150 | 0 | self->pos -= self->write_pos; |
2151 | 0 | self->write_pos = 0; |
2152 | 0 | avail = Py_SAFE_DOWNCAST(self->buffer_size - self->write_end, |
2153 | 0 | Py_off_t, Py_ssize_t); |
2154 | 0 | if (buffer->len <= avail) { |
2155 | | /* Everything can be buffered */ |
2156 | 0 | PyErr_Clear(); |
2157 | 0 | memcpy(self->buffer + self->write_end, buffer->buf, buffer->len); |
2158 | 0 | self->write_end += buffer->len; |
2159 | 0 | self->pos += buffer->len; |
2160 | 0 | written = buffer->len; |
2161 | 0 | goto end; |
2162 | 0 | } |
2163 | | /* Buffer as much as possible. */ |
2164 | 0 | memcpy(self->buffer + self->write_end, buffer->buf, avail); |
2165 | 0 | self->write_end += avail; |
2166 | 0 | self->pos += avail; |
2167 | | /* XXX Modifying the existing exception e using the pointer w |
2168 | | will change e.characters_written but not e.args[2]. |
2169 | | Therefore we just replace with a new error. */ |
2170 | 0 | _set_BlockingIOError("write could not complete without blocking", |
2171 | 0 | avail); |
2172 | 0 | goto error; |
2173 | 0 | } |
2174 | 568 | Py_CLEAR(res); |
2175 | | |
2176 | | /* Adjust the raw stream position if it is away from the logical stream |
2177 | | position. This happens if the read buffer has been filled but not |
2178 | | modified (and therefore _bufferedwriter_flush_unlocked() didn't rewind |
2179 | | the raw stream by itself). |
2180 | | Fixes issue #6629. |
2181 | | */ |
2182 | 568 | offset = RAW_OFFSET(self); |
2183 | 568 | if (offset != 0) { |
2184 | 0 | if (_buffered_raw_seek(self, -offset, 1) < 0) |
2185 | 0 | goto error; |
2186 | 0 | self->raw_pos -= offset; |
2187 | 0 | } |
2188 | | |
2189 | | /* Then write buf itself. At this point the buffer has been emptied. */ |
2190 | 568 | remaining = buffer->len; |
2191 | 568 | written = 0; |
2192 | 1.13k | while (remaining >= self->buffer_size) { |
2193 | 568 | Py_ssize_t n = _bufferedwriter_raw_write( |
2194 | 568 | self, (char *) buffer->buf + written, buffer->len - written); |
2195 | 568 | if (n == -1) { |
2196 | 0 | goto error; |
2197 | 568 | } else if (n == -2) { |
2198 | | /* Write failed because raw file is non-blocking */ |
2199 | 0 | if (remaining > self->buffer_size) { |
2200 | | /* Can't buffer everything, still buffer as much as possible */ |
2201 | 0 | memcpy(self->buffer, |
2202 | 0 | (char *) buffer->buf + written, self->buffer_size); |
2203 | 0 | self->raw_pos = 0; |
2204 | 0 | ADJUST_POSITION(self, self->buffer_size); |
2205 | 0 | self->write_end = self->buffer_size; |
2206 | 0 | written += self->buffer_size; |
2207 | 0 | _set_BlockingIOError("write could not complete without " |
2208 | 0 | "blocking", written); |
2209 | 0 | goto error; |
2210 | 0 | } |
2211 | 0 | PyErr_Clear(); |
2212 | 0 | break; |
2213 | 0 | } |
2214 | 568 | written += n; |
2215 | 568 | remaining -= n; |
2216 | | /* Partial writes can return successfully when interrupted by a |
2217 | | signal (see write(2)). We must run signal handlers before |
2218 | | blocking another time, possibly indefinitely. */ |
2219 | 568 | if (PyErr_CheckSignals() < 0) |
2220 | 0 | goto error; |
2221 | 568 | } |
2222 | 568 | if (self->readable) |
2223 | 0 | _bufferedreader_reset_buf(self); |
2224 | 568 | if (remaining > 0) { |
2225 | 0 | memcpy(self->buffer, (char *) buffer->buf + written, remaining); |
2226 | 0 | written += remaining; |
2227 | 0 | } |
2228 | 568 | self->write_pos = 0; |
2229 | | /* TODO: sanity check (remaining >= 0) */ |
2230 | 568 | self->write_end = remaining; |
2231 | 568 | ADJUST_POSITION(self, remaining); |
2232 | 568 | self->raw_pos = 0; |
2233 | | |
2234 | 195k | end: |
2235 | 195k | res = PyLong_FromSsize_t(written); |
2236 | | |
2237 | 195k | error: |
2238 | 195k | LEAVE_BUFFERED(self) |
2239 | 195k | return res; |
2240 | 195k | } |
2241 | | |
2242 | | |
2243 | | /* |
2244 | | * BufferedRWPair |
2245 | | */ |
2246 | | |
2247 | | /* XXX The usefulness of this (compared to having two separate IO objects) is |
2248 | | * questionable. |
2249 | | */ |
2250 | | |
2251 | | typedef struct { |
2252 | | PyObject_HEAD |
2253 | | buffered *reader; |
2254 | | buffered *writer; |
2255 | | } rwpair; |
2256 | | |
2257 | 0 | #define rwpair_CAST(op) ((rwpair *)(op)) |
2258 | | |
2259 | | /*[clinic input] |
2260 | | _io.BufferedRWPair.__init__ |
2261 | | reader: object |
2262 | | writer: object |
2263 | | buffer_size: Py_ssize_t(c_default="DEFAULT_BUFFER_SIZE") = DEFAULT_BUFFER_SIZE |
2264 | | / |
2265 | | |
2266 | | A buffered reader and writer object together. |
2267 | | |
2268 | | A buffered reader object and buffered writer object put together to |
2269 | | form a sequential IO object that can read and write. This is typically |
2270 | | used with a socket or two-way pipe. |
2271 | | |
2272 | | reader and writer are RawIOBase objects that are readable and |
2273 | | writeable respectively. If the buffer_size is omitted it defaults to |
2274 | | DEFAULT_BUFFER_SIZE. |
2275 | | [clinic start generated code]*/ |
2276 | | |
2277 | | static int |
2278 | | _io_BufferedRWPair___init___impl(rwpair *self, PyObject *reader, |
2279 | | PyObject *writer, Py_ssize_t buffer_size) |
2280 | | /*[clinic end generated code: output=327e73d1aee8f984 input=620d42d71f33a031]*/ |
2281 | 0 | { |
2282 | 0 | _PyIO_State *state = find_io_state_by_def(Py_TYPE(self)); |
2283 | 0 | if (_PyIOBase_check_readable(state, reader, Py_True) == NULL) { |
2284 | 0 | return -1; |
2285 | 0 | } |
2286 | 0 | if (_PyIOBase_check_writable(state, writer, Py_True) == NULL) { |
2287 | 0 | return -1; |
2288 | 0 | } |
2289 | | |
2290 | 0 | buffered *new_reader = (buffered *) PyObject_CallFunction( |
2291 | 0 | (PyObject *)state->PyBufferedReader_Type, |
2292 | 0 | "On", reader, buffer_size); |
2293 | 0 | if (new_reader == NULL) { |
2294 | 0 | return -1; |
2295 | 0 | } |
2296 | | |
2297 | 0 | buffered *new_writer = (buffered *) PyObject_CallFunction( |
2298 | 0 | (PyObject *)state->PyBufferedWriter_Type, |
2299 | 0 | "On", writer, buffer_size); |
2300 | 0 | if (new_writer == NULL) { |
2301 | 0 | Py_DECREF(new_reader); |
2302 | 0 | return -1; |
2303 | 0 | } |
2304 | | |
2305 | | /* Release a previous writer before a previous reader, see |
2306 | | bufferedrwpair_clear(). */ |
2307 | 0 | Py_XSETREF(self->writer, new_writer); |
2308 | 0 | Py_XSETREF(self->reader, new_reader); |
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->reader); |
2318 | 0 | Py_VISIT(self->writer); |
2319 | 0 | return PyObject_VisitManagedDict(op, visit, arg); |
2320 | 0 | } |
2321 | | |
2322 | | static int |
2323 | | bufferedrwpair_clear(PyObject *op) |
2324 | 0 | { |
2325 | 0 | rwpair *self = rwpair_CAST(op); |
2326 | | /* Let the writer flush before the reader closes a shared raw stream. */ |
2327 | 0 | Py_CLEAR(self->writer); |
2328 | 0 | Py_CLEAR(self->reader); |
2329 | 0 | PyObject_ClearManagedDict(op); |
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 | PyObject_ClearWeakRefs(op); |
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 | | {NULL} |
2666 | | }; |
2667 | | |
2668 | | static PyGetSetDef bufferedreader_getset[] = { |
2669 | | _IO__BUFFERED_CLOSED_GETSETDEF |
2670 | | _IO__BUFFERED_NAME_GETSETDEF |
2671 | | _IO__BUFFERED_MODE_GETSETDEF |
2672 | | {NULL} |
2673 | | }; |
2674 | | |
2675 | | |
2676 | | static PyType_Slot bufferedreader_slots[] = { |
2677 | | {Py_tp_dealloc, buffered_dealloc}, |
2678 | | {Py_tp_repr, buffered_repr}, |
2679 | | {Py_tp_doc, (void *)_io_BufferedReader___init____doc__}, |
2680 | | {Py_tp_traverse, buffered_traverse}, |
2681 | | {Py_tp_clear, buffered_clear}, |
2682 | | {Py_tp_iternext, buffered_iternext}, |
2683 | | {Py_tp_methods, bufferedreader_methods}, |
2684 | | {Py_tp_members, bufferedreader_members}, |
2685 | | {Py_tp_getset, bufferedreader_getset}, |
2686 | | {Py_tp_init, _io_BufferedReader___init__}, |
2687 | | {0, NULL}, |
2688 | | }; |
2689 | | |
2690 | | PyType_Spec _Py_bufferedreader_spec = { |
2691 | | .name = "_io.BufferedReader", |
2692 | | .basicsize = sizeof(buffered), |
2693 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | |
2694 | | Py_TPFLAGS_IMMUTABLETYPE), |
2695 | | .slots = bufferedreader_slots, |
2696 | | }; |
2697 | | |
2698 | | static PyMethodDef bufferedwriter_methods[] = { |
2699 | | /* BufferedIOMixin methods */ |
2700 | | _IO__BUFFERED_CLOSE_METHODDEF |
2701 | | _IO__BUFFERED_DETACH_METHODDEF |
2702 | | _IO__BUFFERED_SEEKABLE_METHODDEF |
2703 | | _IO__BUFFERED_WRITABLE_METHODDEF |
2704 | | _IO__BUFFERED_FILENO_METHODDEF |
2705 | | _IO__BUFFERED_ISATTY_METHODDEF |
2706 | | _IO__BUFFERED__DEALLOC_WARN_METHODDEF |
2707 | | |
2708 | | _IO_BUFFEREDWRITER_WRITE_METHODDEF |
2709 | | _IO__BUFFERED_TRUNCATE_METHODDEF |
2710 | | _IO__BUFFERED_FLUSH_METHODDEF |
2711 | | _IO__BUFFERED_SEEK_METHODDEF |
2712 | | _IO__BUFFERED_TELL_METHODDEF |
2713 | | _IO__BUFFERED___SIZEOF___METHODDEF |
2714 | | |
2715 | | {"__getstate__", _PyIOBase_cannot_pickle, METH_NOARGS}, |
2716 | | {NULL, NULL} |
2717 | | }; |
2718 | | |
2719 | | static PyMemberDef bufferedwriter_members[] = { |
2720 | | {"raw", _Py_T_OBJECT, offsetof(buffered, raw), Py_READONLY}, |
2721 | | {"_finalizing", Py_T_BOOL, offsetof(buffered, finalizing), 0}, |
2722 | | {NULL} |
2723 | | }; |
2724 | | |
2725 | | static PyGetSetDef bufferedwriter_getset[] = { |
2726 | | _IO__BUFFERED_CLOSED_GETSETDEF |
2727 | | _IO__BUFFERED_NAME_GETSETDEF |
2728 | | _IO__BUFFERED_MODE_GETSETDEF |
2729 | | {NULL} |
2730 | | }; |
2731 | | |
2732 | | |
2733 | | static PyType_Slot bufferedwriter_slots[] = { |
2734 | | {Py_tp_dealloc, buffered_dealloc}, |
2735 | | {Py_tp_repr, buffered_repr}, |
2736 | | {Py_tp_doc, (void *)_io_BufferedWriter___init____doc__}, |
2737 | | {Py_tp_traverse, buffered_traverse}, |
2738 | | {Py_tp_clear, buffered_clear}, |
2739 | | {Py_tp_methods, bufferedwriter_methods}, |
2740 | | {Py_tp_members, bufferedwriter_members}, |
2741 | | {Py_tp_getset, bufferedwriter_getset}, |
2742 | | {Py_tp_init, _io_BufferedWriter___init__}, |
2743 | | {0, NULL}, |
2744 | | }; |
2745 | | |
2746 | | PyType_Spec _Py_bufferedwriter_spec = { |
2747 | | .name = "_io.BufferedWriter", |
2748 | | .basicsize = sizeof(buffered), |
2749 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | |
2750 | | Py_TPFLAGS_IMMUTABLETYPE), |
2751 | | .slots = bufferedwriter_slots, |
2752 | | }; |
2753 | | |
2754 | | static PyMethodDef bufferedrwpair_methods[] = { |
2755 | | _IO_BUFFEREDRWPAIR_READ_METHODDEF |
2756 | | _IO_BUFFEREDRWPAIR_PEEK_METHODDEF |
2757 | | _IO_BUFFEREDRWPAIR_READ1_METHODDEF |
2758 | | _IO_BUFFEREDRWPAIR_READLINE_METHODDEF |
2759 | | _IO_BUFFEREDRWPAIR_READINTO_METHODDEF |
2760 | | _IO_BUFFEREDRWPAIR_READINTO1_METHODDEF |
2761 | | |
2762 | | _IO_BUFFEREDRWPAIR_WRITE_METHODDEF |
2763 | | _IO_BUFFEREDRWPAIR_FLUSH_METHODDEF |
2764 | | |
2765 | | _IO_BUFFEREDRWPAIR_READABLE_METHODDEF |
2766 | | _IO_BUFFEREDRWPAIR_WRITABLE_METHODDEF |
2767 | | |
2768 | | _IO_BUFFEREDRWPAIR_CLOSE_METHODDEF |
2769 | | _IO_BUFFEREDRWPAIR_ISATTY_METHODDEF |
2770 | | |
2771 | | {NULL, NULL} |
2772 | | }; |
2773 | | |
2774 | | static PyGetSetDef bufferedrwpair_getset[] = { |
2775 | | _IO_BUFFEREDRWPAIR_CLOSED_GETSETDEF |
2776 | | {NULL} |
2777 | | }; |
2778 | | |
2779 | | static PyType_Slot bufferedrwpair_slots[] = { |
2780 | | {Py_tp_dealloc, bufferedrwpair_dealloc}, |
2781 | | {Py_tp_doc, (void *)_io_BufferedRWPair___init____doc__}, |
2782 | | {Py_tp_traverse, bufferedrwpair_traverse}, |
2783 | | {Py_tp_clear, bufferedrwpair_clear}, |
2784 | | {Py_tp_methods, bufferedrwpair_methods}, |
2785 | | {Py_tp_getset, bufferedrwpair_getset}, |
2786 | | {Py_tp_init, _io_BufferedRWPair___init__}, |
2787 | | {0, NULL}, |
2788 | | }; |
2789 | | |
2790 | | PyType_Spec _Py_bufferedrwpair_spec = { |
2791 | | .name = "_io.BufferedRWPair", |
2792 | | .basicsize = sizeof(rwpair), |
2793 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | |
2794 | | Py_TPFLAGS_IMMUTABLETYPE), |
2795 | | .slots = bufferedrwpair_slots, |
2796 | | }; |
2797 | | |
2798 | | |
2799 | | static PyMethodDef bufferedrandom_methods[] = { |
2800 | | /* BufferedIOMixin methods */ |
2801 | | _IO__BUFFERED_CLOSE_METHODDEF |
2802 | | _IO__BUFFERED_DETACH_METHODDEF |
2803 | | _IO__BUFFERED_SEEKABLE_METHODDEF |
2804 | | _IO__BUFFERED_READABLE_METHODDEF |
2805 | | _IO__BUFFERED_WRITABLE_METHODDEF |
2806 | | _IO__BUFFERED_FILENO_METHODDEF |
2807 | | _IO__BUFFERED_ISATTY_METHODDEF |
2808 | | _IO__BUFFERED__DEALLOC_WARN_METHODDEF |
2809 | | |
2810 | | _IO__BUFFERED_FLUSH_METHODDEF |
2811 | | |
2812 | | _IO__BUFFERED_SEEK_METHODDEF |
2813 | | _IO__BUFFERED_TELL_METHODDEF |
2814 | | _IO__BUFFERED_TRUNCATE_METHODDEF |
2815 | | _IO__BUFFERED_READ_METHODDEF |
2816 | | _IO__BUFFERED_READ1_METHODDEF |
2817 | | _IO__BUFFERED_READINTO_METHODDEF |
2818 | | _IO__BUFFERED_READINTO1_METHODDEF |
2819 | | _IO__BUFFERED_READLINE_METHODDEF |
2820 | | _IO__BUFFERED_PEEK_METHODDEF |
2821 | | _IO_BUFFEREDWRITER_WRITE_METHODDEF |
2822 | | _IO__BUFFERED___SIZEOF___METHODDEF |
2823 | | |
2824 | | {"__getstate__", _PyIOBase_cannot_pickle, METH_NOARGS}, |
2825 | | {NULL, NULL} |
2826 | | }; |
2827 | | |
2828 | | static PyMemberDef bufferedrandom_members[] = { |
2829 | | {"raw", _Py_T_OBJECT, offsetof(buffered, raw), Py_READONLY}, |
2830 | | {"_finalizing", Py_T_BOOL, offsetof(buffered, finalizing), 0}, |
2831 | | {NULL} |
2832 | | }; |
2833 | | |
2834 | | static PyGetSetDef bufferedrandom_getset[] = { |
2835 | | _IO__BUFFERED_CLOSED_GETSETDEF |
2836 | | _IO__BUFFERED_NAME_GETSETDEF |
2837 | | _IO__BUFFERED_MODE_GETSETDEF |
2838 | | {NULL} |
2839 | | }; |
2840 | | |
2841 | | |
2842 | | static PyType_Slot bufferedrandom_slots[] = { |
2843 | | {Py_tp_dealloc, buffered_dealloc}, |
2844 | | {Py_tp_repr, buffered_repr}, |
2845 | | {Py_tp_doc, (void *)_io_BufferedRandom___init____doc__}, |
2846 | | {Py_tp_traverse, buffered_traverse}, |
2847 | | {Py_tp_clear, buffered_clear}, |
2848 | | {Py_tp_iternext, buffered_iternext}, |
2849 | | {Py_tp_methods, bufferedrandom_methods}, |
2850 | | {Py_tp_members, bufferedrandom_members}, |
2851 | | {Py_tp_getset, bufferedrandom_getset}, |
2852 | | {Py_tp_init, _io_BufferedRandom___init__}, |
2853 | | {0, NULL}, |
2854 | | }; |
2855 | | |
2856 | | PyType_Spec _Py_bufferedrandom_spec = { |
2857 | | .name = "_io.BufferedRandom", |
2858 | | .basicsize = sizeof(buffered), |
2859 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | |
2860 | | Py_TPFLAGS_IMMUTABLETYPE), |
2861 | | .slots = bufferedrandom_slots, |
2862 | | }; |