Back to index

python3.2  3.2.2
Classes | Defines | Typedefs | Functions | Variables
_iomodule.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  PyBlockingIOErrorObject
struct  _PyIO_State

Defines

#define DEFAULT_BUFFER_SIZE   (8 * 1024) /* bytes */
#define PyLong_AsOff_t   PyLong_AsSsize_t
#define PyLong_FromOff_t   PyLong_FromSsize_t
#define PY_OFF_T_MAX   PY_SSIZE_T_MAX
#define PY_OFF_T_MIN   PY_SSIZE_T_MIN
#define PY_OFF_T_COMPAT   Py_ssize_t
#define PY_PRIdOFF   "zd"
#define IO_MOD_STATE(mod)   ((_PyIO_State *)PyModule_GetState(mod))
#define IO_STATE   IO_MOD_STATE(PyState_FindModule(&_PyIO_Module))

Typedefs

typedef off_t Py_off_t

Functions

int _PyIO_ConvertSsize_t (PyObject *, void *)
PyObject_PyIOBase_check_readable (PyObject *self, PyObject *args)
PyObject_PyIOBase_check_writable (PyObject *self, PyObject *args)
PyObject_PyIOBase_check_seekable (PyObject *self, PyObject *args)
PyObject_PyIOBase_check_closed (PyObject *self, PyObject *args)
int _PyIOBase_finalize (PyObject *self)
int _PyFileIO_closed (PyObject *self)
PyObject_PyIncrementalNewlineDecoder_decode (PyObject *self, PyObject *input, int final)
Py_ssize_t _PyIO_find_line_ending (int translated, int universal, PyObject *readnl, Py_UNICODE *start, Py_UNICODE *end, Py_ssize_t *consumed)
 PyAPI_DATA (PyObject *) PyExc_BlockingIOError
Py_off_t PyNumber_AsOff_t (PyObject *item, PyObject *err)

Variables

PyTypeObject PyIOBase_Type
PyTypeObject PyRawIOBase_Type
PyTypeObject PyBufferedIOBase_Type
PyTypeObject PyTextIOBase_Type
PyTypeObject PyFileIO_Type
PyTypeObject PyBytesIO_Type
PyTypeObject PyStringIO_Type
PyTypeObject PyBufferedReader_Type
PyTypeObject PyBufferedWriter_Type
PyTypeObject PyBufferedRWPair_Type
PyTypeObject PyBufferedRandom_Type
PyTypeObject PyTextIOWrapper_Type
PyTypeObject PyIncrementalNewlineDecoder_Type
PyModuleDef _PyIO_Module
PyObject_PyIO_str_close
PyObject_PyIO_str_closed
PyObject_PyIO_str_decode
PyObject_PyIO_str_encode
PyObject_PyIO_str_fileno
PyObject_PyIO_str_flush
PyObject_PyIO_str_getstate
PyObject_PyIO_str_isatty
PyObject_PyIO_str_newlines
PyObject_PyIO_str_nl
PyObject_PyIO_str_read
PyObject_PyIO_str_read1
PyObject_PyIO_str_readable
PyObject_PyIO_str_readinto
PyObject_PyIO_str_readline
PyObject_PyIO_str_reset
PyObject_PyIO_str_seek
PyObject_PyIO_str_seekable
PyObject_PyIO_str_setstate
PyObject_PyIO_str_tell
PyObject_PyIO_str_truncate
PyObject_PyIO_str_writable
PyObject_PyIO_str_write
PyObject_PyIO_empty_str
PyObject_PyIO_empty_bytes
PyObject_PyIO_zero
PyTypeObject _PyBytesIOBuffer_Type

Class Documentation

struct PyBlockingIOErrorObject

Definition at line 63 of file _iomodule.h.

Collaboration diagram for PyBlockingIOErrorObject:
Class Members
PyObject * filename
PyException_HEAD PyObject * myerrno
PyObject * strerror
Py_ssize_t written
struct _PyIO_State

Definition at line 134 of file _iomodule.h.

Collaboration diagram for _PyIO_State:
Class Members
int initialized
PyObject * locale_module
PyObject * os_module
PyObject * unsupported_operation

Define Documentation

#define DEFAULT_BUFFER_SIZE   (8 * 1024) /* bytes */

Definition at line 61 of file _iomodule.h.

