/src/cpython/Modules/_io/iobase.c
Line | Count | Source |
1 | | /* |
2 | | An implementation of the I/O abstract base classes hierarchy |
3 | | as defined by PEP 3116 - "New I/O" |
4 | | |
5 | | Classes defined here: IOBase, RawIOBase. |
6 | | |
7 | | Written by Amaury Forgeot d'Arc and Antoine Pitrou |
8 | | */ |
9 | | |
10 | | |
11 | | #include "Python.h" |
12 | | #include "pycore_call.h" // _PyObject_CallMethod() |
13 | | #include "pycore_fileutils.h" // _PyFile_Flush |
14 | | #include "pycore_long.h" // _PyLong_GetOne() |
15 | | #include "pycore_object.h" // _PyType_HasFeature() |
16 | | #include "pycore_pyerrors.h" // _PyErr_ChainExceptions1() |
17 | | |
18 | | #include <stddef.h> // offsetof() |
19 | | #include "_iomodule.h" |
20 | | |
21 | | /*[clinic input] |
22 | | module _io |
23 | | class _io._IOBase "PyObject *" "clinic_state()->PyIOBase_Type" |
24 | | class _io._RawIOBase "PyObject *" "clinic_state()->PyRawIOBase_Type" |
25 | | [clinic start generated code]*/ |
26 | | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=9006b7802ab8ea85]*/ |
27 | | |
28 | | /* |
29 | | * IOBase class, an abstract class |
30 | | */ |
31 | | |
32 | | typedef struct { |
33 | | PyObject_HEAD |
34 | | |
35 | | } iobase; |
36 | | |
37 | 46.3k | #define iobase_CAST(op) ((iobase *)(op)) |
38 | | |
39 | | PyDoc_STRVAR(iobase_doc, |
40 | | "The abstract base class for all I/O classes.\n" |
41 | | "\n" |
42 | | "This class provides dummy implementations for many methods that\n" |
43 | | "derived classes can override selectively; the default implementations\n" |
44 | | "represent a file that cannot be read, written or seeked.\n" |
45 | | "\n" |
46 | | "Even though IOBase does not declare read, readinto, or write because\n" |
47 | | "their signatures will vary, implementations and clients should\n" |
48 | | "consider those methods part of the interface. Also, implementations\n" |
49 | | "may raise UnsupportedOperation when operations they do not support are\n" |
50 | | "called.\n" |
51 | | "\n" |
52 | | "The basic type used for binary data read from or written to a file is\n" |
53 | | "bytes. Other bytes-like objects are accepted as method arguments too.\n" |
54 | | "In some cases (such as readinto), a writable object is required. Text\n" |
55 | | "I/O classes work with str data.\n" |
56 | | "\n" |
57 | | "Note that calling any method (except additional calls to close(),\n" |
58 | | "which are ignored) on a closed stream should raise a ValueError.\n" |
59 | | "\n" |
60 | | "IOBase (and its subclasses) support the iterator protocol, meaning\n" |
61 | | "that an IOBase object can be iterated over yielding the lines in a\n" |
62 | | "stream.\n" |
63 | | "\n" |
64 | | "IOBase also supports the :keyword:`with` statement. In this example,\n" |
65 | | "fp is closed after the suite of the with statement is complete:\n" |
66 | | "\n" |
67 | | "with open('spam.txt', 'r') as fp:\n" |
68 | | " fp.write('Spam and eggs!')\n"); |
69 | | |
70 | | |
71 | | /* Internal methods */ |
72 | | |
73 | | /* Use this function whenever you want to check the internal `closed` status |
74 | | of the IOBase object rather than the virtual `closed` attribute as returned |
75 | | by whatever subclass. */ |
76 | | |
77 | | static int |
78 | | iobase_is_closed(PyObject *self) |
79 | 8.55M | { |
80 | 8.55M | return PyObject_HasAttrWithError(self, &_Py_ID(__IOBase_closed)); |
81 | 8.55M | } |
82 | | |
83 | | static PyObject * |
84 | | iobase_unsupported(_PyIO_State *state, const char *message) |
85 | 5 | { |
86 | 5 | PyErr_SetString(state->unsupported_operation, message); |
87 | 5 | return NULL; |
88 | 5 | } |
89 | | |
90 | | /* Positioning */ |
91 | | |
92 | | /*[clinic input] |
93 | | _io._IOBase.seek |
94 | | cls: defining_class |
95 | | offset: int(unused=True) |
96 | | The stream position, relative to 'whence'. |
97 | | whence: int(unused=True, c_default='0') = os.SEEK_SET |
98 | | The relative position to seek from. |
99 | | / |
100 | | |
101 | | Change the stream position to the given byte offset. |
102 | | |
103 | | The offset is interpreted relative to the position indicated by |
104 | | whence. Values for whence are: |
105 | | |
106 | | * os.SEEK_SET or 0 -- start of stream (the default); offset should |
107 | | be zero or positive |
108 | | * os.SEEK_CUR or 1 -- current stream position; offset may be |
109 | | negative |
110 | | * os.SEEK_END or 2 -- end of stream; offset is usually negative |
111 | | |
112 | | Return the new absolute position. |
113 | | [clinic start generated code]*/ |
114 | | |
115 | | static PyObject * |
116 | | _io__IOBase_seek_impl(PyObject *self, PyTypeObject *cls, |
117 | | int Py_UNUSED(offset), int Py_UNUSED(whence)) |
118 | | /*[clinic end generated code: output=8bd74ea6538ded53 input=22eaf07a7a0ee289]*/ |
119 | 5 | { |
120 | 5 | _PyIO_State *state = get_io_state_by_cls(cls); |
121 | 5 | return iobase_unsupported(state, "seek"); |
122 | 5 | } |
123 | | |
124 | | /*[clinic input] |
125 | | _io._IOBase.tell |
126 | | |
127 | | Return current stream position. |
128 | | [clinic start generated code]*/ |
129 | | |
130 | | static PyObject * |
131 | | _io__IOBase_tell_impl(PyObject *self) |
132 | | /*[clinic end generated code: output=89a1c0807935abe2 input=04e615fec128801f]*/ |
133 | 1.41M | { |
134 | 1.41M | return _PyObject_CallMethod(self, &_Py_ID(seek), "ii", 0, 1); |
135 | 1.41M | } |
136 | | |
137 | | /*[clinic input] |
138 | | _io._IOBase.truncate |
139 | | cls: defining_class |
140 | | size: object(unused=True) = None |
141 | | / |
142 | | |
143 | | Truncate file to size bytes. |
144 | | |
145 | | File pointer is left unchanged. Size defaults to the current IO |
146 | | position as reported by tell(). Return the new size. |
147 | | [clinic start generated code]*/ |
148 | | |
149 | | static PyObject * |
150 | | _io__IOBase_truncate_impl(PyObject *self, PyTypeObject *cls, |
151 | | PyObject *Py_UNUSED(size)) |
152 | | /*[clinic end generated code: output=2013179bff1fe8ef input=5b3b6ab3c7abd806]*/ |
153 | 0 | { |
154 | 0 | _PyIO_State *state = get_io_state_by_cls(cls); |
155 | 0 | return iobase_unsupported(state, "truncate"); |
156 | 0 | } |
157 | | |
158 | | /* Flush and close methods */ |
159 | | |
160 | | /*[clinic input] |
161 | | _io._IOBase.flush |
162 | | |
163 | | Flush write buffers, if applicable. |
164 | | |
165 | | This is not implemented for read-only and non-blocking streams. |
166 | | [clinic start generated code]*/ |
167 | | |
168 | | static PyObject * |
169 | | _io__IOBase_flush_impl(PyObject *self) |
170 | | /*[clinic end generated code: output=7cef4b4d54656a3b input=773be121abe270aa]*/ |
171 | 1.48M | { |
172 | | /* XXX Should this return the number of bytes written??? */ |
173 | 1.48M | int closed = iobase_is_closed(self); |
174 | | |
175 | 1.48M | if (!closed) { |
176 | 1.48M | Py_RETURN_NONE; |
177 | 1.48M | } |
178 | 0 | if (closed > 0) { |
179 | 0 | PyErr_SetString(PyExc_ValueError, "I/O operation on closed file."); |
180 | 0 | } |
181 | 0 | return NULL; |
182 | 1.48M | } |
183 | | |
184 | | /*[clinic input] |
185 | | @getter |
186 | | _io._IOBase.closed -> bool |
187 | | |
188 | | True if the file is closed. |
189 | | [clinic start generated code]*/ |
190 | | |
191 | | static int |
192 | | _io__IOBase_closed_get_impl(PyObject *self) |
193 | | /*[clinic end generated code: output=561af4ea236d16d9 input=e730fe4ccc4c096c]*/ |
194 | 7.01M | { |
195 | 7.01M | return iobase_is_closed(self); |
196 | 7.01M | } |
197 | | |
198 | | static int |
199 | | iobase_check_closed(PyObject *self) |
200 | 89.3k | { |
201 | 89.3k | PyObject *res; |
202 | 89.3k | int closed; |
203 | | /* This gets the derived attribute, which is *not* __IOBase_closed |
204 | | in most cases! */ |
205 | 89.3k | closed = PyObject_GetOptionalAttr(self, &_Py_ID(closed), &res); |
206 | 89.3k | if (closed > 0) { |
207 | 89.3k | closed = PyObject_IsTrue(res); |
208 | 89.3k | Py_DECREF(res); |
209 | 89.3k | if (closed > 0) { |
210 | 0 | PyErr_SetString(PyExc_ValueError, "I/O operation on closed file."); |
211 | 0 | return -1; |
212 | 0 | } |
213 | 89.3k | } |
214 | 89.3k | return closed; |
215 | 89.3k | } |
216 | | |
217 | | PyObject * |
218 | | _PyIOBase_check_closed(PyObject *self, PyObject *args) |
219 | 0 | { |
220 | 0 | if (iobase_check_closed(self)) { |
221 | 0 | return NULL; |
222 | 0 | } |
223 | 0 | if (args == Py_True) { |
224 | 0 | return Py_None; |
225 | 0 | } |
226 | 0 | Py_RETURN_NONE; |
227 | 0 | } |
228 | | |
229 | | /*[clinic input] |
230 | | _io._IOBase._checkClosed |
231 | | [clinic start generated code]*/ |
232 | | |
233 | | static PyObject * |
234 | | _io__IOBase__checkClosed_impl(PyObject *self) |
235 | | /*[clinic end generated code: output=8fb8412185623f1a input=8d18d2e67b2270bb]*/ |
236 | 0 | { |
237 | 0 | return _PyIOBase_check_closed(self, NULL); |
238 | 0 | } |
239 | | |
240 | | /*[clinic input] |
241 | | _io._IOBase._checkSeekable |
242 | | [clinic start generated code]*/ |
243 | | |
244 | | static PyObject * |
245 | | _io__IOBase__checkSeekable_impl(PyObject *self) |
246 | | /*[clinic end generated code: output=10f09b515a9f4c4f input=479853eded776b0e]*/ |
247 | 0 | { |
248 | 0 | _PyIO_State *state = find_io_state_by_def(Py_TYPE(self)); |
249 | 0 | return _PyIOBase_check_seekable(state, self, NULL); |
250 | 0 | } |
251 | | |
252 | | /*[clinic input] |
253 | | _io._IOBase._checkReadable |
254 | | [clinic start generated code]*/ |
255 | | |
256 | | static PyObject * |
257 | | _io__IOBase__checkReadable_impl(PyObject *self) |
258 | | /*[clinic end generated code: output=33ccab8a7c4550fb input=63fbf50b36772323]*/ |
259 | 0 | { |
260 | 0 | _PyIO_State *state = find_io_state_by_def(Py_TYPE(self)); |
261 | 0 | return _PyIOBase_check_readable(state, self, NULL); |
262 | 0 | } |
263 | | |
264 | | /*[clinic input] |
265 | | _io._IOBase._checkWritable |
266 | | [clinic start generated code]*/ |
267 | | |
268 | | static PyObject * |
269 | | _io__IOBase__checkWritable_impl(PyObject *self) |
270 | | /*[clinic end generated code: output=8cfc6b4b2469d2a3 input=2758c7291cf5911e]*/ |
271 | 0 | { |
272 | 0 | _PyIO_State *state = find_io_state_by_def(Py_TYPE(self)); |
273 | 0 | return _PyIOBase_check_writable(state, self, NULL); |
274 | 0 | } |
275 | | |
276 | | PyObject * |
277 | | _PyIOBase_cannot_pickle(PyObject *self, PyObject *args) |
278 | 0 | { |
279 | 0 | PyErr_Format(PyExc_TypeError, |
280 | 0 | "cannot pickle '%.100s' instances", _PyType_Name(Py_TYPE(self))); |
281 | 0 | return NULL; |
282 | 0 | } |
283 | | |
284 | | /* XXX: IOBase thinks it has to maintain its own internal state in |
285 | | `__IOBase_closed` and call flush() by itself, but it is redundant with |
286 | | whatever behaviour a non-trivial derived class will implement. */ |
287 | | |
288 | | /*[clinic input] |
289 | | _io._IOBase.close |
290 | | |
291 | | Flush and close the IO object. |
292 | | |
293 | | This method has no effect if the file is already closed. |
294 | | [clinic start generated code]*/ |
295 | | |
296 | | static PyObject * |
297 | | _io__IOBase_close_impl(PyObject *self) |
298 | | /*[clinic end generated code: output=63c6a6f57d783d6d input=f4494d5c31dbc6b7]*/ |
299 | 58.6k | { |
300 | 58.6k | int rc1, rc2, closed = iobase_is_closed(self); |
301 | | |
302 | 58.6k | if (closed < 0) { |
303 | 0 | return NULL; |
304 | 0 | } |
305 | 58.6k | if (closed) { |
306 | 0 | Py_RETURN_NONE; |
307 | 0 | } |
308 | | |
309 | 58.6k | rc1 = _PyFile_Flush(self); |
310 | 58.6k | PyObject *exc = PyErr_GetRaisedException(); |
311 | 58.6k | rc2 = PyObject_SetAttr(self, &_Py_ID(__IOBase_closed), Py_True); |
312 | 58.6k | _PyErr_ChainExceptions1(exc); |
313 | 58.6k | if (rc1 < 0 || rc2 < 0) { |
314 | 0 | return NULL; |
315 | 0 | } |
316 | | |
317 | 58.6k | Py_RETURN_NONE; |
318 | 58.6k | } |
319 | | |
320 | | /* Finalization and garbage collection support */ |
321 | | |
322 | | static void |
323 | | iobase_finalize(PyObject *self) |
324 | 92.6k | { |
325 | 92.6k | PyObject *res; |
326 | 92.6k | int closed; |
327 | | |
328 | | /* Save the current exception, if any. */ |
329 | 92.6k | PyObject *exc = PyErr_GetRaisedException(); |
330 | | |
331 | | /* If `closed` doesn't exist or can't be evaluated as bool, then the |
332 | | object is probably in an unusable state, so ignore. */ |
333 | 92.6k | if (PyObject_GetOptionalAttr(self, &_Py_ID(closed), &res) <= 0) { |
334 | 0 | PyErr_Clear(); |
335 | 0 | closed = -1; |
336 | 0 | } |
337 | 92.6k | else { |
338 | 92.6k | closed = PyObject_IsTrue(res); |
339 | 92.6k | Py_DECREF(res); |
340 | 92.6k | if (closed == -1) |
341 | 0 | PyErr_Clear(); |
342 | 92.6k | } |
343 | 92.6k | if (closed == 0) { |
344 | | /* Signal close() that it was called as part of the object |
345 | | finalization process. */ |
346 | 10.1k | if (PyObject_SetAttr(self, &_Py_ID(_finalizing), Py_True)) |
347 | 0 | PyErr_Clear(); |
348 | 10.1k | res = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(close)); |
349 | 10.1k | if (res == NULL) { |
350 | 0 | PyErr_FormatUnraisable("Exception ignored " |
351 | 0 | "while finalizing file %R", self); |
352 | 0 | } |
353 | 10.1k | else { |
354 | 10.1k | Py_DECREF(res); |
355 | 10.1k | } |
356 | 10.1k | } |
357 | | |
358 | | /* Restore the saved exception. */ |
359 | 92.6k | PyErr_SetRaisedException(exc); |
360 | 92.6k | } |
361 | | |
362 | | int |
363 | | _PyIOBase_finalize(PyObject *self) |
364 | 92.6k | { |
365 | 92.6k | int is_zombie; |
366 | | |
367 | | /* If _PyIOBase_finalize() is called from a destructor, we need to |
368 | | resurrect the object as calling close() can invoke arbitrary code. */ |
369 | 92.6k | is_zombie = (Py_REFCNT(self) == 0); |
370 | 92.6k | if (is_zombie) |
371 | 92.6k | return PyObject_CallFinalizerFromDealloc(self); |
372 | 0 | else { |
373 | 0 | PyObject_CallFinalizer(self); |
374 | 0 | return 0; |
375 | 0 | } |
376 | 92.6k | } |
377 | | |
378 | | static int |
379 | | iobase_traverse(PyObject *op, visitproc visit, void *arg) |
380 | 3.98k | { |
381 | 3.98k | iobase *self = iobase_CAST(op); |
382 | 3.98k | Py_VISIT(Py_TYPE(self)); |
383 | 3.98k | return PyObject_VisitManagedDict(op, visit, arg); |
384 | 3.98k | } |
385 | | |
386 | | static int |
387 | | iobase_clear(PyObject *op) |
388 | 0 | { |
389 | 0 | PyObject_ClearManagedDict(op); |
390 | 0 | return 0; |
391 | 0 | } |
392 | | |
393 | | /* Destructor */ |
394 | | |
395 | | static void |
396 | | iobase_dealloc(PyObject *op) |
397 | 42.3k | { |
398 | | /* NOTE: since the managed dict is only cleared below, Python-defined |
399 | | attributes are still available here for close() to use. |
400 | | However, if the derived class declares a __slots__, those slots are |
401 | | already gone. |
402 | | */ |
403 | 42.3k | iobase *self = iobase_CAST(op); |
404 | 42.3k | if (_PyIOBase_finalize(op) < 0) { |
405 | | /* When called from a heap type's dealloc, the type will be |
406 | | decref'ed on return (see e.g. subtype_dealloc in typeobject.c). */ |
407 | 0 | if (_PyType_HasFeature(Py_TYPE(self), Py_TPFLAGS_HEAPTYPE)) { |
408 | 0 | Py_INCREF(Py_TYPE(self)); |
409 | 0 | } |
410 | 0 | return; |
411 | 0 | } |
412 | 42.3k | PyTypeObject *tp = Py_TYPE(self); |
413 | 42.3k | _PyObject_GC_UNTRACK(self); |
414 | 42.3k | PyObject_ClearWeakRefs(op); |
415 | 42.3k | PyObject_ClearManagedDict(op); |
416 | 42.3k | tp->tp_free(self); |
417 | 42.3k | Py_DECREF(tp); |
418 | 42.3k | } |
419 | | |
420 | | /* Inquiry methods */ |
421 | | |
422 | | /*[clinic input] |
423 | | _io._IOBase.seekable |
424 | | |
425 | | Return whether object supports random access. |
426 | | |
427 | | If False, seek(), tell() and truncate() will raise OSError. |
428 | | This method may need to do a test seek(). |
429 | | [clinic start generated code]*/ |
430 | | |
431 | | static PyObject * |
432 | | _io__IOBase_seekable_impl(PyObject *self) |
433 | | /*[clinic end generated code: output=4c24c67f5f32a43d input=b976622f7fdf3063]*/ |
434 | 0 | { |
435 | 0 | Py_RETURN_FALSE; |
436 | 0 | } |
437 | | |
438 | | PyObject * |
439 | | _PyIOBase_check_seekable(_PyIO_State *state, PyObject *self, PyObject *args) |
440 | 1.41M | { |
441 | 1.41M | PyObject *res = PyObject_CallMethodNoArgs(self, &_Py_ID(seekable)); |
442 | 1.41M | if (res == NULL) |
443 | 0 | return NULL; |
444 | 1.41M | if (res != Py_True) { |
445 | 0 | Py_CLEAR(res); |
446 | 0 | iobase_unsupported(state, "File or stream is not seekable."); |
447 | 0 | return NULL; |
448 | 0 | } |
449 | 1.41M | if (args == Py_True) { |
450 | 1.41M | Py_DECREF(res); |
451 | 1.41M | } |
452 | 1.41M | return res; |
453 | 1.41M | } |
454 | | |
455 | | /*[clinic input] |
456 | | _io._IOBase.readable |
457 | | |
458 | | Return whether object was opened for reading. |
459 | | |
460 | | If False, read() will raise OSError. |
461 | | [clinic start generated code]*/ |
462 | | |
463 | | static PyObject * |
464 | | _io__IOBase_readable_impl(PyObject *self) |
465 | | /*[clinic end generated code: output=e48089250686388b input=285b3b866a0ec35f]*/ |
466 | 75 | { |
467 | 75 | Py_RETURN_FALSE; |
468 | 75 | } |
469 | | |
470 | | /* May be called with any object */ |
471 | | PyObject * |
472 | | _PyIOBase_check_readable(_PyIO_State *state, PyObject *self, PyObject *args) |
473 | 7.99k | { |
474 | 7.99k | PyObject *res = PyObject_CallMethodNoArgs(self, &_Py_ID(readable)); |
475 | 7.99k | if (res == NULL) |
476 | 0 | return NULL; |
477 | 7.99k | if (res != Py_True) { |
478 | 0 | Py_CLEAR(res); |
479 | 0 | iobase_unsupported(state, "File or stream is not readable."); |
480 | 0 | return NULL; |
481 | 0 | } |
482 | 7.99k | if (args == Py_True) { |
483 | 7.99k | Py_DECREF(res); |
484 | 7.99k | } |
485 | 7.99k | return res; |
486 | 7.99k | } |
487 | | |
488 | | /*[clinic input] |
489 | | _io._IOBase.writable |
490 | | |
491 | | Return whether object was opened for writing. |
492 | | |
493 | | If False, write() will raise OSError. |
494 | | [clinic start generated code]*/ |
495 | | |
496 | | static PyObject * |
497 | | _io__IOBase_writable_impl(PyObject *self) |
498 | | /*[clinic end generated code: output=406001d0985be14f input=9dcac18a013a05b5]*/ |
499 | 55 | { |
500 | 55 | Py_RETURN_FALSE; |
501 | 55 | } |
502 | | |
503 | | /* May be called with any object */ |
504 | | PyObject * |
505 | | _PyIOBase_check_writable(_PyIO_State *state, PyObject *self, PyObject *args) |
506 | 20.1k | { |
507 | 20.1k | PyObject *res = PyObject_CallMethodNoArgs(self, &_Py_ID(writable)); |
508 | 20.1k | if (res == NULL) |
509 | 0 | return NULL; |
510 | 20.1k | if (res != Py_True) { |
511 | 0 | Py_CLEAR(res); |
512 | 0 | iobase_unsupported(state, "File or stream is not writable."); |
513 | 0 | return NULL; |
514 | 0 | } |
515 | 20.1k | if (args == Py_True) { |
516 | 20.1k | Py_DECREF(res); |
517 | 20.1k | } |
518 | 20.1k | return res; |
519 | 20.1k | } |
520 | | |
521 | | /* Context manager */ |
522 | | |
523 | | /*[clinic input] |
524 | | _io._IOBase.__enter__ |
525 | | |
526 | | Context management protocol. Returns the stream itself. |
527 | | [clinic start generated code]*/ |
528 | | |
529 | | static PyObject * |
530 | | _io__IOBase___enter___impl(PyObject *self) |
531 | | /*[clinic end generated code: output=d1e8e5b58bde3680 input=92e2d5e34ee714e5]*/ |
532 | 42.7k | { |
533 | 42.7k | if (iobase_check_closed(self)) |
534 | 0 | return NULL; |
535 | | |
536 | 42.7k | return Py_NewRef(self); |
537 | 42.7k | } |
538 | | |
539 | | /*[clinic input] |
540 | | _io._IOBase.__exit__ |
541 | | *args: tuple |
542 | | |
543 | | Context management protocol. Calls close(). |
544 | | [clinic start generated code]*/ |
545 | | |
546 | | static PyObject * |
547 | | _io__IOBase___exit___impl(PyObject *self, PyObject *args) |
548 | | /*[clinic end generated code: output=452f3e33a34e2c5c input=fd3f46a32773a170]*/ |
549 | 42.7k | { |
550 | 42.7k | return PyObject_CallMethodNoArgs(self, &_Py_ID(close)); |
551 | 42.7k | } |
552 | | |
553 | | /* Lower-level APIs */ |
554 | | |
555 | | /* XXX Should these be present even if unimplemented? */ |
556 | | |
557 | | /*[clinic input] |
558 | | _io._IOBase.fileno |
559 | | cls: defining_class |
560 | | / |
561 | | |
562 | | Return underlying file descriptor if one exists. |
563 | | |
564 | | Raise OSError if the IO object does not use a file descriptor. |
565 | | [clinic start generated code]*/ |
566 | | |
567 | | static PyObject * |
568 | | _io__IOBase_fileno_impl(PyObject *self, PyTypeObject *cls) |
569 | | /*[clinic end generated code: output=7caaa32a6f4ada3d input=1927c8bea5c85099]*/ |
570 | 0 | { |
571 | 0 | _PyIO_State *state = get_io_state_by_cls(cls); |
572 | 0 | return iobase_unsupported(state, "fileno"); |
573 | 0 | } |
574 | | |
575 | | /*[clinic input] |
576 | | _io._IOBase.isatty |
577 | | |
578 | | Return whether this is an 'interactive' stream. |
579 | | |
580 | | Return False if it can't be determined. |
581 | | [clinic start generated code]*/ |
582 | | |
583 | | static PyObject * |
584 | | _io__IOBase_isatty_impl(PyObject *self) |
585 | | /*[clinic end generated code: output=60cab77cede41cdd input=9ef76530d368458b]*/ |
586 | 0 | { |
587 | 0 | if (iobase_check_closed(self)) |
588 | 0 | return NULL; |
589 | 0 | Py_RETURN_FALSE; |
590 | 0 | } |
591 | | |
592 | | /* Readline(s) and writelines */ |
593 | | |
594 | | /*[clinic input] |
595 | | _io._IOBase.readline |
596 | | size as limit: Py_ssize_t(accept={int, NoneType}) = -1 |
597 | | / |
598 | | |
599 | | Read and return a line from the stream. |
600 | | |
601 | | If size is specified, at most size bytes will be read. |
602 | | |
603 | | The line terminator is always b'\n' for binary files; for text |
604 | | files, the newlines argument to open can be used to select the line |
605 | | terminator(s) recognized. |
606 | | [clinic start generated code]*/ |
607 | | |
608 | | static PyObject * |
609 | | _io__IOBase_readline_impl(PyObject *self, Py_ssize_t limit) |
610 | | /*[clinic end generated code: output=4479f79b58187840 input=d0c596794e877bff]*/ |
611 | 0 | { |
612 | | /* For backwards compatibility, a (slowish) readline(). */ |
613 | |
|
614 | 0 | PyObject *peek; |
615 | |
|
616 | 0 | if (PyObject_GetOptionalAttr(self, &_Py_ID(peek), &peek) < 0) { |
617 | 0 | return NULL; |
618 | 0 | } |
619 | | |
620 | 0 | PyBytesWriter *writer = PyBytesWriter_Create(0); |
621 | 0 | if (writer == NULL) { |
622 | 0 | Py_XDECREF(peek); |
623 | 0 | return NULL; |
624 | 0 | } |
625 | | |
626 | 0 | while (limit < 0 || PyBytesWriter_GetSize(writer) < limit) { |
627 | 0 | Py_ssize_t nreadahead = 1; |
628 | 0 | PyObject *b; |
629 | |
|
630 | 0 | if (peek != NULL) { |
631 | 0 | PyObject *readahead = PyObject_CallOneArg(peek, _PyLong_GetOne()); |
632 | 0 | if (readahead == NULL) { |
633 | | /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() |
634 | | when EINTR occurs so we needn't do it ourselves. */ |
635 | 0 | if (_PyIO_trap_eintr()) { |
636 | 0 | continue; |
637 | 0 | } |
638 | 0 | goto fail; |
639 | 0 | } |
640 | 0 | if (!PyBytes_Check(readahead)) { |
641 | 0 | PyErr_Format(PyExc_OSError, |
642 | 0 | "peek() should have returned a bytes object, " |
643 | 0 | "not '%.200s'", Py_TYPE(readahead)->tp_name); |
644 | 0 | Py_DECREF(readahead); |
645 | 0 | goto fail; |
646 | 0 | } |
647 | 0 | if (PyBytes_GET_SIZE(readahead) > 0) { |
648 | 0 | Py_ssize_t n = 0; |
649 | 0 | const char *buf = PyBytes_AS_STRING(readahead); |
650 | 0 | if (limit >= 0) { |
651 | 0 | Py_ssize_t remaining = limit - PyBytesWriter_GetSize(writer); |
652 | 0 | do { |
653 | 0 | if (n >= PyBytes_GET_SIZE(readahead) || n >= remaining) |
654 | 0 | break; |
655 | 0 | if (buf[n++] == '\n') |
656 | 0 | break; |
657 | 0 | } while (1); |
658 | 0 | } |
659 | 0 | else { |
660 | 0 | do { |
661 | 0 | if (n >= PyBytes_GET_SIZE(readahead)) |
662 | 0 | break; |
663 | 0 | if (buf[n++] == '\n') |
664 | 0 | break; |
665 | 0 | } while (1); |
666 | 0 | } |
667 | 0 | nreadahead = n; |
668 | 0 | } |
669 | 0 | Py_DECREF(readahead); |
670 | 0 | } |
671 | | |
672 | 0 | b = _PyObject_CallMethod(self, &_Py_ID(read), "n", nreadahead); |
673 | 0 | if (b == NULL) { |
674 | | /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() |
675 | | when EINTR occurs so we needn't do it ourselves. */ |
676 | 0 | if (_PyIO_trap_eintr()) { |
677 | 0 | continue; |
678 | 0 | } |
679 | 0 | goto fail; |
680 | 0 | } |
681 | 0 | if (!PyBytes_Check(b)) { |
682 | 0 | PyErr_Format(PyExc_OSError, |
683 | 0 | "read() should have returned a bytes object, " |
684 | 0 | "not '%.200s'", Py_TYPE(b)->tp_name); |
685 | 0 | Py_DECREF(b); |
686 | 0 | goto fail; |
687 | 0 | } |
688 | 0 | if (PyBytes_GET_SIZE(b) == 0) { |
689 | 0 | Py_DECREF(b); |
690 | 0 | break; |
691 | 0 | } |
692 | | |
693 | 0 | if (PyBytesWriter_WriteBytes(writer, |
694 | 0 | PyBytes_AS_STRING(b), |
695 | 0 | PyBytes_GET_SIZE(b)) < 0) { |
696 | 0 | Py_DECREF(b); |
697 | 0 | goto fail; |
698 | 0 | } |
699 | | |
700 | 0 | Py_DECREF(b); |
701 | |
|
702 | 0 | char *data = PyBytesWriter_GetData(writer); |
703 | 0 | if (data[PyBytesWriter_GetSize(writer) - 1] == '\n') |
704 | 0 | break; |
705 | 0 | } |
706 | | |
707 | 0 | Py_XDECREF(peek); |
708 | 0 | return PyBytesWriter_Finish(writer); |
709 | 0 | fail: |
710 | 0 | Py_XDECREF(peek); |
711 | 0 | PyBytesWriter_Discard(writer); |
712 | 0 | return NULL; |
713 | 0 | } |
714 | | |
715 | | static PyObject * |
716 | | iobase_iter(PyObject *self) |
717 | 46.6k | { |
718 | 46.6k | if (iobase_check_closed(self)) |
719 | 0 | return NULL; |
720 | | |
721 | 46.6k | return Py_NewRef(self); |
722 | 46.6k | } |
723 | | |
724 | | static PyObject * |
725 | | iobase_iternext(PyObject *self) |
726 | 0 | { |
727 | 0 | PyObject *line = PyObject_CallMethodNoArgs(self, &_Py_ID(readline)); |
728 | |
|
729 | 0 | if (line == NULL) |
730 | 0 | return NULL; |
731 | | |
732 | 0 | if (PyObject_Size(line) <= 0) { |
733 | | /* Error or empty */ |
734 | 0 | Py_DECREF(line); |
735 | 0 | return NULL; |
736 | 0 | } |
737 | | |
738 | 0 | return line; |
739 | 0 | } |
740 | | |
741 | | /*[clinic input] |
742 | | _io._IOBase.readlines |
743 | | hint: Py_ssize_t(accept={int, NoneType}) = -1 |
744 | | / |
745 | | |
746 | | Return a list of lines from the stream. |
747 | | |
748 | | hint can be specified to control the number of lines read: no more |
749 | | lines will be read if the total size (in bytes/characters) of all |
750 | | lines so far exceeds hint. |
751 | | [clinic start generated code]*/ |
752 | | |
753 | | static PyObject * |
754 | | _io__IOBase_readlines_impl(PyObject *self, Py_ssize_t hint) |
755 | | /*[clinic end generated code: output=2f50421677fa3dea input=9400c786ea9dc416]*/ |
756 | 40.0k | { |
757 | 40.0k | Py_ssize_t length = 0; |
758 | 40.0k | PyObject *result, *it = NULL; |
759 | | |
760 | 40.0k | result = PyList_New(0); |
761 | 40.0k | if (result == NULL) |
762 | 0 | return NULL; |
763 | | |
764 | 40.0k | if (hint <= 0) { |
765 | | /* XXX special-casing this made sense in the Python version in order |
766 | | to remove the bytecode interpretation overhead, but it could |
767 | | probably be removed here. */ |
768 | 40.0k | PyObject *ret = PyObject_CallMethodObjArgs(result, &_Py_ID(extend), |
769 | 40.0k | self, NULL); |
770 | 40.0k | if (ret == NULL) { |
771 | 0 | goto error; |
772 | 0 | } |
773 | 40.0k | Py_DECREF(ret); |
774 | 40.0k | return result; |
775 | 40.0k | } |
776 | | |
777 | 0 | it = PyObject_GetIter(self); |
778 | 0 | if (it == NULL) { |
779 | 0 | goto error; |
780 | 0 | } |
781 | | |
782 | 0 | while (1) { |
783 | 0 | Py_ssize_t line_length; |
784 | 0 | PyObject *line = PyIter_Next(it); |
785 | 0 | if (line == NULL) { |
786 | 0 | if (PyErr_Occurred()) { |
787 | 0 | goto error; |
788 | 0 | } |
789 | 0 | else |
790 | 0 | break; /* StopIteration raised */ |
791 | 0 | } |
792 | | |
793 | 0 | if (PyList_Append(result, line) < 0) { |
794 | 0 | Py_DECREF(line); |
795 | 0 | goto error; |
796 | 0 | } |
797 | 0 | line_length = PyObject_Size(line); |
798 | 0 | Py_DECREF(line); |
799 | 0 | if (line_length < 0) { |
800 | 0 | goto error; |
801 | 0 | } |
802 | 0 | if (line_length > hint - length) |
803 | 0 | break; |
804 | 0 | length += line_length; |
805 | 0 | } |
806 | | |
807 | 0 | Py_DECREF(it); |
808 | 0 | return result; |
809 | | |
810 | 0 | error: |
811 | 0 | Py_XDECREF(it); |
812 | 0 | Py_DECREF(result); |
813 | 0 | return NULL; |
814 | 0 | } |
815 | | |
816 | | /*[clinic input] |
817 | | _io._IOBase.writelines |
818 | | lines: object |
819 | | / |
820 | | |
821 | | Write a list of lines to stream. |
822 | | |
823 | | Line separators are not added, so it is usual for each of the |
824 | | lines provided to have a line separator at the end. |
825 | | [clinic start generated code]*/ |
826 | | |
827 | | static PyObject * |
828 | | _io__IOBase_writelines(PyObject *self, PyObject *lines) |
829 | | /*[clinic end generated code: output=976eb0a9b60a6628 input=cac3fc8864183359]*/ |
830 | 0 | { |
831 | 0 | PyObject *iter, *res; |
832 | |
|
833 | 0 | if (iobase_check_closed(self)) |
834 | 0 | return NULL; |
835 | | |
836 | 0 | iter = PyObject_GetIter(lines); |
837 | 0 | if (iter == NULL) |
838 | 0 | return NULL; |
839 | | |
840 | 0 | while (1) { |
841 | 0 | PyObject *line = PyIter_Next(iter); |
842 | 0 | if (line == NULL) { |
843 | 0 | if (PyErr_Occurred()) { |
844 | 0 | Py_DECREF(iter); |
845 | 0 | return NULL; |
846 | 0 | } |
847 | 0 | else |
848 | 0 | break; /* Stop Iteration */ |
849 | 0 | } |
850 | | |
851 | 0 | res = NULL; |
852 | 0 | do { |
853 | 0 | res = PyObject_CallMethodObjArgs(self, &_Py_ID(write), line, NULL); |
854 | 0 | } while (res == NULL && _PyIO_trap_eintr()); |
855 | 0 | Py_DECREF(line); |
856 | 0 | if (res == NULL) { |
857 | 0 | Py_DECREF(iter); |
858 | 0 | return NULL; |
859 | 0 | } |
860 | 0 | Py_DECREF(res); |
861 | 0 | } |
862 | 0 | Py_DECREF(iter); |
863 | 0 | Py_RETURN_NONE; |
864 | 0 | } |
865 | | |
866 | | #define clinic_state() (find_io_state_by_def(Py_TYPE(self))) |
867 | | #include "clinic/iobase.c.h" |
868 | | #undef clinic_state |
869 | | |
870 | | static PyMethodDef iobase_methods[] = { |
871 | | _IO__IOBASE_SEEK_METHODDEF |
872 | | _IO__IOBASE_TELL_METHODDEF |
873 | | _IO__IOBASE_TRUNCATE_METHODDEF |
874 | | _IO__IOBASE_FLUSH_METHODDEF |
875 | | _IO__IOBASE_CLOSE_METHODDEF |
876 | | |
877 | | _IO__IOBASE_SEEKABLE_METHODDEF |
878 | | _IO__IOBASE_READABLE_METHODDEF |
879 | | _IO__IOBASE_WRITABLE_METHODDEF |
880 | | |
881 | | _IO__IOBASE__CHECKCLOSED_METHODDEF |
882 | | _IO__IOBASE__CHECKSEEKABLE_METHODDEF |
883 | | _IO__IOBASE__CHECKREADABLE_METHODDEF |
884 | | _IO__IOBASE__CHECKWRITABLE_METHODDEF |
885 | | |
886 | | _IO__IOBASE_FILENO_METHODDEF |
887 | | _IO__IOBASE_ISATTY_METHODDEF |
888 | | |
889 | | _IO__IOBASE___ENTER___METHODDEF |
890 | | _IO__IOBASE___EXIT___METHODDEF |
891 | | |
892 | | _IO__IOBASE_READLINE_METHODDEF |
893 | | _IO__IOBASE_READLINES_METHODDEF |
894 | | _IO__IOBASE_WRITELINES_METHODDEF |
895 | | |
896 | | {NULL, NULL} |
897 | | }; |
898 | | |
899 | | static PyGetSetDef iobase_getset[] = { |
900 | | {"__dict__", PyObject_GenericGetDict, NULL, NULL}, |
901 | | _IO__IOBASE_CLOSED_GETSETDEF |
902 | | {NULL} |
903 | | }; |
904 | | |
905 | | |
906 | | static PyType_Slot iobase_slots[] = { |
907 | | {Py_tp_dealloc, iobase_dealloc}, |
908 | | {Py_tp_doc, (void *)iobase_doc}, |
909 | | {Py_tp_traverse, iobase_traverse}, |
910 | | {Py_tp_clear, iobase_clear}, |
911 | | {Py_tp_iter, iobase_iter}, |
912 | | {Py_tp_iternext, iobase_iternext}, |
913 | | {Py_tp_methods, iobase_methods}, |
914 | | {Py_tp_getset, iobase_getset}, |
915 | | {Py_tp_finalize, iobase_finalize}, |
916 | | {0, NULL}, |
917 | | }; |
918 | | |
919 | | PyType_Spec _Py_iobase_spec = { |
920 | | .name = "_io._IOBase", |
921 | | .basicsize = sizeof(iobase), |
922 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | |
923 | | Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_MANAGED_DICT | |
924 | | Py_TPFLAGS_MANAGED_WEAKREF), |
925 | | .slots = iobase_slots, |
926 | | }; |
927 | | |
928 | | /* |
929 | | * RawIOBase class, Inherits from IOBase. |
930 | | */ |
931 | | PyDoc_STRVAR(rawiobase_doc, |
932 | | "Base class for raw binary I/O."); |
933 | | |
934 | | /* |
935 | | * The read() method is implemented by calling readinto(); derived classes |
936 | | * that want to support read() only need to implement readinto() as a |
937 | | * primitive operation. In general, readinto() can be more efficient than |
938 | | * read(). |
939 | | * |
940 | | * (It would be tempting to also provide an implementation of readinto() in |
941 | | * terms of read(), in case the latter is a more suitable primitive operation, |
942 | | * but that would lead to nasty recursion in case a subclass doesn't implement |
943 | | * either.) |
944 | | */ |
945 | | |
946 | | /*[clinic input] |
947 | | _io._RawIOBase.read |
948 | | size as n: Py_ssize_t = -1 |
949 | | / |
950 | | [clinic start generated code]*/ |
951 | | |
952 | | static PyObject * |
953 | | _io__RawIOBase_read_impl(PyObject *self, Py_ssize_t n) |
954 | | /*[clinic end generated code: output=6cdeb731e3c9f13c input=b6d0dcf6417d1374]*/ |
955 | 0 | { |
956 | 0 | PyObject *b, *res; |
957 | |
|
958 | 0 | if (n < 0) { |
959 | 0 | return PyObject_CallMethodNoArgs(self, &_Py_ID(readall)); |
960 | 0 | } |
961 | | |
962 | 0 | b = PyByteArray_FromStringAndSize(NULL, n); |
963 | 0 | if (b == NULL) { |
964 | 0 | return NULL; |
965 | 0 | } |
966 | | |
967 | 0 | res = PyObject_CallMethodObjArgs(self, &_Py_ID(readinto), b, NULL); |
968 | 0 | if (res == NULL || res == Py_None) { |
969 | 0 | goto cleanup; |
970 | 0 | } |
971 | | |
972 | 0 | Py_ssize_t bytes_filled = PyNumber_AsSsize_t(res, PyExc_ValueError); |
973 | 0 | Py_CLEAR(res); |
974 | 0 | if (bytes_filled == -1 && PyErr_Occurred()) { |
975 | 0 | goto cleanup; |
976 | 0 | } |
977 | 0 | if (bytes_filled < 0 || bytes_filled > n) { |
978 | 0 | PyErr_Format(PyExc_ValueError, |
979 | 0 | "readinto returned %zd outside buffer size %zd", |
980 | 0 | bytes_filled, n); |
981 | 0 | goto cleanup; |
982 | 0 | } |
983 | 0 | if (PyByteArray_Resize(b, bytes_filled) < 0) { |
984 | 0 | goto cleanup; |
985 | 0 | } |
986 | 0 | res = PyObject_CallMethodNoArgs(b, &_Py_ID(take_bytes)); |
987 | |
|
988 | 0 | cleanup: |
989 | 0 | Py_DECREF(b); |
990 | 0 | return res; |
991 | 0 | } |
992 | | |
993 | | |
994 | | /*[clinic input] |
995 | | _io._RawIOBase.readall |
996 | | |
997 | | Read until EOF, using multiple read() call. |
998 | | [clinic start generated code]*/ |
999 | | |
1000 | | static PyObject * |
1001 | | _io__RawIOBase_readall_impl(PyObject *self) |
1002 | | /*[clinic end generated code: output=1987b9ce929425a0 input=688874141213622a]*/ |
1003 | 0 | { |
1004 | 0 | PyBytesWriter *writer = PyBytesWriter_Create(0); |
1005 | 0 | if (writer == NULL) { |
1006 | 0 | return NULL; |
1007 | 0 | } |
1008 | | |
1009 | 0 | while (1) { |
1010 | 0 | PyObject *data = _PyObject_CallMethod(self, &_Py_ID(read), |
1011 | 0 | "i", DEFAULT_BUFFER_SIZE); |
1012 | 0 | if (!data) { |
1013 | | /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() |
1014 | | when EINTR occurs so we needn't do it ourselves. */ |
1015 | 0 | if (_PyIO_trap_eintr()) { |
1016 | 0 | continue; |
1017 | 0 | } |
1018 | 0 | PyBytesWriter_Discard(writer); |
1019 | 0 | return NULL; |
1020 | 0 | } |
1021 | 0 | if (data == Py_None) { |
1022 | 0 | if (PyBytesWriter_GetSize(writer) == 0) { |
1023 | 0 | PyBytesWriter_Discard(writer); |
1024 | 0 | return data; |
1025 | 0 | } |
1026 | 0 | Py_DECREF(data); |
1027 | 0 | break; |
1028 | 0 | } |
1029 | 0 | if (!PyBytes_Check(data)) { |
1030 | 0 | Py_DECREF(data); |
1031 | 0 | PyErr_SetString(PyExc_TypeError, "read() should return bytes"); |
1032 | 0 | PyBytesWriter_Discard(writer); |
1033 | 0 | return NULL; |
1034 | 0 | } |
1035 | 0 | if (PyBytes_GET_SIZE(data) == 0) { |
1036 | | /* EOF */ |
1037 | 0 | Py_DECREF(data); |
1038 | 0 | break; |
1039 | 0 | } |
1040 | 0 | if (PyBytesWriter_WriteBytes(writer, |
1041 | 0 | PyBytes_AS_STRING(data), |
1042 | 0 | PyBytes_GET_SIZE(data)) < 0) { |
1043 | 0 | Py_DECREF(data); |
1044 | 0 | PyBytesWriter_Discard(writer); |
1045 | 0 | return NULL; |
1046 | 0 | } |
1047 | 0 | Py_DECREF(data); |
1048 | 0 | } |
1049 | 0 | return PyBytesWriter_Finish(writer); |
1050 | 0 | } |
1051 | | |
1052 | | /*[clinic input] |
1053 | | _io._RawIOBase.readinto |
1054 | | buffer: object |
1055 | | / |
1056 | | [clinic start generated code]*/ |
1057 | | |
1058 | | static PyObject * |
1059 | | _io__RawIOBase_readinto(PyObject *self, PyObject *buffer) |
1060 | | /*[clinic end generated code: output=081b8cdfaf3a40ff input=99d4bea2acd659c8]*/ |
1061 | 0 | { |
1062 | 0 | PyErr_SetNone(PyExc_NotImplementedError); |
1063 | 0 | return NULL; |
1064 | 0 | } |
1065 | | |
1066 | | /*[clinic input] |
1067 | | _io._RawIOBase.write |
1068 | | buffer: object |
1069 | | / |
1070 | | [clinic start generated code]*/ |
1071 | | |
1072 | | static PyObject * |
1073 | | _io__RawIOBase_write(PyObject *self, PyObject *buffer) |
1074 | | /*[clinic end generated code: output=7add3f2c8715c3a8 input=e6a1534adb876fe2]*/ |
1075 | 0 | { |
1076 | 0 | PyErr_SetNone(PyExc_NotImplementedError); |
1077 | | return NULL; |
1078 | 0 | } |
1079 | | |
1080 | | static PyMethodDef rawiobase_methods[] = { |
1081 | | _IO__RAWIOBASE_READ_METHODDEF |
1082 | | _IO__RAWIOBASE_READALL_METHODDEF |
1083 | | _IO__RAWIOBASE_READINTO_METHODDEF |
1084 | | _IO__RAWIOBASE_WRITE_METHODDEF |
1085 | | {NULL, NULL} |
1086 | | }; |
1087 | | |
1088 | | static PyType_Slot rawiobase_slots[] = { |
1089 | | {Py_tp_doc, (void *)rawiobase_doc}, |
1090 | | {Py_tp_methods, rawiobase_methods}, |
1091 | | {0, NULL}, |
1092 | | }; |
1093 | | |
1094 | | /* Do not set Py_TPFLAGS_HAVE_GC so that tp_traverse and tp_clear are inherited */ |
1095 | | PyType_Spec _Py_rawiobase_spec = { |
1096 | | .name = "_io._RawIOBase", |
1097 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
1098 | | Py_TPFLAGS_IMMUTABLETYPE), |
1099 | | .slots = rawiobase_slots, |
1100 | | }; |