Debian Codegraph @

Modules/_io/bufferedio.c

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