blob: 23ec5e9e5fad4cd45dca14800b071d5c5c92906b [file] [log] [blame]
Georg Brandl54a3faa2008-01-20 09:30:57 +00001.. highlightlang:: c
2
3.. _fileobjects:
4
5File Objects
6------------
7
8.. index:: object: file
9
10Python's built-in file objects are implemented entirely on the :ctype:`FILE\*`
11support from the C standard library. This is an implementation detail and may
Benjamin Peterson487b9dc2008-08-23 21:04:47 +000012change in future releases of Python. The ``PyFile_`` APIs are a wrapper over
13the :mod:`io` module.
Georg Brandl54a3faa2008-01-20 09:30:57 +000014
15
Antoine Pitrou5d8a6c82010-06-15 17:00:21 +000016.. cfunction:: PyFile_FromFd(int fd, char *name, char *mode, int buffering, char *encoding, char *errors, char *newline, int closefd)
Georg Brandl54a3faa2008-01-20 09:30:57 +000017
18 Create a new :ctype:`PyFileObject` from the file descriptor of an already
Antoine Pitrou5d8a6c82010-06-15 17:00:21 +000019 opened file *fd*. The arguments *name*, *encoding*, *errors* and *newline*
20 can be *NULL* to use the defaults; *buffering* can be *-1* to use the
21 default. Return *NULL* on failure. For a more comprehensive description of
22 the arguments, please refer to the :func:`io.open` function documentation.
Georg Brandl54a3faa2008-01-20 09:30:57 +000023
24 .. warning::
25
Georg Brandl48310cd2009-01-03 21:18:54 +000026 Take care when you are mixing streams and descriptors! For more
Georg Brandl54a3faa2008-01-20 09:30:57 +000027 information, see `the GNU C Library docs
28 <http://www.gnu.org/software/libc/manual/html_node/Stream_002fDescriptor-Precautions.html#Stream_002fDescriptor-Precautions>`_.
29
30
31.. cfunction:: int PyObject_AsFileDescriptor(PyObject *p)
32
33 Return the file descriptor associated with *p* as an :ctype:`int`. If the
34 object is an integer, its value is returned. If not, the
35 object's :meth:`fileno` method is called if it exists; the method must return
36 an integer, which is returned as the file descriptor value. Sets an
37 exception and returns ``-1`` on failure.
38
39
40.. cfunction:: PyObject* PyFile_GetLine(PyObject *p, int n)
41
42 .. index:: single: EOFError (built-in exception)
43
44 Equivalent to ``p.readline([n])``, this function reads one line from the
45 object *p*. *p* may be a file object or any object with a :meth:`readline`
46 method. If *n* is ``0``, exactly one line is read, regardless of the length of
47 the line. If *n* is greater than ``0``, no more than *n* bytes will be read
48 from the file; a partial line can be returned. In both cases, an empty string
49 is returned if the end of the file is reached immediately. If *n* is less than
50 ``0``, however, one line is read regardless of length, but :exc:`EOFError` is
51 raised if the end of the file is reached immediately.
52
53
Georg Brandl54a3faa2008-01-20 09:30:57 +000054.. cfunction:: int PyFile_WriteObject(PyObject *obj, PyObject *p, int flags)
55
56 .. index:: single: Py_PRINT_RAW
57
58 Write object *obj* to file object *p*. The only supported flag for *flags* is
59 :const:`Py_PRINT_RAW`; if given, the :func:`str` of the object is written
60 instead of the :func:`repr`. Return ``0`` on success or ``-1`` on failure; the
61 appropriate exception will be set.
62
63
64.. cfunction:: int PyFile_WriteString(const char *s, PyObject *p)
65
66 Write string *s* to file object *p*. Return ``0`` on success or ``-1`` on
67 failure; the appropriate exception will be set.