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