1 | n/a | /* |
---|
2 | n/a | An implementation of the I/O abstract base classes hierarchy |
---|
3 | n/a | as defined by PEP 3116 - "New I/O" |
---|
4 | n/a | |
---|
5 | n/a | Classes defined here: IOBase, RawIOBase. |
---|
6 | n/a | |
---|
7 | n/a | Written by Amaury Forgeot d'Arc and Antoine Pitrou |
---|
8 | n/a | */ |
---|
9 | n/a | |
---|
10 | n/a | |
---|
11 | n/a | #define PY_SSIZE_T_CLEAN |
---|
12 | n/a | #include "Python.h" |
---|
13 | n/a | #include "structmember.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._IOBase "PyObject *" "&PyIOBase_Type" |
---|
19 | n/a | class _io._RawIOBase "PyObject *" "&PyRawIOBase_Type" |
---|
20 | n/a | [clinic start generated code]*/ |
---|
21 | n/a | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=d29a4d076c2b211c]*/ |
---|
22 | n/a | |
---|
23 | n/a | /*[python input] |
---|
24 | n/a | class io_ssize_t_converter(CConverter): |
---|
25 | n/a | type = 'Py_ssize_t' |
---|
26 | n/a | converter = '_PyIO_ConvertSsize_t' |
---|
27 | n/a | [python start generated code]*/ |
---|
28 | n/a | /*[python end generated code: output=da39a3ee5e6b4b0d input=d0a811d3cbfd1b33]*/ |
---|
29 | n/a | |
---|
30 | n/a | /* |
---|
31 | n/a | * IOBase class, an abstract class |
---|
32 | n/a | */ |
---|
33 | n/a | |
---|
34 | n/a | typedef struct { |
---|
35 | n/a | PyObject_HEAD |
---|
36 | n/a | |
---|
37 | n/a | PyObject *dict; |
---|
38 | n/a | PyObject *weakreflist; |
---|
39 | n/a | } iobase; |
---|
40 | n/a | |
---|
41 | n/a | PyDoc_STRVAR(iobase_doc, |
---|
42 | n/a | "The abstract base class for all I/O classes, acting on streams of\n" |
---|
43 | n/a | "bytes. There is no public constructor.\n" |
---|
44 | n/a | "\n" |
---|
45 | n/a | "This class provides dummy implementations for many methods that\n" |
---|
46 | n/a | "derived classes can override selectively; the default implementations\n" |
---|
47 | n/a | "represent a file that cannot be read, written or seeked.\n" |
---|
48 | n/a | "\n" |
---|
49 | n/a | "Even though IOBase does not declare read, readinto, or write because\n" |
---|
50 | n/a | "their signatures will vary, implementations and clients should\n" |
---|
51 | n/a | "consider those methods part of the interface. Also, implementations\n" |
---|
52 | n/a | "may raise UnsupportedOperation when operations they do not support are\n" |
---|
53 | n/a | "called.\n" |
---|
54 | n/a | "\n" |
---|
55 | n/a | "The basic type used for binary data read from or written to a file is\n" |
---|
56 | n/a | "bytes. Other bytes-like objects are accepted as method arguments too.\n" |
---|
57 | n/a | "In some cases (such as readinto), a writable object is required. Text\n" |
---|
58 | n/a | "I/O classes work with str data.\n" |
---|
59 | n/a | "\n" |
---|
60 | n/a | "Note that calling any method (except additional calls to close(),\n" |
---|
61 | n/a | "which are ignored) on a closed stream should raise a ValueError.\n" |
---|
62 | n/a | "\n" |
---|
63 | n/a | "IOBase (and its subclasses) support the iterator protocol, meaning\n" |
---|
64 | n/a | "that an IOBase object can be iterated over yielding the lines in a\n" |
---|
65 | n/a | "stream.\n" |
---|
66 | n/a | "\n" |
---|
67 | n/a | "IOBase also supports the :keyword:`with` statement. In this example,\n" |
---|
68 | n/a | "fp is closed after the suite of the with statement is complete:\n" |
---|
69 | n/a | "\n" |
---|
70 | n/a | "with open('spam.txt', 'r') as fp:\n" |
---|
71 | n/a | " fp.write('Spam and eggs!')\n"); |
---|
72 | n/a | |
---|
73 | n/a | /* Use this macro whenever you want to check the internal `closed` status |
---|
74 | n/a | of the IOBase object rather than the virtual `closed` attribute as returned |
---|
75 | n/a | by whatever subclass. */ |
---|
76 | n/a | |
---|
77 | n/a | _Py_IDENTIFIER(__IOBase_closed); |
---|
78 | n/a | #define IS_CLOSED(self) \ |
---|
79 | n/a | _PyObject_HasAttrId(self, &PyId___IOBase_closed) |
---|
80 | n/a | |
---|
81 | n/a | _Py_IDENTIFIER(read); |
---|
82 | n/a | |
---|
83 | n/a | /* Internal methods */ |
---|
84 | n/a | static PyObject * |
---|
85 | n/a | iobase_unsupported(const char *message) |
---|
86 | n/a | { |
---|
87 | n/a | _PyIO_State *state = IO_STATE(); |
---|
88 | n/a | if (state != NULL) |
---|
89 | n/a | PyErr_SetString(state->unsupported_operation, message); |
---|
90 | n/a | return NULL; |
---|
91 | n/a | } |
---|
92 | n/a | |
---|
93 | n/a | /* Positioning */ |
---|
94 | n/a | |
---|
95 | n/a | PyDoc_STRVAR(iobase_seek_doc, |
---|
96 | n/a | "Change stream position.\n" |
---|
97 | n/a | "\n" |
---|
98 | n/a | "Change the stream position to the given byte offset. The offset is\n" |
---|
99 | n/a | "interpreted relative to the position indicated by whence. Values\n" |
---|
100 | n/a | "for whence are:\n" |
---|
101 | n/a | "\n" |
---|
102 | n/a | "* 0 -- start of stream (the default); offset should be zero or positive\n" |
---|
103 | n/a | "* 1 -- current stream position; offset may be negative\n" |
---|
104 | n/a | "* 2 -- end of stream; offset is usually negative\n" |
---|
105 | n/a | "\n" |
---|
106 | n/a | "Return the new absolute position."); |
---|
107 | n/a | |
---|
108 | n/a | static PyObject * |
---|
109 | n/a | iobase_seek(PyObject *self, PyObject *args) |
---|
110 | n/a | { |
---|
111 | n/a | return iobase_unsupported("seek"); |
---|
112 | n/a | } |
---|
113 | n/a | |
---|
114 | n/a | /*[clinic input] |
---|
115 | n/a | _io._IOBase.tell |
---|
116 | n/a | |
---|
117 | n/a | Return current stream position. |
---|
118 | n/a | [clinic start generated code]*/ |
---|
119 | n/a | |
---|
120 | n/a | static PyObject * |
---|
121 | n/a | _io__IOBase_tell_impl(PyObject *self) |
---|
122 | n/a | /*[clinic end generated code: output=89a1c0807935abe2 input=04e615fec128801f]*/ |
---|
123 | n/a | { |
---|
124 | n/a | _Py_IDENTIFIER(seek); |
---|
125 | n/a | |
---|
126 | n/a | return _PyObject_CallMethodId(self, &PyId_seek, "ii", 0, 1); |
---|
127 | n/a | } |
---|
128 | n/a | |
---|
129 | n/a | PyDoc_STRVAR(iobase_truncate_doc, |
---|
130 | n/a | "Truncate file to size bytes.\n" |
---|
131 | n/a | "\n" |
---|
132 | n/a | "File pointer is left unchanged. Size defaults to the current IO\n" |
---|
133 | n/a | "position as reported by tell(). Returns the new size."); |
---|
134 | n/a | |
---|
135 | n/a | static PyObject * |
---|
136 | n/a | iobase_truncate(PyObject *self, PyObject *args) |
---|
137 | n/a | { |
---|
138 | n/a | return iobase_unsupported("truncate"); |
---|
139 | n/a | } |
---|
140 | n/a | |
---|
141 | n/a | /* Flush and close methods */ |
---|
142 | n/a | |
---|
143 | n/a | /*[clinic input] |
---|
144 | n/a | _io._IOBase.flush |
---|
145 | n/a | |
---|
146 | n/a | Flush write buffers, if applicable. |
---|
147 | n/a | |
---|
148 | n/a | This is not implemented for read-only and non-blocking streams. |
---|
149 | n/a | [clinic start generated code]*/ |
---|
150 | n/a | |
---|
151 | n/a | static PyObject * |
---|
152 | n/a | _io__IOBase_flush_impl(PyObject *self) |
---|
153 | n/a | /*[clinic end generated code: output=7cef4b4d54656a3b input=773be121abe270aa]*/ |
---|
154 | n/a | { |
---|
155 | n/a | /* XXX Should this return the number of bytes written??? */ |
---|
156 | n/a | if (IS_CLOSED(self)) { |
---|
157 | n/a | PyErr_SetString(PyExc_ValueError, "I/O operation on closed file."); |
---|
158 | n/a | return NULL; |
---|
159 | n/a | } |
---|
160 | n/a | Py_RETURN_NONE; |
---|
161 | n/a | } |
---|
162 | n/a | |
---|
163 | n/a | static int |
---|
164 | n/a | iobase_closed(PyObject *self) |
---|
165 | n/a | { |
---|
166 | n/a | PyObject *res; |
---|
167 | n/a | int closed; |
---|
168 | n/a | /* This gets the derived attribute, which is *not* __IOBase_closed |
---|
169 | n/a | in most cases! */ |
---|
170 | n/a | res = PyObject_GetAttr(self, _PyIO_str_closed); |
---|
171 | n/a | if (res == NULL) |
---|
172 | n/a | return 0; |
---|
173 | n/a | closed = PyObject_IsTrue(res); |
---|
174 | n/a | Py_DECREF(res); |
---|
175 | n/a | return closed; |
---|
176 | n/a | } |
---|
177 | n/a | |
---|
178 | n/a | static PyObject * |
---|
179 | n/a | iobase_closed_get(PyObject *self, void *context) |
---|
180 | n/a | { |
---|
181 | n/a | return PyBool_FromLong(IS_CLOSED(self)); |
---|
182 | n/a | } |
---|
183 | n/a | |
---|
184 | n/a | PyObject * |
---|
185 | n/a | _PyIOBase_check_closed(PyObject *self, PyObject *args) |
---|
186 | n/a | { |
---|
187 | n/a | if (iobase_closed(self)) { |
---|
188 | n/a | PyErr_SetString(PyExc_ValueError, "I/O operation on closed file."); |
---|
189 | n/a | return NULL; |
---|
190 | n/a | } |
---|
191 | n/a | if (args == Py_True) |
---|
192 | n/a | return Py_None; |
---|
193 | n/a | else |
---|
194 | n/a | Py_RETURN_NONE; |
---|
195 | n/a | } |
---|
196 | n/a | |
---|
197 | n/a | /* XXX: IOBase thinks it has to maintain its own internal state in |
---|
198 | n/a | `__IOBase_closed` and call flush() by itself, but it is redundant with |
---|
199 | n/a | whatever behaviour a non-trivial derived class will implement. */ |
---|
200 | n/a | |
---|
201 | n/a | /*[clinic input] |
---|
202 | n/a | _io._IOBase.close |
---|
203 | n/a | |
---|
204 | n/a | Flush and close the IO object. |
---|
205 | n/a | |
---|
206 | n/a | This method has no effect if the file is already closed. |
---|
207 | n/a | [clinic start generated code]*/ |
---|
208 | n/a | |
---|
209 | n/a | static PyObject * |
---|
210 | n/a | _io__IOBase_close_impl(PyObject *self) |
---|
211 | n/a | /*[clinic end generated code: output=63c6a6f57d783d6d input=f4494d5c31dbc6b7]*/ |
---|
212 | n/a | { |
---|
213 | n/a | PyObject *res; |
---|
214 | n/a | |
---|
215 | n/a | if (IS_CLOSED(self)) |
---|
216 | n/a | Py_RETURN_NONE; |
---|
217 | n/a | |
---|
218 | n/a | res = PyObject_CallMethodObjArgs(self, _PyIO_str_flush, NULL); |
---|
219 | n/a | |
---|
220 | n/a | if (_PyObject_SetAttrId(self, &PyId___IOBase_closed, Py_True) < 0) { |
---|
221 | n/a | Py_XDECREF(res); |
---|
222 | n/a | return NULL; |
---|
223 | n/a | } |
---|
224 | n/a | |
---|
225 | n/a | if (res == NULL) |
---|
226 | n/a | return NULL; |
---|
227 | n/a | |
---|
228 | n/a | Py_DECREF(res); |
---|
229 | n/a | Py_RETURN_NONE; |
---|
230 | n/a | } |
---|
231 | n/a | |
---|
232 | n/a | /* Finalization and garbage collection support */ |
---|
233 | n/a | |
---|
234 | n/a | static void |
---|
235 | n/a | iobase_finalize(PyObject *self) |
---|
236 | n/a | { |
---|
237 | n/a | PyObject *res; |
---|
238 | n/a | PyObject *error_type, *error_value, *error_traceback; |
---|
239 | n/a | int closed; |
---|
240 | n/a | _Py_IDENTIFIER(_finalizing); |
---|
241 | n/a | |
---|
242 | n/a | /* Save the current exception, if any. */ |
---|
243 | n/a | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
---|
244 | n/a | |
---|
245 | n/a | /* If `closed` doesn't exist or can't be evaluated as bool, then the |
---|
246 | n/a | object is probably in an unusable state, so ignore. */ |
---|
247 | n/a | res = PyObject_GetAttr(self, _PyIO_str_closed); |
---|
248 | n/a | if (res == NULL) { |
---|
249 | n/a | PyErr_Clear(); |
---|
250 | n/a | closed = -1; |
---|
251 | n/a | } |
---|
252 | n/a | else { |
---|
253 | n/a | closed = PyObject_IsTrue(res); |
---|
254 | n/a | Py_DECREF(res); |
---|
255 | n/a | if (closed == -1) |
---|
256 | n/a | PyErr_Clear(); |
---|
257 | n/a | } |
---|
258 | n/a | if (closed == 0) { |
---|
259 | n/a | /* Signal close() that it was called as part of the object |
---|
260 | n/a | finalization process. */ |
---|
261 | n/a | if (_PyObject_SetAttrId(self, &PyId__finalizing, Py_True)) |
---|
262 | n/a | PyErr_Clear(); |
---|
263 | n/a | res = PyObject_CallMethodObjArgs((PyObject *) self, _PyIO_str_close, |
---|
264 | n/a | NULL); |
---|
265 | n/a | /* Silencing I/O errors is bad, but printing spurious tracebacks is |
---|
266 | n/a | equally as bad, and potentially more frequent (because of |
---|
267 | n/a | shutdown issues). */ |
---|
268 | n/a | if (res == NULL) |
---|
269 | n/a | PyErr_Clear(); |
---|
270 | n/a | else |
---|
271 | n/a | Py_DECREF(res); |
---|
272 | n/a | } |
---|
273 | n/a | |
---|
274 | n/a | /* Restore the saved exception. */ |
---|
275 | n/a | PyErr_Restore(error_type, error_value, error_traceback); |
---|
276 | n/a | } |
---|
277 | n/a | |
---|
278 | n/a | int |
---|
279 | n/a | _PyIOBase_finalize(PyObject *self) |
---|
280 | n/a | { |
---|
281 | n/a | int is_zombie; |
---|
282 | n/a | |
---|
283 | n/a | /* If _PyIOBase_finalize() is called from a destructor, we need to |
---|
284 | n/a | resurrect the object as calling close() can invoke arbitrary code. */ |
---|
285 | n/a | is_zombie = (Py_REFCNT(self) == 0); |
---|
286 | n/a | if (is_zombie) |
---|
287 | n/a | return PyObject_CallFinalizerFromDealloc(self); |
---|
288 | n/a | else { |
---|
289 | n/a | PyObject_CallFinalizer(self); |
---|
290 | n/a | return 0; |
---|
291 | n/a | } |
---|
292 | n/a | } |
---|
293 | n/a | |
---|
294 | n/a | static int |
---|
295 | n/a | iobase_traverse(iobase *self, visitproc visit, void *arg) |
---|
296 | n/a | { |
---|
297 | n/a | Py_VISIT(self->dict); |
---|
298 | n/a | return 0; |
---|
299 | n/a | } |
---|
300 | n/a | |
---|
301 | n/a | static int |
---|
302 | n/a | iobase_clear(iobase *self) |
---|
303 | n/a | { |
---|
304 | n/a | Py_CLEAR(self->dict); |
---|
305 | n/a | return 0; |
---|
306 | n/a | } |
---|
307 | n/a | |
---|
308 | n/a | /* Destructor */ |
---|
309 | n/a | |
---|
310 | n/a | static void |
---|
311 | n/a | iobase_dealloc(iobase *self) |
---|
312 | n/a | { |
---|
313 | n/a | /* NOTE: since IOBaseObject has its own dict, Python-defined attributes |
---|
314 | n/a | are still available here for close() to use. |
---|
315 | n/a | However, if the derived class declares a __slots__, those slots are |
---|
316 | n/a | already gone. |
---|
317 | n/a | */ |
---|
318 | n/a | if (_PyIOBase_finalize((PyObject *) self) < 0) { |
---|
319 | n/a | /* When called from a heap type's dealloc, the type will be |
---|
320 | n/a | decref'ed on return (see e.g. subtype_dealloc in typeobject.c). */ |
---|
321 | n/a | if (PyType_HasFeature(Py_TYPE(self), Py_TPFLAGS_HEAPTYPE)) |
---|
322 | n/a | Py_INCREF(Py_TYPE(self)); |
---|
323 | n/a | return; |
---|
324 | n/a | } |
---|
325 | n/a | _PyObject_GC_UNTRACK(self); |
---|
326 | n/a | if (self->weakreflist != NULL) |
---|
327 | n/a | PyObject_ClearWeakRefs((PyObject *) self); |
---|
328 | n/a | Py_CLEAR(self->dict); |
---|
329 | n/a | Py_TYPE(self)->tp_free((PyObject *) self); |
---|
330 | n/a | } |
---|
331 | n/a | |
---|
332 | n/a | /* Inquiry methods */ |
---|
333 | n/a | |
---|
334 | n/a | /*[clinic input] |
---|
335 | n/a | _io._IOBase.seekable |
---|
336 | n/a | |
---|
337 | n/a | Return whether object supports random access. |
---|
338 | n/a | |
---|
339 | n/a | If False, seek(), tell() and truncate() will raise OSError. |
---|
340 | n/a | This method may need to do a test seek(). |
---|
341 | n/a | [clinic start generated code]*/ |
---|
342 | n/a | |
---|
343 | n/a | static PyObject * |
---|
344 | n/a | _io__IOBase_seekable_impl(PyObject *self) |
---|
345 | n/a | /*[clinic end generated code: output=4c24c67f5f32a43d input=b976622f7fdf3063]*/ |
---|
346 | n/a | { |
---|
347 | n/a | Py_RETURN_FALSE; |
---|
348 | n/a | } |
---|
349 | n/a | |
---|
350 | n/a | PyObject * |
---|
351 | n/a | _PyIOBase_check_seekable(PyObject *self, PyObject *args) |
---|
352 | n/a | { |
---|
353 | n/a | PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_seekable, NULL); |
---|
354 | n/a | if (res == NULL) |
---|
355 | n/a | return NULL; |
---|
356 | n/a | if (res != Py_True) { |
---|
357 | n/a | Py_CLEAR(res); |
---|
358 | n/a | iobase_unsupported("File or stream is not seekable."); |
---|
359 | n/a | return NULL; |
---|
360 | n/a | } |
---|
361 | n/a | if (args == Py_True) { |
---|
362 | n/a | Py_DECREF(res); |
---|
363 | n/a | } |
---|
364 | n/a | return res; |
---|
365 | n/a | } |
---|
366 | n/a | |
---|
367 | n/a | /*[clinic input] |
---|
368 | n/a | _io._IOBase.readable |
---|
369 | n/a | |
---|
370 | n/a | Return whether object was opened for reading. |
---|
371 | n/a | |
---|
372 | n/a | If False, read() will raise OSError. |
---|
373 | n/a | [clinic start generated code]*/ |
---|
374 | n/a | |
---|
375 | n/a | static PyObject * |
---|
376 | n/a | _io__IOBase_readable_impl(PyObject *self) |
---|
377 | n/a | /*[clinic end generated code: output=e48089250686388b input=285b3b866a0ec35f]*/ |
---|
378 | n/a | { |
---|
379 | n/a | Py_RETURN_FALSE; |
---|
380 | n/a | } |
---|
381 | n/a | |
---|
382 | n/a | /* May be called with any object */ |
---|
383 | n/a | PyObject * |
---|
384 | n/a | _PyIOBase_check_readable(PyObject *self, PyObject *args) |
---|
385 | n/a | { |
---|
386 | n/a | PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_readable, NULL); |
---|
387 | n/a | if (res == NULL) |
---|
388 | n/a | return NULL; |
---|
389 | n/a | if (res != Py_True) { |
---|
390 | n/a | Py_CLEAR(res); |
---|
391 | n/a | iobase_unsupported("File or stream is not readable."); |
---|
392 | n/a | return NULL; |
---|
393 | n/a | } |
---|
394 | n/a | if (args == Py_True) { |
---|
395 | n/a | Py_DECREF(res); |
---|
396 | n/a | } |
---|
397 | n/a | return res; |
---|
398 | n/a | } |
---|
399 | n/a | |
---|
400 | n/a | /*[clinic input] |
---|
401 | n/a | _io._IOBase.writable |
---|
402 | n/a | |
---|
403 | n/a | Return whether object was opened for writing. |
---|
404 | n/a | |
---|
405 | n/a | If False, write() will raise OSError. |
---|
406 | n/a | [clinic start generated code]*/ |
---|
407 | n/a | |
---|
408 | n/a | static PyObject * |
---|
409 | n/a | _io__IOBase_writable_impl(PyObject *self) |
---|
410 | n/a | /*[clinic end generated code: output=406001d0985be14f input=9dcac18a013a05b5]*/ |
---|
411 | n/a | { |
---|
412 | n/a | Py_RETURN_FALSE; |
---|
413 | n/a | } |
---|
414 | n/a | |
---|
415 | n/a | /* May be called with any object */ |
---|
416 | n/a | PyObject * |
---|
417 | n/a | _PyIOBase_check_writable(PyObject *self, PyObject *args) |
---|
418 | n/a | { |
---|
419 | n/a | PyObject *res = PyObject_CallMethodObjArgs(self, _PyIO_str_writable, NULL); |
---|
420 | n/a | if (res == NULL) |
---|
421 | n/a | return NULL; |
---|
422 | n/a | if (res != Py_True) { |
---|
423 | n/a | Py_CLEAR(res); |
---|
424 | n/a | iobase_unsupported("File or stream is not writable."); |
---|
425 | n/a | return NULL; |
---|
426 | n/a | } |
---|
427 | n/a | if (args == Py_True) { |
---|
428 | n/a | Py_DECREF(res); |
---|
429 | n/a | } |
---|
430 | n/a | return res; |
---|
431 | n/a | } |
---|
432 | n/a | |
---|
433 | n/a | /* Context manager */ |
---|
434 | n/a | |
---|
435 | n/a | static PyObject * |
---|
436 | n/a | iobase_enter(PyObject *self, PyObject *args) |
---|
437 | n/a | { |
---|
438 | n/a | if (_PyIOBase_check_closed(self, Py_True) == NULL) |
---|
439 | n/a | return NULL; |
---|
440 | n/a | |
---|
441 | n/a | Py_INCREF(self); |
---|
442 | n/a | return self; |
---|
443 | n/a | } |
---|
444 | n/a | |
---|
445 | n/a | static PyObject * |
---|
446 | n/a | iobase_exit(PyObject *self, PyObject *args) |
---|
447 | n/a | { |
---|
448 | n/a | return PyObject_CallMethodObjArgs(self, _PyIO_str_close, NULL); |
---|
449 | n/a | } |
---|
450 | n/a | |
---|
451 | n/a | /* Lower-level APIs */ |
---|
452 | n/a | |
---|
453 | n/a | /* XXX Should these be present even if unimplemented? */ |
---|
454 | n/a | |
---|
455 | n/a | /*[clinic input] |
---|
456 | n/a | _io._IOBase.fileno |
---|
457 | n/a | |
---|
458 | n/a | Returns underlying file descriptor if one exists. |
---|
459 | n/a | |
---|
460 | n/a | OSError is raised if the IO object does not use a file descriptor. |
---|
461 | n/a | [clinic start generated code]*/ |
---|
462 | n/a | |
---|
463 | n/a | static PyObject * |
---|
464 | n/a | _io__IOBase_fileno_impl(PyObject *self) |
---|
465 | n/a | /*[clinic end generated code: output=7cc0973f0f5f3b73 input=4e37028947dc1cc8]*/ |
---|
466 | n/a | { |
---|
467 | n/a | return iobase_unsupported("fileno"); |
---|
468 | n/a | } |
---|
469 | n/a | |
---|
470 | n/a | /*[clinic input] |
---|
471 | n/a | _io._IOBase.isatty |
---|
472 | n/a | |
---|
473 | n/a | Return whether this is an 'interactive' stream. |
---|
474 | n/a | |
---|
475 | n/a | Return False if it can't be determined. |
---|
476 | n/a | [clinic start generated code]*/ |
---|
477 | n/a | |
---|
478 | n/a | static PyObject * |
---|
479 | n/a | _io__IOBase_isatty_impl(PyObject *self) |
---|
480 | n/a | /*[clinic end generated code: output=60cab77cede41cdd input=9ef76530d368458b]*/ |
---|
481 | n/a | { |
---|
482 | n/a | if (_PyIOBase_check_closed(self, Py_True) == NULL) |
---|
483 | n/a | return NULL; |
---|
484 | n/a | Py_RETURN_FALSE; |
---|
485 | n/a | } |
---|
486 | n/a | |
---|
487 | n/a | /* Readline(s) and writelines */ |
---|
488 | n/a | |
---|
489 | n/a | /*[clinic input] |
---|
490 | n/a | _io._IOBase.readline |
---|
491 | n/a | size as limit: io_ssize_t = -1 |
---|
492 | n/a | / |
---|
493 | n/a | |
---|
494 | n/a | Read and return a line from the stream. |
---|
495 | n/a | |
---|
496 | n/a | If size is specified, at most size bytes will be read. |
---|
497 | n/a | |
---|
498 | n/a | The line terminator is always b'\n' for binary files; for text |
---|
499 | n/a | files, the newlines argument to open can be used to select the line |
---|
500 | n/a | terminator(s) recognized. |
---|
501 | n/a | [clinic start generated code]*/ |
---|
502 | n/a | |
---|
503 | n/a | static PyObject * |
---|
504 | n/a | _io__IOBase_readline_impl(PyObject *self, Py_ssize_t limit) |
---|
505 | n/a | /*[clinic end generated code: output=4479f79b58187840 input=df4cc8884f553cab]*/ |
---|
506 | n/a | { |
---|
507 | n/a | /* For backwards compatibility, a (slowish) readline(). */ |
---|
508 | n/a | |
---|
509 | n/a | int has_peek = 0; |
---|
510 | n/a | PyObject *buffer, *result; |
---|
511 | n/a | Py_ssize_t old_size = -1; |
---|
512 | n/a | _Py_IDENTIFIER(peek); |
---|
513 | n/a | |
---|
514 | n/a | if (_PyObject_HasAttrId(self, &PyId_peek)) |
---|
515 | n/a | has_peek = 1; |
---|
516 | n/a | |
---|
517 | n/a | buffer = PyByteArray_FromStringAndSize(NULL, 0); |
---|
518 | n/a | if (buffer == NULL) |
---|
519 | n/a | return NULL; |
---|
520 | n/a | |
---|
521 | n/a | while (limit < 0 || Py_SIZE(buffer) < limit) { |
---|
522 | n/a | Py_ssize_t nreadahead = 1; |
---|
523 | n/a | PyObject *b; |
---|
524 | n/a | |
---|
525 | n/a | if (has_peek) { |
---|
526 | n/a | PyObject *readahead = _PyObject_CallMethodId(self, &PyId_peek, "i", 1); |
---|
527 | n/a | if (readahead == NULL) { |
---|
528 | n/a | /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() |
---|
529 | n/a | when EINTR occurs so we needn't do it ourselves. */ |
---|
530 | n/a | if (_PyIO_trap_eintr()) { |
---|
531 | n/a | continue; |
---|
532 | n/a | } |
---|
533 | n/a | goto fail; |
---|
534 | n/a | } |
---|
535 | n/a | if (!PyBytes_Check(readahead)) { |
---|
536 | n/a | PyErr_Format(PyExc_IOError, |
---|
537 | n/a | "peek() should have returned a bytes object, " |
---|
538 | n/a | "not '%.200s'", Py_TYPE(readahead)->tp_name); |
---|
539 | n/a | Py_DECREF(readahead); |
---|
540 | n/a | goto fail; |
---|
541 | n/a | } |
---|
542 | n/a | if (PyBytes_GET_SIZE(readahead) > 0) { |
---|
543 | n/a | Py_ssize_t n = 0; |
---|
544 | n/a | const char *buf = PyBytes_AS_STRING(readahead); |
---|
545 | n/a | if (limit >= 0) { |
---|
546 | n/a | do { |
---|
547 | n/a | if (n >= PyBytes_GET_SIZE(readahead) || n >= limit) |
---|
548 | n/a | break; |
---|
549 | n/a | if (buf[n++] == '\n') |
---|
550 | n/a | break; |
---|
551 | n/a | } while (1); |
---|
552 | n/a | } |
---|
553 | n/a | else { |
---|
554 | n/a | do { |
---|
555 | n/a | if (n >= PyBytes_GET_SIZE(readahead)) |
---|
556 | n/a | break; |
---|
557 | n/a | if (buf[n++] == '\n') |
---|
558 | n/a | break; |
---|
559 | n/a | } while (1); |
---|
560 | n/a | } |
---|
561 | n/a | nreadahead = n; |
---|
562 | n/a | } |
---|
563 | n/a | Py_DECREF(readahead); |
---|
564 | n/a | } |
---|
565 | n/a | |
---|
566 | n/a | b = _PyObject_CallMethodId(self, &PyId_read, "n", nreadahead); |
---|
567 | n/a | if (b == NULL) { |
---|
568 | n/a | /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() |
---|
569 | n/a | when EINTR occurs so we needn't do it ourselves. */ |
---|
570 | n/a | if (_PyIO_trap_eintr()) { |
---|
571 | n/a | continue; |
---|
572 | n/a | } |
---|
573 | n/a | goto fail; |
---|
574 | n/a | } |
---|
575 | n/a | if (!PyBytes_Check(b)) { |
---|
576 | n/a | PyErr_Format(PyExc_IOError, |
---|
577 | n/a | "read() should have returned a bytes object, " |
---|
578 | n/a | "not '%.200s'", Py_TYPE(b)->tp_name); |
---|
579 | n/a | Py_DECREF(b); |
---|
580 | n/a | goto fail; |
---|
581 | n/a | } |
---|
582 | n/a | if (PyBytes_GET_SIZE(b) == 0) { |
---|
583 | n/a | Py_DECREF(b); |
---|
584 | n/a | break; |
---|
585 | n/a | } |
---|
586 | n/a | |
---|
587 | n/a | old_size = PyByteArray_GET_SIZE(buffer); |
---|
588 | n/a | if (PyByteArray_Resize(buffer, old_size + PyBytes_GET_SIZE(b)) < 0) { |
---|
589 | n/a | Py_DECREF(b); |
---|
590 | n/a | goto fail; |
---|
591 | n/a | } |
---|
592 | n/a | memcpy(PyByteArray_AS_STRING(buffer) + old_size, |
---|
593 | n/a | PyBytes_AS_STRING(b), PyBytes_GET_SIZE(b)); |
---|
594 | n/a | |
---|
595 | n/a | Py_DECREF(b); |
---|
596 | n/a | |
---|
597 | n/a | if (PyByteArray_AS_STRING(buffer)[PyByteArray_GET_SIZE(buffer) - 1] == '\n') |
---|
598 | n/a | break; |
---|
599 | n/a | } |
---|
600 | n/a | |
---|
601 | n/a | result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(buffer), |
---|
602 | n/a | PyByteArray_GET_SIZE(buffer)); |
---|
603 | n/a | Py_DECREF(buffer); |
---|
604 | n/a | return result; |
---|
605 | n/a | fail: |
---|
606 | n/a | Py_DECREF(buffer); |
---|
607 | n/a | return NULL; |
---|
608 | n/a | } |
---|
609 | n/a | |
---|
610 | n/a | static PyObject * |
---|
611 | n/a | iobase_iter(PyObject *self) |
---|
612 | n/a | { |
---|
613 | n/a | if (_PyIOBase_check_closed(self, Py_True) == NULL) |
---|
614 | n/a | return NULL; |
---|
615 | n/a | |
---|
616 | n/a | Py_INCREF(self); |
---|
617 | n/a | return self; |
---|
618 | n/a | } |
---|
619 | n/a | |
---|
620 | n/a | static PyObject * |
---|
621 | n/a | iobase_iternext(PyObject *self) |
---|
622 | n/a | { |
---|
623 | n/a | PyObject *line = PyObject_CallMethodObjArgs(self, _PyIO_str_readline, NULL); |
---|
624 | n/a | |
---|
625 | n/a | if (line == NULL) |
---|
626 | n/a | return NULL; |
---|
627 | n/a | |
---|
628 | n/a | if (PyObject_Size(line) == 0) { |
---|
629 | n/a | Py_DECREF(line); |
---|
630 | n/a | return NULL; |
---|
631 | n/a | } |
---|
632 | n/a | |
---|
633 | n/a | return line; |
---|
634 | n/a | } |
---|
635 | n/a | |
---|
636 | n/a | /*[clinic input] |
---|
637 | n/a | _io._IOBase.readlines |
---|
638 | n/a | hint: io_ssize_t = -1 |
---|
639 | n/a | / |
---|
640 | n/a | |
---|
641 | n/a | Return a list of lines from the stream. |
---|
642 | n/a | |
---|
643 | n/a | hint can be specified to control the number of lines read: no more |
---|
644 | n/a | lines will be read if the total size (in bytes/characters) of all |
---|
645 | n/a | lines so far exceeds hint. |
---|
646 | n/a | [clinic start generated code]*/ |
---|
647 | n/a | |
---|
648 | n/a | static PyObject * |
---|
649 | n/a | _io__IOBase_readlines_impl(PyObject *self, Py_ssize_t hint) |
---|
650 | n/a | /*[clinic end generated code: output=2f50421677fa3dea input=1961c4a95e96e661]*/ |
---|
651 | n/a | { |
---|
652 | n/a | Py_ssize_t length = 0; |
---|
653 | n/a | PyObject *result; |
---|
654 | n/a | |
---|
655 | n/a | result = PyList_New(0); |
---|
656 | n/a | if (result == NULL) |
---|
657 | n/a | return NULL; |
---|
658 | n/a | |
---|
659 | n/a | if (hint <= 0) { |
---|
660 | n/a | /* XXX special-casing this made sense in the Python version in order |
---|
661 | n/a | to remove the bytecode interpretation overhead, but it could |
---|
662 | n/a | probably be removed here. */ |
---|
663 | n/a | _Py_IDENTIFIER(extend); |
---|
664 | n/a | PyObject *ret = _PyObject_CallMethodIdObjArgs(result, &PyId_extend, |
---|
665 | n/a | self, NULL); |
---|
666 | n/a | |
---|
667 | n/a | if (ret == NULL) { |
---|
668 | n/a | Py_DECREF(result); |
---|
669 | n/a | return NULL; |
---|
670 | n/a | } |
---|
671 | n/a | Py_DECREF(ret); |
---|
672 | n/a | return result; |
---|
673 | n/a | } |
---|
674 | n/a | |
---|
675 | n/a | while (1) { |
---|
676 | n/a | PyObject *line = PyIter_Next(self); |
---|
677 | n/a | if (line == NULL) { |
---|
678 | n/a | if (PyErr_Occurred()) { |
---|
679 | n/a | Py_DECREF(result); |
---|
680 | n/a | return NULL; |
---|
681 | n/a | } |
---|
682 | n/a | else |
---|
683 | n/a | break; /* StopIteration raised */ |
---|
684 | n/a | } |
---|
685 | n/a | |
---|
686 | n/a | if (PyList_Append(result, line) < 0) { |
---|
687 | n/a | Py_DECREF(line); |
---|
688 | n/a | Py_DECREF(result); |
---|
689 | n/a | return NULL; |
---|
690 | n/a | } |
---|
691 | n/a | length += PyObject_Size(line); |
---|
692 | n/a | Py_DECREF(line); |
---|
693 | n/a | |
---|
694 | n/a | if (length > hint) |
---|
695 | n/a | break; |
---|
696 | n/a | } |
---|
697 | n/a | return result; |
---|
698 | n/a | } |
---|
699 | n/a | |
---|
700 | n/a | /*[clinic input] |
---|
701 | n/a | _io._IOBase.writelines |
---|
702 | n/a | lines: object |
---|
703 | n/a | / |
---|
704 | n/a | [clinic start generated code]*/ |
---|
705 | n/a | |
---|
706 | n/a | static PyObject * |
---|
707 | n/a | _io__IOBase_writelines(PyObject *self, PyObject *lines) |
---|
708 | n/a | /*[clinic end generated code: output=976eb0a9b60a6628 input=432e729a8450b3cb]*/ |
---|
709 | n/a | { |
---|
710 | n/a | PyObject *iter, *res; |
---|
711 | n/a | |
---|
712 | n/a | if (_PyIOBase_check_closed(self, Py_True) == NULL) |
---|
713 | n/a | return NULL; |
---|
714 | n/a | |
---|
715 | n/a | iter = PyObject_GetIter(lines); |
---|
716 | n/a | if (iter == NULL) |
---|
717 | n/a | return NULL; |
---|
718 | n/a | |
---|
719 | n/a | while (1) { |
---|
720 | n/a | PyObject *line = PyIter_Next(iter); |
---|
721 | n/a | if (line == NULL) { |
---|
722 | n/a | if (PyErr_Occurred()) { |
---|
723 | n/a | Py_DECREF(iter); |
---|
724 | n/a | return NULL; |
---|
725 | n/a | } |
---|
726 | n/a | else |
---|
727 | n/a | break; /* Stop Iteration */ |
---|
728 | n/a | } |
---|
729 | n/a | |
---|
730 | n/a | res = NULL; |
---|
731 | n/a | do { |
---|
732 | n/a | res = PyObject_CallMethodObjArgs(self, _PyIO_str_write, line, NULL); |
---|
733 | n/a | } while (res == NULL && _PyIO_trap_eintr()); |
---|
734 | n/a | Py_DECREF(line); |
---|
735 | n/a | if (res == NULL) { |
---|
736 | n/a | Py_DECREF(iter); |
---|
737 | n/a | return NULL; |
---|
738 | n/a | } |
---|
739 | n/a | Py_DECREF(res); |
---|
740 | n/a | } |
---|
741 | n/a | Py_DECREF(iter); |
---|
742 | n/a | Py_RETURN_NONE; |
---|
743 | n/a | } |
---|
744 | n/a | |
---|
745 | n/a | #include "clinic/iobase.c.h" |
---|
746 | n/a | |
---|
747 | n/a | static PyMethodDef iobase_methods[] = { |
---|
748 | n/a | {"seek", iobase_seek, METH_VARARGS, iobase_seek_doc}, |
---|
749 | n/a | _IO__IOBASE_TELL_METHODDEF |
---|
750 | n/a | {"truncate", iobase_truncate, METH_VARARGS, iobase_truncate_doc}, |
---|
751 | n/a | _IO__IOBASE_FLUSH_METHODDEF |
---|
752 | n/a | _IO__IOBASE_CLOSE_METHODDEF |
---|
753 | n/a | |
---|
754 | n/a | _IO__IOBASE_SEEKABLE_METHODDEF |
---|
755 | n/a | _IO__IOBASE_READABLE_METHODDEF |
---|
756 | n/a | _IO__IOBASE_WRITABLE_METHODDEF |
---|
757 | n/a | |
---|
758 | n/a | {"_checkClosed", _PyIOBase_check_closed, METH_NOARGS}, |
---|
759 | n/a | {"_checkSeekable", _PyIOBase_check_seekable, METH_NOARGS}, |
---|
760 | n/a | {"_checkReadable", _PyIOBase_check_readable, METH_NOARGS}, |
---|
761 | n/a | {"_checkWritable", _PyIOBase_check_writable, METH_NOARGS}, |
---|
762 | n/a | |
---|
763 | n/a | _IO__IOBASE_FILENO_METHODDEF |
---|
764 | n/a | _IO__IOBASE_ISATTY_METHODDEF |
---|
765 | n/a | |
---|
766 | n/a | {"__enter__", iobase_enter, METH_NOARGS}, |
---|
767 | n/a | {"__exit__", iobase_exit, METH_VARARGS}, |
---|
768 | n/a | |
---|
769 | n/a | _IO__IOBASE_READLINE_METHODDEF |
---|
770 | n/a | _IO__IOBASE_READLINES_METHODDEF |
---|
771 | n/a | _IO__IOBASE_WRITELINES_METHODDEF |
---|
772 | n/a | |
---|
773 | n/a | {NULL, NULL} |
---|
774 | n/a | }; |
---|
775 | n/a | |
---|
776 | n/a | static PyGetSetDef iobase_getset[] = { |
---|
777 | n/a | {"__dict__", PyObject_GenericGetDict, NULL, NULL}, |
---|
778 | n/a | {"closed", (getter)iobase_closed_get, NULL, NULL}, |
---|
779 | n/a | {NULL} |
---|
780 | n/a | }; |
---|
781 | n/a | |
---|
782 | n/a | |
---|
783 | n/a | PyTypeObject PyIOBase_Type = { |
---|
784 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
785 | n/a | "_io._IOBase", /*tp_name*/ |
---|
786 | n/a | sizeof(iobase), /*tp_basicsize*/ |
---|
787 | n/a | 0, /*tp_itemsize*/ |
---|
788 | n/a | (destructor)iobase_dealloc, /*tp_dealloc*/ |
---|
789 | n/a | 0, /*tp_print*/ |
---|
790 | n/a | 0, /*tp_getattr*/ |
---|
791 | n/a | 0, /*tp_setattr*/ |
---|
792 | n/a | 0, /*tp_compare */ |
---|
793 | n/a | 0, /*tp_repr*/ |
---|
794 | n/a | 0, /*tp_as_number*/ |
---|
795 | n/a | 0, /*tp_as_sequence*/ |
---|
796 | n/a | 0, /*tp_as_mapping*/ |
---|
797 | n/a | 0, /*tp_hash */ |
---|
798 | n/a | 0, /*tp_call*/ |
---|
799 | n/a | 0, /*tp_str*/ |
---|
800 | n/a | 0, /*tp_getattro*/ |
---|
801 | n/a | 0, /*tp_setattro*/ |
---|
802 | n/a | 0, /*tp_as_buffer*/ |
---|
803 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
---|
804 | n/a | | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /*tp_flags*/ |
---|
805 | n/a | iobase_doc, /* tp_doc */ |
---|
806 | n/a | (traverseproc)iobase_traverse, /* tp_traverse */ |
---|
807 | n/a | (inquiry)iobase_clear, /* tp_clear */ |
---|
808 | n/a | 0, /* tp_richcompare */ |
---|
809 | n/a | offsetof(iobase, weakreflist), /* tp_weaklistoffset */ |
---|
810 | n/a | iobase_iter, /* tp_iter */ |
---|
811 | n/a | iobase_iternext, /* tp_iternext */ |
---|
812 | n/a | iobase_methods, /* tp_methods */ |
---|
813 | n/a | 0, /* tp_members */ |
---|
814 | n/a | iobase_getset, /* tp_getset */ |
---|
815 | n/a | 0, /* tp_base */ |
---|
816 | n/a | 0, /* tp_dict */ |
---|
817 | n/a | 0, /* tp_descr_get */ |
---|
818 | n/a | 0, /* tp_descr_set */ |
---|
819 | n/a | offsetof(iobase, dict), /* tp_dictoffset */ |
---|
820 | n/a | 0, /* tp_init */ |
---|
821 | n/a | 0, /* tp_alloc */ |
---|
822 | n/a | PyType_GenericNew, /* tp_new */ |
---|
823 | n/a | 0, /* tp_free */ |
---|
824 | n/a | 0, /* tp_is_gc */ |
---|
825 | n/a | 0, /* tp_bases */ |
---|
826 | n/a | 0, /* tp_mro */ |
---|
827 | n/a | 0, /* tp_cache */ |
---|
828 | n/a | 0, /* tp_subclasses */ |
---|
829 | n/a | 0, /* tp_weaklist */ |
---|
830 | n/a | 0, /* tp_del */ |
---|
831 | n/a | 0, /* tp_version_tag */ |
---|
832 | n/a | iobase_finalize, /* tp_finalize */ |
---|
833 | n/a | }; |
---|
834 | n/a | |
---|
835 | n/a | |
---|
836 | n/a | /* |
---|
837 | n/a | * RawIOBase class, Inherits from IOBase. |
---|
838 | n/a | */ |
---|
839 | n/a | PyDoc_STRVAR(rawiobase_doc, |
---|
840 | n/a | "Base class for raw binary I/O."); |
---|
841 | n/a | |
---|
842 | n/a | /* |
---|
843 | n/a | * The read() method is implemented by calling readinto(); derived classes |
---|
844 | n/a | * that want to support read() only need to implement readinto() as a |
---|
845 | n/a | * primitive operation. In general, readinto() can be more efficient than |
---|
846 | n/a | * read(). |
---|
847 | n/a | * |
---|
848 | n/a | * (It would be tempting to also provide an implementation of readinto() in |
---|
849 | n/a | * terms of read(), in case the latter is a more suitable primitive operation, |
---|
850 | n/a | * but that would lead to nasty recursion in case a subclass doesn't implement |
---|
851 | n/a | * either.) |
---|
852 | n/a | */ |
---|
853 | n/a | |
---|
854 | n/a | /*[clinic input] |
---|
855 | n/a | _io._RawIOBase.read |
---|
856 | n/a | size as n: Py_ssize_t = -1 |
---|
857 | n/a | / |
---|
858 | n/a | [clinic start generated code]*/ |
---|
859 | n/a | |
---|
860 | n/a | static PyObject * |
---|
861 | n/a | _io__RawIOBase_read_impl(PyObject *self, Py_ssize_t n) |
---|
862 | n/a | /*[clinic end generated code: output=6cdeb731e3c9f13c input=b6d0dcf6417d1374]*/ |
---|
863 | n/a | { |
---|
864 | n/a | PyObject *b, *res; |
---|
865 | n/a | |
---|
866 | n/a | if (n < 0) { |
---|
867 | n/a | _Py_IDENTIFIER(readall); |
---|
868 | n/a | |
---|
869 | n/a | return _PyObject_CallMethodId(self, &PyId_readall, NULL); |
---|
870 | n/a | } |
---|
871 | n/a | |
---|
872 | n/a | /* TODO: allocate a bytes object directly instead and manually construct |
---|
873 | n/a | a writable memoryview pointing to it. */ |
---|
874 | n/a | b = PyByteArray_FromStringAndSize(NULL, n); |
---|
875 | n/a | if (b == NULL) |
---|
876 | n/a | return NULL; |
---|
877 | n/a | |
---|
878 | n/a | res = PyObject_CallMethodObjArgs(self, _PyIO_str_readinto, b, NULL); |
---|
879 | n/a | if (res == NULL || res == Py_None) { |
---|
880 | n/a | Py_DECREF(b); |
---|
881 | n/a | return res; |
---|
882 | n/a | } |
---|
883 | n/a | |
---|
884 | n/a | n = PyNumber_AsSsize_t(res, PyExc_ValueError); |
---|
885 | n/a | Py_DECREF(res); |
---|
886 | n/a | if (n == -1 && PyErr_Occurred()) { |
---|
887 | n/a | Py_DECREF(b); |
---|
888 | n/a | return NULL; |
---|
889 | n/a | } |
---|
890 | n/a | |
---|
891 | n/a | res = PyBytes_FromStringAndSize(PyByteArray_AsString(b), n); |
---|
892 | n/a | Py_DECREF(b); |
---|
893 | n/a | return res; |
---|
894 | n/a | } |
---|
895 | n/a | |
---|
896 | n/a | |
---|
897 | n/a | /*[clinic input] |
---|
898 | n/a | _io._RawIOBase.readall |
---|
899 | n/a | |
---|
900 | n/a | Read until EOF, using multiple read() call. |
---|
901 | n/a | [clinic start generated code]*/ |
---|
902 | n/a | |
---|
903 | n/a | static PyObject * |
---|
904 | n/a | _io__RawIOBase_readall_impl(PyObject *self) |
---|
905 | n/a | /*[clinic end generated code: output=1987b9ce929425a0 input=688874141213622a]*/ |
---|
906 | n/a | { |
---|
907 | n/a | int r; |
---|
908 | n/a | PyObject *chunks = PyList_New(0); |
---|
909 | n/a | PyObject *result; |
---|
910 | n/a | |
---|
911 | n/a | if (chunks == NULL) |
---|
912 | n/a | return NULL; |
---|
913 | n/a | |
---|
914 | n/a | while (1) { |
---|
915 | n/a | PyObject *data = _PyObject_CallMethodId(self, &PyId_read, |
---|
916 | n/a | "i", DEFAULT_BUFFER_SIZE); |
---|
917 | n/a | if (!data) { |
---|
918 | n/a | /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() |
---|
919 | n/a | when EINTR occurs so we needn't do it ourselves. */ |
---|
920 | n/a | if (_PyIO_trap_eintr()) { |
---|
921 | n/a | continue; |
---|
922 | n/a | } |
---|
923 | n/a | Py_DECREF(chunks); |
---|
924 | n/a | return NULL; |
---|
925 | n/a | } |
---|
926 | n/a | if (data == Py_None) { |
---|
927 | n/a | if (PyList_GET_SIZE(chunks) == 0) { |
---|
928 | n/a | Py_DECREF(chunks); |
---|
929 | n/a | return data; |
---|
930 | n/a | } |
---|
931 | n/a | Py_DECREF(data); |
---|
932 | n/a | break; |
---|
933 | n/a | } |
---|
934 | n/a | if (!PyBytes_Check(data)) { |
---|
935 | n/a | Py_DECREF(chunks); |
---|
936 | n/a | Py_DECREF(data); |
---|
937 | n/a | PyErr_SetString(PyExc_TypeError, "read() should return bytes"); |
---|
938 | n/a | return NULL; |
---|
939 | n/a | } |
---|
940 | n/a | if (PyBytes_GET_SIZE(data) == 0) { |
---|
941 | n/a | /* EOF */ |
---|
942 | n/a | Py_DECREF(data); |
---|
943 | n/a | break; |
---|
944 | n/a | } |
---|
945 | n/a | r = PyList_Append(chunks, data); |
---|
946 | n/a | Py_DECREF(data); |
---|
947 | n/a | if (r < 0) { |
---|
948 | n/a | Py_DECREF(chunks); |
---|
949 | n/a | return NULL; |
---|
950 | n/a | } |
---|
951 | n/a | } |
---|
952 | n/a | result = _PyBytes_Join(_PyIO_empty_bytes, chunks); |
---|
953 | n/a | Py_DECREF(chunks); |
---|
954 | n/a | return result; |
---|
955 | n/a | } |
---|
956 | n/a | |
---|
957 | n/a | static PyObject * |
---|
958 | n/a | rawiobase_readinto(PyObject *self, PyObject *args) |
---|
959 | n/a | { |
---|
960 | n/a | PyErr_SetNone(PyExc_NotImplementedError); |
---|
961 | n/a | return NULL; |
---|
962 | n/a | } |
---|
963 | n/a | |
---|
964 | n/a | static PyObject * |
---|
965 | n/a | rawiobase_write(PyObject *self, PyObject *args) |
---|
966 | n/a | { |
---|
967 | n/a | PyErr_SetNone(PyExc_NotImplementedError); |
---|
968 | n/a | return NULL; |
---|
969 | n/a | } |
---|
970 | n/a | |
---|
971 | n/a | static PyMethodDef rawiobase_methods[] = { |
---|
972 | n/a | _IO__RAWIOBASE_READ_METHODDEF |
---|
973 | n/a | _IO__RAWIOBASE_READALL_METHODDEF |
---|
974 | n/a | {"readinto", rawiobase_readinto, METH_VARARGS}, |
---|
975 | n/a | {"write", rawiobase_write, METH_VARARGS}, |
---|
976 | n/a | {NULL, NULL} |
---|
977 | n/a | }; |
---|
978 | n/a | |
---|
979 | n/a | PyTypeObject PyRawIOBase_Type = { |
---|
980 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
981 | n/a | "_io._RawIOBase", /*tp_name*/ |
---|
982 | n/a | 0, /*tp_basicsize*/ |
---|
983 | n/a | 0, /*tp_itemsize*/ |
---|
984 | n/a | 0, /*tp_dealloc*/ |
---|
985 | n/a | 0, /*tp_print*/ |
---|
986 | n/a | 0, /*tp_getattr*/ |
---|
987 | n/a | 0, /*tp_setattr*/ |
---|
988 | n/a | 0, /*tp_compare */ |
---|
989 | n/a | 0, /*tp_repr*/ |
---|
990 | n/a | 0, /*tp_as_number*/ |
---|
991 | n/a | 0, /*tp_as_sequence*/ |
---|
992 | n/a | 0, /*tp_as_mapping*/ |
---|
993 | n/a | 0, /*tp_hash */ |
---|
994 | n/a | 0, /*tp_call*/ |
---|
995 | n/a | 0, /*tp_str*/ |
---|
996 | n/a | 0, /*tp_getattro*/ |
---|
997 | n/a | 0, /*tp_setattro*/ |
---|
998 | n/a | 0, /*tp_as_buffer*/ |
---|
999 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_FINALIZE, /*tp_flags*/ |
---|
1000 | n/a | rawiobase_doc, /* tp_doc */ |
---|
1001 | n/a | 0, /* tp_traverse */ |
---|
1002 | n/a | 0, /* tp_clear */ |
---|
1003 | n/a | 0, /* tp_richcompare */ |
---|
1004 | n/a | 0, /* tp_weaklistoffset */ |
---|
1005 | n/a | 0, /* tp_iter */ |
---|
1006 | n/a | 0, /* tp_iternext */ |
---|
1007 | n/a | rawiobase_methods, /* tp_methods */ |
---|
1008 | n/a | 0, /* tp_members */ |
---|
1009 | n/a | 0, /* tp_getset */ |
---|
1010 | n/a | &PyIOBase_Type, /* tp_base */ |
---|
1011 | n/a | 0, /* tp_dict */ |
---|
1012 | n/a | 0, /* tp_descr_get */ |
---|
1013 | n/a | 0, /* tp_descr_set */ |
---|
1014 | n/a | 0, /* tp_dictoffset */ |
---|
1015 | n/a | 0, /* tp_init */ |
---|
1016 | n/a | 0, /* tp_alloc */ |
---|
1017 | n/a | 0, /* tp_new */ |
---|
1018 | n/a | 0, /* tp_free */ |
---|
1019 | n/a | 0, /* tp_is_gc */ |
---|
1020 | n/a | 0, /* tp_bases */ |
---|
1021 | n/a | 0, /* tp_mro */ |
---|
1022 | n/a | 0, /* tp_cache */ |
---|
1023 | n/a | 0, /* tp_subclasses */ |
---|
1024 | n/a | 0, /* tp_weaklist */ |
---|
1025 | n/a | 0, /* tp_del */ |
---|
1026 | n/a | 0, /* tp_version_tag */ |
---|
1027 | n/a | 0, /* tp_finalize */ |
---|
1028 | n/a | }; |
---|