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