ยปCore Development>Code coverage>Modules/_io/iobase.c

Python code coverage for Modules/_io/iobase.c

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