Debian Codegraph @

Modules/_io/iobase.c

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