Back to index

python3.2  3.2.2
Classes | Defines | Functions | Variables
iobase.c File Reference
#include "Python.h"
#include "structmember.h"
#include "_iomodule.h"

Go to the source code of this file.

Classes

struct  iobase

Defines

#define PY_SSIZE_T_CLEAN
#define IS_CLOSED(self)   PyObject_HasAttrString(self, "__IOBase_closed")

Functions

 PyDoc_STRVAR (iobase_doc,"The abstract base class for all I/O classes, acting on streams of\n""bytes. There is no public constructor.\n""\n""This class provides dummy implementations for many methods that\n""derived classes can override selectively; the default implementations\n""represent a file that cannot be read, written or seeked.\n""\n""Even though IOBase does not declare read, readinto, or write because\n""their signatures will vary, implementations and clients should\n""consider those methods part of the interface. Also, implementations\n""may raise UnsupportedOperation when operations they do not support are\n""called.\n""\n""The basic type used for binary data read from or written to a file is\n""bytes. bytearrays are accepted too, and in some cases (such as\n""readinto) needed. Text I/O classes work with str data.\n""\n""Note that calling any method (even inquiries) on a closed stream is\n""undefined. Implementations may raise IOError in this case.\n""\n""IOBase (and its subclasses) support the iterator protocol, meaning\n""that an IOBase object can be iterated over yielding the lines in a\n""stream.\n""\n""IOBase also supports the :keyword:`with` statement. In this example,\n""fp is closed after the suite of the with statement is complete:\n""\n""with open('spam.txt', 'r') as fp:\n"" fp.write('Spam and eggs!')\n")
static PyObjectiobase_unsupported (const char *message)
 PyDoc_STRVAR (iobase_seek_doc,"Change stream position.\n""\n""Change the stream position to byte offset offset. offset is\n""interpreted relative to the position indicated by whence. Values\n""for whence are:\n""\n""* 0 -- start of stream (the default); offset should be zero or positive\n""* 1 -- current stream position; offset may be negative\n""* 2 -- end of stream; offset is usually negative\n""\n""Return the new absolute position.")
static PyObjectiobase_seek (PyObject *self, PyObject *args)
 PyDoc_STRVAR (iobase_tell_doc,"Return current stream position.")
static PyObjectiobase_tell (PyObject *self, PyObject *args)
 PyDoc_STRVAR (iobase_truncate_doc,"Truncate file to size bytes.\n""\n""File pointer is left unchanged. Size defaults to the current IO\n""position as reported by tell(). Returns the new size.")
static PyObjectiobase_truncate (PyObject *self, PyObject *args)
 PyDoc_STRVAR (iobase_flush_doc,"Flush write buffers, if applicable.\n""\n""This is not implemented for read-only and non-blocking streams.\n")
static PyObjectiobase_flush (PyObject *self, PyObject *args)
 PyDoc_STRVAR (iobase_close_doc,"Flush and close the IO object.\n""\n""This method has no effect if the file is already closed.\n")
static int iobase_closed (PyObject *self)
static PyObjectiobase_closed_get (PyObject *self, void *context)
PyObject_PyIOBase_check_closed (PyObject *self, PyObject *args)
static PyObjectiobase_close (PyObject *self, PyObject *args)
int _PyIOBase_finalize (PyObject *self)
static int iobase_traverse (iobase *self, visitproc visit, void *arg)
static int iobase_clear (iobase *self)
static void iobase_dealloc (iobase *self)
 PyDoc_STRVAR (iobase_seekable_doc,"Return whether object supports random access.\n""\n""If False, seek(), tell() and truncate() will raise UnsupportedOperation.\n""This method may need to do a test seek().")
static PyObjectiobase_seekable (PyObject *self, PyObject *args)
PyObject_PyIOBase_check_seekable (PyObject *self, PyObject *args)
 PyDoc_STRVAR (iobase_readable_doc,"Return whether object was opened for reading.\n""\n""If False, read() will raise UnsupportedOperation.")
static PyObjectiobase_readable (PyObject *self, PyObject *args)
PyObject_PyIOBase_check_readable (PyObject *self, PyObject *args)
 PyDoc_STRVAR (iobase_writable_doc,"Return whether object was opened for writing.\n""\n""If False, write() will raise UnsupportedOperation.")
