Back to index

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

Go to the source code of this file.

Classes

struct  PyStdPrinter_Object

Defines

#define PY_SSIZE_T_CLEAN
#define GETC(f)   getc(f)
#define FLOCKFILE(f)
#define FUNLOCKFILE(f)
#define NEWLINE_UNKNOWN   0 /* No newline seen, yet */
#define NEWLINE_CR   1 /* \r newline seen */
#define NEWLINE_LF   2 /* \n newline seen */
#define NEWLINE_CRLF   4 /* \r\n newline seen */

Functions

PyObjectPyFile_FromFd (int fd, char *name, char *mode, int buffering, char *encoding, char *errors, char *newline, int closefd)
PyObjectPyFile_GetLine (PyObject *f, int n)
int PyFile_WriteObject (PyObject *v, PyObject *f, int flags)
int PyFile_WriteString (const char *s, PyObject *f)
int PyObject_AsFileDescriptor (PyObject *o)
char * Py_UniversalNewlineFgets (char *buf, int n, FILE *stream, PyObject *fobj)
static PyObjectstdprinter_new (PyTypeObject *type, PyObject *args, PyObject *kews)
static int stdprinter_init (PyObject *self, PyObject *args, PyObject *kwds)
PyObjectPyFile_NewStdPrinter (int fd)
static PyObjectstdprinter_write (PyStdPrinter_Object *self, PyObject *args)
static PyObjectstdprinter_fileno (PyStdPrinter_Object *self)
static PyObjectstdprinter_repr (PyStdPrinter_Object *self)
static PyObjectstdprinter_noop (PyStdPrinter_Object *self)
static PyObjectstdprinter_isatty (PyStdPrinter_Object *self)
static PyObjectget_closed (PyStdPrinter_Object *self, void *closure)
static PyObjectget_mode (PyStdPrinter_Object *self, void *closure)
static PyObjectget_encoding (PyStdPrinter_Object *self, void *closure)

Variables

static PyMethodDef stdprinter_methods []
static PyGetSetDef stdprinter_getsetlist []
PyTypeObject PyStdPrinter_Type

Class Documentation

struct PyStdPrinter_Object

Definition at line 326 of file fileobject.c.

Class Members
PyObject_HEAD int fd

Define Documentation

#define FLOCKFILE (   f)

Definition at line 12 of file fileobject.c.

#define FUNLOCKFILE (   f)

Definition at line 13 of file fileobject.c.

#define GETC (   f)    getc(f)

Definition at line 11 of file fileobject.c.

#define NEWLINE_CR   1 /* \r newline seen */

Definition at line 18 of file fileobject.c.

#define NEWLINE_CRLF   4 /* \r\n newline seen */

Definition at line 20 of file fileobject.c.

#define NEWLINE_LF   2 /* \n newline seen */

Definition at line 19 of file fileobject.c.

#define NEWLINE_UNKNOWN   0 /* No newline seen, yet */

Definition at line 17 of file fileobject.c.

Definition at line 3 of file fileobject.c.


Function Documentation

static PyObject* get_closed ( PyStdPrinter_Object self,
void closure 
) [static]

Definition at line 456 of file fileobject.c.

{
    Py_INCREF(Py_False);
    return Py_False;
}
static PyObject* get_encoding ( PyStdPrinter_Object self,
void closure 
) [static]

Definition at line 469 of file fileobject.c.

static PyObject* get_mode ( PyStdPrinter_Object self,
void closure 
) [static]

Definition at line 463 of file fileobject.c.

{
    return PyUnicode_FromString("w");
}

Here is the call graph for this function:

char* Py_UniversalNewlineFgets ( char *  buf,
int  n,
FILE *  stream,
PyObject fobj 
)

Definition at line 255 of file fileobject.c.

{
    char *p = buf;
    int c;
    int newlinetypes = 0;
    int skipnextlf = 0;

    if (fobj) {
        errno = ENXIO;          /* What can you do... */
        return NULL;
    }
    FLOCKFILE(stream);
    c = 'x'; /* Shut up gcc warning */
    while (--n > 0 && (c = GETC(stream)) != EOF ) {
        if (skipnextlf ) {
            skipnextlf = 0;
            if (c == '\n') {
                /* Seeing a \n here with skipnextlf true
                ** means we saw a \r before.
                */
                newlinetypes |= NEWLINE_CRLF;
                c = GETC(stream);
                if (c == EOF) break;
            } else {
                /*
                ** Note that c == EOF also brings us here,
                ** so we're okay if the last char in the file
                ** is a CR.
                */
                newlinetypes |= NEWLINE_CR;
            }
        }
        if (c == '\r') {
            /* A \r is translated into a \n, and we skip
            ** an adjacent \n, if any. We don't set the
            ** newlinetypes flag until we've seen the next char.
            */
            skipnextlf = 1;
            c = '\n';
        } else if ( c == '\n') {
            newlinetypes |= NEWLINE_LF;
        }
        *p++ = c;
        if (c == '\n') break;
    }
    if ( c == EOF && skipnextlf )
        newlinetypes |= NEWLINE_CR;
    FUNLOCKFILE(stream);
    *p = '\0';
    if ( skipnextlf ) {
        /* If we have no file object we cannot save the
        ** skipnextlf flag. We have to readahead, which
        ** will cause a pause if we're reading from an
        ** interactive stream, but that is very unlikely
        ** unless we're doing something silly like
        ** exec(open("/dev/tty").read()).
        */
        c = GETC(stream);
        if ( c != '\n' )
            ungetc(c, stream);
    }
    if (p == buf)
        return NULL;
    return buf;
}

