blob: ddd17a2863c2492347b5f93dd35fcf91e418fc38 [file] [log] [blame]
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001/*
2 An implementation of Buffered I/O as defined by PEP 3116 - "New I/O"
Antoine Pitrou3486a982011-05-12 01:57:53 +02003
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00004 Classes defined here: BufferedIOBase, BufferedReader, BufferedWriter,
5 BufferedRandom.
Antoine Pitrou3486a982011-05-12 01:57:53 +02006
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00007 Written by Amaury Forgeot d'Arc and Antoine Pitrou
8*/
9
10#define PY_SSIZE_T_CLEAN
11#include "Python.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +010012#include "pycore_object.h"
Victor Stinner621cebe2018-11-12 16:53:38 +010013#include "pycore_pystate.h"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000014#include "structmember.h"
15#include "pythread.h"
16#include "_iomodule.h"
17
Serhiy Storchakaf24131f2015-04-16 11:19:43 +030018/*[clinic input]
19module _io
20class _io._BufferedIOBase "PyObject *" "&PyBufferedIOBase_Type"
21class _io._Buffered "buffered *" "&PyBufferedIOBase_Type"
22class _io.BufferedReader "buffered *" "&PyBufferedReader_Type"
23class _io.BufferedWriter "buffered *" "&PyBufferedWriter_Type"
24class _io.BufferedRWPair "rwpair *" "&PyBufferedRWPair_Type"
25class _io.BufferedRandom "buffered *" "&PyBufferedRandom_Type"
26[clinic start generated code]*/
27/*[clinic end generated code: output=da39a3ee5e6b4b0d input=59460b9c5639984d]*/
28
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020029_Py_IDENTIFIER(close);
30_Py_IDENTIFIER(_dealloc_warn);
31_Py_IDENTIFIER(flush);
32_Py_IDENTIFIER(isatty);
Martin v. Löwis767046a2011-10-14 15:35:36 +020033_Py_IDENTIFIER(mode);
34_Py_IDENTIFIER(name);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020035_Py_IDENTIFIER(peek);
36_Py_IDENTIFIER(read);
37_Py_IDENTIFIER(read1);
38_Py_IDENTIFIER(readable);
39_Py_IDENTIFIER(readinto);
Benjamin Petersona96fea02014-06-22 14:17:44 -070040_Py_IDENTIFIER(readinto1);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020041_Py_IDENTIFIER(writable);
42_Py_IDENTIFIER(write);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +020043
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000044/*
45 * BufferedIOBase class, inherits from IOBase.
46 */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +000047PyDoc_STRVAR(bufferediobase_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000048 "Base class for buffered IO objects.\n"
49 "\n"
50 "The main difference with RawIOBase is that the read() method\n"
51 "supports omitting the size argument, and does not have a default\n"
52 "implementation that defers to readinto().\n"
53 "\n"
54 "In addition, read(), readinto() and write() may raise\n"
55 "BlockingIOError if the underlying raw stream is in non-blocking\n"
56 "mode and not ready; unlike their raw counterparts, they will never\n"
57 "return None.\n"
58 "\n"
59 "A typical implementation should not inherit from a RawIOBase\n"
60 "implementation, but wrap one.\n"
61 );
62
63static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +030064_bufferediobase_readinto_generic(PyObject *self, Py_buffer *buffer, char readinto1)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000065{
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000066 Py_ssize_t len;
67 PyObject *data;
68
Benjamin Petersona96fea02014-06-22 14:17:44 -070069 data = _PyObject_CallMethodId(self,
70 readinto1 ? &PyId_read1 : &PyId_read,
Serhiy Storchakaf24131f2015-04-16 11:19:43 +030071 "n", buffer->len);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000072 if (data == NULL)
Serhiy Storchakaf24131f2015-04-16 11:19:43 +030073 return NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000074
75 if (!PyBytes_Check(data)) {
76 Py_DECREF(data);
77 PyErr_SetString(PyExc_TypeError, "read() should return bytes");
Serhiy Storchakaf24131f2015-04-16 11:19:43 +030078 return NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000079 }
80
Serhiy Storchakafff9a312017-03-21 08:53:25 +020081 len = PyBytes_GET_SIZE(data);
Serhiy Storchakaf24131f2015-04-16 11:19:43 +030082 if (len > buffer->len) {
Serhiy Storchaka37a79a12013-05-28 16:24:45 +030083 PyErr_Format(PyExc_ValueError,
84 "read() returned too much data: "
85 "%zd bytes requested, %zd returned",
Serhiy Storchakaf24131f2015-04-16 11:19:43 +030086 buffer->len, len);
Serhiy Storchaka37a79a12013-05-28 16:24:45 +030087 Py_DECREF(data);
Serhiy Storchakaf24131f2015-04-16 11:19:43 +030088 return NULL;
Serhiy Storchaka37a79a12013-05-28 16:24:45 +030089 }
Serhiy Storchakaf24131f2015-04-16 11:19:43 +030090 memcpy(buffer->buf, PyBytes_AS_STRING(data), len);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000091
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000092 Py_DECREF(data);
93
94 return PyLong_FromSsize_t(len);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +000095}
96
Serhiy Storchakaf24131f2015-04-16 11:19:43 +030097/*[clinic input]
98_io._BufferedIOBase.readinto
Larry Hastingsdbfdc382015-05-04 06:59:46 -070099 buffer: Py_buffer(accept={rwbuffer})
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300100 /
101[clinic start generated code]*/
Benjamin Petersona96fea02014-06-22 14:17:44 -0700102
103static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300104_io__BufferedIOBase_readinto_impl(PyObject *self, Py_buffer *buffer)
Larry Hastingsdbfdc382015-05-04 06:59:46 -0700105/*[clinic end generated code: output=8c8cda6684af8038 input=00a6b9a38f29830a]*/
Benjamin Petersona96fea02014-06-22 14:17:44 -0700106{
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300107 return _bufferediobase_readinto_generic(self, buffer, 0);
108}
109
110/*[clinic input]
111_io._BufferedIOBase.readinto1
Larry Hastingsdbfdc382015-05-04 06:59:46 -0700112 buffer: Py_buffer(accept={rwbuffer})
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300113 /
114[clinic start generated code]*/
115
116static PyObject *
117_io__BufferedIOBase_readinto1_impl(PyObject *self, Py_buffer *buffer)
Larry Hastingsdbfdc382015-05-04 06:59:46 -0700118/*[clinic end generated code: output=358623e4fd2b69d3 input=ebad75b4aadfb9be]*/
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300119{
120 return _bufferediobase_readinto_generic(self, buffer, 1);
Benjamin Petersona96fea02014-06-22 14:17:44 -0700121}
122
123static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000124bufferediobase_unsupported(const char *message)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000125{
Antoine Pitrou712cb732013-12-21 15:51:54 +0100126 _PyIO_State *state = IO_STATE();
127 if (state != NULL)
128 PyErr_SetString(state->unsupported_operation, message);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000129 return NULL;
130}
131
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300132/*[clinic input]
133_io._BufferedIOBase.detach
134
135Disconnect this buffer from its underlying raw stream and return it.
136
137After the raw stream has been detached, the buffer is in an unusable
138state.
139[clinic start generated code]*/
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000140
141static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300142_io__BufferedIOBase_detach_impl(PyObject *self)
143/*[clinic end generated code: output=754977c8d10ed88c input=822427fb58fe4169]*/
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000144{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000145 return bufferediobase_unsupported("detach");
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000146}
147
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000148PyDoc_STRVAR(bufferediobase_read_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000149 "Read and return up to n bytes.\n"
150 "\n"
151 "If the argument is omitted, None, or negative, reads and\n"
152 "returns all data until EOF.\n"
153 "\n"
154 "If the argument is positive, and the underlying raw stream is\n"
155 "not 'interactive', multiple raw reads may be issued to satisfy\n"
156 "the byte count (unless EOF is reached first). But for\n"
157 "interactive raw streams (as well as sockets and pipes), at most\n"
158 "one raw read will be issued, and a short result does not imply\n"
159 "that EOF is imminent.\n"
160 "\n"
161 "Returns an empty bytes object on EOF.\n"
162 "\n"
163 "Returns None if the underlying raw stream was open in non-blocking\n"
164 "mode and no data is available at the moment.\n");
165
166static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000167bufferediobase_read(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000168{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000169 return bufferediobase_unsupported("read");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000170}
171
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000172PyDoc_STRVAR(bufferediobase_read1_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000173 "Read and return up to n bytes, with at most one read() call\n"
174 "to the underlying raw stream. A short result does not imply\n"
175 "that EOF is imminent.\n"
176 "\n"
177 "Returns an empty bytes object on EOF.\n");
178
179static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000180bufferediobase_read1(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000181{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000182 return bufferediobase_unsupported("read1");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000183}
184
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000185PyDoc_STRVAR(bufferediobase_write_doc,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000186 "Write the given buffer to the IO stream.\n"
187 "\n"
Martin Panter6bb91f32016-05-28 00:41:57 +0000188 "Returns the number of bytes written, which is always the length of b\n"
189 "in bytes.\n"
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000190 "\n"
191 "Raises BlockingIOError if the buffer is full and the\n"
192 "underlying raw stream cannot accept more data at the moment.\n");
193
194static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000195bufferediobase_write(PyObject *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000196{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000197 return bufferediobase_unsupported("write");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000198}
199
200
Antoine Pitrou317def92017-12-13 01:39:26 +0100201typedef struct {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000202 PyObject_HEAD
203
204 PyObject *raw;
205 int ok; /* Initialized? */
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000206 int detached;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000207 int readable;
208 int writable;
Antoine Pitrou796564c2013-07-30 19:59:21 +0200209 char finalizing;
Antoine Pitrou3486a982011-05-12 01:57:53 +0200210
Antoine Pitrou711af3a2009-04-11 15:39:24 +0000211 /* True if this is a vanilla Buffered object (rather than a user derived
212 class) *and* the raw stream is a vanilla FileIO object. */
213 int fast_closed_checks;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000214
215 /* Absolute position inside the raw stream (-1 if unknown). */
216 Py_off_t abs_pos;
217
218 /* A static buffer of size `buffer_size` */
219 char *buffer;
220 /* Current logical position in the buffer. */
221 Py_off_t pos;
222 /* Position of the raw stream in the buffer. */
223 Py_off_t raw_pos;
224
225 /* Just after the last buffered byte in the buffer, or -1 if the buffer
226 isn't ready for reading. */
227 Py_off_t read_end;
228
229 /* Just after the last byte actually written */
230 Py_off_t write_pos;
231 /* Just after the last byte waiting to be written, or -1 if the buffer
232 isn't ready for writing. */
233 Py_off_t write_end;
234
235 PyThread_type_lock lock;
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +0200236 volatile unsigned long owner;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000237
238 Py_ssize_t buffer_size;
239 Py_ssize_t buffer_mask;
240
241 PyObject *dict;
242 PyObject *weakreflist;
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000243} buffered;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000244
245/*
246 Implementation notes:
Antoine Pitrou3486a982011-05-12 01:57:53 +0200247
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000248 * BufferedReader, BufferedWriter and BufferedRandom try to share most
249 methods (this is helped by the members `readable` and `writable`, which
250 are initialized in the respective constructors)
251 * They also share a single buffer for reading and writing. This enables
252 interleaved reads and writes without flushing. It also makes the logic
253 a bit trickier to get right.
254 * The absolute position of the raw stream is cached, if possible, in the
255 `abs_pos` member. It must be updated every time an operation is done
256 on the raw stream. If not sure, it can be reinitialized by calling
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000257 _buffered_raw_tell(), which queries the raw stream (_buffered_raw_seek()
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000258 also does it). To read it, use RAW_TELL().
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000259 * Three helpers, _bufferedreader_raw_read, _bufferedwriter_raw_write and
260 _bufferedwriter_flush_unlocked do a lot of useful housekeeping.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000261
262 NOTE: we should try to maintain block alignment of reads and writes to the
263 raw stream (according to the buffer size), but for now it is only done
264 in read() and friends.
Antoine Pitrou3486a982011-05-12 01:57:53 +0200265
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000266*/
267
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000268/* These macros protect the buffered object against concurrent operations. */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000269
Antoine Pitrouf3b68b32010-12-03 18:41:39 +0000270static int
271_enter_buffered_busy(buffered *self)
272{
Antoine Pitrou25f85d42015-04-13 19:41:47 +0200273 int relax_locking;
274 PyLockStatus st;
Antoine Pitrouf3b68b32010-12-03 18:41:39 +0000275 if (self->owner == PyThread_get_thread_ident()) {
276 PyErr_Format(PyExc_RuntimeError,
277 "reentrant call inside %R", self);
278 return 0;
Antoine Pitrou5800b272009-11-01 12:05:48 +0000279 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600280 relax_locking = _Py_IsFinalizing();
Antoine Pitrouf3b68b32010-12-03 18:41:39 +0000281 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou25f85d42015-04-13 19:41:47 +0200282 if (!relax_locking)
283 st = PyThread_acquire_lock(self->lock, 1);
284 else {
285 /* When finalizing, we don't want a deadlock to happen with daemon
286 * threads abruptly shut down while they owned the lock.
287 * Therefore, only wait for a grace period (1 s.).
288 * Note that non-daemon threads have already exited here, so this
289 * shouldn't affect carefully written threaded I/O code.
290 */
Steve Dower6baa0f92015-05-23 08:59:25 -0700291 st = PyThread_acquire_lock_timed(self->lock, (PY_TIMEOUT_T)1e6, 0);
Antoine Pitrou25f85d42015-04-13 19:41:47 +0200292 }
Antoine Pitrouf3b68b32010-12-03 18:41:39 +0000293 Py_END_ALLOW_THREADS
Antoine Pitrou25f85d42015-04-13 19:41:47 +0200294 if (relax_locking && st != PY_LOCK_ACQUIRED) {
295 PyObject *msgobj = PyUnicode_FromFormat(
296 "could not acquire lock for %A at interpreter "
297 "shutdown, possibly due to daemon threads",
298 (PyObject *) self);
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200299 const char *msg = PyUnicode_AsUTF8(msgobj);
Antoine Pitrou25f85d42015-04-13 19:41:47 +0200300 Py_FatalError(msg);
301 }
Antoine Pitrouf3b68b32010-12-03 18:41:39 +0000302 return 1;
303}
304
305#define ENTER_BUFFERED(self) \
306 ( (PyThread_acquire_lock(self->lock, 0) ? \
307 1 : _enter_buffered_busy(self)) \
308 && (self->owner = PyThread_get_thread_ident(), 1) )
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000309
310#define LEAVE_BUFFERED(self) \
Antoine Pitrouf3b68b32010-12-03 18:41:39 +0000311 do { \
312 self->owner = 0; \
313 PyThread_release_lock(self->lock); \
314 } while(0);
315
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000316#define CHECK_INITIALIZED(self) \
317 if (self->ok <= 0) { \
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000318 if (self->detached) { \
319 PyErr_SetString(PyExc_ValueError, \
320 "raw stream has been detached"); \
321 } else { \
322 PyErr_SetString(PyExc_ValueError, \
323 "I/O operation on uninitialized object"); \
324 } \
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000325 return NULL; \
326 }
327
328#define CHECK_INITIALIZED_INT(self) \
329 if (self->ok <= 0) { \
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000330 if (self->detached) { \
331 PyErr_SetString(PyExc_ValueError, \
332 "raw stream has been detached"); \
333 } else { \
334 PyErr_SetString(PyExc_ValueError, \
335 "I/O operation on uninitialized object"); \
336 } \
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000337 return -1; \
338 }
339
Antoine Pitrou711af3a2009-04-11 15:39:24 +0000340#define IS_CLOSED(self) \
benfogle9703f092017-11-10 16:03:40 -0500341 (!self->buffer || \
Antoine Pitrou711af3a2009-04-11 15:39:24 +0000342 (self->fast_closed_checks \
343 ? _PyFileIO_closed(self->raw) \
benfogle9703f092017-11-10 16:03:40 -0500344 : buffered_closed(self)))
Antoine Pitrou711af3a2009-04-11 15:39:24 +0000345
346#define CHECK_CLOSED(self, error_msg) \
347 if (IS_CLOSED(self)) { \
348 PyErr_SetString(PyExc_ValueError, error_msg); \
349 return NULL; \
350 }
351
352
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000353#define VALID_READ_BUFFER(self) \
354 (self->readable && self->read_end != -1)
355
356#define VALID_WRITE_BUFFER(self) \
357 (self->writable && self->write_end != -1)
358
359#define ADJUST_POSITION(self, _new_pos) \
360 do { \
361 self->pos = _new_pos; \
362 if (VALID_READ_BUFFER(self) && self->read_end < self->pos) \
363 self->read_end = self->pos; \
364 } while(0)
365
366#define READAHEAD(self) \
367 ((self->readable && VALID_READ_BUFFER(self)) \
368 ? (self->read_end - self->pos) : 0)
369
370#define RAW_OFFSET(self) \
371 (((VALID_READ_BUFFER(self) || VALID_WRITE_BUFFER(self)) \
372 && self->raw_pos >= 0) ? self->raw_pos - self->pos : 0)
373
374#define RAW_TELL(self) \
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000375 (self->abs_pos != -1 ? self->abs_pos : _buffered_raw_tell(self))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000376
377#define MINUS_LAST_BLOCK(self, size) \
378 (self->buffer_mask ? \
379 (size & ~self->buffer_mask) : \
380 (self->buffer_size * (size / self->buffer_size)))
381
382
383static void
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000384buffered_dealloc(buffered *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000385{
Antoine Pitrou796564c2013-07-30 19:59:21 +0200386 self->finalizing = 1;
387 if (_PyIOBase_finalize((PyObject *) self) < 0)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000388 return;
389 _PyObject_GC_UNTRACK(self);
390 self->ok = 0;
391 if (self->weakreflist != NULL)
392 PyObject_ClearWeakRefs((PyObject *)self);
393 Py_CLEAR(self->raw);
394 if (self->buffer) {
395 PyMem_Free(self->buffer);
396 self->buffer = NULL;
397 }
398 if (self->lock) {
399 PyThread_free_lock(self->lock);
400 self->lock = NULL;
401 }
402 Py_CLEAR(self->dict);
403 Py_TYPE(self)->tp_free((PyObject *)self);
404}
405
Antoine Pitrou10f0c502012-07-29 19:02:46 +0200406static PyObject *
jdemeyerfc512e32018-08-02 13:14:54 +0200407buffered_sizeof(buffered *self, PyObject *Py_UNUSED(ignored))
Antoine Pitrou10f0c502012-07-29 19:02:46 +0200408{
409 Py_ssize_t res;
410
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +0200411 res = _PyObject_SIZE(Py_TYPE(self));
Antoine Pitrou10f0c502012-07-29 19:02:46 +0200412 if (self->buffer)
413 res += self->buffer_size;
414 return PyLong_FromSsize_t(res);
415}
416
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000417static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000418buffered_traverse(buffered *self, visitproc visit, void *arg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000419{
420 Py_VISIT(self->raw);
421 Py_VISIT(self->dict);
422 return 0;
423}
424
425static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000426buffered_clear(buffered *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000427{
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000428 self->ok = 0;
429 Py_CLEAR(self->raw);
430 Py_CLEAR(self->dict);
431 return 0;
432}
433
Antoine Pitroue033e062010-10-29 10:38:18 +0000434/* Because this can call arbitrary code, it shouldn't be called when
435 the refcount is 0 (that is, not directly from tp_dealloc unless
436 the refcount has been temporarily re-incremented). */
Matthias Klosebee33162010-11-16 20:07:51 +0000437static PyObject *
Antoine Pitroue033e062010-10-29 10:38:18 +0000438buffered_dealloc_warn(buffered *self, PyObject *source)
439{
440 if (self->ok && self->raw) {
441 PyObject *r;
Jeroen Demeyer59ad1102019-07-11 10:59:05 +0200442 r = _PyObject_CallMethodIdOneArg(self->raw, &PyId__dealloc_warn,
443 source);
Antoine Pitroue033e062010-10-29 10:38:18 +0000444 if (r)
445 Py_DECREF(r);
446 else
447 PyErr_Clear();
448 }
449 Py_RETURN_NONE;
450}
451
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000452/*
453 * _BufferedIOMixin methods
454 * This is not a class, just a collection of methods that will be reused
455 * by BufferedReader and BufferedWriter
456 */
457
458/* Flush and close */
459
460static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000461buffered_simple_flush(buffered *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000462{
463 CHECK_INITIALIZED(self)
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200464 return _PyObject_CallMethodNoArgs(self->raw, _PyIO_str_flush);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000465}
466
467static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000468buffered_closed(buffered *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000469{
470 int closed;
471 PyObject *res;
472 CHECK_INITIALIZED_INT(self)
473 res = PyObject_GetAttr(self->raw, _PyIO_str_closed);
474 if (res == NULL)
475 return -1;
476 closed = PyObject_IsTrue(res);
477 Py_DECREF(res);
478 return closed;
479}
480
481static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000482buffered_closed_get(buffered *self, void *context)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000483{
484 CHECK_INITIALIZED(self)
485 return PyObject_GetAttr(self->raw, _PyIO_str_closed);
486}
487
488static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000489buffered_close(buffered *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000490{
Benjamin Peterson68623612012-12-20 11:53:11 -0600491 PyObject *res = NULL, *exc = NULL, *val, *tb;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000492 int r;
493
494 CHECK_INITIALIZED(self)
Antoine Pitrouf3b68b32010-12-03 18:41:39 +0000495 if (!ENTER_BUFFERED(self))
496 return NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000497
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000498 r = buffered_closed(self);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000499 if (r < 0)
500 goto end;
501 if (r > 0) {
502 res = Py_None;
503 Py_INCREF(res);
504 goto end;
505 }
Antoine Pitroue033e062010-10-29 10:38:18 +0000506
Antoine Pitrou796564c2013-07-30 19:59:21 +0200507 if (self->finalizing) {
Antoine Pitroue033e062010-10-29 10:38:18 +0000508 PyObject *r = buffered_dealloc_warn(self, (PyObject *) self);
509 if (r)
510 Py_DECREF(r);
511 else
512 PyErr_Clear();
513 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000514 /* flush() will most probably re-take the lock, so drop it first */
515 LEAVE_BUFFERED(self)
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200516 res = _PyObject_CallMethodNoArgs((PyObject *)self, _PyIO_str_flush);
Antoine Pitrouf3b68b32010-12-03 18:41:39 +0000517 if (!ENTER_BUFFERED(self))
518 return NULL;
Benjamin Peterson68623612012-12-20 11:53:11 -0600519 if (res == NULL)
520 PyErr_Fetch(&exc, &val, &tb);
521 else
522 Py_DECREF(res);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000523
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200524 res = _PyObject_CallMethodNoArgs(self->raw, _PyIO_str_close);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000525
Jesus Ceadc469452012-10-04 12:37:56 +0200526 if (self->buffer) {
527 PyMem_Free(self->buffer);
528 self->buffer = NULL;
529 }
530
Benjamin Peterson68623612012-12-20 11:53:11 -0600531 if (exc != NULL) {
Serhiy Storchakae2bd2a72014-10-08 22:31:52 +0300532 _PyErr_ChainExceptions(exc, val, tb);
533 Py_CLEAR(res);
Benjamin Peterson68623612012-12-20 11:53:11 -0600534 }
535
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000536end:
537 LEAVE_BUFFERED(self)
538 return res;
539}
540
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000541/* detach */
542
543static PyObject *
jdemeyerfc512e32018-08-02 13:14:54 +0200544buffered_detach(buffered *self, PyObject *Py_UNUSED(ignored))
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000545{
546 PyObject *raw, *res;
547 CHECK_INITIALIZED(self)
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200548 res = _PyObject_CallMethodNoArgs((PyObject *)self, _PyIO_str_flush);
Benjamin Petersond2e0c792009-05-01 20:40:59 +0000549 if (res == NULL)
550 return NULL;
551 Py_DECREF(res);
552 raw = self->raw;
553 self->raw = NULL;
554 self->detached = 1;
555 self->ok = 0;
556 return raw;
557}
558
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000559/* Inquiries */
560
561static PyObject *
jdemeyerfc512e32018-08-02 13:14:54 +0200562buffered_seekable(buffered *self, PyObject *Py_UNUSED(ignored))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000563{
564 CHECK_INITIALIZED(self)
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200565 return _PyObject_CallMethodNoArgs(self->raw, _PyIO_str_seekable);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000566}
567
568static PyObject *
jdemeyerfc512e32018-08-02 13:14:54 +0200569buffered_readable(buffered *self, PyObject *Py_UNUSED(ignored))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000570{
571 CHECK_INITIALIZED(self)
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200572 return _PyObject_CallMethodNoArgs(self->raw, _PyIO_str_readable);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000573}
574
575static PyObject *
jdemeyerfc512e32018-08-02 13:14:54 +0200576buffered_writable(buffered *self, PyObject *Py_UNUSED(ignored))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000577{
578 CHECK_INITIALIZED(self)
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200579 return _PyObject_CallMethodNoArgs(self->raw, _PyIO_str_writable);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000580}
581
582static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000583buffered_name_get(buffered *self, void *context)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000584{
585 CHECK_INITIALIZED(self)
Martin v. Löwis767046a2011-10-14 15:35:36 +0200586 return _PyObject_GetAttrId(self->raw, &PyId_name);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000587}
588
589static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000590buffered_mode_get(buffered *self, void *context)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000591{
592 CHECK_INITIALIZED(self)
Martin v. Löwis767046a2011-10-14 15:35:36 +0200593 return _PyObject_GetAttrId(self->raw, &PyId_mode);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000594}
595
596/* Lower-level APIs */
597
598static PyObject *
jdemeyerfc512e32018-08-02 13:14:54 +0200599buffered_fileno(buffered *self, PyObject *Py_UNUSED(ignored))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000600{
601 CHECK_INITIALIZED(self)
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200602 return _PyObject_CallMethodNoArgs(self->raw, _PyIO_str_fileno);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000603}
604
605static PyObject *
jdemeyerfc512e32018-08-02 13:14:54 +0200606buffered_isatty(buffered *self, PyObject *Py_UNUSED(ignored))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000607{
608 CHECK_INITIALIZED(self)
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200609 return _PyObject_CallMethodNoArgs(self->raw, _PyIO_str_isatty);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000610}
Antoine Pitrou711af3a2009-04-11 15:39:24 +0000611
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000612/* Forward decls */
613static PyObject *
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +0100614_bufferedwriter_flush_unlocked(buffered *);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000615static Py_ssize_t
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000616_bufferedreader_fill_buffer(buffered *self);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000617static void
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000618_bufferedreader_reset_buf(buffered *self);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000619static void
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000620_bufferedwriter_reset_buf(buffered *self);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000621static PyObject *
Victor Stinnerbc93a112011-06-01 00:01:24 +0200622_bufferedreader_peek_unlocked(buffered *self);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000623static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000624_bufferedreader_read_all(buffered *self);
Antoine Pitrou711af3a2009-04-11 15:39:24 +0000625static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000626_bufferedreader_read_fast(buffered *self, Py_ssize_t);
Antoine Pitrou711af3a2009-04-11 15:39:24 +0000627static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000628_bufferedreader_read_generic(buffered *self, Py_ssize_t);
Antoine Pitrou3486a982011-05-12 01:57:53 +0200629static Py_ssize_t
630_bufferedreader_raw_read(buffered *self, char *start, Py_ssize_t len);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000631
632/*
633 * Helpers
634 */
635
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +0100636/* Sets the current error to BlockingIOError */
637static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200638_set_BlockingIOError(const char *msg, Py_ssize_t written)
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +0100639{
640 PyObject *err;
Victor Stinnerace47d72013-07-18 01:41:08 +0200641 PyErr_Clear();
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +0100642 err = PyObject_CallFunction(PyExc_BlockingIOError, "isn",
643 errno, msg, written);
644 if (err)
645 PyErr_SetObject(PyExc_BlockingIOError, err);
646 Py_XDECREF(err);
647}
648
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000649/* Returns the address of the `written` member if a BlockingIOError was
650 raised, NULL otherwise. The error is always re-raised. */
651static Py_ssize_t *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000652_buffered_check_blocking_error(void)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000653{
654 PyObject *t, *v, *tb;
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200655 PyOSErrorObject *err;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000656
657 PyErr_Fetch(&t, &v, &tb);
658 if (v == NULL || !PyErr_GivenExceptionMatches(v, PyExc_BlockingIOError)) {
659 PyErr_Restore(t, v, tb);
660 return NULL;
661 }
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200662 err = (PyOSErrorObject *) v;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000663 /* TODO: sanity check (err->written >= 0) */
664 PyErr_Restore(t, v, tb);
665 return &err->written;
666}
667
668static Py_off_t
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000669_buffered_raw_tell(buffered *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000670{
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000671 Py_off_t n;
Antoine Pitrou711af3a2009-04-11 15:39:24 +0000672 PyObject *res;
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200673 res = _PyObject_CallMethodNoArgs(self->raw, _PyIO_str_tell);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000674 if (res == NULL)
675 return -1;
676 n = PyNumber_AsOff_t(res, PyExc_ValueError);
677 Py_DECREF(res);
678 if (n < 0) {
679 if (!PyErr_Occurred())
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300680 PyErr_Format(PyExc_OSError,
Mark Dickinson1a0aaaa2009-11-24 20:54:11 +0000681 "Raw stream returned invalid position %" PY_PRIdOFF,
Antoine Pitrou3486a982011-05-12 01:57:53 +0200682 (PY_OFF_T_COMPAT)n);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000683 return -1;
684 }
685 self->abs_pos = n;
686 return n;
687}
688
689static Py_off_t
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000690_buffered_raw_seek(buffered *self, Py_off_t target, int whence)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000691{
692 PyObject *res, *posobj, *whenceobj;
693 Py_off_t n;
694
695 posobj = PyLong_FromOff_t(target);
696 if (posobj == NULL)
697 return -1;
698 whenceobj = PyLong_FromLong(whence);
699 if (whenceobj == NULL) {
700 Py_DECREF(posobj);
701 return -1;
702 }
703 res = PyObject_CallMethodObjArgs(self->raw, _PyIO_str_seek,
704 posobj, whenceobj, NULL);
705 Py_DECREF(posobj);
706 Py_DECREF(whenceobj);
707 if (res == NULL)
708 return -1;
709 n = PyNumber_AsOff_t(res, PyExc_ValueError);
710 Py_DECREF(res);
711 if (n < 0) {
712 if (!PyErr_Occurred())
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300713 PyErr_Format(PyExc_OSError,
Mark Dickinson1a0aaaa2009-11-24 20:54:11 +0000714 "Raw stream returned invalid position %" PY_PRIdOFF,
Antoine Pitrou3486a982011-05-12 01:57:53 +0200715 (PY_OFF_T_COMPAT)n);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000716 return -1;
717 }
718 self->abs_pos = n;
719 return n;
720}
721
722static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000723_buffered_init(buffered *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000724{
725 Py_ssize_t n;
726 if (self->buffer_size <= 0) {
727 PyErr_SetString(PyExc_ValueError,
728 "buffer size must be strictly positive");
729 return -1;
730 }
731 if (self->buffer)
732 PyMem_Free(self->buffer);
733 self->buffer = PyMem_Malloc(self->buffer_size);
734 if (self->buffer == NULL) {
735 PyErr_NoMemory();
736 return -1;
737 }
Antoine Pitrouc881f152010-08-01 16:53:42 +0000738 if (self->lock)
739 PyThread_free_lock(self->lock);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000740 self->lock = PyThread_allocate_lock();
741 if (self->lock == NULL) {
742 PyErr_SetString(PyExc_RuntimeError, "can't allocate read lock");
743 return -1;
744 }
Antoine Pitrouf3b68b32010-12-03 18:41:39 +0000745 self->owner = 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000746 /* Find out whether buffer_size is a power of 2 */
747 /* XXX is this optimization useful? */
748 for (n = self->buffer_size - 1; n & 1; n >>= 1)
749 ;
750 if (n == 0)
751 self->buffer_mask = self->buffer_size - 1;
752 else
753 self->buffer_mask = 0;
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000754 if (_buffered_raw_tell(self) == -1)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000755 PyErr_Clear();
756 return 0;
757}
758
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300759/* Return 1 if an OSError with errno == EINTR is set (and then
Antoine Pitrou707ce822011-02-25 21:24:11 +0000760 clears the error indicator), 0 otherwise.
761 Should only be called when PyErr_Occurred() is true.
762*/
Gregory P. Smith51359922012-06-23 23:55:39 -0700763int
764_PyIO_trap_eintr(void)
Antoine Pitrou707ce822011-02-25 21:24:11 +0000765{
766 static PyObject *eintr_int = NULL;
767 PyObject *typ, *val, *tb;
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300768 PyOSErrorObject *env_err;
Antoine Pitrou707ce822011-02-25 21:24:11 +0000769
770 if (eintr_int == NULL) {
771 eintr_int = PyLong_FromLong(EINTR);
772 assert(eintr_int != NULL);
773 }
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300774 if (!PyErr_ExceptionMatches(PyExc_OSError))
Antoine Pitrou707ce822011-02-25 21:24:11 +0000775 return 0;
776 PyErr_Fetch(&typ, &val, &tb);
777 PyErr_NormalizeException(&typ, &val, &tb);
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300778 env_err = (PyOSErrorObject *) val;
Antoine Pitrou707ce822011-02-25 21:24:11 +0000779 assert(env_err != NULL);
780 if (env_err->myerrno != NULL &&
781 PyObject_RichCompareBool(env_err->myerrno, eintr_int, Py_EQ) > 0) {
782 Py_DECREF(typ);
783 Py_DECREF(val);
784 Py_XDECREF(tb);
785 return 1;
786 }
787 /* This silences any error set by PyObject_RichCompareBool() */
788 PyErr_Restore(typ, val, tb);
789 return 0;
790}
791
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000792/*
793 * Shared methods and wrappers
794 */
795
796static PyObject *
Antoine Pitroue05565e2011-08-20 14:39:23 +0200797buffered_flush_and_rewind_unlocked(buffered *self)
798{
799 PyObject *res;
800
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +0100801 res = _bufferedwriter_flush_unlocked(self);
Antoine Pitroue05565e2011-08-20 14:39:23 +0200802 if (res == NULL)
803 return NULL;
804 Py_DECREF(res);
805
806 if (self->readable) {
807 /* Rewind the raw stream so that its position corresponds to
808 the current logical position. */
809 Py_off_t n;
810 n = _buffered_raw_seek(self, -RAW_OFFSET(self), 1);
811 _bufferedreader_reset_buf(self);
812 if (n == -1)
813 return NULL;
814 }
815 Py_RETURN_NONE;
816}
817
818static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000819buffered_flush(buffered *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000820{
821 PyObject *res;
822
823 CHECK_INITIALIZED(self)
Antoine Pitrou711af3a2009-04-11 15:39:24 +0000824 CHECK_CLOSED(self, "flush of closed file")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000825
Antoine Pitrouf3b68b32010-12-03 18:41:39 +0000826 if (!ENTER_BUFFERED(self))
827 return NULL;
Antoine Pitroue05565e2011-08-20 14:39:23 +0200828 res = buffered_flush_and_rewind_unlocked(self);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000829 LEAVE_BUFFERED(self)
830
831 return res;
832}
833
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300834/*[clinic input]
835_io._Buffered.peek
836 size: Py_ssize_t = 0
837 /
838
839[clinic start generated code]*/
840
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000841static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300842_io__Buffered_peek_impl(buffered *self, Py_ssize_t size)
843/*[clinic end generated code: output=ba7a097ca230102b input=37ffb97d06ff4adb]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000844{
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000845 PyObject *res = NULL;
846
847 CHECK_INITIALIZED(self)
Berker Peksagd10d6ae2015-05-12 17:01:05 +0300848 CHECK_CLOSED(self, "peek of closed file")
849
Antoine Pitrouf3b68b32010-12-03 18:41:39 +0000850 if (!ENTER_BUFFERED(self))
851 return NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000852
853 if (self->writable) {
Antoine Pitroue05565e2011-08-20 14:39:23 +0200854 res = buffered_flush_and_rewind_unlocked(self);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000855 if (res == NULL)
856 goto end;
857 Py_CLEAR(res);
858 }
Victor Stinnerbc93a112011-06-01 00:01:24 +0200859 res = _bufferedreader_peek_unlocked(self);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000860
861end:
862 LEAVE_BUFFERED(self)
863 return res;
864}
865
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300866/*[clinic input]
867_io._Buffered.read
Serhiy Storchaka762bf402017-03-30 09:15:31 +0300868 size as n: Py_ssize_t(accept={int, NoneType}) = -1
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300869 /
870[clinic start generated code]*/
871
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000872static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300873_io__Buffered_read_impl(buffered *self, Py_ssize_t n)
Serhiy Storchaka762bf402017-03-30 09:15:31 +0300874/*[clinic end generated code: output=f41c78bb15b9bbe9 input=7df81e82e08a68a2]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000875{
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000876 PyObject *res;
877
878 CHECK_INITIALIZED(self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000879 if (n < -1) {
880 PyErr_SetString(PyExc_ValueError,
Martin Panterccb2c0e2016-10-20 23:48:14 +0000881 "read length must be non-negative or -1");
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000882 return NULL;
883 }
884
Antoine Pitrou711af3a2009-04-11 15:39:24 +0000885 CHECK_CLOSED(self, "read of closed file")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000886
Antoine Pitrou711af3a2009-04-11 15:39:24 +0000887 if (n == -1) {
888 /* The number of bytes is unspecified, read until the end of stream */
Antoine Pitrouf3b68b32010-12-03 18:41:39 +0000889 if (!ENTER_BUFFERED(self))
890 return NULL;
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000891 res = _bufferedreader_read_all(self);
Antoine Pitrou711af3a2009-04-11 15:39:24 +0000892 }
893 else {
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000894 res = _bufferedreader_read_fast(self, n);
Antoine Pitroue05565e2011-08-20 14:39:23 +0200895 if (res != Py_None)
896 return res;
897 Py_DECREF(res);
898 if (!ENTER_BUFFERED(self))
899 return NULL;
900 res = _bufferedreader_read_generic(self, n);
Antoine Pitrou711af3a2009-04-11 15:39:24 +0000901 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000902
Antoine Pitroue05565e2011-08-20 14:39:23 +0200903 LEAVE_BUFFERED(self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000904 return res;
905}
906
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300907/*[clinic input]
908_io._Buffered.read1
Martin Panterccb2c0e2016-10-20 23:48:14 +0000909 size as n: Py_ssize_t = -1
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300910 /
911[clinic start generated code]*/
912
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000913static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300914_io__Buffered_read1_impl(buffered *self, Py_ssize_t n)
Martin Panterccb2c0e2016-10-20 23:48:14 +0000915/*[clinic end generated code: output=bcc4fb4e54d103a3 input=7d22de9630b61774]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000916{
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300917 Py_ssize_t have, r;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000918 PyObject *res = NULL;
919
920 CHECK_INITIALIZED(self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000921 if (n < 0) {
Martin Panterccb2c0e2016-10-20 23:48:14 +0000922 n = self->buffer_size;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000923 }
Berker Peksagd10d6ae2015-05-12 17:01:05 +0300924
925 CHECK_CLOSED(self, "read of closed file")
926
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000927 if (n == 0)
928 return PyBytes_FromStringAndSize(NULL, 0);
929
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000930 /* Return up to n bytes. If at least one byte is buffered, we
931 only return buffered bytes. Otherwise, we do one raw read. */
932
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000933 have = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
934 if (have > 0) {
Antoine Pitrou56a220a2011-11-16 00:56:10 +0100935 n = Py_MIN(have, n);
936 res = _bufferedreader_read_fast(self, n);
937 assert(res != Py_None);
938 return res;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000939 }
Antoine Pitrou56a220a2011-11-16 00:56:10 +0100940 res = PyBytes_FromStringAndSize(NULL, n);
941 if (res == NULL)
942 return NULL;
943 if (!ENTER_BUFFERED(self)) {
Antoine Pitroue05565e2011-08-20 14:39:23 +0200944 Py_DECREF(res);
Antoine Pitrou56a220a2011-11-16 00:56:10 +0100945 return NULL;
Antoine Pitroue05565e2011-08-20 14:39:23 +0200946 }
Benjamin Peterson680bf1a2009-06-12 02:07:12 +0000947 _bufferedreader_reset_buf(self);
Antoine Pitrou56a220a2011-11-16 00:56:10 +0100948 r = _bufferedreader_raw_read(self, PyBytes_AS_STRING(res), n);
949 LEAVE_BUFFERED(self)
950 if (r == -1) {
951 Py_DECREF(res);
952 return NULL;
953 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000954 if (r == -2)
955 r = 0;
956 if (n > r)
Antoine Pitrou56a220a2011-11-16 00:56:10 +0100957 _PyBytes_Resize(&res, r);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000958 return res;
959}
960
961static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300962_buffered_readinto_generic(buffered *self, Py_buffer *buffer, char readinto1)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000963{
Antoine Pitrou3486a982011-05-12 01:57:53 +0200964 Py_ssize_t n, written = 0, remaining;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000965 PyObject *res = NULL;
966
967 CHECK_INITIALIZED(self)
Philipp Gesangcb1c0742020-02-04 22:25:16 +0100968 CHECK_CLOSED(self, "readinto of closed file")
Antoine Pitrou3486a982011-05-12 01:57:53 +0200969
Antoine Pitrou3486a982011-05-12 01:57:53 +0200970 n = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
971 if (n > 0) {
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300972 if (n >= buffer->len) {
973 memcpy(buffer->buf, self->buffer + self->pos, buffer->len);
974 self->pos += buffer->len;
975 return PyLong_FromSsize_t(buffer->len);
Antoine Pitrou3486a982011-05-12 01:57:53 +0200976 }
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300977 memcpy(buffer->buf, self->buffer + self->pos, n);
Antoine Pitrou3486a982011-05-12 01:57:53 +0200978 self->pos += n;
979 written = n;
980 }
981
982 if (!ENTER_BUFFERED(self))
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300983 return NULL;
Antoine Pitrou3486a982011-05-12 01:57:53 +0200984
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000985 if (self->writable) {
Antoine Pitroue8bb1a02011-08-20 14:52:04 +0200986 res = buffered_flush_and_rewind_unlocked(self);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000987 if (res == NULL)
988 goto end;
Antoine Pitrou3486a982011-05-12 01:57:53 +0200989 Py_CLEAR(res);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +0000990 }
Antoine Pitrou3486a982011-05-12 01:57:53 +0200991
992 _bufferedreader_reset_buf(self);
993 self->pos = 0;
994
Serhiy Storchakaf24131f2015-04-16 11:19:43 +0300995 for (remaining = buffer->len - written;
Antoine Pitrou3486a982011-05-12 01:57:53 +0200996 remaining > 0;
997 written += n, remaining -= n) {
998 /* If remaining bytes is larger than internal buffer size, copy
999 * directly into caller's buffer. */
1000 if (remaining > self->buffer_size) {
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001001 n = _bufferedreader_raw_read(self, (char *) buffer->buf + written,
Antoine Pitrou4e19e112011-05-12 02:07:00 +02001002 remaining);
Antoine Pitrou3486a982011-05-12 01:57:53 +02001003 }
Benjamin Petersona96fea02014-06-22 14:17:44 -07001004
1005 /* In readinto1 mode, we do not want to fill the internal
1006 buffer if we already have some data to return */
1007 else if (!(readinto1 && written)) {
Antoine Pitrou3486a982011-05-12 01:57:53 +02001008 n = _bufferedreader_fill_buffer(self);
1009 if (n > 0) {
1010 if (n > remaining)
1011 n = remaining;
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001012 memcpy((char *) buffer->buf + written,
Antoine Pitrou4e19e112011-05-12 02:07:00 +02001013 self->buffer + self->pos, n);
Antoine Pitrou3486a982011-05-12 01:57:53 +02001014 self->pos += n;
1015 continue; /* short circuit */
1016 }
1017 }
Benjamin Petersona96fea02014-06-22 14:17:44 -07001018 else
1019 n = 0;
Serhiy Storchaka009b8112015-03-18 21:53:15 +02001020
Antoine Pitrou3486a982011-05-12 01:57:53 +02001021 if (n == 0 || (n == -2 && written > 0))
1022 break;
1023 if (n < 0) {
1024 if (n == -2) {
1025 Py_INCREF(Py_None);
1026 res = Py_None;
1027 }
1028 goto end;
1029 }
Serhiy Storchaka009b8112015-03-18 21:53:15 +02001030
Benjamin Petersona96fea02014-06-22 14:17:44 -07001031 /* At most one read in readinto1 mode */
1032 if (readinto1) {
1033 written += n;
1034 break;
1035 }
Antoine Pitrou3486a982011-05-12 01:57:53 +02001036 }
1037 res = PyLong_FromSsize_t(written);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001038
1039end:
Antoine Pitrou3486a982011-05-12 01:57:53 +02001040 LEAVE_BUFFERED(self);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001041 return res;
1042}
1043
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001044/*[clinic input]
1045_io._Buffered.readinto
Larry Hastingsdbfdc382015-05-04 06:59:46 -07001046 buffer: Py_buffer(accept={rwbuffer})
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001047 /
1048[clinic start generated code]*/
Benjamin Petersona96fea02014-06-22 14:17:44 -07001049
1050static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001051_io__Buffered_readinto_impl(buffered *self, Py_buffer *buffer)
Larry Hastingsdbfdc382015-05-04 06:59:46 -07001052/*[clinic end generated code: output=bcb376580b1d8170 input=ed6b98b7a20a3008]*/
Benjamin Petersona96fea02014-06-22 14:17:44 -07001053{
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001054 return _buffered_readinto_generic(self, buffer, 0);
1055}
1056
1057/*[clinic input]
1058_io._Buffered.readinto1
Larry Hastingsdbfdc382015-05-04 06:59:46 -07001059 buffer: Py_buffer(accept={rwbuffer})
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001060 /
1061[clinic start generated code]*/
1062
1063static PyObject *
1064_io__Buffered_readinto1_impl(buffered *self, Py_buffer *buffer)
Larry Hastingsdbfdc382015-05-04 06:59:46 -07001065/*[clinic end generated code: output=6e5c6ac5868205d6 input=4455c5d55fdf1687]*/
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001066{
1067 return _buffered_readinto_generic(self, buffer, 1);
Benjamin Petersona96fea02014-06-22 14:17:44 -07001068}
1069
1070
1071static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001072_buffered_readline(buffered *self, Py_ssize_t limit)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001073{
1074 PyObject *res = NULL;
1075 PyObject *chunks = NULL;
1076 Py_ssize_t n, written = 0;
1077 const char *start, *s, *end;
1078
Antoine Pitrou711af3a2009-04-11 15:39:24 +00001079 CHECK_CLOSED(self, "readline of closed file")
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001080
Antoine Pitrou711af3a2009-04-11 15:39:24 +00001081 /* First, try to find a line in the buffer. This can run unlocked because
1082 the calls to the C API are simple enough that they can't trigger
1083 any thread switch. */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001084 n = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
1085 if (limit >= 0 && n > limit)
1086 n = limit;
1087 start = self->buffer + self->pos;
Antoine Pitrou711af3a2009-04-11 15:39:24 +00001088 s = memchr(start, '\n', n);
1089 if (s != NULL) {
1090 res = PyBytes_FromStringAndSize(start, s - start + 1);
1091 if (res != NULL)
1092 self->pos += s - start + 1;
1093 goto end_unlocked;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001094 }
1095 if (n == limit) {
1096 res = PyBytes_FromStringAndSize(start, n);
1097 if (res != NULL)
1098 self->pos += n;
Antoine Pitrou711af3a2009-04-11 15:39:24 +00001099 goto end_unlocked;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001100 }
1101
Antoine Pitrouf3b68b32010-12-03 18:41:39 +00001102 if (!ENTER_BUFFERED(self))
1103 goto end_unlocked;
Antoine Pitrou711af3a2009-04-11 15:39:24 +00001104
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001105 /* Now we try to get some more from the raw stream */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001106 chunks = PyList_New(0);
1107 if (chunks == NULL)
1108 goto end;
1109 if (n > 0) {
1110 res = PyBytes_FromStringAndSize(start, n);
1111 if (res == NULL)
1112 goto end;
1113 if (PyList_Append(chunks, res) < 0) {
1114 Py_CLEAR(res);
1115 goto end;
1116 }
1117 Py_CLEAR(res);
1118 written += n;
Antoine Pitroue05565e2011-08-20 14:39:23 +02001119 self->pos += n;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001120 if (limit >= 0)
1121 limit -= n;
1122 }
Antoine Pitroue05565e2011-08-20 14:39:23 +02001123 if (self->writable) {
1124 PyObject *r = buffered_flush_and_rewind_unlocked(self);
1125 if (r == NULL)
1126 goto end;
1127 Py_DECREF(r);
1128 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001129
1130 for (;;) {
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001131 _bufferedreader_reset_buf(self);
1132 n = _bufferedreader_fill_buffer(self);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001133 if (n == -1)
1134 goto end;
1135 if (n <= 0)
1136 break;
1137 if (limit >= 0 && n > limit)
1138 n = limit;
1139 start = self->buffer;
1140 end = start + n;
1141 s = start;
1142 while (s < end) {
1143 if (*s++ == '\n') {
1144 res = PyBytes_FromStringAndSize(start, s - start);
1145 if (res == NULL)
1146 goto end;
1147 self->pos = s - start;
1148 goto found;
1149 }
1150 }
1151 res = PyBytes_FromStringAndSize(start, n);
1152 if (res == NULL)
1153 goto end;
1154 if (n == limit) {
1155 self->pos = n;
1156 break;
1157 }
1158 if (PyList_Append(chunks, res) < 0) {
1159 Py_CLEAR(res);
1160 goto end;
1161 }
1162 Py_CLEAR(res);
1163 written += n;
1164 if (limit >= 0)
1165 limit -= n;
1166 }
1167found:
1168 if (res != NULL && PyList_Append(chunks, res) < 0) {
1169 Py_CLEAR(res);
1170 goto end;
1171 }
Serhiy Storchaka48842712016-04-06 09:45:48 +03001172 Py_XSETREF(res, _PyBytes_Join(_PyIO_empty_bytes, chunks));
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001173
1174end:
1175 LEAVE_BUFFERED(self)
Antoine Pitrou711af3a2009-04-11 15:39:24 +00001176end_unlocked:
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001177 Py_XDECREF(chunks);
1178 return res;
1179}
1180
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001181/*[clinic input]
1182_io._Buffered.readline
Serhiy Storchaka762bf402017-03-30 09:15:31 +03001183 size: Py_ssize_t(accept={int, NoneType}) = -1
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001184 /
1185[clinic start generated code]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001186
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001187static PyObject *
1188_io__Buffered_readline_impl(buffered *self, Py_ssize_t size)
Serhiy Storchaka762bf402017-03-30 09:15:31 +03001189/*[clinic end generated code: output=24dd2aa6e33be83c input=673b6240e315ef8a]*/
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001190{
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001191 CHECK_INITIALIZED(self)
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001192 return _buffered_readline(self, size);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001193}
1194
1195
1196static PyObject *
jdemeyerfc512e32018-08-02 13:14:54 +02001197buffered_tell(buffered *self, PyObject *Py_UNUSED(ignored))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001198{
1199 Py_off_t pos;
1200
1201 CHECK_INITIALIZED(self)
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001202 pos = _buffered_raw_tell(self);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001203 if (pos == -1)
1204 return NULL;
1205 pos -= RAW_OFFSET(self);
1206 /* TODO: sanity check (pos >= 0) */
1207 return PyLong_FromOff_t(pos);
1208}
1209
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001210/*[clinic input]
1211_io._Buffered.seek
1212 target as targetobj: object
1213 whence: int = 0
1214 /
1215[clinic start generated code]*/
1216
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001217static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001218_io__Buffered_seek_impl(buffered *self, PyObject *targetobj, int whence)
1219/*[clinic end generated code: output=7ae0e8dc46efdefb input=a9c4920bfcba6163]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001220{
1221 Py_off_t target, n;
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001222 PyObject *res = NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001223
1224 CHECK_INITIALIZED(self)
Jesus Cea94363612012-06-22 18:32:07 +02001225
1226 /* Do some error checking instead of trusting OS 'seek()'
1227 ** error detection, just in case.
1228 */
1229 if ((whence < 0 || whence >2)
1230#ifdef SEEK_HOLE
1231 && (whence != SEEK_HOLE)
1232#endif
1233#ifdef SEEK_DATA
1234 && (whence != SEEK_DATA)
1235#endif
1236 ) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001237 PyErr_Format(PyExc_ValueError,
Jesus Cea94363612012-06-22 18:32:07 +02001238 "whence value %d unsupported", whence);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001239 return NULL;
1240 }
Antoine Pitrou711af3a2009-04-11 15:39:24 +00001241
1242 CHECK_CLOSED(self, "seek of closed file")
1243
Antoine Pitrou1e44fec2011-10-04 12:26:20 +02001244 if (_PyIOBase_check_seekable(self->raw, Py_True) == NULL)
1245 return NULL;
1246
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001247 target = PyNumber_AsOff_t(targetobj, PyExc_ValueError);
1248 if (target == -1 && PyErr_Occurred())
1249 return NULL;
1250
Jesus Cea94363612012-06-22 18:32:07 +02001251 /* SEEK_SET and SEEK_CUR are special because we could seek inside the
1252 buffer. Other whence values must be managed without this optimization.
1253 Some Operating Systems can provide additional values, like
1254 SEEK_HOLE/SEEK_DATA. */
1255 if (((whence == 0) || (whence == 1)) && self->readable) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001256 Py_off_t current, avail;
1257 /* Check if seeking leaves us inside the current buffer,
Antoine Pitrou711af3a2009-04-11 15:39:24 +00001258 so as to return quickly if possible. Also, we needn't take the
1259 lock in this fast path.
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001260 Don't know how to do that when whence == 2, though. */
Antoine Pitrou711af3a2009-04-11 15:39:24 +00001261 /* NOTE: RAW_TELL() can release the GIL but the object is in a stable
1262 state at this point. */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001263 current = RAW_TELL(self);
1264 avail = READAHEAD(self);
1265 if (avail > 0) {
1266 Py_off_t offset;
1267 if (whence == 0)
1268 offset = target - (current - RAW_OFFSET(self));
1269 else
1270 offset = target;
1271 if (offset >= -self->pos && offset <= avail) {
1272 self->pos += offset;
Antoine Pitrou711af3a2009-04-11 15:39:24 +00001273 return PyLong_FromOff_t(current - avail + offset);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001274 }
1275 }
1276 }
1277
Antoine Pitrouf3b68b32010-12-03 18:41:39 +00001278 if (!ENTER_BUFFERED(self))
1279 return NULL;
Antoine Pitrou711af3a2009-04-11 15:39:24 +00001280
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001281 /* Fallback: invoke raw seek() method and clear buffer */
1282 if (self->writable) {
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01001283 res = _bufferedwriter_flush_unlocked(self);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001284 if (res == NULL)
1285 goto end;
1286 Py_CLEAR(res);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001287 }
1288
1289 /* TODO: align on block boundary and read buffer if needed? */
1290 if (whence == 1)
1291 target -= RAW_OFFSET(self);
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001292 n = _buffered_raw_seek(self, target, whence);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001293 if (n == -1)
1294 goto end;
1295 self->raw_pos = -1;
1296 res = PyLong_FromOff_t(n);
1297 if (res != NULL && self->readable)
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001298 _bufferedreader_reset_buf(self);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001299
1300end:
1301 LEAVE_BUFFERED(self)
1302 return res;
1303}
1304
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001305/*[clinic input]
1306_io._Buffered.truncate
1307 pos: object = None
1308 /
1309[clinic start generated code]*/
1310
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001311static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001312_io__Buffered_truncate_impl(buffered *self, PyObject *pos)
1313/*[clinic end generated code: output=667ca03c60c270de input=8a1be34d57cca2d3]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001314{
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001315 PyObject *res = NULL;
1316
1317 CHECK_INITIALIZED(self)
Antoine Pitrouf3b68b32010-12-03 18:41:39 +00001318 if (!ENTER_BUFFERED(self))
1319 return NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001320
1321 if (self->writable) {
Antoine Pitroue05565e2011-08-20 14:39:23 +02001322 res = buffered_flush_and_rewind_unlocked(self);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001323 if (res == NULL)
1324 goto end;
1325 Py_CLEAR(res);
1326 }
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001327 res = _PyObject_CallMethodOneArg(self->raw, _PyIO_str_truncate, pos);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001328 if (res == NULL)
1329 goto end;
1330 /* Reset cached position */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001331 if (_buffered_raw_tell(self) == -1)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001332 PyErr_Clear();
1333
1334end:
1335 LEAVE_BUFFERED(self)
1336 return res;
1337}
1338
1339static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001340buffered_iternext(buffered *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001341{
1342 PyObject *line;
1343 PyTypeObject *tp;
1344
1345 CHECK_INITIALIZED(self);
1346
1347 tp = Py_TYPE(self);
1348 if (tp == &PyBufferedReader_Type ||
1349 tp == &PyBufferedRandom_Type) {
1350 /* Skip method call overhead for speed */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001351 line = _buffered_readline(self, -1);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001352 }
1353 else {
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001354 line = _PyObject_CallMethodNoArgs((PyObject *)self,
1355 _PyIO_str_readline);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001356 if (line && !PyBytes_Check(line)) {
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001357 PyErr_Format(PyExc_OSError,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001358 "readline() should have returned a bytes object, "
1359 "not '%.200s'", Py_TYPE(line)->tp_name);
1360 Py_DECREF(line);
1361 return NULL;
1362 }
1363 }
1364
1365 if (line == NULL)
1366 return NULL;
1367
1368 if (PyBytes_GET_SIZE(line) == 0) {
1369 /* Reached EOF or would have blocked */
1370 Py_DECREF(line);
1371 return NULL;
1372 }
1373
1374 return line;
1375}
1376
Antoine Pitrou716c4442009-05-23 19:04:03 +00001377static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001378buffered_repr(buffered *self)
Antoine Pitrou716c4442009-05-23 19:04:03 +00001379{
1380 PyObject *nameobj, *res;
1381
Serhiy Storchakab235a1b2019-08-29 09:25:22 +03001382 if (_PyObject_LookupAttrId((PyObject *) self, &PyId_name, &nameobj) < 0) {
1383 if (!PyErr_ExceptionMatches(PyExc_ValueError)) {
Antoine Pitrou716c4442009-05-23 19:04:03 +00001384 return NULL;
Serhiy Storchakab235a1b2019-08-29 09:25:22 +03001385 }
1386 /* Ignore ValueError raised if the underlying stream was detached */
1387 PyErr_Clear();
1388 }
1389 if (nameobj == NULL) {
Antoine Pitrou716c4442009-05-23 19:04:03 +00001390 res = PyUnicode_FromFormat("<%s>", Py_TYPE(self)->tp_name);
1391 }
1392 else {
Serhiy Storchakaa5af6e12017-03-19 19:25:29 +02001393 int status = Py_ReprEnter((PyObject *)self);
1394 res = NULL;
1395 if (status == 0) {
1396 res = PyUnicode_FromFormat("<%s name=%R>",
1397 Py_TYPE(self)->tp_name, nameobj);
1398 Py_ReprLeave((PyObject *)self);
1399 }
1400 else if (status > 0) {
1401 PyErr_Format(PyExc_RuntimeError,
1402 "reentrant call inside %s.__repr__",
1403 Py_TYPE(self)->tp_name);
1404 }
Antoine Pitrou716c4442009-05-23 19:04:03 +00001405 Py_DECREF(nameobj);
1406 }
1407 return res;
1408}
1409
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001410/*
1411 * class BufferedReader
1412 */
1413
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001414static void _bufferedreader_reset_buf(buffered *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001415{
1416 self->read_end = -1;
1417}
1418
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001419/*[clinic input]
1420_io.BufferedReader.__init__
1421 raw: object
1422 buffer_size: Py_ssize_t(c_default="DEFAULT_BUFFER_SIZE") = DEFAULT_BUFFER_SIZE
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001423
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001424Create a new buffered reader using the given readable raw IO object.
1425[clinic start generated code]*/
1426
1427static int
1428_io_BufferedReader___init___impl(buffered *self, PyObject *raw,
1429 Py_ssize_t buffer_size)
1430/*[clinic end generated code: output=cddcfefa0ed294c4 input=fb887e06f11b4e48]*/
1431{
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001432 self->ok = 0;
Benjamin Petersond2e0c792009-05-01 20:40:59 +00001433 self->detached = 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001434
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001435 if (_PyIOBase_check_readable(raw, Py_True) == NULL)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001436 return -1;
1437
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001438 Py_INCREF(raw);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001439 Py_XSETREF(self->raw, raw);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001440 self->buffer_size = buffer_size;
1441 self->readable = 1;
1442 self->writable = 0;
1443
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001444 if (_buffered_init(self) < 0)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001445 return -1;
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001446 _bufferedreader_reset_buf(self);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001447
Antoine Pitrou711af3a2009-04-11 15:39:24 +00001448 self->fast_closed_checks = (Py_TYPE(self) == &PyBufferedReader_Type &&
1449 Py_TYPE(raw) == &PyFileIO_Type);
1450
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001451 self->ok = 1;
1452 return 0;
1453}
1454
1455static Py_ssize_t
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001456_bufferedreader_raw_read(buffered *self, char *start, Py_ssize_t len)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001457{
1458 Py_buffer buf;
1459 PyObject *memobj, *res;
1460 Py_ssize_t n;
1461 /* NOTE: the buffer needn't be released as its object is NULL. */
1462 if (PyBuffer_FillInfo(&buf, NULL, start, len, 0, PyBUF_CONTIG) == -1)
1463 return -1;
1464 memobj = PyMemoryView_FromBuffer(&buf);
1465 if (memobj == NULL)
1466 return -1;
Antoine Pitrou707ce822011-02-25 21:24:11 +00001467 /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() when EINTR
1468 occurs so we needn't do it ourselves.
1469 We then retry reading, ignoring the signal if no handler has
1470 raised (see issue #10956).
1471 */
1472 do {
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001473 res = _PyObject_CallMethodOneArg(self->raw, _PyIO_str_readinto, memobj);
Gregory P. Smith51359922012-06-23 23:55:39 -07001474 } while (res == NULL && _PyIO_trap_eintr());
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001475 Py_DECREF(memobj);
1476 if (res == NULL)
1477 return -1;
1478 if (res == Py_None) {
1479 /* Non-blocking stream would have blocked. Special return code! */
1480 Py_DECREF(res);
1481 return -2;
1482 }
1483 n = PyNumber_AsSsize_t(res, PyExc_ValueError);
1484 Py_DECREF(res);
1485 if (n < 0 || n > len) {
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001486 PyErr_Format(PyExc_OSError,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001487 "raw readinto() returned invalid length %zd "
1488 "(should have been between 0 and %zd)", n, len);
1489 return -1;
1490 }
1491 if (n > 0 && self->abs_pos != -1)
1492 self->abs_pos += n;
1493 return n;
1494}
1495
1496static Py_ssize_t
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001497_bufferedreader_fill_buffer(buffered *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001498{
1499 Py_ssize_t start, len, n;
1500 if (VALID_READ_BUFFER(self))
1501 start = Py_SAFE_DOWNCAST(self->read_end, Py_off_t, Py_ssize_t);
1502 else
1503 start = 0;
1504 len = self->buffer_size - start;
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001505 n = _bufferedreader_raw_read(self, self->buffer + start, len);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001506 if (n <= 0)
1507 return n;
1508 self->read_end = start + n;
1509 self->raw_pos = start + n;
1510 return n;
1511}
1512
1513static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001514_bufferedreader_read_all(buffered *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001515{
Antoine Pitrou711af3a2009-04-11 15:39:24 +00001516 Py_ssize_t current_size;
Serhiy Storchaka4d9aec02018-01-16 18:34:21 +02001517 PyObject *res = NULL, *data = NULL, *tmp = NULL, *chunks = NULL, *readall;
Antoine Pitrou711af3a2009-04-11 15:39:24 +00001518
1519 /* First copy what we have in the current buffer. */
1520 current_size = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
1521 if (current_size) {
1522 data = PyBytes_FromStringAndSize(
1523 self->buffer + self->pos, current_size);
Victor Stinnerb57f1082011-05-26 00:19:38 +02001524 if (data == NULL)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001525 return NULL;
Antoine Pitroue05565e2011-08-20 14:39:23 +02001526 self->pos += current_size;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001527 }
Antoine Pitrou711af3a2009-04-11 15:39:24 +00001528 /* We're going past the buffer's bounds, flush it */
1529 if (self->writable) {
Richard Oudkerk9ad51ec2013-07-15 16:05:22 +01001530 tmp = buffered_flush_and_rewind_unlocked(self);
1531 if (tmp == NULL)
1532 goto cleanup;
1533 Py_CLEAR(tmp);
Antoine Pitrou711af3a2009-04-11 15:39:24 +00001534 }
Antoine Pitroue05565e2011-08-20 14:39:23 +02001535 _bufferedreader_reset_buf(self);
Victor Stinnerb57f1082011-05-26 00:19:38 +02001536
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001537 if (_PyObject_LookupAttr(self->raw, _PyIO_str_readall, &readall) < 0) {
1538 goto cleanup;
1539 }
Serhiy Storchaka4d9aec02018-01-16 18:34:21 +02001540 if (readall) {
1541 tmp = _PyObject_CallNoArg(readall);
1542 Py_DECREF(readall);
Richard Oudkerk9ad51ec2013-07-15 16:05:22 +01001543 if (tmp == NULL)
1544 goto cleanup;
1545 if (tmp != Py_None && !PyBytes_Check(tmp)) {
Victor Stinnerb57f1082011-05-26 00:19:38 +02001546 PyErr_SetString(PyExc_TypeError, "readall() should return bytes");
Richard Oudkerk9ad51ec2013-07-15 16:05:22 +01001547 goto cleanup;
Victor Stinnerb57f1082011-05-26 00:19:38 +02001548 }
Serhiy Storchaka4d9aec02018-01-16 18:34:21 +02001549 if (current_size == 0) {
Richard Oudkerk9ad51ec2013-07-15 16:05:22 +01001550 res = tmp;
Serhiy Storchaka4d9aec02018-01-16 18:34:21 +02001551 } else {
1552 if (tmp != Py_None) {
1553 PyBytes_Concat(&data, tmp);
1554 }
1555 res = data;
Richard Oudkerk9ad51ec2013-07-15 16:05:22 +01001556 }
Serhiy Storchaka4d9aec02018-01-16 18:34:21 +02001557 goto cleanup;
1558 }
Victor Stinnerb57f1082011-05-26 00:19:38 +02001559
1560 chunks = PyList_New(0);
Richard Oudkerk9ad51ec2013-07-15 16:05:22 +01001561 if (chunks == NULL)
1562 goto cleanup;
Victor Stinnerb57f1082011-05-26 00:19:38 +02001563
Antoine Pitrou711af3a2009-04-11 15:39:24 +00001564 while (1) {
1565 if (data) {
Richard Oudkerk9ad51ec2013-07-15 16:05:22 +01001566 if (PyList_Append(chunks, data) < 0)
1567 goto cleanup;
1568 Py_CLEAR(data);
Antoine Pitrou711af3a2009-04-11 15:39:24 +00001569 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001570
Antoine Pitrou711af3a2009-04-11 15:39:24 +00001571 /* Read until EOF or until read() would block. */
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001572 data = _PyObject_CallMethodNoArgs(self->raw, _PyIO_str_read);
Richard Oudkerk9ad51ec2013-07-15 16:05:22 +01001573 if (data == NULL)
1574 goto cleanup;
Antoine Pitrou711af3a2009-04-11 15:39:24 +00001575 if (data != Py_None && !PyBytes_Check(data)) {
Antoine Pitrou711af3a2009-04-11 15:39:24 +00001576 PyErr_SetString(PyExc_TypeError, "read() should return bytes");
Richard Oudkerk9ad51ec2013-07-15 16:05:22 +01001577 goto cleanup;
Antoine Pitrou711af3a2009-04-11 15:39:24 +00001578 }
1579 if (data == Py_None || PyBytes_GET_SIZE(data) == 0) {
1580 if (current_size == 0) {
Richard Oudkerk9ad51ec2013-07-15 16:05:22 +01001581 res = data;
1582 goto cleanup;
Antoine Pitrou711af3a2009-04-11 15:39:24 +00001583 }
1584 else {
Richard Oudkerk9ad51ec2013-07-15 16:05:22 +01001585 tmp = _PyBytes_Join(_PyIO_empty_bytes, chunks);
1586 res = tmp;
1587 goto cleanup;
Antoine Pitrou711af3a2009-04-11 15:39:24 +00001588 }
1589 }
1590 current_size += PyBytes_GET_SIZE(data);
1591 if (self->abs_pos != -1)
1592 self->abs_pos += PyBytes_GET_SIZE(data);
1593 }
Richard Oudkerk9ad51ec2013-07-15 16:05:22 +01001594cleanup:
1595 /* res is either NULL or a borrowed ref */
1596 Py_XINCREF(res);
1597 Py_XDECREF(data);
1598 Py_XDECREF(tmp);
1599 Py_XDECREF(chunks);
1600 return res;
Antoine Pitrou711af3a2009-04-11 15:39:24 +00001601}
1602
1603/* Read n bytes from the buffer if it can, otherwise return None.
1604 This function is simple enough that it can run unlocked. */
1605static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001606_bufferedreader_read_fast(buffered *self, Py_ssize_t n)
Antoine Pitrou711af3a2009-04-11 15:39:24 +00001607{
1608 Py_ssize_t current_size;
1609
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001610 current_size = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
1611 if (n <= current_size) {
1612 /* Fast path: the data to read is fully buffered. */
Antoine Pitrou711af3a2009-04-11 15:39:24 +00001613 PyObject *res = PyBytes_FromStringAndSize(self->buffer + self->pos, n);
1614 if (res != NULL)
1615 self->pos += n;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001616 return res;
1617 }
Antoine Pitrou711af3a2009-04-11 15:39:24 +00001618 Py_RETURN_NONE;
1619}
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001620
Antoine Pitrou711af3a2009-04-11 15:39:24 +00001621/* Generic read function: read from the stream until enough bytes are read,
1622 * or until an EOF occurs or until read() would block.
1623 */
1624static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001625_bufferedreader_read_generic(buffered *self, Py_ssize_t n)
Antoine Pitrou711af3a2009-04-11 15:39:24 +00001626{
1627 PyObject *res = NULL;
1628 Py_ssize_t current_size, remaining, written;
1629 char *out;
1630
1631 current_size = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
1632 if (n <= current_size)
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001633 return _bufferedreader_read_fast(self, n);
Antoine Pitrou711af3a2009-04-11 15:39:24 +00001634
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001635 res = PyBytes_FromStringAndSize(NULL, n);
1636 if (res == NULL)
1637 goto error;
1638 out = PyBytes_AS_STRING(res);
1639 remaining = n;
1640 written = 0;
1641 if (current_size > 0) {
1642 memcpy(out, self->buffer + self->pos, current_size);
1643 remaining -= current_size;
1644 written += current_size;
Antoine Pitroue05565e2011-08-20 14:39:23 +02001645 self->pos += current_size;
1646 }
1647 /* Flush the write buffer if necessary */
1648 if (self->writable) {
1649 PyObject *r = buffered_flush_and_rewind_unlocked(self);
1650 if (r == NULL)
1651 goto error;
1652 Py_DECREF(r);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001653 }
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001654 _bufferedreader_reset_buf(self);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001655 while (remaining > 0) {
1656 /* We want to read a whole block at the end into buffer.
1657 If we had readv() we could do this in one pass. */
1658 Py_ssize_t r = MINUS_LAST_BLOCK(self, remaining);
1659 if (r == 0)
1660 break;
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001661 r = _bufferedreader_raw_read(self, out + written, r);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001662 if (r == -1)
1663 goto error;
1664 if (r == 0 || r == -2) {
1665 /* EOF occurred or read() would block. */
1666 if (r == 0 || written > 0) {
1667 if (_PyBytes_Resize(&res, written))
1668 goto error;
1669 return res;
1670 }
1671 Py_DECREF(res);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001672 Py_RETURN_NONE;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001673 }
1674 remaining -= r;
1675 written += r;
1676 }
1677 assert(remaining <= self->buffer_size);
1678 self->pos = 0;
1679 self->raw_pos = 0;
1680 self->read_end = 0;
Antoine Pitrou32cfede2010-08-11 13:31:33 +00001681 /* NOTE: when the read is satisfied, we avoid issuing any additional
1682 reads, which could block indefinitely (e.g. on a socket).
1683 See issue #9550. */
1684 while (remaining > 0 && self->read_end < self->buffer_size) {
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001685 Py_ssize_t r = _bufferedreader_fill_buffer(self);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001686 if (r == -1)
1687 goto error;
1688 if (r == 0 || r == -2) {
1689 /* EOF occurred or read() would block. */
1690 if (r == 0 || written > 0) {
1691 if (_PyBytes_Resize(&res, written))
1692 goto error;
1693 return res;
1694 }
1695 Py_DECREF(res);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001696 Py_RETURN_NONE;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001697 }
1698 if (remaining > r) {
1699 memcpy(out + written, self->buffer + self->pos, r);
1700 written += r;
1701 self->pos += r;
1702 remaining -= r;
1703 }
1704 else if (remaining > 0) {
1705 memcpy(out + written, self->buffer + self->pos, remaining);
1706 written += remaining;
1707 self->pos += remaining;
1708 remaining = 0;
1709 }
1710 if (remaining == 0)
1711 break;
1712 }
1713
1714 return res;
1715
1716error:
1717 Py_XDECREF(res);
1718 return NULL;
1719}
1720
1721static PyObject *
Victor Stinnerbc93a112011-06-01 00:01:24 +02001722_bufferedreader_peek_unlocked(buffered *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001723{
1724 Py_ssize_t have, r;
1725
1726 have = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
1727 /* Constraints:
1728 1. we don't want to advance the file position.
1729 2. we don't want to lose block alignment, so we can't shift the buffer
1730 to make some place.
1731 Therefore, we either return `have` bytes (if > 0), or a full buffer.
1732 */
1733 if (have > 0) {
1734 return PyBytes_FromStringAndSize(self->buffer + self->pos, have);
1735 }
1736
1737 /* Fill the buffer from the raw stream, and copy it to the result. */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001738 _bufferedreader_reset_buf(self);
1739 r = _bufferedreader_fill_buffer(self);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001740 if (r == -1)
1741 return NULL;
1742 if (r == -2)
1743 r = 0;
1744 self->pos = 0;
1745 return PyBytes_FromStringAndSize(self->buffer, r);
1746}
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001747
1748
Benjamin Peterson59406a92009-03-26 17:10:29 +00001749
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001750/*
1751 * class BufferedWriter
1752 */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001753static void
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001754_bufferedwriter_reset_buf(buffered *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001755{
1756 self->write_pos = 0;
1757 self->write_end = -1;
1758}
1759
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001760/*[clinic input]
1761_io.BufferedWriter.__init__
1762 raw: object
1763 buffer_size: Py_ssize_t(c_default="DEFAULT_BUFFER_SIZE") = DEFAULT_BUFFER_SIZE
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001764
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001765A buffer for a writeable sequential RawIO object.
1766
1767The constructor creates a BufferedWriter for the given writeable raw
1768stream. If the buffer_size is not given, it defaults to
1769DEFAULT_BUFFER_SIZE.
1770[clinic start generated code]*/
1771
1772static int
1773_io_BufferedWriter___init___impl(buffered *self, PyObject *raw,
1774 Py_ssize_t buffer_size)
1775/*[clinic end generated code: output=c8942a020c0dee64 input=914be9b95e16007b]*/
1776{
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001777 self->ok = 0;
Benjamin Petersond2e0c792009-05-01 20:40:59 +00001778 self->detached = 0;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001779
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001780 if (_PyIOBase_check_writable(raw, Py_True) == NULL)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001781 return -1;
1782
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001783 Py_INCREF(raw);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001784 Py_XSETREF(self->raw, raw);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001785 self->readable = 0;
1786 self->writable = 1;
1787
1788 self->buffer_size = buffer_size;
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001789 if (_buffered_init(self) < 0)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001790 return -1;
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001791 _bufferedwriter_reset_buf(self);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001792 self->pos = 0;
1793
Antoine Pitrou711af3a2009-04-11 15:39:24 +00001794 self->fast_closed_checks = (Py_TYPE(self) == &PyBufferedWriter_Type &&
1795 Py_TYPE(raw) == &PyFileIO_Type);
1796
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001797 self->ok = 1;
1798 return 0;
1799}
1800
1801static Py_ssize_t
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001802_bufferedwriter_raw_write(buffered *self, char *start, Py_ssize_t len)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001803{
1804 Py_buffer buf;
1805 PyObject *memobj, *res;
1806 Py_ssize_t n;
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01001807 int errnum;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001808 /* NOTE: the buffer needn't be released as its object is NULL. */
1809 if (PyBuffer_FillInfo(&buf, NULL, start, len, 1, PyBUF_CONTIG_RO) == -1)
1810 return -1;
1811 memobj = PyMemoryView_FromBuffer(&buf);
1812 if (memobj == NULL)
1813 return -1;
Antoine Pitrou707ce822011-02-25 21:24:11 +00001814 /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() when EINTR
1815 occurs so we needn't do it ourselves.
1816 We then retry writing, ignoring the signal if no handler has
1817 raised (see issue #10956).
1818 */
1819 do {
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01001820 errno = 0;
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001821 res = _PyObject_CallMethodOneArg(self->raw, _PyIO_str_write, memobj);
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01001822 errnum = errno;
Gregory P. Smith51359922012-06-23 23:55:39 -07001823 } while (res == NULL && _PyIO_trap_eintr());
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001824 Py_DECREF(memobj);
1825 if (res == NULL)
1826 return -1;
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01001827 if (res == Py_None) {
1828 /* Non-blocking stream would have blocked. Special return code!
1829 Being paranoid we reset errno in case it is changed by code
1830 triggered by a decref. errno is used by _set_BlockingIOError(). */
1831 Py_DECREF(res);
1832 errno = errnum;
1833 return -2;
1834 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001835 n = PyNumber_AsSsize_t(res, PyExc_ValueError);
1836 Py_DECREF(res);
1837 if (n < 0 || n > len) {
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001838 PyErr_Format(PyExc_OSError,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001839 "raw write() returned invalid length %zd "
1840 "(should have been between 0 and %zd)", n, len);
1841 return -1;
1842 }
1843 if (n > 0 && self->abs_pos != -1)
1844 self->abs_pos += n;
1845 return n;
1846}
1847
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001848static PyObject *
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01001849_bufferedwriter_flush_unlocked(buffered *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001850{
1851 Py_ssize_t written = 0;
1852 Py_off_t n, rewind;
1853
1854 if (!VALID_WRITE_BUFFER(self) || self->write_pos == self->write_end)
1855 goto end;
1856 /* First, rewind */
1857 rewind = RAW_OFFSET(self) + (self->pos - self->write_pos);
1858 if (rewind != 0) {
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001859 n = _buffered_raw_seek(self, -rewind, 1);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001860 if (n < 0) {
1861 goto error;
1862 }
1863 self->raw_pos -= rewind;
1864 }
1865 while (self->write_pos < self->write_end) {
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001866 n = _bufferedwriter_raw_write(self,
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001867 self->buffer + self->write_pos,
1868 Py_SAFE_DOWNCAST(self->write_end - self->write_pos,
1869 Py_off_t, Py_ssize_t));
1870 if (n == -1) {
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01001871 goto error;
1872 }
1873 else if (n == -2) {
1874 _set_BlockingIOError("write could not complete without blocking",
1875 0);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001876 goto error;
1877 }
1878 self->write_pos += n;
1879 self->raw_pos = self->write_pos;
1880 written += Py_SAFE_DOWNCAST(n, Py_off_t, Py_ssize_t);
Antoine Pitroub46b9d52010-08-21 19:09:32 +00001881 /* Partial writes can return successfully when interrupted by a
1882 signal (see write(2)). We must run signal handlers before
1883 blocking another time, possibly indefinitely. */
1884 if (PyErr_CheckSignals() < 0)
1885 goto error;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001886 }
1887
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001888
1889end:
Nitish Chandra059f58c2018-01-28 21:30:09 +05301890 /* This ensures that after return from this function,
1891 VALID_WRITE_BUFFER(self) returns false.
1892
1893 This is a required condition because when a tell() is called
1894 after flushing and if VALID_READ_BUFFER(self) is false, we need
1895 VALID_WRITE_BUFFER(self) to be false to have
1896 RAW_OFFSET(self) == 0.
1897
1898 Issue: https://bugs.python.org/issue32228 */
1899 _bufferedwriter_reset_buf(self);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001900 Py_RETURN_NONE;
1901
1902error:
1903 return NULL;
1904}
1905
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001906/*[clinic input]
1907_io.BufferedWriter.write
1908 buffer: Py_buffer
1909 /
1910[clinic start generated code]*/
1911
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001912static PyObject *
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001913_io_BufferedWriter_write_impl(buffered *self, Py_buffer *buffer)
1914/*[clinic end generated code: output=7f8d1365759bfc6b input=dd87dd85fc7f8850]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001915{
1916 PyObject *res = NULL;
Amaury Forgeot d'Arce1b60d42009-10-05 21:09:00 +00001917 Py_ssize_t written, avail, remaining;
1918 Py_off_t offset;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001919
1920 CHECK_INITIALIZED(self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001921
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001922 if (!ENTER_BUFFERED(self))
Antoine Pitrouf3b68b32010-12-03 18:41:39 +00001923 return NULL;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001924
benfogle9703f092017-11-10 16:03:40 -05001925 /* Issue #31976: Check for closed file after acquiring the lock. Another
1926 thread could be holding the lock while closing the file. */
1927 if (IS_CLOSED(self)) {
1928 PyErr_SetString(PyExc_ValueError, "write to closed file");
1929 goto error;
1930 }
1931
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001932 /* Fast path: the data to write can be fully buffered. */
1933 if (!VALID_READ_BUFFER(self) && !VALID_WRITE_BUFFER(self)) {
1934 self->pos = 0;
1935 self->raw_pos = 0;
1936 }
1937 avail = Py_SAFE_DOWNCAST(self->buffer_size - self->pos, Py_off_t, Py_ssize_t);
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001938 if (buffer->len <= avail) {
1939 memcpy(self->buffer + self->pos, buffer->buf, buffer->len);
Antoine Pitrou7c404892011-05-13 00:13:33 +02001940 if (!VALID_WRITE_BUFFER(self) || self->write_pos > self->pos) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001941 self->write_pos = self->pos;
1942 }
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001943 ADJUST_POSITION(self, self->pos + buffer->len);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001944 if (self->pos > self->write_end)
1945 self->write_end = self->pos;
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001946 written = buffer->len;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001947 goto end;
1948 }
1949
1950 /* First write the current buffer */
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01001951 res = _bufferedwriter_flush_unlocked(self);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001952 if (res == NULL) {
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001953 Py_ssize_t *w = _buffered_check_blocking_error();
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001954 if (w == NULL)
1955 goto error;
1956 if (self->readable)
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00001957 _bufferedreader_reset_buf(self);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001958 /* Make some place by shifting the buffer. */
1959 assert(VALID_WRITE_BUFFER(self));
1960 memmove(self->buffer, self->buffer + self->write_pos,
1961 Py_SAFE_DOWNCAST(self->write_end - self->write_pos,
1962 Py_off_t, Py_ssize_t));
1963 self->write_end -= self->write_pos;
1964 self->raw_pos -= self->write_pos;
1965 self->pos -= self->write_pos;
1966 self->write_pos = 0;
1967 avail = Py_SAFE_DOWNCAST(self->buffer_size - self->write_end,
1968 Py_off_t, Py_ssize_t);
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001969 if (buffer->len <= avail) {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001970 /* Everything can be buffered */
1971 PyErr_Clear();
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001972 memcpy(self->buffer + self->write_end, buffer->buf, buffer->len);
1973 self->write_end += buffer->len;
1974 self->pos += buffer->len;
1975 written = buffer->len;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001976 goto end;
1977 }
1978 /* Buffer as much as possible. */
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03001979 memcpy(self->buffer + self->write_end, buffer->buf, avail);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001980 self->write_end += avail;
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01001981 self->pos += avail;
1982 /* XXX Modifying the existing exception e using the pointer w
1983 will change e.characters_written but not e.args[2].
1984 Therefore we just replace with a new error. */
1985 _set_BlockingIOError("write could not complete without blocking",
1986 avail);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00001987 goto error;
1988 }
1989 Py_CLEAR(res);
1990
Antoine Pitroua0ceb732009-08-06 20:29:56 +00001991 /* Adjust the raw stream position if it is away from the logical stream
1992 position. This happens if the read buffer has been filled but not
1993 modified (and therefore _bufferedwriter_flush_unlocked() didn't rewind
1994 the raw stream by itself).
1995 Fixes issue #6629.
1996 */
Amaury Forgeot d'Arce1b60d42009-10-05 21:09:00 +00001997 offset = RAW_OFFSET(self);
1998 if (offset != 0) {
1999 if (_buffered_raw_seek(self, -offset, 1) < 0)
Antoine Pitroua0ceb732009-08-06 20:29:56 +00002000 goto error;
Amaury Forgeot d'Arce1b60d42009-10-05 21:09:00 +00002001 self->raw_pos -= offset;
Antoine Pitroua0ceb732009-08-06 20:29:56 +00002002 }
2003
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002004 /* Then write buf itself. At this point the buffer has been emptied. */
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03002005 remaining = buffer->len;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002006 written = 0;
2007 while (remaining > self->buffer_size) {
Amaury Forgeot d'Arce1b60d42009-10-05 21:09:00 +00002008 Py_ssize_t n = _bufferedwriter_raw_write(
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03002009 self, (char *) buffer->buf + written, buffer->len - written);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002010 if (n == -1) {
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01002011 goto error;
2012 } else if (n == -2) {
2013 /* Write failed because raw file is non-blocking */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002014 if (remaining > self->buffer_size) {
2015 /* Can't buffer everything, still buffer as much as possible */
2016 memcpy(self->buffer,
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03002017 (char *) buffer->buf + written, self->buffer_size);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002018 self->raw_pos = 0;
2019 ADJUST_POSITION(self, self->buffer_size);
2020 self->write_end = self->buffer_size;
Antoine Pitrou58fcf9f2011-11-21 20:16:44 +01002021 written += self->buffer_size;
2022 _set_BlockingIOError("write could not complete without "
2023 "blocking", written);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002024 goto error;
2025 }
2026 PyErr_Clear();
2027 break;
2028 }
2029 written += n;
2030 remaining -= n;
Antoine Pitroub46b9d52010-08-21 19:09:32 +00002031 /* Partial writes can return successfully when interrupted by a
2032 signal (see write(2)). We must run signal handlers before
2033 blocking another time, possibly indefinitely. */
2034 if (PyErr_CheckSignals() < 0)
2035 goto error;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002036 }
2037 if (self->readable)
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002038 _bufferedreader_reset_buf(self);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002039 if (remaining > 0) {
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03002040 memcpy(self->buffer, (char *) buffer->buf + written, remaining);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002041 written += remaining;
2042 }
2043 self->write_pos = 0;
2044 /* TODO: sanity check (remaining >= 0) */
2045 self->write_end = remaining;
2046 ADJUST_POSITION(self, remaining);
2047 self->raw_pos = 0;
2048
2049end:
2050 res = PyLong_FromSsize_t(written);
2051
2052error:
2053 LEAVE_BUFFERED(self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002054 return res;
2055}
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002056
2057
2058
2059/*
2060 * BufferedRWPair
2061 */
2062
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002063/* XXX The usefulness of this (compared to having two separate IO objects) is
2064 * questionable.
2065 */
2066
2067typedef struct {
2068 PyObject_HEAD
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002069 buffered *reader;
2070 buffered *writer;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002071 PyObject *dict;
2072 PyObject *weakreflist;
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002073} rwpair;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002074
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03002075/*[clinic input]
2076_io.BufferedRWPair.__init__
2077 reader: object
2078 writer: object
2079 buffer_size: Py_ssize_t(c_default="DEFAULT_BUFFER_SIZE") = DEFAULT_BUFFER_SIZE
2080 /
2081
2082A buffered reader and writer object together.
2083
2084A buffered reader object and buffered writer object put together to
2085form a sequential IO object that can read and write. This is typically
2086used with a socket or two-way pipe.
2087
2088reader and writer are RawIOBase objects that are readable and
2089writeable respectively. If the buffer_size is omitted it defaults to
2090DEFAULT_BUFFER_SIZE.
2091[clinic start generated code]*/
2092
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002093static int
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03002094_io_BufferedRWPair___init___impl(rwpair *self, PyObject *reader,
2095 PyObject *writer, Py_ssize_t buffer_size)
2096/*[clinic end generated code: output=327e73d1aee8f984 input=620d42d71f33a031]*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002097{
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002098 if (_PyIOBase_check_readable(reader, Py_True) == NULL)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002099 return -1;
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002100 if (_PyIOBase_check_writable(writer, Py_True) == NULL)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002101 return -1;
2102
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002103 self->reader = (buffered *) PyObject_CallFunction(
Antoine Pitroucf4c7492009-04-19 00:09:36 +00002104 (PyObject *) &PyBufferedReader_Type, "On", reader, buffer_size);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002105 if (self->reader == NULL)
2106 return -1;
2107
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002108 self->writer = (buffered *) PyObject_CallFunction(
Antoine Pitroucf4c7492009-04-19 00:09:36 +00002109 (PyObject *) &PyBufferedWriter_Type, "On", writer, buffer_size);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002110 if (self->writer == NULL) {
2111 Py_CLEAR(self->reader);
2112 return -1;
2113 }
Antoine Pitroucf4c7492009-04-19 00:09:36 +00002114
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002115 return 0;
2116}
2117
2118static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002119bufferedrwpair_traverse(rwpair *self, visitproc visit, void *arg)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002120{
2121 Py_VISIT(self->dict);
2122 return 0;
2123}
2124
2125static int
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002126bufferedrwpair_clear(rwpair *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002127{
2128 Py_CLEAR(self->reader);
2129 Py_CLEAR(self->writer);
2130 Py_CLEAR(self->dict);
2131 return 0;
2132}
2133
2134static void
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002135bufferedrwpair_dealloc(rwpair *self)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002136{
2137 _PyObject_GC_UNTRACK(self);
Benjamin Petersonbbd0a322014-09-29 22:46:57 -04002138 if (self->weakreflist != NULL)
2139 PyObject_ClearWeakRefs((PyObject *)self);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002140 Py_CLEAR(self->reader);
2141 Py_CLEAR(self->writer);
2142 Py_CLEAR(self->dict);
2143 Py_TYPE(self)->tp_free((PyObject *) self);
2144}
2145
2146static PyObject *
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002147_forward_call(buffered *self, _Py_Identifier *name, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002148{
Serhiy Storchaka61e24932014-02-12 10:52:35 +02002149 PyObject *func, *ret;
2150 if (self == NULL) {
2151 PyErr_SetString(PyExc_ValueError,
2152 "I/O operation on uninitialized object");
2153 return NULL;
2154 }
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002155
Serhiy Storchaka61e24932014-02-12 10:52:35 +02002156 func = _PyObject_GetAttrId((PyObject *)self, name);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002157 if (func == NULL) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002158 PyErr_SetString(PyExc_AttributeError, name->string);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002159 return NULL;
2160 }
2161
2162 ret = PyObject_CallObject(func, args);
2163 Py_DECREF(func);
2164 return ret;
2165}
2166
2167static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002168bufferedrwpair_read(rwpair *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002169{
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002170 return _forward_call(self->reader, &PyId_read, args);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002171}
2172
2173static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002174bufferedrwpair_peek(rwpair *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002175{
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002176 return _forward_call(self->reader, &PyId_peek, args);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002177}
2178
2179static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002180bufferedrwpair_read1(rwpair *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002181{
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002182 return _forward_call(self->reader, &PyId_read1, args);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002183}
2184
2185static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002186bufferedrwpair_readinto(rwpair *self, PyObject *args)
Antoine Pitroucf4c7492009-04-19 00:09:36 +00002187{
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002188 return _forward_call(self->reader, &PyId_readinto, args);
Antoine Pitroucf4c7492009-04-19 00:09:36 +00002189}
2190
2191static PyObject *
Benjamin Petersona96fea02014-06-22 14:17:44 -07002192bufferedrwpair_readinto1(rwpair *self, PyObject *args)
2193{
2194 return _forward_call(self->reader, &PyId_readinto1, args);
2195}
2196
2197static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002198bufferedrwpair_write(rwpair *self, PyObject *args)
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002199{
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002200 return _forward_call(self->writer, &PyId_write, args);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002201}
2202
2203static PyObject *
jdemeyerfc512e32018-08-02 13:14:54 +02002204bufferedrwpair_flush(rwpair *self, PyObject *Py_UNUSED(ignored))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002205{
jdemeyerfc512e32018-08-02 13:14:54 +02002206 return _forward_call(self->writer, &PyId_flush, NULL);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002207}
2208
2209static PyObject *
jdemeyerfc512e32018-08-02 13:14:54 +02002210bufferedrwpair_readable(rwpair *self, PyObject *Py_UNUSED(ignored))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002211{
jdemeyerfc512e32018-08-02 13:14:54 +02002212 return _forward_call(self->reader, &PyId_readable, NULL);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002213}
2214
2215static PyObject *
jdemeyerfc512e32018-08-02 13:14:54 +02002216bufferedrwpair_writable(rwpair *self, PyObject *Py_UNUSED(ignored))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002217{
jdemeyerfc512e32018-08-02 13:14:54 +02002218 return _forward_call(self->writer, &PyId_writable, NULL);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002219}
2220
2221static PyObject *
jdemeyerfc512e32018-08-02 13:14:54 +02002222bufferedrwpair_close(rwpair *self, PyObject *Py_UNUSED(ignored))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002223{
Serhiy Storchaka7665be62015-03-24 23:21:57 +02002224 PyObject *exc = NULL, *val, *tb;
jdemeyerfc512e32018-08-02 13:14:54 +02002225 PyObject *ret = _forward_call(self->writer, &PyId_close, NULL);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002226 if (ret == NULL)
Serhiy Storchaka7665be62015-03-24 23:21:57 +02002227 PyErr_Fetch(&exc, &val, &tb);
2228 else
2229 Py_DECREF(ret);
jdemeyerfc512e32018-08-02 13:14:54 +02002230 ret = _forward_call(self->reader, &PyId_close, NULL);
Serhiy Storchaka7665be62015-03-24 23:21:57 +02002231 if (exc != NULL) {
2232 _PyErr_ChainExceptions(exc, val, tb);
2233 Py_CLEAR(ret);
2234 }
2235 return ret;
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002236}
2237
2238static PyObject *
jdemeyerfc512e32018-08-02 13:14:54 +02002239bufferedrwpair_isatty(rwpair *self, PyObject *Py_UNUSED(ignored))
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002240{
jdemeyerfc512e32018-08-02 13:14:54 +02002241 PyObject *ret = _forward_call(self->writer, &PyId_isatty, NULL);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002242
2243 if (ret != Py_False) {
2244 /* either True or exception */
2245 return ret;
2246 }
2247 Py_DECREF(ret);
2248
jdemeyerfc512e32018-08-02 13:14:54 +02002249 return _forward_call(self->reader, &PyId_isatty, NULL);
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002250}
2251
Antoine Pitroucf4c7492009-04-19 00:09:36 +00002252static PyObject *
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002253bufferedrwpair_closed_get(rwpair *self, void *context)
Antoine Pitroucf4c7492009-04-19 00:09:36 +00002254{
Charles-François Natali42c28cd2011-10-05 19:53:43 +02002255 if (self->writer == NULL) {
2256 PyErr_SetString(PyExc_RuntimeError,
2257 "the BufferedRWPair object is being garbage-collected");
2258 return NULL;
2259 }
Antoine Pitroucf4c7492009-04-19 00:09:36 +00002260 return PyObject_GetAttr((PyObject *) self->writer, _PyIO_str_closed);
2261}
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03002262
2263
2264
2265/*
2266 * BufferedRandom
2267 */
2268
2269/*[clinic input]
2270_io.BufferedRandom.__init__
2271 raw: object
2272 buffer_size: Py_ssize_t(c_default="DEFAULT_BUFFER_SIZE") = DEFAULT_BUFFER_SIZE
2273
2274A buffered interface to random access streams.
2275
2276The constructor creates a reader and writer for a seekable stream,
2277raw, given in the first argument. If the buffer_size is omitted it
2278defaults to DEFAULT_BUFFER_SIZE.
2279[clinic start generated code]*/
2280
2281static int
2282_io_BufferedRandom___init___impl(buffered *self, PyObject *raw,
2283 Py_ssize_t buffer_size)
2284/*[clinic end generated code: output=d3d64eb0f64e64a3 input=a4e818fb86d0e50c]*/
2285{
2286 self->ok = 0;
2287 self->detached = 0;
2288
2289 if (_PyIOBase_check_seekable(raw, Py_True) == NULL)
2290 return -1;
2291 if (_PyIOBase_check_readable(raw, Py_True) == NULL)
2292 return -1;
2293 if (_PyIOBase_check_writable(raw, Py_True) == NULL)
2294 return -1;
2295
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03002296 Py_INCREF(raw);
Serhiy Storchaka48842712016-04-06 09:45:48 +03002297 Py_XSETREF(self->raw, raw);
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03002298 self->buffer_size = buffer_size;
2299 self->readable = 1;
2300 self->writable = 1;
2301
2302 if (_buffered_init(self) < 0)
2303 return -1;
2304 _bufferedreader_reset_buf(self);
2305 _bufferedwriter_reset_buf(self);
2306 self->pos = 0;
2307
2308 self->fast_closed_checks = (Py_TYPE(self) == &PyBufferedRandom_Type &&
2309 Py_TYPE(raw) == &PyFileIO_Type);
2310
2311 self->ok = 1;
2312 return 0;
2313}
2314
2315#include "clinic/bufferedio.c.h"
2316
2317
2318static PyMethodDef bufferediobase_methods[] = {
2319 _IO__BUFFEREDIOBASE_DETACH_METHODDEF
2320 {"read", bufferediobase_read, METH_VARARGS, bufferediobase_read_doc},
2321 {"read1", bufferediobase_read1, METH_VARARGS, bufferediobase_read1_doc},
2322 _IO__BUFFEREDIOBASE_READINTO_METHODDEF
2323 _IO__BUFFEREDIOBASE_READINTO1_METHODDEF
2324 {"write", bufferediobase_write, METH_VARARGS, bufferediobase_write_doc},
2325 {NULL, NULL}
2326};
2327
2328PyTypeObject PyBufferedIOBase_Type = {
2329 PyVarObject_HEAD_INIT(NULL, 0)
2330 "_io._BufferedIOBase", /*tp_name*/
2331 0, /*tp_basicsize*/
2332 0, /*tp_itemsize*/
2333 0, /*tp_dealloc*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002334 0, /*tp_vectorcall_offset*/
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03002335 0, /*tp_getattr*/
2336 0, /*tp_setattr*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002337 0, /*tp_as_async*/
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03002338 0, /*tp_repr*/
2339 0, /*tp_as_number*/
2340 0, /*tp_as_sequence*/
2341 0, /*tp_as_mapping*/
2342 0, /*tp_hash */
2343 0, /*tp_call*/
2344 0, /*tp_str*/
2345 0, /*tp_getattro*/
2346 0, /*tp_setattro*/
2347 0, /*tp_as_buffer*/
Antoine Pitrouada319b2019-05-29 22:12:38 +02002348 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03002349 bufferediobase_doc, /* tp_doc */
2350 0, /* tp_traverse */
2351 0, /* tp_clear */
2352 0, /* tp_richcompare */
2353 0, /* tp_weaklistoffset */
2354 0, /* tp_iter */
2355 0, /* tp_iternext */
2356 bufferediobase_methods, /* tp_methods */
2357 0, /* tp_members */
2358 0, /* tp_getset */
2359 &PyIOBase_Type, /* tp_base */
2360 0, /* tp_dict */
2361 0, /* tp_descr_get */
2362 0, /* tp_descr_set */
2363 0, /* tp_dictoffset */
2364 0, /* tp_init */
2365 0, /* tp_alloc */
2366 0, /* tp_new */
2367 0, /* tp_free */
2368 0, /* tp_is_gc */
2369 0, /* tp_bases */
2370 0, /* tp_mro */
2371 0, /* tp_cache */
2372 0, /* tp_subclasses */
2373 0, /* tp_weaklist */
2374 0, /* tp_del */
2375 0, /* tp_version_tag */
2376 0, /* tp_finalize */
2377};
2378
2379
2380static PyMethodDef bufferedreader_methods[] = {
2381 /* BufferedIOMixin methods */
2382 {"detach", (PyCFunction)buffered_detach, METH_NOARGS},
2383 {"flush", (PyCFunction)buffered_simple_flush, METH_NOARGS},
2384 {"close", (PyCFunction)buffered_close, METH_NOARGS},
2385 {"seekable", (PyCFunction)buffered_seekable, METH_NOARGS},
2386 {"readable", (PyCFunction)buffered_readable, METH_NOARGS},
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03002387 {"fileno", (PyCFunction)buffered_fileno, METH_NOARGS},
2388 {"isatty", (PyCFunction)buffered_isatty, METH_NOARGS},
2389 {"_dealloc_warn", (PyCFunction)buffered_dealloc_warn, METH_O},
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03002390
2391 _IO__BUFFERED_READ_METHODDEF
2392 _IO__BUFFERED_PEEK_METHODDEF
2393 _IO__BUFFERED_READ1_METHODDEF
2394 _IO__BUFFERED_READINTO_METHODDEF
2395 _IO__BUFFERED_READINTO1_METHODDEF
2396 _IO__BUFFERED_READLINE_METHODDEF
2397 _IO__BUFFERED_SEEK_METHODDEF
2398 {"tell", (PyCFunction)buffered_tell, METH_NOARGS},
2399 _IO__BUFFERED_TRUNCATE_METHODDEF
2400 {"__sizeof__", (PyCFunction)buffered_sizeof, METH_NOARGS},
2401 {NULL, NULL}
2402};
2403
2404static PyMemberDef bufferedreader_members[] = {
2405 {"raw", T_OBJECT, offsetof(buffered, raw), READONLY},
2406 {"_finalizing", T_BOOL, offsetof(buffered, finalizing), 0},
2407 {NULL}
2408};
2409
2410static PyGetSetDef bufferedreader_getset[] = {
2411 {"closed", (getter)buffered_closed_get, NULL, NULL},
2412 {"name", (getter)buffered_name_get, NULL, NULL},
2413 {"mode", (getter)buffered_mode_get, NULL, NULL},
2414 {NULL}
2415};
2416
2417
2418PyTypeObject PyBufferedReader_Type = {
2419 PyVarObject_HEAD_INIT(NULL, 0)
2420 "_io.BufferedReader", /*tp_name*/
2421 sizeof(buffered), /*tp_basicsize*/
2422 0, /*tp_itemsize*/
2423 (destructor)buffered_dealloc, /*tp_dealloc*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002424 0, /*tp_vectorcall_offset*/
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03002425 0, /*tp_getattr*/
2426 0, /*tp_setattr*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002427 0, /*tp_as_async*/
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03002428 (reprfunc)buffered_repr, /*tp_repr*/
2429 0, /*tp_as_number*/
2430 0, /*tp_as_sequence*/
2431 0, /*tp_as_mapping*/
2432 0, /*tp_hash */
2433 0, /*tp_call*/
2434 0, /*tp_str*/
2435 0, /*tp_getattro*/
2436 0, /*tp_setattro*/
2437 0, /*tp_as_buffer*/
2438 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
Antoine Pitrouada319b2019-05-29 22:12:38 +02002439 | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03002440 _io_BufferedReader___init____doc__, /* tp_doc */
2441 (traverseproc)buffered_traverse, /* tp_traverse */
2442 (inquiry)buffered_clear, /* tp_clear */
2443 0, /* tp_richcompare */
2444 offsetof(buffered, weakreflist), /*tp_weaklistoffset*/
2445 0, /* tp_iter */
2446 (iternextfunc)buffered_iternext, /* tp_iternext */
2447 bufferedreader_methods, /* tp_methods */
2448 bufferedreader_members, /* tp_members */
2449 bufferedreader_getset, /* tp_getset */
2450 0, /* tp_base */
2451 0, /* tp_dict */
2452 0, /* tp_descr_get */
2453 0, /* tp_descr_set */
2454 offsetof(buffered, dict), /* tp_dictoffset */
2455 _io_BufferedReader___init__, /* tp_init */
2456 0, /* tp_alloc */
2457 PyType_GenericNew, /* tp_new */
2458 0, /* tp_free */
2459 0, /* tp_is_gc */
2460 0, /* tp_bases */
2461 0, /* tp_mro */
2462 0, /* tp_cache */
2463 0, /* tp_subclasses */
2464 0, /* tp_weaklist */
2465 0, /* tp_del */
2466 0, /* tp_version_tag */
2467 0, /* tp_finalize */
2468};
2469
2470
2471static PyMethodDef bufferedwriter_methods[] = {
2472 /* BufferedIOMixin methods */
2473 {"close", (PyCFunction)buffered_close, METH_NOARGS},
2474 {"detach", (PyCFunction)buffered_detach, METH_NOARGS},
2475 {"seekable", (PyCFunction)buffered_seekable, METH_NOARGS},
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03002476 {"writable", (PyCFunction)buffered_writable, METH_NOARGS},
2477 {"fileno", (PyCFunction)buffered_fileno, METH_NOARGS},
2478 {"isatty", (PyCFunction)buffered_isatty, METH_NOARGS},
2479 {"_dealloc_warn", (PyCFunction)buffered_dealloc_warn, METH_O},
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03002480
2481 _IO_BUFFEREDWRITER_WRITE_METHODDEF
2482 _IO__BUFFERED_TRUNCATE_METHODDEF
2483 {"flush", (PyCFunction)buffered_flush, METH_NOARGS},
2484 _IO__BUFFERED_SEEK_METHODDEF
2485 {"tell", (PyCFunction)buffered_tell, METH_NOARGS},
2486 {"__sizeof__", (PyCFunction)buffered_sizeof, METH_NOARGS},
2487 {NULL, NULL}
2488};
2489
2490static PyMemberDef bufferedwriter_members[] = {
2491 {"raw", T_OBJECT, offsetof(buffered, raw), READONLY},
2492 {"_finalizing", T_BOOL, offsetof(buffered, finalizing), 0},
2493 {NULL}
2494};
2495
2496static PyGetSetDef bufferedwriter_getset[] = {
2497 {"closed", (getter)buffered_closed_get, NULL, NULL},
2498 {"name", (getter)buffered_name_get, NULL, NULL},
2499 {"mode", (getter)buffered_mode_get, NULL, NULL},
2500 {NULL}
2501};
2502
2503
2504PyTypeObject PyBufferedWriter_Type = {
2505 PyVarObject_HEAD_INIT(NULL, 0)
2506 "_io.BufferedWriter", /*tp_name*/
2507 sizeof(buffered), /*tp_basicsize*/
2508 0, /*tp_itemsize*/
2509 (destructor)buffered_dealloc, /*tp_dealloc*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002510 0, /*tp_vectorcall_offset*/
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03002511 0, /*tp_getattr*/
2512 0, /*tp_setattr*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002513 0, /*tp_as_async*/
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03002514 (reprfunc)buffered_repr, /*tp_repr*/
2515 0, /*tp_as_number*/
2516 0, /*tp_as_sequence*/
2517 0, /*tp_as_mapping*/
2518 0, /*tp_hash */
2519 0, /*tp_call*/
2520 0, /*tp_str*/
2521 0, /*tp_getattro*/
2522 0, /*tp_setattro*/
2523 0, /*tp_as_buffer*/
2524 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
Antoine Pitrouada319b2019-05-29 22:12:38 +02002525 | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03002526 _io_BufferedWriter___init____doc__, /* tp_doc */
2527 (traverseproc)buffered_traverse, /* tp_traverse */
2528 (inquiry)buffered_clear, /* tp_clear */
2529 0, /* tp_richcompare */
2530 offsetof(buffered, weakreflist), /*tp_weaklistoffset*/
2531 0, /* tp_iter */
2532 0, /* tp_iternext */
2533 bufferedwriter_methods, /* tp_methods */
2534 bufferedwriter_members, /* tp_members */
2535 bufferedwriter_getset, /* tp_getset */
2536 0, /* tp_base */
2537 0, /* tp_dict */
2538 0, /* tp_descr_get */
2539 0, /* tp_descr_set */
2540 offsetof(buffered, dict), /* tp_dictoffset */
2541 _io_BufferedWriter___init__, /* tp_init */
2542 0, /* tp_alloc */
2543 PyType_GenericNew, /* tp_new */
2544 0, /* tp_free */
2545 0, /* tp_is_gc */
2546 0, /* tp_bases */
2547 0, /* tp_mro */
2548 0, /* tp_cache */
2549 0, /* tp_subclasses */
2550 0, /* tp_weaklist */
2551 0, /* tp_del */
2552 0, /* tp_version_tag */
2553 0, /* tp_finalize */
2554};
2555
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002556
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002557static PyMethodDef bufferedrwpair_methods[] = {
2558 {"read", (PyCFunction)bufferedrwpair_read, METH_VARARGS},
2559 {"peek", (PyCFunction)bufferedrwpair_peek, METH_VARARGS},
2560 {"read1", (PyCFunction)bufferedrwpair_read1, METH_VARARGS},
2561 {"readinto", (PyCFunction)bufferedrwpair_readinto, METH_VARARGS},
Benjamin Petersona96fea02014-06-22 14:17:44 -07002562 {"readinto1", (PyCFunction)bufferedrwpair_readinto1, METH_VARARGS},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002563
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002564 {"write", (PyCFunction)bufferedrwpair_write, METH_VARARGS},
2565 {"flush", (PyCFunction)bufferedrwpair_flush, METH_NOARGS},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002566
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002567 {"readable", (PyCFunction)bufferedrwpair_readable, METH_NOARGS},
2568 {"writable", (PyCFunction)bufferedrwpair_writable, METH_NOARGS},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002569
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002570 {"close", (PyCFunction)bufferedrwpair_close, METH_NOARGS},
2571 {"isatty", (PyCFunction)bufferedrwpair_isatty, METH_NOARGS},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002572
2573 {NULL, NULL}
2574};
2575
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002576static PyGetSetDef bufferedrwpair_getset[] = {
2577 {"closed", (getter)bufferedrwpair_closed_get, NULL, NULL},
Benjamin Peterson1fea3212009-04-19 03:15:20 +00002578 {NULL}
Antoine Pitroucf4c7492009-04-19 00:09:36 +00002579};
2580
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002581PyTypeObject PyBufferedRWPair_Type = {
2582 PyVarObject_HEAD_INIT(NULL, 0)
2583 "_io.BufferedRWPair", /*tp_name*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002584 sizeof(rwpair), /*tp_basicsize*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002585 0, /*tp_itemsize*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002586 (destructor)bufferedrwpair_dealloc, /*tp_dealloc*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002587 0, /*tp_vectorcall_offset*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002588 0, /*tp_getattr*/
2589 0, /*tp_setattr*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002590 0, /*tp_as_async*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002591 0, /*tp_repr*/
2592 0, /*tp_as_number*/
2593 0, /*tp_as_sequence*/
2594 0, /*tp_as_mapping*/
2595 0, /*tp_hash */
2596 0, /*tp_call*/
2597 0, /*tp_str*/
2598 0, /*tp_getattro*/
2599 0, /*tp_setattro*/
2600 0, /*tp_as_buffer*/
2601 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
Antoine Pitrouada319b2019-05-29 22:12:38 +02002602 | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03002603 _io_BufferedRWPair___init____doc__, /* tp_doc */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002604 (traverseproc)bufferedrwpair_traverse, /* tp_traverse */
2605 (inquiry)bufferedrwpair_clear, /* tp_clear */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002606 0, /* tp_richcompare */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002607 offsetof(rwpair, weakreflist), /*tp_weaklistoffset*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002608 0, /* tp_iter */
2609 0, /* tp_iternext */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002610 bufferedrwpair_methods, /* tp_methods */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002611 0, /* tp_members */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002612 bufferedrwpair_getset, /* tp_getset */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002613 0, /* tp_base */
2614 0, /* tp_dict */
2615 0, /* tp_descr_get */
2616 0, /* tp_descr_set */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002617 offsetof(rwpair, dict), /* tp_dictoffset */
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03002618 _io_BufferedRWPair___init__, /* tp_init */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002619 0, /* tp_alloc */
2620 PyType_GenericNew, /* tp_new */
Antoine Pitrou796564c2013-07-30 19:59:21 +02002621 0, /* tp_free */
2622 0, /* tp_is_gc */
2623 0, /* tp_bases */
2624 0, /* tp_mro */
2625 0, /* tp_cache */
2626 0, /* tp_subclasses */
2627 0, /* tp_weaklist */
2628 0, /* tp_del */
2629 0, /* tp_version_tag */
2630 0, /* tp_finalize */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002631};
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002632
2633
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002634static PyMethodDef bufferedrandom_methods[] = {
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002635 /* BufferedIOMixin methods */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002636 {"close", (PyCFunction)buffered_close, METH_NOARGS},
2637 {"detach", (PyCFunction)buffered_detach, METH_NOARGS},
2638 {"seekable", (PyCFunction)buffered_seekable, METH_NOARGS},
2639 {"readable", (PyCFunction)buffered_readable, METH_NOARGS},
2640 {"writable", (PyCFunction)buffered_writable, METH_NOARGS},
2641 {"fileno", (PyCFunction)buffered_fileno, METH_NOARGS},
2642 {"isatty", (PyCFunction)buffered_isatty, METH_NOARGS},
Antoine Pitroue033e062010-10-29 10:38:18 +00002643 {"_dealloc_warn", (PyCFunction)buffered_dealloc_warn, METH_O},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002644
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002645 {"flush", (PyCFunction)buffered_flush, METH_NOARGS},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002646
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03002647 _IO__BUFFERED_SEEK_METHODDEF
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002648 {"tell", (PyCFunction)buffered_tell, METH_NOARGS},
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03002649 _IO__BUFFERED_TRUNCATE_METHODDEF
2650 _IO__BUFFERED_READ_METHODDEF
2651 _IO__BUFFERED_READ1_METHODDEF
2652 _IO__BUFFERED_READINTO_METHODDEF
2653 _IO__BUFFERED_READINTO1_METHODDEF
2654 _IO__BUFFERED_READLINE_METHODDEF
2655 _IO__BUFFERED_PEEK_METHODDEF
2656 _IO_BUFFEREDWRITER_WRITE_METHODDEF
Antoine Pitrou10f0c502012-07-29 19:02:46 +02002657 {"__sizeof__", (PyCFunction)buffered_sizeof, METH_NOARGS},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002658 {NULL, NULL}
2659};
2660
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002661static PyMemberDef bufferedrandom_members[] = {
Antoine Pitrou7f8f4182010-12-21 21:20:59 +00002662 {"raw", T_OBJECT, offsetof(buffered, raw), READONLY},
Antoine Pitrou796564c2013-07-30 19:59:21 +02002663 {"_finalizing", T_BOOL, offsetof(buffered, finalizing), 0},
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002664 {NULL}
2665};
2666
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002667static PyGetSetDef bufferedrandom_getset[] = {
2668 {"closed", (getter)buffered_closed_get, NULL, NULL},
2669 {"name", (getter)buffered_name_get, NULL, NULL},
2670 {"mode", (getter)buffered_mode_get, NULL, NULL},
Benjamin Peterson1fea3212009-04-19 03:15:20 +00002671 {NULL}
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002672};
2673
2674
2675PyTypeObject PyBufferedRandom_Type = {
2676 PyVarObject_HEAD_INIT(NULL, 0)
2677 "_io.BufferedRandom", /*tp_name*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002678 sizeof(buffered), /*tp_basicsize*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002679 0, /*tp_itemsize*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002680 (destructor)buffered_dealloc, /*tp_dealloc*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002681 0, /*tp_vectorcall_offset*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002682 0, /*tp_getattr*/
2683 0, /*tp_setattr*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +02002684 0, /*tp_as_async*/
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002685 (reprfunc)buffered_repr, /*tp_repr*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002686 0, /*tp_as_number*/
2687 0, /*tp_as_sequence*/
2688 0, /*tp_as_mapping*/
2689 0, /*tp_hash */
2690 0, /*tp_call*/
2691 0, /*tp_str*/
2692 0, /*tp_getattro*/
2693 0, /*tp_setattro*/
2694 0, /*tp_as_buffer*/
2695 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
Antoine Pitrouada319b2019-05-29 22:12:38 +02002696 | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03002697 _io_BufferedRandom___init____doc__, /* tp_doc */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002698 (traverseproc)buffered_traverse, /* tp_traverse */
2699 (inquiry)buffered_clear, /* tp_clear */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002700 0, /* tp_richcompare */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002701 offsetof(buffered, weakreflist), /*tp_weaklistoffset*/
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002702 0, /* tp_iter */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002703 (iternextfunc)buffered_iternext, /* tp_iternext */
2704 bufferedrandom_methods, /* tp_methods */
2705 bufferedrandom_members, /* tp_members */
2706 bufferedrandom_getset, /* tp_getset */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002707 0, /* tp_base */
2708 0, /*tp_dict*/
2709 0, /* tp_descr_get */
2710 0, /* tp_descr_set */
Benjamin Peterson680bf1a2009-06-12 02:07:12 +00002711 offsetof(buffered, dict), /*tp_dictoffset*/
Serhiy Storchakaf24131f2015-04-16 11:19:43 +03002712 _io_BufferedRandom___init__, /* tp_init */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002713 0, /* tp_alloc */
2714 PyType_GenericNew, /* tp_new */
Antoine Pitrou796564c2013-07-30 19:59:21 +02002715 0, /* tp_free */
2716 0, /* tp_is_gc */
2717 0, /* tp_bases */
2718 0, /* tp_mro */
2719 0, /* tp_cache */
2720 0, /* tp_subclasses */
2721 0, /* tp_weaklist */
2722 0, /* tp_del */
2723 0, /* tp_version_tag */
2724 0, /* tp_finalize */
Benjamin Peterson4fa88fa2009-03-04 00:14:51 +00002725};