8
8
.. index:: object: file
10
Python's built-in file objects are implemented entirely on the :ctype:`FILE\*`
11
support from the C standard library. This is an implementation detail and may
12
change in future releases of Python. The ``PyFile_`` APIs are a wrapper over
16
.. cfunction:: PyFile_FromFd(int fd, char *name, char *mode, int buffering, char *encoding, char *newline, int closefd)
18
Create a new :ctype:`PyFileObject` from the file descriptor of an already
19
opened file *fd*. The arguments *name*, *encoding* and *newline* can be
20
*NULL* to use the defaults; *buffering* can be *-1* to use the default.
21
Return *NULL* on failure.
10
These APIs are a minimal emulation of the Python 2 C API for built-in file
11
objects, which used to rely on the buffered I/O (:ctype:`FILE\*`) support
12
from the C standard library. In Python 3, files and streams use the new
13
:mod:`io` module, which defines several layers over the low-level unbuffered
14
I/O of the operating system. The functions described below are
15
convenience C wrappers over these new APIs, and meant mostly for internal
16
error reporting in the interpreter; third-party code is advised to access
17
the :mod:`io` APIs instead.
20
.. cfunction:: PyFile_FromFd(int fd, char *name, char *mode, int buffering, char *encoding, char *errors, char *newline, int closefd)
22
Create a Python file object from the file descriptor of an already
23
opened file *fd*. The arguments *name*, *encoding*, *errors* and *newline*
24
can be *NULL* to use the defaults; *buffering* can be *-1* to use the
25
default. Return *NULL* on failure. For a more comprehensive description of
26
the arguments, please refer to the :func:`io.open` function documentation.
25
Take care when you are mixing streams and descriptors! For more
26
information, see `the GNU C Library docs
27
<http://www.gnu.org/software/libc/manual/html_node/Stream_002fDescriptor-Precautions.html#Stream_002fDescriptor-Precautions>`_.
30
Since Python streams have their own buffering layer, mixing them with
31
OS-level file descriptors can produce various issues (such as unexpected
30
35
.. cfunction:: int PyObject_AsFileDescriptor(PyObject *p)