static PyObjectiobase_writable (PyObject *self, PyObject *args)
PyObject_PyIOBase_check_writable (PyObject *self, PyObject *args)
static PyObjectiobase_enter (PyObject *self, PyObject *args)
static PyObjectiobase_exit (PyObject *self, PyObject *args)
 PyDoc_STRVAR (iobase_fileno_doc,"Returns underlying file descriptor if one exists.\n""\n""An IOError is raised if the IO object does not use a file descriptor.\n")
static PyObjectiobase_fileno (PyObject *self, PyObject *args)
 PyDoc_STRVAR (iobase_isatty_doc,"Return whether this is an 'interactive' stream.\n""\n""Return False if it can't be determined.\n")
static PyObjectiobase_isatty (PyObject *self, PyObject *args)
 PyDoc_STRVAR (iobase_readline_doc,"Read and return a line from the stream.\n""\n""If limit is specified, at most limit bytes will be read.\n""\n""The line terminator is always b'\n' for binary files; for text\n""files, the newlines argument to open can be used to select the line\n""terminator(s) recognized.\n")
static PyObjectiobase_readline (PyObject *self, PyObject *args)
static PyObjectiobase_iter (PyObject *self)
static PyObjectiobase_iternext (PyObject *self)
 PyDoc_STRVAR (iobase_readlines_doc,"Return a list of lines from the stream.\n""\n""hint can be specified to control the number of lines read: no more\n""lines will be read if the total size (in bytes/characters) of all\n""lines so far exceeds hint.")
static PyObjectiobase_readlines (PyObject *self, PyObject *args)
static PyObjectiobase_writelines (PyObject *self, PyObject *args)
 PyDoc_STRVAR (rawiobase_doc,"Base class for raw binary I/O.")
static PyObjectrawiobase_read (PyObject *self, PyObject *args)
 PyDoc_STRVAR (rawiobase_readall_doc,"Read until EOF, using multiple read() call.")
static PyObjectrawiobase_readall (PyObject *self, PyObject *args)

Variables

static PyMethodDef iobase_methods []
static PyGetSetDef iobase_getset []
PyTypeObject PyIOBase_Type
static PyMethodDef rawiobase_methods []
PyTypeObject PyRawIOBase_Type

Class Documentation

struct iobase

Definition at line 20 of file iobase.c.

Collaboration diagram for iobase:
Class Members
PyObject_HEAD PyObject * dict
PyObject * weakreflist

Define Documentation

#define IS_CLOSED (   self)    PyObject_HasAttrString(self, "__IOBase_closed")

Definition at line 62 of file iobase.c.

Definition at line 11 of file iobase.c.


Function Documentation

PyObject* _PyIOBase_check_closed ( PyObject self,
PyObject args 
)

Definition at line 160 of file iobase.c.