#define IO_MOD_STATE (   mod)    ((_PyIO_State *)PyModule_GetState(mod))

Definition at line 142 of file _iomodule.h.

Definition at line 143 of file _iomodule.h.

Definition at line 104 of file _iomodule.h.

Definition at line 102 of file _iomodule.h.

Definition at line 103 of file _iomodule.h.

#define PY_PRIdOFF   "zd"

Definition at line 105 of file _iomodule.h.

Definition at line 100 of file _iomodule.h.

Definition at line 101 of file _iomodule.h.


Typedef Documentation

typedef off_t Py_off_t

Definition at line 98 of file _iomodule.h.


Function Documentation

Definition at line 69 of file fileio.c.

{
    return ((fileio *)self)->fd < 0;
}
PyObject* _PyIncrementalNewlineDecoder_decode ( PyObject self,
PyObject input,
int  final 
)

Definition at line 245 of file textio.c.

{
    PyObject *output;
    Py_ssize_t output_len;
    nldecoder_object *self = (nldecoder_object *) _self;

    if (self->decoder == NULL) {
        PyErr_SetString(PyExc_ValueError,
                        "IncrementalNewlineDecoder.__init__ not called");
        return NULL;
    }

    /* decode input (with the eventual \r from a previous pass) */
    if (self->decoder != Py_None) {
        output = PyObject_CallMethodObjArgs(self->decoder,
            _PyIO_str_decode, input, final ? Py_True : Py_False, NULL);
    }
    else {
        output = input;
        Py_INCREF(output);
    }

    if (output == NULL)
        return NULL;

    if (!PyUnicode_Check(output)) {
        PyErr_SetString(PyExc_TypeError,
                        "decoder should return a string result");
        goto error;
    }

    output_len = PyUnicode_GET_SIZE(output);
    if (self->pendingcr && (final || output_len > 0)) {
        Py_UNICODE *out;
        PyObject *modified = PyUnicode_FromUnicode(NULL, output_len + 1);
        if (modified == NULL)
            goto error;
        out = PyUnicode_AS_UNICODE(modified);
        out[0] = '\r';
        memcpy(out + 1, PyUnicode_AS_UNICODE(output),
               output_len * sizeof(Py_UNICODE));
        Py_DECREF(output);
        output = modified;
        self->pendingcr = 0;
        output_len++;
    }

    /* retain last \r even when not translating data:
     * then readline() is sure to get \r\n in one pass
     */
    if (!final) {
        if (output_len > 0
            && PyUnicode_AS_UNICODE(output)[output_len - 1] == '\r') {

            if (Py_REFCNT(output) == 1) {
                if (PyUnicode_Resize(&output, output_len - 1) < 0)
                    goto error;
            }
            else {
                PyObject *modified = PyUnicode_FromUnicode(
                    PyUnicode_AS_UNICODE(output),
                    output_len - 1);
                if (modified == NULL)
                    goto error;
                Py_DECREF(output);
                output = modified;
            }
            self->pendingcr = 1;
        }
    }

    /* Record which newlines are read and do newline translation if desired,
       all in one pass. */
    {
        Py_UNICODE *in_str;
        Py_ssize_t len;
        int seennl = self->seennl;
        int only_lf = 0;

        in_str = PyUnicode_AS_UNICODE(output);
        len = PyUnicode_GET_SIZE(output);

        if (len == 0)
            return output;

        /* If, up to now, newlines are consistently \n, do a quick check
           for the \r *byte* with the libc's optimized memchr.
           */
        if (seennl == SEEN_LF || seennl == 0) {
            only_lf = (memchr(in_str, '\r', len * sizeof(Py_UNICODE)) == NULL);
        }

        if (only_lf) {
            /* If not already seen, quick scan for a possible "\n" character.
               (there's nothing else to be done, even when in translation mode)
            */
            if (seennl == 0 &&
                memchr(in_str, '\n', len * sizeof(Py_UNICODE)) != NULL) {
                Py_UNICODE *s, *end;
                s = in_str;
                end = in_str + len;
                for (;;) {
                    Py_UNICODE c;
                    /* Fast loop for non-control characters */
                    while (*s > '\n')
                        s++;
                    c = *s++;
                    if (c == '\n') {
                        seennl |= SEEN_LF;
                        break;
                    }
                    if (s > end)
                        break;
                }
            }
            /* Finished: we have scanned for newlines, and none of them
               need translating */
        }
        else if (!self->translate) {
            Py_UNICODE *s, *end;
            /* We have already seen all newline types, no need to scan again */
            if (seennl == SEEN_ALL)
                goto endscan;
            s = in_str;
            end = in_str + len;
            for (;;) {
                Py_UNICODE c;
                /* Fast loop for non-control characters */
                while (*s > '\r')
                    s++;
                c = *s++;
                if (c == '\n')
                    seennl |= SEEN_LF;
                else if (c == '\r') {
                    if (*s == '\n') {
                        seennl |= SEEN_CRLF;
                        s++;
                    }
                    else
                        seennl |= SEEN_CR;
                }
                if (s > end)
                    break;
                if (seennl == SEEN_ALL)
                    break;
            }
        endscan:
            ;
        }
        else {
            PyObject *translated = NULL;
            Py_UNICODE *out_str;
            Py_UNICODE *in, *out, *end;
            if (Py_REFCNT(output) != 1) {
                /* We could try to optimize this so that we only do a copy
                   when there is something to translate. On the other hand,
                   most decoders should only output non-shared strings, i.e.
                   translation is done in place. */
                translated = PyUnicode_FromUnicode(NULL, len);
                if (translated == NULL)
                    goto error;
                assert(Py_REFCNT(translated) == 1);
                memcpy(PyUnicode_AS_UNICODE(translated),
                       PyUnicode_AS_UNICODE(output),
                       len * sizeof(Py_UNICODE));
            }
            else {
                translated = output;
            }
            out_str = PyUnicode_AS_UNICODE(translated);
            in = in_str;
            out = out_str;
            end = in_str + len;
            for (;;) {
                Py_UNICODE c;
                /* Fast loop for non-control characters */
                while ((c = *in++) > '\r')
                    *out++ = c;
                if (c == '\n') {
                    *out++ = c;
                    seennl |= SEEN_LF;
                    continue;
                }
                if (c == '\r') {
                    if (*in == '\n') {
                        in++;
                        seennl |= SEEN_CRLF;
                    }
                    else
                        seennl |= SEEN_CR;
                    *out++ = '\n';
                    continue;
                }
                if (in > end)
                    break;
                *out++ = c;
            }
            if (translated != output) {
                Py_DECREF(output);
                output = translated;
            }
            if (out - out_str != len) {
                if (PyUnicode_Resize(&output, out - out_str) < 0)
                    goto error;
            }
        }
        self->seennl |= seennl;
    }

    return output;

  error:
    Py_DECREF(output);
    return NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 580 of file _iomodule.c.

                                                  {
    Py_ssize_t limit;
    if (obj == Py_None) {
        limit = -1;
    }
    else if (PyNumber_Check(obj)) {
        limit = PyNumber_AsSsize_t(obj, PyExc_OverflowError);
        if (limit == -1 && PyErr_Occurred())
            return 0;
    }
    else {
        PyErr_Format(PyExc_TypeError,
                     "integer argument expected, got '%.200s'",
                     Py_TYPE(obj)->tp_name);
        return 0;
    }
    *((Py_ssize_t *)result) = limit;
    return 1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Py_ssize_t _PyIO_find_line_ending ( int  translated,
int  universal,
PyObject readnl,
Py_UNICODE start,
Py_UNICODE end,
Py_ssize_t consumed 
)

Definition at line 1598 of file textio.c.

{
    Py_ssize_t len = end - start;

    if (translated) {
        /* Newlines are already translated, only search for \n */
        Py_UNICODE *pos = find_control_char(start, end, '\n');
        if (pos != NULL)
            return pos - start + 1;
        else {
            *consumed = len;
            return -1;
        }
    }
    else if (universal) {
        /* Universal newline search. Find any of \r, \r\n, \n
         * The decoder ensures that \r\n are not split in two pieces
         */
        Py_UNICODE *s = start;
        for (;;) {
            Py_UNICODE ch;
            /* Fast path for non-control chars. The loop always ends
               since the Py_UNICODE storage is NUL-terminated. */
            while (*s > '\r')
                s++;
            if (s >= end) {
                *consumed = len;
                return -1;
            }
            ch = *s++;
            if (ch == '\n')
                return s - start;
            if (ch == '\r') {
                if (*s == '\n')
                    return s - start + 1;
                else
                    return s - start;
            }
        }
    }
    else {
        /* Non-universal mode. */
        Py_ssize_t readnl_len = PyUnicode_GET_SIZE(readnl);
        Py_UNICODE *nl = PyUnicode_AS_UNICODE(readnl);
        if (readnl_len == 1) {
            Py_UNICODE *pos = find_control_char(start, end, nl[0]);
            if (pos != NULL)
                return pos - start + 1;
            *consumed = len;
            return -1;
        }
        else {
            Py_UNICODE *s = start;
            Py_UNICODE *e = end - readnl_len + 1;
            Py_UNICODE *pos;
            if (e < s)
                e = s;
            while (s < e) {
                Py_ssize_t i;
                Py_UNICODE *pos = find_control_char(s, end, nl[0]);
                if (pos == NULL || pos >= e)
                    break;
                for (i = 1; i < readnl_len; i++) {
                    if (pos[i] != nl[i])
                        break;
                }
                if (i == readnl_len)
                    return pos - start + readnl_len;
                s = pos + 1;
            }
            pos = find_control_char(e, end, nl[0]);
            if (pos == NULL)
                *consumed = len;
            else
                *consumed = pos - start;
            return -1;
        }
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

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:

Py_off_t PyNumber_AsOff_t ( PyObject item,
PyObject err 
)

Definition at line 534 of file _iomodule.c.

{
    Py_off_t result;
    PyObject *runerr;
    PyObject *value = PyNumber_Index(item);
    if (value == NULL)
        return -1;

    /* We're done if PyLong_AsSsize_t() returns without error. */
    result = PyLong_AsOff_t(value);
    if (result != -1 || !(runerr = PyErr_Occurred()))
        goto finish;

    /* Error handling code -- only manage OverflowError differently */
    if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError))
        goto finish;

    PyErr_Clear();
    /* If no error-handling desired then the default clipping
       is sufficient.
     */
    if (!err) {
        assert(PyLong_Check(value));
        /* Whether or not it is less than or equal to
           zero is determined by the sign of ob_size
        */
        if (_PyLong_Sign(value) < 0)
            result = PY_OFF_T_MIN;
        else
            result = PY_OFF_T_MAX;
    }
    else {
        /* Otherwise replace the error with caller's error object. */
        PyErr_Format(err,
                     "cannot fit '%.200s' into an offset-sized integer",
                     item->ob_type->tp_name);
    }

 finish:
    Py_DECREF(value);
    return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 982 of file bytesio.c.

Definition at line 51 of file _iomodule.c.

Definition at line 50 of file _iomodule.c.

Definition at line 26 of file _iomodule.c.

Definition at line 27 of file _iomodule.c.

Definition at line 28 of file _iomodule.c.

Definition at line 29 of file _iomodule.c.

Definition at line 30 of file _iomodule.c.

Definition at line 31 of file _iomodule.c.

Definition at line 32 of file _iomodule.c.

Definition at line 33 of file _iomodule.c.

Definition at line 34 of file _iomodule.c.

Definition at line 35 of file _iomodule.c.

Definition at line 36 of file _iomodule.c.

Definition at line 37 of file _iomodule.c.

Definition at line 38 of file _iomodule.c.

Definition at line 39 of file _iomodule.c.

Definition at line 40 of file _iomodule.c.

Definition at line 41 of file _iomodule.c.

Definition at line 42 of file _iomodule.c.

Definition at line 43 of file _iomodule.c.

Definition at line 44 of file _iomodule.c.

Definition at line 45 of file _iomodule.c.

Definition at line 46 of file _iomodule.c.

Definition at line 47 of file _iomodule.c.

Definition at line 48 of file _iomodule.c.

Definition at line 52 of file _iomodule.c.

Definition at line 150 of file bufferedio.c.

Definition at line 2382 of file bufferedio.c.

Definition at line 1596 of file bufferedio.c.

Definition at line 2241 of file bufferedio.c.

Definition at line 1988 of file bufferedio.c.

Definition at line 888 of file bytesio.c.

Definition at line 1125 of file fileio.c.

Definition at line 572 of file textio.c.

Definition at line 699 of file iobase.c.

Definition at line 855 of file iobase.c.

Definition at line 860 of file stringio.c.

Definition at line 135 of file textio.c.

Definition at line 2619 of file textio.c.