Here is the caller graph for this function:

PyObject* PyFile_FromFd ( int  fd,
char *  name,
char *  mode,
int  buffering,
char *  encoding,
char *  errors,
char *  newline,
int  closefd 
)

Definition at line 29 of file fileobject.c.

{
    PyObject *io, *stream;

    io = PyImport_ImportModule("io");
    if (io == NULL)
        return NULL;
    stream = PyObject_CallMethod(io, "open", "isisssi", fd, mode,
                                 buffering, encoding, errors,
                                 newline, closefd);
    Py_DECREF(io);
    if (stream == NULL)
        return NULL;
    /* ignore name attribute because the name attribute of _BufferedIOMixin
       and TextIOWrapper is read only */
    return stream;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PyObject* PyFile_GetLine ( PyObject f,
int  n 
)

Definition at line 49 of file fileobject.c.

{
    PyObject *result;

    if (f == NULL) {
        PyErr_BadInternalCall();
        return NULL;
    }

    {
        PyObject *reader;
        PyObject *args;

        reader = PyObject_GetAttrString(f, "readline");
        if (reader == NULL)
            return NULL;
        if (n <= 0)
            args = PyTuple_New(0);
        else
            args = Py_BuildValue("(i)", n);
        if (args == NULL) {
            Py_DECREF(reader);
            return NULL;
        }
        result = PyEval_CallObject(reader, args);
        Py_DECREF(reader);
        Py_DECREF(args);
        if (result != NULL && !PyBytes_Check(result) &&
            !PyUnicode_Check(result)) {
            Py_DECREF(result);
            result = NULL;
            PyErr_SetString(PyExc_TypeError,
                       "object.readline() returned non-string");
        }
    }

    if (n < 0 && result != NULL && PyBytes_Check(result)) {
        char *s = PyBytes_AS_STRING(result);
        Py_ssize_t len = PyBytes_GET_SIZE(result);
        if (len == 0) {
            Py_DECREF(result);
            result = NULL;
            PyErr_SetString(PyExc_EOFError,
                            "EOF when reading a line");
        }
        else if (s[len-1] == '\n') {
            if (result->ob_refcnt == 1)
                _PyBytes_Resize(&result, len-1);
            else {
                PyObject *v;
                v = PyBytes_FromStringAndSize(s, len-1);
                Py_DECREF(result);
                result = v;
            }
        }
    }
    if (n < 0 && result != NULL && PyUnicode_Check(result)) {
        Py_UNICODE *s = PyUnicode_AS_UNICODE(result);
        Py_ssize_t len = PyUnicode_GET_SIZE(result);
        if (len == 0) {
            Py_DECREF(result);
            result = NULL;
            PyErr_SetString(PyExc_EOFError,
                            "EOF when reading a line");
        }
        else if (s[len-1] == '\n') {
            if (result->ob_refcnt == 1)
                PyUnicode_Resize(&result, len-1);
            else {
                PyObject *v;
                v = PyUnicode_FromUnicode(s, len-1);
                Py_DECREF(result);
                result = v;
            }
        }
    }
    return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 355 of file fileobject.c.

{
    PyStdPrinter_Object *self;

    if (fd != fileno(stdout) && fd != fileno(stderr)) {
        /* not enough infrastructure for PyErr_BadInternalCall() */
        return NULL;
    }

    self = PyObject_New(PyStdPrinter_Object,
                        &PyStdPrinter_Type);
    if (self != NULL) {
        self->fd = fd;
    }
    return (PyObject*)self;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int PyFile_WriteObject ( PyObject v,
PyObject f,
int  flags 
)

Definition at line 131 of file fileobject.c.

{
    PyObject *writer, *value, *args, *result;
    if (f == NULL) {
        PyErr_SetString(PyExc_TypeError, "writeobject with NULL file");
        return -1;
    }
    writer = PyObject_GetAttrString(f, "write");
    if (writer == NULL)
        return -1;
    if (flags & Py_PRINT_RAW) {
        value = PyObject_Str(v);
    }
    else
        value = PyObject_Repr(v);
    if (value == NULL) {
        Py_DECREF(writer);
        return -1;
    }
    args = PyTuple_Pack(1, value);
    if (args == NULL) {
        Py_DECREF(value);
        Py_DECREF(writer);
        return -1;
    }
    result = PyEval_CallObject(writer, args);
    Py_DECREF(args);
    Py_DECREF(value);
    Py_DECREF(writer);
    if (result == NULL)
        return -1;
    Py_DECREF(result);
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int PyFile_WriteString ( const char *  s,
PyObject f 
)

Definition at line 167 of file fileobject.c.

{
    if (f == NULL) {
        /* Should be caused by a pre-existing error */
        if (!PyErr_Occurred())
            PyErr_SetString(PyExc_SystemError,
                            "null file for PyFile_WriteString");
        return -1;
    }
    else if (!PyErr_Occurred()) {
        PyObject *v = PyUnicode_FromString(s);
        int err;
        if (v == NULL)
            return -1;
        err = PyFile_WriteObject(v, f, Py_PRINT_RAW);
        Py_DECREF(v);
        return err;
    }
    else
        return -1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 197 of file fileobject.c.

{
    int fd;
    PyObject *meth;

    if (PyLong_Check(o)) {
        fd = PyLong_AsLong(o);
    }
    else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL)
    {
        PyObject *fno = PyEval_CallObject(meth, NULL);
        Py_DECREF(meth);
        if (fno == NULL)
            return -1;

        if (PyLong_Check(fno)) {
            fd = PyLong_AsLong(fno);
            Py_DECREF(fno);
        }
        else {
            PyErr_SetString(PyExc_TypeError,
                            "fileno() returned a non-integer");
            Py_DECREF(fno);
            return -1;
        }
    }
    else {
        PyErr_SetString(PyExc_TypeError,
                        "argument must be an int, or have a fileno() method.");
        return -1;
    }

    if (fd == -1 && PyErr_Occurred())
        return -1;
    if (fd < 0) {
        PyErr_Format(PyExc_ValueError,
                     "file descriptor cannot be a negative integer (%i)",
                     fd);
        return -1;
    }
    return fd;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PyObject* stdprinter_fileno ( PyStdPrinter_Object self) [static]

Definition at line 413 of file fileobject.c.

{
    return PyLong_FromLong((long) self->fd);
}

Here is the call graph for this function:

static int stdprinter_init ( PyObject self,
PyObject args,
PyObject kwds 
) [static]

Definition at line 347 of file fileobject.c.

{
    PyErr_SetString(PyExc_TypeError,
                    "cannot create 'stderrprinter' instances");
    return -1;
}

Here is the call graph for this function:

static PyObject* stdprinter_isatty ( PyStdPrinter_Object self) [static]

Definition at line 432 of file fileobject.c.

{
    long res;
    if (self->fd < 0) {
        Py_RETURN_FALSE;
    }

    Py_BEGIN_ALLOW_THREADS
    res = isatty(self->fd);
    Py_END_ALLOW_THREADS

    return PyBool_FromLong(res);
}

Here is the call graph for this function:

static PyObject* stdprinter_new ( PyTypeObject type,
PyObject args,
PyObject kews 
) [static]

Definition at line 332 of file fileobject.c.

{
    PyStdPrinter_Object *self;

    assert(type != NULL && type->tp_alloc != NULL);

    self = (PyStdPrinter_Object *) type->tp_alloc(type, 0);
    if (self != NULL) {
        self->fd = -1;
    }

    return (PyObject *) self;
}
static PyObject* stdprinter_noop ( PyStdPrinter_Object self) [static]

Definition at line 426 of file fileobject.c.

static PyObject* stdprinter_repr ( PyStdPrinter_Object self) [static]

Definition at line 419 of file fileobject.c.

{
    return PyUnicode_FromFormat("<stdprinter(fd=%d) object at 0x%x>",
                                self->fd, self);
}
static PyObject* stdprinter_write ( PyStdPrinter_Object self,
PyObject args 
) [static]

Definition at line 373 of file fileobject.c.

{
    char *c;
    Py_ssize_t n;

    if (self->fd < 0) {
        /* fd might be invalid on Windows
         * I can't raise an exception here. It may lead to an
         * unlimited recursion in the case stderr is invalid.
         */
        Py_RETURN_NONE;
    }

    if (!PyArg_ParseTuple(args, "s", &c)) {
        return NULL;
    }
    n = strlen(c);

    Py_BEGIN_ALLOW_THREADS
    errno = 0;
#if defined(MS_WIN64) || defined(MS_WINDOWS)
    if (n > INT_MAX)
        n = INT_MAX;
    n = write(self->fd, c, (int)n);
#else
    n = write(self->fd, c, n);
#endif
    Py_END_ALLOW_THREADS

    if (n < 0) {
        if (errno == EAGAIN)
            Py_RETURN_NONE;
        PyErr_SetFromErrno(PyExc_IOError);
        return NULL;
    }

    return PyLong_FromSsize_t(n);
}

Here is the call graph for this function:


Variable Documentation

Definition at line 481 of file fileobject.c.

Initial value:
 {
    {"closed", (getter)get_closed, NULL, "True if the file is closed"},
    {"encoding", (getter)get_encoding, NULL, "Encoding of the file"},
    {"mode", (getter)get_mode, NULL, "String giving the file mode"},
    {0},
}

Definition at line 474 of file fileobject.c.

Initial value:

Definition at line 446 of file fileobject.c.