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 Py_BEGIN_CRITICAL_SECTION(self);
1501 line = _buffered_readline(self, -1);
1502 Py_END_CRITICAL_SECTION();
1503 }
1504 else {
1505 line = PyObject_CallMethodNoArgs((PyObject *)self,
1506 &_Py_ID(readline));
1507 if (line && !PyBytes_Check(line)) {
1508 PyErr_Format(PyExc_OSError,
1509 "readline() should have returned a bytes object, "
1510 "not '%.200s'", Py_TYPE(line)->tp_name);
1511 Py_DECREF(line);
1512 return NULL;
1513 }
1514 }
1515
1516 if (line == NULL)
1517 return NULL;
1518
1519 if (PyBytes_GET_SIZE(line) == 0) {
1520 /* Reached EOF or would have blocked */
1521 Py_DECREF(line);
1522 return NULL;
1523 }
1524
1525 return line;
1526 }
1527
1528 static PyObject *
1529 buffered_repr(PyObject *op)
1530 {
1531 buffered *self = buffered_CAST(op);
1532 PyObject *nameobj, *res;
1533
1534 if (PyObject_GetOptionalAttr((PyObject *) self, &_Py_ID(name), &nameobj) < 0) {
1535 if (!PyErr_ExceptionMatches(PyExc_ValueError)) {
1536 return NULL;
1537 }
1538 /* Ignore ValueError raised if the underlying stream was detached */
1539 PyErr_Clear();
1540 }
1541 if (nameobj == NULL) {
1542 res = PyUnicode_FromFormat("<%s>", Py_TYPE(self)->tp_name);
1543 }
1544 else {
1545 int status = Py_ReprEnter((PyObject *)self);
1546 res = NULL;
1547 if (status == 0) {
1548 res = PyUnicode_FromFormat("<%s name=%R>",
1549 Py_TYPE(self)->tp_name, nameobj);
1550 Py_ReprLeave((PyObject *)self);
1551 }
1552 else if (status > 0) {
1553 PyErr_Format(PyExc_RuntimeError,
1554 "reentrant call inside %s.__repr__",
1555 Py_TYPE(self)->tp_name);
1556 }
1557 Py_DECREF(nameobj);
1558 }
1559 return res;
1560 }
1561
1562 /*
1563 * class BufferedReader
1564 */
1565
1566 static void _bufferedreader_reset_buf(buffered *self)
1567 {
1568 self->read_end = -1;
1569 }
1570
1571 /*[clinic input]
1572 _io.BufferedReader.__init__
1573 raw: object
1574 buffer_size: Py_ssize_t(c_default="DEFAULT_BUFFER_SIZE") = DEFAULT_BUFFER_SIZE
1575
1576 Create a new buffered reader using the given readable raw IO object.
1577 [clinic start generated code]*/
1578
1579 static int
1580 _io_BufferedReader___init___impl(buffered *self, PyObject *raw,
1581 Py_ssize_t buffer_size)
1582 /*[clinic end generated code: output=cddcfefa0ed294c4 input=fb887e06f11b4e48]*/
1583 {
1584 self->ok = 0;
1585 self->detached = 0;
1586
1587 _PyIO_State *state = find_io_state_by_def(Py_TYPE(self));
1588 if (_PyIOBase_check_readable(state, raw, Py_True) == NULL) {
1589 return -1;
1590 }
1591
1592 Py_XSETREF(self->raw, Py_NewRef(raw));
1593 self->buffer_size = buffer_size;
1594 self->readable = 1;
1595 self->writable = 0;
1596
1597 if (_buffered_init(self) < 0)
1598 return -1;
1599 _bufferedreader_reset_buf(self);
1600
1601 self->fast_closed_checks = (
1602 Py_IS_TYPE(self, state->PyBufferedReader_Type) &&
1603 Py_IS_TYPE(raw, state->PyFileIO_Type)
1604 );
1605
1606 self->ok = 1;
1607 return 0;
1608 }
1609
1610 static Py_ssize_t
1611 _bufferedreader_raw_read(buffered *self, char *start, Py_ssize_t len)
1612 {
1613 Py_buffer buf;
1614 PyObject *memobj, *res;
1615 Py_ssize_t n;
1616 /* NOTE: the buffer needn't be released as its object is NULL. */
1617 if (PyBuffer_FillInfo(&buf, NULL, start, len, 0, PyBUF_CONTIG) == -1)
1618 return -1;
1619 memobj = PyMemoryView_FromBuffer(&buf);
1620 if (memobj == NULL)
1621 return -1;
1622 /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() when EINTR
1623 occurs so we needn't do it ourselves.
1624 We then retry reading, ignoring the signal if no handler has
1625 raised (see issue #10956).
1626 */
1627 do {
1628 res = PyObject_CallMethodOneArg(self->raw, &_Py_ID(readinto), memobj);
1629 } while (res == NULL && _PyIO_trap_eintr());
1630 Py_DECREF(memobj);
1631 if (res == NULL)
1632 return -1;
1633 if (res == Py_None) {
1634 /* Non-blocking stream would have blocked. Special return code! */
1635 Py_DECREF(res);
1636 return -2;
1637 }
1638 n = PyNumber_AsSsize_t(res, PyExc_ValueError);
1639 Py_DECREF(res);
1640
1641 if (n == -1 && PyErr_Occurred()) {
1642 _PyErr_FormatFromCause(
1643 PyExc_OSError,
1644 "raw readinto() failed"
1645 );
1646 return -1;
1647 }
1648
1649 if (n < 0 || n > len) {
1650 PyErr_Format(PyExc_OSError,
1651 "raw readinto() returned invalid length %zd "
1652 "(should have been between 0 and %zd)", n, len);
1653 return -1;
1654 }
1655 if (n > 0 && self->abs_pos != -1)
1656 self->abs_pos += n;
1657 return n;
1658 }
1659
1660 static Py_ssize_t
1661 _bufferedreader_fill_buffer(buffered *self)
1662 {
1663 Py_ssize_t start, len, n;
1664 if (VALID_READ_BUFFER(self))
1665 start = Py_SAFE_DOWNCAST(self->read_end, Py_off_t, Py_ssize_t);
1666 else
1667 start = 0;
1668 len = self->buffer_size - start;
1669 n = _bufferedreader_raw_read(self, self->buffer + start, len);
1670 if (n <= 0)
1671 return n;
1672 self->read_end = start + n;
1673 self->raw_pos = start + n;
1674 return n;
1675 }
1676
1677 static PyObject *
1678 _bufferedreader_read_all(buffered *self)
1679 {
1680 Py_ssize_t current_size;
1681 PyObject *res = NULL, *data = NULL, *tmp = NULL, *chunks = NULL, *readall;
1682
1683 /* First copy what we have in the current buffer. */
1684 current_size = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
1685 if (current_size) {
1686 data = PyBytes_FromStringAndSize(
1687 self->buffer + self->pos, current_size);
1688 if (data == NULL)
1689 return NULL;
1690 self->pos += current_size;
1691 }
1692 /* We're going past the buffer's bounds, flush it */
1693 if (self->writable) {
1694 tmp = buffered_flush_and_rewind_unlocked(self);
1695 if (tmp == NULL)
1696 goto cleanup;
1697 Py_CLEAR(tmp);
1698 }
1699 _bufferedreader_reset_buf(self);
1700
1701 if (PyObject_GetOptionalAttr(self->raw, &_Py_ID(readall), &readall) < 0) {
1702 goto cleanup;
1703 }
1704 if (readall) {
1705 tmp = _PyObject_CallNoArgs(readall);
1706 Py_DECREF(readall);
1707 if (tmp == NULL)
1708 goto cleanup;
1709 if (tmp != Py_None && !PyBytes_Check(tmp)) {
1710 PyErr_SetString(PyExc_TypeError, "readall() should return bytes");
1711 goto cleanup;
1712 }
1713 if (current_size == 0) {
1714 res = tmp;
1715 } else {
1716 if (tmp != Py_None) {
1717 PyBytes_Concat(&data, tmp);
1718 }
1719 res = data;
1720 }
1721 goto cleanup;
1722 }
1723
1724 chunks = PyList_New(0);
1725 if (chunks == NULL)
1726 goto cleanup;
1727
1728 while (1) {
1729 if (data) {
1730 if (PyList_Append(chunks, data) < 0)
1731 goto cleanup;
1732 Py_CLEAR(data);
1733 }
1734
1735 /* Read until EOF or until read() would block. */
1736 data = PyObject_CallMethodNoArgs(self->raw, &_Py_ID(read));
1737 if (data == NULL)
1738 goto cleanup;
1739 if (data != Py_None && !PyBytes_Check(data)) {
1740 PyErr_SetString(PyExc_TypeError, "read() should return bytes");
1741 goto cleanup;
1742 }
1743 if (data == Py_None || PyBytes_GET_SIZE(data) == 0) {
1744 if (current_size == 0) {
1745 res = data;
1746 goto cleanup;
1747 }
1748 else {
1749 tmp = PyBytes_Join((PyObject *)&_Py_SINGLETON(bytes_empty), chunks);
1750 res = tmp;
1751 goto cleanup;
1752 }
1753 }
1754 current_size += PyBytes_GET_SIZE(data);
1755 if (self->abs_pos != -1)
1756 self->abs_pos += PyBytes_GET_SIZE(data);
1757 }
1758 cleanup:
1759 /* res is either NULL or a borrowed ref */
1760 Py_XINCREF(res);
1761 Py_XDECREF(data);
1762 Py_XDECREF(tmp);
1763 Py_XDECREF(chunks);
1764 return res;
1765 }
1766
1767 /* Read n bytes from the buffer if it can, otherwise return None.
1768 This function is simple enough that it can run unlocked. */
1769 static PyObject *
1770 _bufferedreader_read_fast(buffered *self, Py_ssize_t n)
1771 {
1772 Py_ssize_t current_size;
1773
1774 current_size = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
1775 if (n <= current_size) {
1776 /* Fast path: the data to read is fully buffered. */
1777 PyObject *res = PyBytes_FromStringAndSize(self->buffer + self->pos, n);
1778 if (res != NULL)
1779 self->pos += n;
1780 return res;
1781 }
1782 Py_RETURN_NONE;
1783 }
1784
1785 /* Generic read function: read from the stream until enough bytes are read,
1786 * or until an EOF occurs or until read() would block.
1787 */
1788 static PyObject *
1789 _bufferedreader_read_generic(buffered *self, Py_ssize_t n)
1790 {
1791 PyObject *res = NULL;
1792 Py_ssize_t current_size, remaining, written;
1793 char *out;
1794
1795 current_size = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
1796 if (n <= current_size)
1797 return _bufferedreader_read_fast(self, n);
1798
1799 res = PyBytes_FromStringAndSize(NULL, n);
1800 if (res == NULL)
1801 goto error;
1802 out = PyBytes_AS_STRING(res);
1803 remaining = n;
1804 written = 0;
1805 if (current_size > 0) {
1806 memcpy(out, self->buffer + self->pos, current_size);
1807 remaining -= current_size;
1808 written += current_size;
1809 self->pos += current_size;
1810 }
1811 /* Flush the write buffer if necessary */
1812 if (self->writable) {
1813 PyObject *r = buffered_flush_and_rewind_unlocked(self);
1814 if (r == NULL)
1815 goto error;
1816 Py_DECREF(r);
1817 }
1818 _bufferedreader_reset_buf(self);
1819 while (remaining > 0) {
1820 /* We want to read a whole block at the end into buffer.
1821 If we had readv() we could do this in one pass. */
1822 Py_ssize_t r = MINUS_LAST_BLOCK(self, remaining);
1823 if (r == 0)
1824 break;
1825 r = _bufferedreader_raw_read(self, out + written, r);
1826 if (r == -1)
1827 goto error;
1828 if (r == 0 || r == -2) {
1829 /* EOF occurred or read() would block. */
1830 if (r == 0 || written > 0) {
1831 if (_PyBytes_Resize(&res, written))
1832 goto error;
1833 return res;
1834 }
1835 Py_DECREF(res);
1836 Py_RETURN_NONE;
1837 }
1838 remaining -= r;
1839 written += r;
1840 }
1841 assert(remaining <= self->buffer_size);
1842 self->pos = 0;
1843 self->raw_pos = 0;
1844 self->read_end = 0;
1845 /* NOTE: when the read is satisfied, we avoid issuing any additional
1846 reads, which could block indefinitely (e.g. on a socket).
1847 See issue #9550. */
1848 while (remaining > 0 && self->read_end < self->buffer_size) {
1849 Py_ssize_t r = _bufferedreader_fill_buffer(self);
1850 if (r == -1)
1851 goto error;
1852 if (r == 0 || r == -2) {
1853 /* EOF occurred or read() would block. */
1854 if (r == 0 || written > 0) {
1855 if (_PyBytes_Resize(&res, written))
1856 goto error;
1857 return res;
1858 }
1859 Py_DECREF(res);
1860 Py_RETURN_NONE;
1861 }
1862 if (remaining > r) {
1863 memcpy(out + written, self->buffer + self->pos, r);
1864 written += r;
1865 self->pos += r;
1866 remaining -= r;
1867 }
1868 else if (remaining > 0) {
1869 memcpy(out + written, self->buffer + self->pos, remaining);
1870 written += remaining;
1871 self->pos += remaining;
1872 remaining = 0;
1873 }
1874 if (remaining == 0)
1875 break;
1876 }
1877
1878 return res;
1879
1880 error:
1881 Py_XDECREF(res);
1882 return NULL;
1883 }
1884
1885 static PyObject *
1886 _bufferedreader_peek_unlocked(buffered *self)
1887 {
1888 Py_ssize_t have, r;
1889
1890 have = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
1891 /* Constraints:
1892 1. we don't want to advance the file position.
1893 2. we don't want to lose block alignment, so we can't shift the buffer
1894 to make some place.
1895 Therefore, we either return `have` bytes (if > 0), or a full buffer.
1896 */
1897 if (have > 0) {
1898 return PyBytes_FromStringAndSize(self->buffer + self->pos, have);
1899 }
1900
1901 /* Fill the buffer from the raw stream, and copy it to the result. */
1902 _bufferedreader_reset_buf(self);
1903 r = _bufferedreader_fill_buffer(self);
1904 if (r == -1)
1905 return NULL;
1906 if (r == -2)
1907 r = 0;
1908 self->pos = 0;
1909 return PyBytes_FromStringAndSize(self->buffer, r);
1910 }
1911
1912
1913 /*
1914 * class BufferedWriter
1915 */
1916 static void
1917 _bufferedwriter_reset_buf(buffered *self)
1918 {
1919 self->write_pos = 0;
1920 self->write_end = -1;
1921 }
1922
1923 /*[clinic input]
1924 _io.BufferedWriter.__init__
1925 raw: object
1926 buffer_size: Py_ssize_t(c_default="DEFAULT_BUFFER_SIZE") = DEFAULT_BUFFER_SIZE
1927
1928 A buffer for a writeable sequential RawIO object.
1929
1930 The constructor creates a BufferedWriter for the given writeable raw
1931 stream. If the buffer_size is not given, it defaults to
1932 DEFAULT_BUFFER_SIZE.
1933 [clinic start generated code]*/
1934
1935 static int
1936 _io_BufferedWriter___init___impl(buffered *self, PyObject *raw,
1937 Py_ssize_t buffer_size)
1938 /*[clinic end generated code: output=c8942a020c0dee64 input=914be9b95e16007b]*/
1939 {
1940 self->ok = 0;
1941 self->detached = 0;
1942
1943 _PyIO_State *state = find_io_state_by_def(Py_TYPE(self));
1944 if (_PyIOBase_check_writable(state, raw, Py_True) == NULL) {
1945 return -1;
1946 }
1947
1948 Py_INCREF(raw);
1949 Py_XSETREF(self->raw, raw);
1950 self->readable = 0;
1951 self->writable = 1;
1952
1953 self->buffer_size = buffer_size;
1954 if (_buffered_init(self) < 0)
1955 return -1;
1956 _bufferedwriter_reset_buf(self);
1957 self->pos = 0;
1958
1959 self->fast_closed_checks = (
1960 Py_IS_TYPE(self, state->PyBufferedWriter_Type) &&
1961 Py_IS_TYPE(raw, state->PyFileIO_Type)
1962 );
1963
1964 self->ok = 1;
1965 return 0;
1966 }
1967
1968 static Py_ssize_t
1969 _bufferedwriter_raw_write(buffered *self, char *start, Py_ssize_t len)
1970 {
1971 Py_buffer buf;
1972 PyObject *memobj, *res;
1973 Py_ssize_t n;
1974 int errnum;
1975 /* NOTE: the buffer needn't be released as its object is NULL. */
1976 if (PyBuffer_FillInfo(&buf, NULL, start, len, 1, PyBUF_CONTIG_RO) == -1)
1977 return -1;
1978 memobj = PyMemoryView_FromBuffer(&buf);
1979 if (memobj == NULL)
1980 return -1;
1981 /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() when EINTR
1982 occurs so we needn't do it ourselves.
1983 We then retry writing, ignoring the signal if no handler has
1984 raised (see issue #10956).
1985 */
1986 do {
1987 errno = 0;
1988 res = PyObject_CallMethodOneArg(self->raw, &_Py_ID(write), memobj);
1989 errnum = errno;
1990 } while (res == NULL && _PyIO_trap_eintr());
1991 Py_DECREF(memobj);
1992 if (res == NULL)
1993 return -1;
1994 if (res == Py_None) {
1995 /* Non-blocking stream would have blocked. Special return code!
1996 Being paranoid we reset errno in case it is changed by code
1997 triggered by a decref. errno is used by _set_BlockingIOError(). */
1998 Py_DECREF(res);
1999 errno = errnum;
2000 return -2;
2001 }
2002 n = PyNumber_AsSsize_t(res, PyExc_ValueError);
2003 Py_DECREF(res);
2004 if (n < 0 || n > len) {
2005 PyErr_Format(PyExc_OSError,
2006 "raw write() returned invalid length %zd "
2007 "(should have been between 0 and %zd)", n, len);
2008 return -1;
2009 }
2010 if (n > 0 && self->abs_pos != -1)
2011 self->abs_pos += n;
2012 return n;
2013 }
2014
2015 static PyObject *
2016 _bufferedwriter_flush_unlocked(buffered *self)
2017 {
2018 Py_off_t n, rewind;
2019
2020 if (!VALID_WRITE_BUFFER(self) || self->write_pos == self->write_end)
2021 goto end;
2022 /* First, rewind */
2023 rewind = RAW_OFFSET(self) + (self->pos - self->write_pos);
2024 if (rewind != 0) {
2025 n = _buffered_raw_seek(self, -rewind, 1);
2026 if (n < 0) {
2027 goto error;
2028 }
2029 self->raw_pos -= rewind;
2030 }
2031 while (self->write_pos < self->write_end) {
2032 n = _bufferedwriter_raw_write(self,
2033 self->buffer + self->write_pos,
2034 Py_SAFE_DOWNCAST(self->write_end - self->write_pos,
2035 Py_off_t, Py_ssize_t));
2036 if (n == -1) {
2037 goto error;
2038 }
2039 else if (n == -2) {
2040 _set_BlockingIOError("write could not complete without blocking",
2041 0);
2042 goto error;
2043 }
2044 self->write_pos += n;
2045 self->raw_pos = self->write_pos;
2046 /* Partial writes can return successfully when interrupted by a
2047 signal (see write(2)). We must run signal handlers before
2048 blocking another time, possibly indefinitely. */
2049 if (PyErr_CheckSignals() < 0)
2050 goto error;
2051 }
2052
2053
2054 end:
2055 /* This ensures that after return from this function,
2056 VALID_WRITE_BUFFER(self) returns false.
2057
2058 This is a required condition because when a tell() is called
2059 after flushing and if VALID_READ_BUFFER(self) is false, we need
2060 VALID_WRITE_BUFFER(self) to be false to have
2061 RAW_OFFSET(self) == 0.
2062
2063 Issue: https://bugs.python.org/issue32228 */
2064 _bufferedwriter_reset_buf(self);
2065 Py_RETURN_NONE;
2066
2067 error:
2068 return NULL;
2069 }
2070
2071 /*[clinic input]
2072 @critical_section
2073 _io.BufferedWriter.write
2074 buffer: Py_buffer
2075 /
2076 [clinic start generated code]*/
2077
2078 static PyObject *
2079 _io_BufferedWriter_write_impl(buffered *self, Py_buffer *buffer)
2080 /*[clinic end generated code: output=7f8d1365759bfc6b input=6a9c041de0c337be]*/
2081 {
2082 PyObject *res = NULL;
2083 Py_ssize_t written, avail, remaining;
2084 Py_off_t offset;
2085
2086 CHECK_INITIALIZED(self)
2087
2088 if (!ENTER_BUFFERED(self))
2089 return NULL;
2090
2091 /* Issue #31976: Check for closed file after acquiring the lock. Another
2092 thread could be holding the lock while closing the file. */
2093 if (IS_CLOSED(self)) {
2094 PyErr_SetString(PyExc_ValueError, "write to closed file");
2095 goto error;
2096 }
2097
2098 /* Fast path: the data to write can be fully buffered. */
2099 if (!VALID_READ_BUFFER(self) && !VALID_WRITE_BUFFER(self)) {
2100 self->pos = 0;
2101 self->raw_pos = 0;
2102 }
2103 avail = Py_SAFE_DOWNCAST(self->buffer_size - self->pos, Py_off_t, Py_ssize_t);
2104 if (buffer->len <= avail && buffer->len < self->buffer_size) {
2105 memcpy(self->buffer + self->pos, buffer->buf, buffer->len);
2106 if (!VALID_WRITE_BUFFER(self) || self->write_pos > self->pos) {
2107 self->write_pos = self->pos;
2108 }
2109 ADJUST_POSITION(self, self->pos + buffer->len);
2110 if (self->pos > self->write_end)
2111 self->write_end = self->pos;
2112 written = buffer->len;
2113 goto end;
2114 }
2115
2116 /* First write the current buffer */
2117 res = _bufferedwriter_flush_unlocked(self);
2118 if (res == NULL) {
2119 Py_ssize_t *w = _buffered_check_blocking_error();
2120 if (w == NULL)
2121 goto error;
2122 if (self->readable)
2123 _bufferedreader_reset_buf(self);
2124 /* Make some place by shifting the buffer. */
2125 assert(VALID_WRITE_BUFFER(self));
2126 memmove(self->buffer, self->buffer + self->write_pos,
2127 Py_SAFE_DOWNCAST(self->write_end - self->write_pos,
2128 Py_off_t, Py_ssize_t));
2129 self->write_end -= self->write_pos;
2130 self->raw_pos -= self->write_pos;
2131 self->pos -= self->write_pos;
2132 self->write_pos = 0;
2133 avail = Py_SAFE_DOWNCAST(self->buffer_size - self->write_end,
2134 Py_off_t, Py_ssize_t);
2135 if (buffer->len <= avail) {
2136 /* Everything can be buffered */
2137 PyErr_Clear();
2138 memcpy(self->buffer + self->write_end, buffer->buf, buffer->len);
2139 self->write_end += buffer->len;
2140 self->pos += buffer->len;
2141 written = buffer->len;
2142 goto end;
2143 }
2144 /* Buffer as much as possible. */
2145 memcpy(self->buffer + self->write_end, buffer->buf, avail);
2146 self->write_end += avail;
2147 self->pos += avail;
2148 /* XXX Modifying the existing exception e using the pointer w
2149 will change e.characters_written but not e.args[2].
2150 Therefore we just replace with a new error. */
2151 _set_BlockingIOError("write could not complete without blocking",
2152 avail);
2153 goto error;
2154 }
2155 Py_CLEAR(res);
2156
2157 /* Adjust the raw stream position if it is away from the logical stream
2158 position. This happens if the read buffer has been filled but not
2159 modified (and therefore _bufferedwriter_flush_unlocked() didn't rewind
2160 the raw stream by itself).
2161 Fixes issue #6629.
2162 */
2163 offset = RAW_OFFSET(self);
2164 if (offset != 0) {
2165 if (_buffered_raw_seek(self, -offset, 1) < 0)
2166 goto error;
2167 self->raw_pos -= offset;
2168 }
2169
2170 /* Then write buf itself. At this point the buffer has been emptied. */
2171 remaining = buffer->len;
2172 written = 0;
2173 while (remaining >= self->buffer_size) {
2174 Py_ssize_t n = _bufferedwriter_raw_write(
2175 self, (char *) buffer->buf + written, buffer->len - written);
2176 if (n == -1) {
2177 goto error;
2178 } else if (n == -2) {
2179 /* Write failed because raw file is non-blocking */
2180 if (remaining > self->buffer_size) {
2181 /* Can't buffer everything, still buffer as much as possible */
2182 memcpy(self->buffer,
2183 (char *) buffer->buf + written, self->buffer_size);
2184 self->raw_pos = 0;
2185 ADJUST_POSITION(self, self->buffer_size);
2186 self->write_end = self->buffer_size;
2187 written += self->buffer_size;
2188 _set_BlockingIOError("write could not complete without "
2189 "blocking", written);
2190 goto error;
2191 }
2192 PyErr_Clear();
2193 break;
2194 }
2195 written += n;
2196 remaining -= n;
2197 /* Partial writes can return successfully when interrupted by a
2198 signal (see write(2)). We must run signal handlers before
2199 blocking another time, possibly indefinitely. */
2200 if (PyErr_CheckSignals() < 0)
2201 goto error;
2202 }
2203 if (self->readable)
2204 _bufferedreader_reset_buf(self);
2205 if (remaining > 0) {
2206 memcpy(self->buffer, (char *) buffer->buf + written, remaining);
2207 written += remaining;
2208 }
2209 self->write_pos = 0;
2210 /* TODO: sanity check (remaining >= 0) */
2211 self->write_end = remaining;
2212 ADJUST_POSITION(self, remaining);
2213 self->raw_pos = 0;
2214
2215 end:
2216 res = PyLong_FromSsize_t(written);
2217
2218 error:
2219 LEAVE_BUFFERED(self)
2220 return res;
2221 }
2222
2223
2224 /*
2225 * BufferedRWPair
2226 */
2227
2228 /* XXX The usefulness of this (compared to having two separate IO objects) is
2229 * questionable.
2230 */
2231
2232 typedef struct {
2233 PyObject_HEAD
2234 buffered *reader;
2235 buffered *writer;
2236 PyObject *dict;
2237 PyObject *weakreflist;
2238 } rwpair;
2239
2240 #define rwpair_CAST(op) ((rwpair *)(op))
2241
2242 /*[clinic input]
2243 _io.BufferedRWPair.__init__
2244 reader: object
2245 writer: object
2246 buffer_size: Py_ssize_t(c_default="DEFAULT_BUFFER_SIZE") = DEFAULT_BUFFER_SIZE
2247 /
2248
2249 A buffered reader and writer object together.
2250
2251 A buffered reader object and buffered writer object put together to
2252 form a sequential IO object that can read and write. This is typically
2253 used with a socket or two-way pipe.
2254
2255 reader and writer are RawIOBase objects that are readable and
2256 writeable respectively. If the buffer_size is omitted it defaults to
2257 DEFAULT_BUFFER_SIZE.
2258 [clinic start generated code]*/
2259
2260 static int
2261 _io_BufferedRWPair___init___impl(rwpair *self, PyObject *reader,
2262 PyObject *writer, Py_ssize_t buffer_size)
2263 /*[clinic end generated code: output=327e73d1aee8f984 input=620d42d71f33a031]*/
2264 {
2265 _PyIO_State *state = find_io_state_by_def(Py_TYPE(self));
2266 if (_PyIOBase_check_readable(state, reader, Py_True) == NULL) {
2267 return -1;
2268 }
2269 if (_PyIOBase_check_writable(state, writer, Py_True) == NULL) {
2270 return -1;
2271 }
2272
2273 self->reader = (buffered *) PyObject_CallFunction(
2274 (PyObject *)state->PyBufferedReader_Type,
2275 "On", reader, buffer_size);
2276 if (self->reader == NULL)
2277 return -1;
2278
2279 self->writer = (buffered *) PyObject_CallFunction(
2280 (PyObject *)state->PyBufferedWriter_Type,
2281 "On", writer, buffer_size);
2282 if (self->writer == NULL) {
2283 Py_CLEAR(self->reader);
2284 return -1;
2285 }
2286
2287 return 0;
2288 }
2289
2290 static int
2291 bufferedrwpair_traverse(PyObject *op, visitproc visit, void *arg)
2292 {
2293 rwpair *self = rwpair_CAST(op);
2294 Py_VISIT(Py_TYPE(self));
2295 Py_VISIT(self->dict);
2296 Py_VISIT(self->reader);
2297 Py_VISIT(self->writer);
2298 return 0;
2299 }
2300
2301 static int
2302 bufferedrwpair_clear(PyObject *op)
2303 {
2304 rwpair *self = rwpair_CAST(op);
2305 Py_CLEAR(self->reader);
2306 Py_CLEAR(self->writer);
2307 Py_CLEAR(self->dict);
2308 return 0;
2309 }
2310
2311 static void
2312 bufferedrwpair_dealloc(PyObject *op)
2313 {
2314 rwpair *self = rwpair_CAST(op);
2315 PyTypeObject *tp = Py_TYPE(self);
2316 _PyObject_GC_UNTRACK(self);
2317 FT_CLEAR_WEAKREFS(op, self->weakreflist);
2318 (void)bufferedrwpair_clear(op);
2319 tp->tp_free(self);
2320 Py_DECREF(tp);
2321 }
2322
2323 static PyObject *
2324 _forward_call(buffered *self, PyObject *name, PyObject *args)
2325 {
2326 PyObject *func, *ret;
2327 if (self == NULL) {
2328 PyErr_SetString(PyExc_ValueError,
2329 "I/O operation on uninitialized object");
2330 return NULL;
2331 }
2332
2333 func = PyObject_GetAttr((PyObject *)self, name);
2334 if (func == NULL) {
2335 PyErr_SetObject(PyExc_AttributeError, name);
2336 return NULL;
2337 }
2338
2339 ret = PyObject_CallObject(func, args);
2340 Py_DECREF(func);
2341 return ret;
2342 }
2343
2344 static PyObject *
2345 bufferedrwpair_read(PyObject *op, PyObject *args)
2346 {
2347 rwpair *self = rwpair_CAST(op);
2348 return _forward_call(self->reader, &_Py_ID(read), args);
2349 }
2350
2351 static PyObject *
2352 bufferedrwpair_peek(PyObject *op, PyObject *args)
2353 {
2354 rwpair *self = rwpair_CAST(op);
2355 return _forward_call(self->reader, &_Py_ID(peek), args);
2356 }
2357
2358 static PyObject *
2359 bufferedrwpair_read1(PyObject *op, PyObject *args)
2360 {
2361 rwpair *self = rwpair_CAST(op);
2362 return _forward_call(self->reader, &_Py_ID(read1), args);
2363 }
2364
2365 static PyObject *
2366 bufferedrwpair_readinto(PyObject *op, PyObject *args)
2367 {
2368 rwpair *self = rwpair_CAST(op);
2369 return _forward_call(self->reader, &_Py_ID(readinto), args);
2370 }
2371
2372 static PyObject *
2373 bufferedrwpair_readinto1(PyObject *op, PyObject *args)
2374 {
2375 rwpair *self = rwpair_CAST(op);
2376 return _forward_call(self->reader, &_Py_ID(readinto1), args);
2377 }
2378
2379 static PyObject *
2380 bufferedrwpair_write(PyObject *op, PyObject *args)
2381 {
2382 rwpair *self = rwpair_CAST(op);
2383 return _forward_call(self->writer, &_Py_ID(write), args);
2384 }
2385
2386 static PyObject *
2387 bufferedrwpair_flush(PyObject *op, PyObject *Py_UNUSED(dummy))
2388 {
2389 rwpair *self = rwpair_CAST(op);
2390 return _forward_call(self->writer, &_Py_ID(flush), NULL);
2391 }
2392
2393 static PyObject *
2394 bufferedrwpair_readable(PyObject *op, PyObject *Py_UNUSED(dummy))
2395 {
2396 rwpair *self = rwpair_CAST(op);
2397 return _forward_call(self->reader, &_Py_ID(readable), NULL);
2398 }
2399
2400 static PyObject *
2401 bufferedrwpair_writable(PyObject *op, PyObject *Py_UNUSED(dummy))
2402 {
2403 rwpair *self = rwpair_CAST(op);
2404 return _forward_call(self->writer, &_Py_ID(writable), NULL);
2405 }
2406
2407 static PyObject *
2408 bufferedrwpair_close(PyObject *op, PyObject *Py_UNUSED(dummy))
2409 {
2410 rwpair *self = rwpair_CAST(op);
2411 PyObject *exc = NULL;
2412 PyObject *ret = _forward_call(self->writer, &_Py_ID(close), NULL);
2413 if (ret == NULL) {
2414 exc = PyErr_GetRaisedException();
2415 }
2416 else {
2417 Py_DECREF(ret);
2418 }
2419 ret = _forward_call(self->reader, &_Py_ID(close), NULL);
2420 if (exc != NULL) {
2421 _PyErr_ChainExceptions1(exc);
2422 Py_CLEAR(ret);
2423 }
2424 return ret;
2425 }
2426
2427 static PyObject *
2428 bufferedrwpair_isatty(PyObject *op, PyObject *Py_UNUSED(dummy))
2429 {
2430 rwpair *self = rwpair_CAST(op);
2431 PyObject *ret = _forward_call(self->writer, &_Py_ID(isatty), NULL);
2432
2433 if (ret != Py_False) {
2434 /* either True or exception */
2435 return ret;
2436 }
2437 Py_DECREF(ret);
2438
2439 return _forward_call(self->reader, &_Py_ID(isatty), NULL);
2440 }
2441
2442 static PyObject *
2443 bufferedrwpair_closed_get(PyObject *op, void *Py_UNUSED(dummy))
2444 {
2445 rwpair *self = rwpair_CAST(op);
2446 if (self->writer == NULL) {
2447 PyErr_SetString(PyExc_RuntimeError,
2448 "the BufferedRWPair object is being garbage-collected");
2449 return NULL;
2450 }
2451 return PyObject_GetAttr((PyObject *) self->writer, &_Py_ID(closed));
2452 }
2453
2454
2455 /*
2456 * BufferedRandom
2457 */
2458
2459 /*[clinic input]
2460 _io.BufferedRandom.__init__
2461 raw: object
2462 buffer_size: Py_ssize_t(c_default="DEFAULT_BUFFER_SIZE") = DEFAULT_BUFFER_SIZE
2463
2464 A buffered interface to random access streams.
2465
2466 The constructor creates a reader and writer for a seekable stream,
2467 raw, given in the first argument. If the buffer_size is omitted it
2468 defaults to DEFAULT_BUFFER_SIZE.
2469 [clinic start generated code]*/
2470
2471 static int
2472 _io_BufferedRandom___init___impl(buffered *self, PyObject *raw,
2473 Py_ssize_t buffer_size)
2474 /*[clinic end generated code: output=d3d64eb0f64e64a3 input=a4e818fb86d0e50c]*/
2475 {
2476 self->ok = 0;
2477 self->detached = 0;
2478
2479 _PyIO_State *state = find_io_state_by_def(Py_TYPE(self));
2480 if (_PyIOBase_check_seekable(state, raw, Py_True) == NULL) {
2481 return -1;
2482 }
2483 if (_PyIOBase_check_readable(state, raw, Py_True) == NULL) {
2484 return -1;
2485 }
2486 if (_PyIOBase_check_writable(state, raw, Py_True) == NULL) {
2487 return -1;
2488 }
2489
2490 Py_INCREF(raw);
2491 Py_XSETREF(self->raw, raw);
2492 self->buffer_size = buffer_size;
2493 self->readable = 1;
2494 self->writable = 1;
2495
2496 if (_buffered_init(self) < 0)
2497 return -1;
2498 _bufferedreader_reset_buf(self);
2499 _bufferedwriter_reset_buf(self);
2500 self->pos = 0;
2501
2502 self->fast_closed_checks = (Py_IS_TYPE(self, state->PyBufferedRandom_Type) &&
2503 Py_IS_TYPE(raw, state->PyFileIO_Type));
2504
2505 self->ok = 1;
2506 return 0;
2507 }
2508
2509 #define clinic_state() (find_io_state_by_def(Py_TYPE(self)))
2510 #include "clinic/bufferedio.c.h"
2511 #undef clinic_state
2512
2513 static PyMethodDef bufferediobase_methods[] = {
2514 _IO__BUFFEREDIOBASE_DETACH_METHODDEF
2515 _IO__BUFFEREDIOBASE_READ_METHODDEF
2516 _IO__BUFFEREDIOBASE_READ1_METHODDEF
2517 _IO__BUFFEREDIOBASE_READINTO_METHODDEF
2518 _IO__BUFFEREDIOBASE_READINTO1_METHODDEF
2519 _IO__BUFFEREDIOBASE_WRITE_METHODDEF
2520 {NULL, NULL}
2521 };
2522
2523 static PyType_Slot bufferediobase_slots[] = {
2524 {Py_tp_doc, (void *)bufferediobase_doc},
2525 {Py_tp_methods, bufferediobase_methods},
2526 {0, NULL},
2527 };
2528
2529 /* Do not set Py_TPFLAGS_HAVE_GC so that tp_traverse and tp_clear are inherited */
2530 PyType_Spec bufferediobase_spec = {
2531 .name = "_io._BufferedIOBase",
2532 .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
2533 Py_TPFLAGS_IMMUTABLETYPE),
2534 .slots = bufferediobase_slots,
2535 };
2536
2537 static PyMethodDef bufferedreader_methods[] = {
2538 /* BufferedIOMixin methods */
2539 _IO__BUFFERED_DETACH_METHODDEF
2540 _IO__BUFFERED_SIMPLE_FLUSH_METHODDEF
2541 _IO__BUFFERED_CLOSE_METHODDEF
2542 _IO__BUFFERED_SEEKABLE_METHODDEF
2543 _IO__BUFFERED_READABLE_METHODDEF
2544 _IO__BUFFERED_FILENO_METHODDEF
2545 _IO__BUFFERED_ISATTY_METHODDEF
2546 _IO__BUFFERED__DEALLOC_WARN_METHODDEF
2547
2548 _IO__BUFFERED_READ_METHODDEF
2549 _IO__BUFFERED_PEEK_METHODDEF
2550 _IO__BUFFERED_READ1_METHODDEF
2551 _IO__BUFFERED_READINTO_METHODDEF
2552 _IO__BUFFERED_READINTO1_METHODDEF
2553 _IO__BUFFERED_READLINE_METHODDEF
2554 _IO__BUFFERED_SEEK_METHODDEF
2555 _IO__BUFFERED_TELL_METHODDEF
2556 _IO__BUFFERED_TRUNCATE_METHODDEF
2557 _IO__BUFFERED___SIZEOF___METHODDEF
2558
2559 {"__getstate__", _PyIOBase_cannot_pickle, METH_NOARGS},
2560 {NULL, NULL}
2561 };
2562
2563 static PyMemberDef bufferedreader_members[] = {
2564 {"raw", _Py_T_OBJECT, offsetof(buffered, raw), Py_READONLY},
2565 {"_finalizing", Py_T_BOOL, offsetof(buffered, finalizing), 0},
2566 {"__weaklistoffset__", Py_T_PYSSIZET, offsetof(buffered, weakreflist), Py_READONLY},
2567 {"__dictoffset__", Py_T_PYSSIZET, offsetof(buffered, dict), Py_READONLY},
2568 {NULL}
2569 };
2570
2571 static PyGetSetDef bufferedreader_getset[] = {
2572 _IO__BUFFERED_CLOSED_GETSETDEF
2573 _IO__BUFFERED_NAME_GETSETDEF
2574 _IO__BUFFERED_MODE_GETSETDEF
2575 {NULL}
2576 };
2577
2578
2579 static PyType_Slot bufferedreader_slots[] = {
2580 {Py_tp_dealloc, buffered_dealloc},
2581 {Py_tp_repr, buffered_repr},
2582 {Py_tp_doc, (void *)_io_BufferedReader___init____doc__},
2583 {Py_tp_traverse, buffered_traverse},
2584 {Py_tp_clear, buffered_clear},
2585 {Py_tp_iternext, buffered_iternext},
2586 {Py_tp_methods, bufferedreader_methods},
2587 {Py_tp_members, bufferedreader_members},
2588 {Py_tp_getset, bufferedreader_getset},
2589 {Py_tp_init, _io_BufferedReader___init__},
2590 {0, NULL},
2591 };
2592
2593 PyType_Spec bufferedreader_spec = {
2594 .name = "_io.BufferedReader",
2595 .basicsize = sizeof(buffered),
2596 .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
2597 Py_TPFLAGS_IMMUTABLETYPE),
2598 .slots = bufferedreader_slots,
2599 };
2600
2601 static PyMethodDef bufferedwriter_methods[] = {
2602 /* BufferedIOMixin methods */
2603 _IO__BUFFERED_CLOSE_METHODDEF
2604 _IO__BUFFERED_DETACH_METHODDEF
2605 _IO__BUFFERED_SEEKABLE_METHODDEF
2606 _IO__BUFFERED_WRITABLE_METHODDEF
2607 _IO__BUFFERED_FILENO_METHODDEF
2608 _IO__BUFFERED_ISATTY_METHODDEF
2609 _IO__BUFFERED__DEALLOC_WARN_METHODDEF
2610
2611 _IO_BUFFEREDWRITER_WRITE_METHODDEF
2612 _IO__BUFFERED_TRUNCATE_METHODDEF
2613 _IO__BUFFERED_FLUSH_METHODDEF
2614 _IO__BUFFERED_SEEK_METHODDEF
2615 _IO__BUFFERED_TELL_METHODDEF
2616 _IO__BUFFERED___SIZEOF___METHODDEF
2617
2618 {"__getstate__", _PyIOBase_cannot_pickle, METH_NOARGS},
2619 {NULL, NULL}
2620 };
2621
2622 static PyMemberDef bufferedwriter_members[] = {
2623 {"raw", _Py_T_OBJECT, offsetof(buffered, raw), Py_READONLY},
2624 {"_finalizing", Py_T_BOOL, offsetof(buffered, finalizing), 0},
2625 {"__weaklistoffset__", Py_T_PYSSIZET, offsetof(buffered, weakreflist), Py_READONLY},
2626 {"__dictoffset__", Py_T_PYSSIZET, offsetof(buffered, dict), Py_READONLY},
2627 {NULL}
2628 };
2629
2630 static PyGetSetDef bufferedwriter_getset[] = {
2631 _IO__BUFFERED_CLOSED_GETSETDEF
2632 _IO__BUFFERED_NAME_GETSETDEF
2633 _IO__BUFFERED_MODE_GETSETDEF
2634 {NULL}
2635 };
2636
2637
2638 static PyType_Slot bufferedwriter_slots[] = {
2639 {Py_tp_dealloc, buffered_dealloc},
2640 {Py_tp_repr, buffered_repr},
2641 {Py_tp_doc, (void *)_io_BufferedWriter___init____doc__},
2642 {Py_tp_traverse, buffered_traverse},
2643 {Py_tp_clear, buffered_clear},
2644 {Py_tp_methods, bufferedwriter_methods},
2645 {Py_tp_members, bufferedwriter_members},
2646 {Py_tp_getset, bufferedwriter_getset},
2647 {Py_tp_init, _io_BufferedWriter___init__},
2648 {0, NULL},
2649 };
2650
2651 PyType_Spec bufferedwriter_spec = {
2652 .name = "_io.BufferedWriter",
2653 .basicsize = sizeof(buffered),
2654 .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
2655 Py_TPFLAGS_IMMUTABLETYPE),
2656 .slots = bufferedwriter_slots,
2657 };
2658
2659 static PyMethodDef bufferedrwpair_methods[] = {
2660 {"read", bufferedrwpair_read, METH_VARARGS},
2661 {"peek", bufferedrwpair_peek, METH_VARARGS},
2662 {"read1", bufferedrwpair_read1, METH_VARARGS},
2663 {"readinto", bufferedrwpair_readinto, METH_VARARGS},
2664 {"readinto1", bufferedrwpair_readinto1, METH_VARARGS},
2665
2666 {"write", bufferedrwpair_write, METH_VARARGS},
2667 {"flush", bufferedrwpair_flush, METH_NOARGS},
2668
2669 {"readable", bufferedrwpair_readable, METH_NOARGS},
2670 {"writable", bufferedrwpair_writable, METH_NOARGS},
2671
2672 {"close", bufferedrwpair_close, METH_NOARGS},
2673 {"isatty", bufferedrwpair_isatty, METH_NOARGS},
2674
2675 {NULL, NULL}
2676 };
2677
2678 static PyMemberDef bufferedrwpair_members[] = {
2679 {"__weaklistoffset__", Py_T_PYSSIZET, offsetof(rwpair, weakreflist), Py_READONLY},
2680 {"__dictoffset__", Py_T_PYSSIZET, offsetof(rwpair, dict), Py_READONLY},
2681 {NULL}
2682 };
2683
2684 static PyGetSetDef bufferedrwpair_getset[] = {
2685 {"closed", bufferedrwpair_closed_get, NULL, NULL},
2686 {NULL}
2687 };
2688
2689 static PyType_Slot bufferedrwpair_slots[] = {
2690 {Py_tp_dealloc, bufferedrwpair_dealloc},
2691 {Py_tp_doc, (void *)_io_BufferedRWPair___init____doc__},
2692 {Py_tp_traverse, bufferedrwpair_traverse},
2693 {Py_tp_clear, bufferedrwpair_clear},
2694 {Py_tp_methods, bufferedrwpair_methods},
2695 {Py_tp_members, bufferedrwpair_members},
2696 {Py_tp_getset, bufferedrwpair_getset},
2697 {Py_tp_init, _io_BufferedRWPair___init__},
2698 {0, NULL},
2699 };
2700
2701 PyType_Spec bufferedrwpair_spec = {
2702 .name = "_io.BufferedRWPair",
2703 .basicsize = sizeof(rwpair),
2704 .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
2705 Py_TPFLAGS_IMMUTABLETYPE),
2706 .slots = bufferedrwpair_slots,
2707 };
2708
2709
2710 static PyMethodDef bufferedrandom_methods[] = {
2711 /* BufferedIOMixin methods */
2712 _IO__BUFFERED_CLOSE_METHODDEF
2713 _IO__BUFFERED_DETACH_METHODDEF
2714 _IO__BUFFERED_SEEKABLE_METHODDEF
2715 _IO__BUFFERED_READABLE_METHODDEF
2716 _IO__BUFFERED_WRITABLE_METHODDEF
2717 _IO__BUFFERED_FILENO_METHODDEF
2718 _IO__BUFFERED_ISATTY_METHODDEF
2719 _IO__BUFFERED__DEALLOC_WARN_METHODDEF
2720
2721 _IO__BUFFERED_FLUSH_METHODDEF
2722
2723 _IO__BUFFERED_SEEK_METHODDEF
2724 _IO__BUFFERED_TELL_METHODDEF
2725 _IO__BUFFERED_TRUNCATE_METHODDEF
2726 _IO__BUFFERED_READ_METHODDEF
2727 _IO__BUFFERED_READ1_METHODDEF
2728 _IO__BUFFERED_READINTO_METHODDEF
2729 _IO__BUFFERED_READINTO1_METHODDEF
2730 _IO__BUFFERED_READLINE_METHODDEF
2731 _IO__BUFFERED_PEEK_METHODDEF
2732 _IO_BUFFEREDWRITER_WRITE_METHODDEF
2733 _IO__BUFFERED___SIZEOF___METHODDEF
2734
2735 {"__getstate__", _PyIOBase_cannot_pickle, METH_NOARGS},
2736 {NULL, NULL}
2737 };
2738
2739 static PyMemberDef bufferedrandom_members[] = {
2740 {"raw", _Py_T_OBJECT, offsetof(buffered, raw), Py_READONLY},
2741 {"_finalizing", Py_T_BOOL, offsetof(buffered, finalizing), 0},
2742 {"__weaklistoffset__", Py_T_PYSSIZET, offsetof(buffered, weakreflist), Py_READONLY},
2743 {"__dictoffset__", Py_T_PYSSIZET, offsetof(buffered, dict), Py_READONLY},
2744 {NULL}
2745 };
2746
2747 static PyGetSetDef bufferedrandom_getset[] = {
2748 _IO__BUFFERED_CLOSED_GETSETDEF
2749 _IO__BUFFERED_NAME_GETSETDEF
2750 _IO__BUFFERED_MODE_GETSETDEF
2751 {NULL}
2752 };
2753
2754
2755 static PyType_Slot bufferedrandom_slots[] = {
2756 {Py_tp_dealloc, buffered_dealloc},
2757 {Py_tp_repr, buffered_repr},
2758 {Py_tp_doc, (void *)_io_BufferedRandom___init____doc__},
2759 {Py_tp_traverse, buffered_traverse},
2760 {Py_tp_clear, buffered_clear},
2761 {Py_tp_iternext, buffered_iternext},
2762 {Py_tp_methods, bufferedrandom_methods},
2763 {Py_tp_members, bufferedrandom_members},
2764 {Py_tp_getset, bufferedrandom_getset},
2765 {Py_tp_init, _io_BufferedRandom___init__},
2766 {0, NULL},
2767 };
2768
2769 PyType_Spec bufferedrandom_spec = {
2770 .name = "_io.BufferedRandom",
2771 .basicsize = sizeof(buffered),
2772 .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
2773 Py_TPFLAGS_IMMUTABLETYPE),
2774 .slots = bufferedrandom_slots,
2775 };