{
    if (iobase_closed(self)) {
        PyErr_SetString(PyExc_ValueError, "I/O operation on closed file.");
        return NULL;
    }
    if (args == Py_True)
        return Py_None;
    else
        Py_RETURN_NONE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 343 of file iobase.c.

{
    PyObject *res  = PyObject_CallMethodObjArgs(self, _PyIO_str_readable, NULL);
    if (res == NULL)
        return NULL;
    if (res != Py_True) {
        Py_CLEAR(res);
        iobase_unsupported("File or stream is not readable.");
        return NULL;
    }
    if (args == Py_True) {
        Py_DECREF(res);
    }
    return res;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 314 of file iobase.c.

{
    PyObject *res  = PyObject_CallMethodObjArgs(self, _PyIO_str_seekable, NULL);
    if (res == NULL)
        return NULL;
    if (res != Py_True) {
        Py_CLEAR(res);
        iobase_unsupported("File or stream is not seekable.");
        return NULL;
    }
    if (args == Py_True) {
        Py_DECREF(res);
    }
    return res;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 372 of file iobase.c.

{
    PyObject *res  = PyObject_CallMethodObjArgs(self, _PyIO_str_writable, NULL);
    if (res == NULL)
        return NULL;
    if (res != Py_True) {
        Py_CLEAR(res);
        iobase_unsupported("File or stream is not writable.");
        return NULL;
    }
    if (args == Py_True) {
        Py_DECREF(res);
    }
    return res;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 196 of file iobase.c.

{
    PyObject *res;
    PyObject *tp, *v, *tb;
    int closed = 1;
    int is_zombie;

    /* If _PyIOBase_finalize() is called from a destructor, we need to
       resurrect the object as calling close() can invoke arbitrary code. */
    is_zombie = (Py_REFCNT(self) == 0);
    if (is_zombie) {
        ++Py_REFCNT(self);
    }
    PyErr_Fetch(&tp, &v, &tb);
    /* If `closed` doesn't exist or can't be evaluated as bool, then the
       object is probably in an unusable state, so ignore. */
    res = PyObject_GetAttr(self, _PyIO_str_closed);
    if (res == NULL)
        PyErr_Clear();
    else {
        closed = PyObject_IsTrue(res);
        Py_DECREF(res);
        if (closed == -1)
            PyErr_Clear();
    }
    if (closed == 0) {
        res = PyObject_CallMethodObjArgs((PyObject *) self, _PyIO_str_close,
                                          NULL);
        /* Silencing I/O errors is bad, but printing spurious tracebacks is
           equally as bad, and potentially more frequent (because of
           shutdown issues). */
        if (res == NULL)
            PyErr_Clear();
        else
            Py_DECREF(res);
    }
    PyErr_Restore(tp, v, tb);
    if (is_zombie) {
        if (--Py_REFCNT(self) != 0) {
            /* The object lives again. The following code is taken from
               slot_tp_del in typeobject.c. */
            Py_ssize_t refcnt = Py_REFCNT(self);
            _Py_NewReference(self);
            Py_REFCNT(self) = refcnt;
            /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
             * we need to undo that. */
            _Py_DEC_REFTOTAL;
            /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
             * chain, so no more to do there.
             * If COUNT_ALLOCS, the original decref bumped tp_frees, and
             * _Py_NewReference bumped tp_allocs:  both of those need to be
             * undone.
             */
#ifdef COUNT_ALLOCS
            --Py_TYPE(self)->tp_frees;
            --Py_TYPE(self)->tp_allocs;
#endif
            return -1;
        }
    }
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int iobase_clear ( iobase self) [static]

Definition at line 267 of file iobase.c.

{
    if (_PyIOBase_finalize((PyObject *) self) < 0)
        return -1;
    Py_CLEAR(self->dict);
    return 0;
}

Here is the call graph for this function:

static PyObject* iobase_close ( PyObject self,
PyObject args 
) [static]

Definition at line 177 of file iobase.c.

{
    PyObject *res;

    if (IS_CLOSED(self))
        Py_RETURN_NONE;

    res = PyObject_CallMethodObjArgs(self, _PyIO_str_flush, NULL);
    PyObject_SetAttrString(self, "__IOBase_closed", Py_True);
    if (res == NULL) {
        return NULL;
    }
    Py_XDECREF(res);
    Py_RETURN_NONE;
}

Here is the call graph for this function:

static int iobase_closed ( PyObject self) [static]

Definition at line 139 of file iobase.c.

{
    PyObject *res;
    int closed;
    /* This gets the derived attribute, which is *not* __IOBase_closed
       in most cases! */
    res = PyObject_GetAttr(self, _PyIO_str_closed);
    if (res == NULL)
        return 0;
    closed = PyObject_IsTrue(res);
    Py_DECREF(res);
    return closed;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PyObject* iobase_closed_get ( PyObject self,
void context 
) [static]

Definition at line 154 of file iobase.c.

{
    return PyBool_FromLong(IS_CLOSED(self));
}

Here is the call graph for this function:

static void iobase_dealloc ( iobase self) [static]

Definition at line 278 of file iobase.c.

{
    /* NOTE: since IOBaseObject has its own dict, Python-defined attributes
       are still available here for close() to use.
       However, if the derived class declares a __slots__, those slots are
       already gone.
    */
    if (_PyIOBase_finalize((PyObject *) self) < 0) {
        /* When called from a heap type's dealloc, the type will be
           decref'ed on return (see e.g. subtype_dealloc in typeobject.c). */
        if (PyType_HasFeature(Py_TYPE(self), Py_TPFLAGS_HEAPTYPE))
            Py_INCREF(Py_TYPE(self));
        return;
    }
    _PyObject_GC_UNTRACK(self);
    if (self->weakreflist != NULL)
        PyObject_ClearWeakRefs((PyObject *) self);
    Py_CLEAR(self->dict);
    Py_TYPE(self)->tp_free((PyObject *) self);
}

Here is the call graph for this function:

static PyObject* iobase_enter ( PyObject self,
PyObject args 
) [static]

Definition at line 391 of file iobase.c.

{
    if (_PyIOBase_check_closed(self, Py_True) == NULL)
        return NULL;

    Py_INCREF(self);
    return self;
}

Here is the call graph for this function:

static PyObject* iobase_exit ( PyObject self,
PyObject args 
) [static]

Definition at line 401 of file iobase.c.

Here is the call graph for this function:

static PyObject* iobase_fileno ( PyObject self,
PyObject args 
) [static]

Definition at line 416 of file iobase.c.

{
    return iobase_unsupported("fileno");
}

Here is the call graph for this function:

static PyObject* iobase_flush ( PyObject self,
PyObject args 
) [static]

Definition at line 123 of file iobase.c.

{
    /* XXX Should this return the number of bytes written??? */
    if (IS_CLOSED(self)) {
        PyErr_SetString(PyExc_ValueError, "I/O operation on closed file.");
        return NULL;
    }
    Py_RETURN_NONE;
}

Here is the call graph for this function:

static PyObject* iobase_isatty ( PyObject self,
PyObject args 
) [static]

Definition at line 427 of file iobase.c.

Here is the call graph for this function:

static PyObject* iobase_iter ( PyObject self) [static]

Definition at line 541 of file iobase.c.

{
    if (_PyIOBase_check_closed(self, Py_True) == NULL)
        return NULL;

    Py_INCREF(self);
    return self;
}

Here is the call graph for this function:

static PyObject* iobase_iternext ( PyObject self) [static]

Definition at line 551 of file iobase.c.

{
    PyObject *line = PyObject_CallMethodObjArgs(self, _PyIO_str_readline, NULL);

    if (line == NULL)
        return NULL;

    if (PyObject_Size(line) == 0) {
        Py_DECREF(line);
        return NULL;
    }

    return line;
}

Here is the call graph for this function:

static PyObject* iobase_readable ( PyObject self,
PyObject args 
) [static]

Definition at line 336 of file iobase.c.

static PyObject* iobase_readline ( PyObject self,
PyObject args 
) [static]

Definition at line 446 of file iobase.c.

{
    /* For backwards compatibility, a (slowish) readline(). */

    Py_ssize_t limit = -1;
    int has_peek = 0;
    PyObject *buffer, *result;
    Py_ssize_t old_size = -1;

    if (!PyArg_ParseTuple(args, "|O&:readline", &_PyIO_ConvertSsize_t, &limit)) {
        return NULL;
    }

    if (PyObject_HasAttrString(self, "peek"))
        has_peek = 1;

    buffer = PyByteArray_FromStringAndSize(NULL, 0);
    if (buffer == NULL)
        return NULL;

    while (limit < 0 || Py_SIZE(buffer) < limit) {
        Py_ssize_t nreadahead = 1;
        PyObject *b;

        if (has_peek) {
            PyObject *readahead = PyObject_CallMethod(self, "peek", "i", 1);
            if (readahead == NULL)
                goto fail;
            if (!PyBytes_Check(readahead)) {
                PyErr_Format(PyExc_IOError,
                             "peek() should have returned a bytes object, "
                             "not '%.200s'", Py_TYPE(readahead)->tp_name);
                Py_DECREF(readahead);
                goto fail;
            }
            if (PyBytes_GET_SIZE(readahead) > 0) {
                Py_ssize_t n = 0;
                const char *buf = PyBytes_AS_STRING(readahead);
                if (limit >= 0) {
                    do {
                        if (n >= PyBytes_GET_SIZE(readahead) || n >= limit)
                            break;
                        if (buf[n++] == '\n')
                            break;
                    } while (1);
                }
                else {
                    do {
                        if (n >= PyBytes_GET_SIZE(readahead))
                            break;
                        if (buf[n++] == '\n')
                            break;
                    } while (1);
                }
                nreadahead = n;
            }
            Py_DECREF(readahead);
        }

        b = PyObject_CallMethod(self, "read", "n", nreadahead);
        if (b == NULL)
            goto fail;
        if (!PyBytes_Check(b)) {
            PyErr_Format(PyExc_IOError,
                         "read() should have returned a bytes object, "
                         "not '%.200s'", Py_TYPE(b)->tp_name);
            Py_DECREF(b);
            goto fail;
        }
        if (PyBytes_GET_SIZE(b) == 0) {
            Py_DECREF(b);
            break;
        }

        old_size = PyByteArray_GET_SIZE(buffer);
        PyByteArray_Resize(buffer, old_size + PyBytes_GET_SIZE(b));
        memcpy(PyByteArray_AS_STRING(buffer) + old_size,
               PyBytes_AS_STRING(b), PyBytes_GET_SIZE(b));

        Py_DECREF(b);

        if (PyByteArray_AS_STRING(buffer)[PyByteArray_GET_SIZE(buffer) - 1] == '\n')
            break;
    }

    result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(buffer),
                                       PyByteArray_GET_SIZE(buffer));
    Py_DECREF(buffer);
    return result;
  fail:
    Py_DECREF(buffer);
    return NULL;
}

Here is the call graph for this function:

static PyObject* iobase_readlines ( PyObject self,
PyObject args 
) [static]

Definition at line 574 of file iobase.c.

{
    Py_ssize_t hint = -1, length = 0;
    PyObject *result;

    if (!PyArg_ParseTuple(args, "|O&:readlines", &_PyIO_ConvertSsize_t, &hint)) {
        return NULL;
    }

    result = PyList_New(0);
    if (result == NULL)
        return NULL;

    if (hint <= 0) {
        /* XXX special-casing this made sense in the Python version in order
           to remove the bytecode interpretation overhead, but it could
           probably be removed here. */
        PyObject *ret = PyObject_CallMethod(result, "extend", "O", self);
        if (ret == NULL) {
            Py_DECREF(result);
            return NULL;
        }
        Py_DECREF(ret);
        return result;
    }

    while (1) {
        PyObject *line = PyIter_Next(self);
        if (line == NULL) {
            if (PyErr_Occurred()) {
                Py_DECREF(result);
                return NULL;
            }
            else
                break; /* StopIteration raised */
        }

        if (PyList_Append(result, line) < 0) {
            Py_DECREF(line);
            Py_DECREF(result);
            return NULL;
        }
        length += PyObject_Size(line);
        Py_DECREF(line);

        if (length > hint)
            break;
    }
    return result;
}

Here is the call graph for this function:

static PyObject* iobase_seek ( PyObject self,
PyObject args 
) [static]

Definition at line 89 of file iobase.c.

{
    return iobase_unsupported("seek");
}

Here is the call graph for this function:

static PyObject* iobase_seekable ( PyObject self,
PyObject args 
) [static]

Definition at line 308 of file iobase.c.

static PyObject* iobase_tell ( PyObject self,
PyObject args 
) [static]

Definition at line 98 of file iobase.c.

{
    return PyObject_CallMethod(self, "seek", "ii", 0, 1);
}

Here is the call graph for this function:

static int iobase_traverse ( iobase self,
visitproc  visit,
void arg 
) [static]

Definition at line 260 of file iobase.c.

{
    Py_VISIT(self->dict);
    return 0;
}
static PyObject* iobase_truncate ( PyObject self,
PyObject args 
) [static]

Definition at line 110 of file iobase.c.

{
    return iobase_unsupported("truncate");
}

Here is the call graph for this function:

static PyObject* iobase_unsupported ( const char *  message) [static]

Definition at line 67 of file iobase.c.

{
    PyErr_SetString(IO_STATE->unsupported_operation, message);
    return NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PyObject* iobase_writable ( PyObject self,
PyObject args 
) [static]

Definition at line 365 of file iobase.c.

static PyObject* iobase_writelines ( PyObject self,
PyObject args 
) [static]

Definition at line 626 of file iobase.c.

{
    PyObject *lines, *iter, *res;

    if (!PyArg_ParseTuple(args, "O:writelines", &lines)) {
        return NULL;
    }

    if (_PyIOBase_check_closed(self, Py_True) == NULL)
        return NULL;

    iter = PyObject_GetIter(lines);
    if (iter == NULL)
        return NULL;

    while (1) {
        PyObject *line = PyIter_Next(iter);
        if (line == NULL) {
            if (PyErr_Occurred()) {
                Py_DECREF(iter);
                return NULL;
            }
            else
                break; /* Stop Iteration */
        }

        res = PyObject_CallMethodObjArgs(self, _PyIO_str_write, line, NULL);
        Py_DECREF(line);
        if (res == NULL) {
            Py_DECREF(iter);
            return NULL;
        }
        Py_DECREF(res);
    }
    Py_DECREF(iter);
    Py_RETURN_NONE;
}

Here is the call graph for this function:

PyDoc_STRVAR ( iobase_doc  ,
"The abstract base class for all I/O  classes,
acting on streams of\n""bytes.There is no public constructor.\n""\n""This class provides dummy implementations for many methods that\n""derived classes can override selectively;the default implementations\n""represent a file that cannot be  read,
written or seeked.\n""\n""Even though IOBase does not declare  read,
readinto  ,
or write because\n""their signatures will  vary,
implementations and clients should\n""consider those methods part of the interface.  Also,
implementations\n""may raise UnsupportedOperation when operations they do not support are\n""called.\n""\n""The basic type used for binary data read from or written to a file is\n""bytes.bytearrays are accepted  too,
and in some cases(such as\n""readinto) needed.Text I/O classes work with str data.\n""\n""Note that calling any method(even inquiries) on a closed stream is\n""undefined.Implementations may raise IOError in this case.\n""\n""IOBase(and its subclasses) support the iterator  protocol,
meaning\n""that an IOBase object can be iterated over yielding the lines in a\n""stream.\n""\n""IOBase also supports the:keyword:`with`statement.In this  example,
\n""fp is closed after the suite of the with statement is complete:\n""\n""with open('spam.txt', 'r') as fp:\n""fp.write('Spam and eggs!')\n  
)
PyDoc_STRVAR ( iobase_seek_doc  ,
"Change stream position.\n""\n""Change the stream position to byte offset offset. offset is\n""interpreted relative to the position indicated by whence. Values\n""for whence are:\n""\n""* 0 -- start of stream (the default); offset should be zero or positive\n""* 1 -- current stream position; offset may be negative\n""* 2 -- end of stream; offset is usually negative\n""\n""Return the new absolute position."   
)
PyDoc_STRVAR ( iobase_tell_doc  ,
"Return current stream position."   
)
PyDoc_STRVAR ( iobase_truncate_doc  ,
"Truncate file to size bytes.\n""\n""File pointer is left unchanged. Size defaults to the current IO\n""position as reported by tell(). Returns the new size."   
)
PyDoc_STRVAR ( iobase_flush_doc  ,
"Flush write  buffers,
if applicable.\n""\n""This is not implemented for read-only and non-blocking streams.\n  
)
PyDoc_STRVAR ( iobase_close_doc  ,
"Flush and close the IO object.\n""\n""This method has no effect if the file is already closed.\n  
)
PyDoc_STRVAR ( iobase_seekable_doc  ,
"Return whether object supports random access.\n""\n""If  False,
seek()  ,
tell() and truncate() will raise UnsupportedOperation.\n""This method may need to do a test seek()."   
)
PyDoc_STRVAR ( iobase_readable_doc  ,
"Return whether object was opened for reading.\n""\n""If  False,
read() will raise UnsupportedOperation."   
)
PyDoc_STRVAR ( iobase_writable_doc  ,
"Return whether object was opened for writing.\n""\n""If  False,
write() will raise UnsupportedOperation."   
)
PyDoc_STRVAR ( iobase_fileno_doc  ,
"Returns underlying file descriptor if one exists.\n""\n""An IOError is raised if the IO object does not use a file descriptor.\n  
)
PyDoc_STRVAR ( iobase_isatty_doc  ,
"Return whether this is an 'interactive' stream.\n""\n""Return False if it can't be determined.\n  
)
PyDoc_STRVAR ( iobase_readline_doc  ,
"Read and return a line from the stream.\n""\n""If limit is  specified,
at most limit bytes will be read.\n""\n""The line terminator is always b'\n'for binary files;for text\n""  files,
the newlines argument to open can be used to select the line\n""terminator(s) recognized.\n  
)
PyDoc_STRVAR ( iobase_readlines_doc  ,
"Return a list of lines from the stream.\n""\n""hint can be specified to control the number of lines read: no more\n""lines will be read if the total size (in bytes/characters) of all\n""lines so far exceeds hint."   
)
PyDoc_STRVAR ( rawiobase_doc  ,
"Base class for raw binary I/O."   
)
PyDoc_STRVAR ( rawiobase_readall_doc  ,
"Read until  EOF,
using multiple read() call."   
)
static PyObject* rawiobase_read ( PyObject self,
PyObject args 
) [static]

Definition at line 761 of file iobase.c.

{
    Py_ssize_t n = -1;
    PyObject *b, *res;

    if (!PyArg_ParseTuple(args, "|n:read", &n)) {
        return NULL;
    }

    if (n < 0)
        return PyObject_CallMethod(self, "readall", NULL);

    /* TODO: allocate a bytes object directly instead and manually construct
       a writable memoryview pointing to it. */
    b = PyByteArray_FromStringAndSize(NULL, n);
    if (b == NULL)
        return NULL;

    res = PyObject_CallMethodObjArgs(self, _PyIO_str_readinto, b, NULL);
    if (res == NULL || res == Py_None) {
        Py_DECREF(b);
        return res;
    }

    n = PyNumber_AsSsize_t(res, PyExc_ValueError);
    Py_DECREF(res);
    if (n == -1 && PyErr_Occurred()) {
        Py_DECREF(b);
        return NULL;
    }

    res = PyBytes_FromStringAndSize(PyByteArray_AsString(b), n);
    Py_DECREF(b);
    return res;
}

Here is the call graph for this function:

static PyObject* rawiobase_readall ( PyObject self,
PyObject args 
) [static]

Definition at line 802 of file iobase.c.

{
    int r;
    PyObject *chunks = PyList_New(0);
    PyObject *result;
    
    if (chunks == NULL)
        return NULL;

    while (1) {
        PyObject *data = PyObject_CallMethod(self, "read",
                                             "i", DEFAULT_BUFFER_SIZE);
        if (!data) {
            Py_DECREF(chunks);
            return NULL;
        }
        if (data == Py_None) {
            if (PyList_GET_SIZE(chunks) == 0) {
                Py_DECREF(chunks);
                return data;
            }
            Py_DECREF(data);
            break;
        }
        if (!PyBytes_Check(data)) {
            Py_DECREF(chunks);
            Py_DECREF(data);
            PyErr_SetString(PyExc_TypeError, "read() should return bytes");
            return NULL;
        }
        if (PyBytes_GET_SIZE(data) == 0) {
            /* EOF */
            Py_DECREF(data);
            break;
        }
        r = PyList_Append(chunks, data);
        Py_DECREF(data);
        if (r < 0) {
            Py_DECREF(chunks);
            return NULL;
        }
    }
    result = _PyBytes_Join(_PyIO_empty_bytes, chunks);
    Py_DECREF(chunks);
    return result;
}

Here is the call graph for this function:


Variable Documentation

Initial value:
 {
    {"closed", (getter)iobase_closed_get, NULL, NULL},
    {NULL}
}

Definition at line 693 of file iobase.c.

Initial value:
 {
    {"seek", iobase_seek, METH_VARARGS, iobase_seek_doc},
    {"tell", iobase_tell, METH_NOARGS, iobase_tell_doc},
    {"truncate", iobase_truncate, METH_VARARGS, iobase_truncate_doc},
    {"flush", iobase_flush, METH_NOARGS, iobase_flush_doc},
    {"close", iobase_close, METH_NOARGS, iobase_close_doc},

    {"seekable", iobase_seekable, METH_NOARGS, iobase_seekable_doc},
    {"readable", iobase_readable, METH_NOARGS, iobase_readable_doc},
    {"writable", iobase_writable, METH_NOARGS, iobase_writable_doc},

    {"_checkClosed",   _PyIOBase_check_closed, METH_NOARGS},
    {"_checkSeekable", _PyIOBase_check_seekable, METH_NOARGS},
    {"_checkReadable", _PyIOBase_check_readable, METH_NOARGS},
    {"_checkWritable", _PyIOBase_check_writable, METH_NOARGS},

    {"fileno", iobase_fileno, METH_NOARGS, iobase_fileno_doc},
    {"isatty", iobase_isatty, METH_NOARGS, iobase_isatty_doc},

    {"__enter__", iobase_enter, METH_NOARGS},
    {"__exit__", iobase_exit, METH_VARARGS},

    {"readline", iobase_readline, METH_VARARGS, iobase_readline_doc},
    {"readlines", iobase_readlines, METH_VARARGS, iobase_readlines_doc},
    {"writelines", iobase_writelines, METH_VARARGS},

    {NULL, NULL}
}

Definition at line 664 of file iobase.c.

Definition at line 699 of file iobase.c.

Definition at line 855 of file iobase.c.

Initial value:
 {
    {"read", rawiobase_read, METH_VARARGS},
    {"readall", rawiobase_readall, METH_NOARGS, rawiobase_readall_doc},
    {NULL, NULL}
}

Definition at line 849 of file iobase.c.