blob: ed3735aa83608a8545f906a909a0ae7d0a57fdc6 [file] [log] [blame]
Stéphane Wirtelcbb64842019-05-17 11:55:34 +02001.. highlight:: c
Georg Brandl54a3faa2008-01-20 09:30:57 +00002
3.. _fileobjects:
4
5File Objects
6------------
7
8.. index:: object: file
9
Antoine Pitrou04602882010-06-15 17:30:16 +000010These APIs are a minimal emulation of the Python 2 C API for built-in file
Victor Stinner474652f2020-08-13 22:11:50 +020011objects, which used to rely on the buffered I/O (:c:type:`FILE*`) support
Antoine Pitrou04602882010-06-15 17:30:16 +000012from 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
14I/O of the operating system. The functions described below are
15convenience C wrappers over these new APIs, and meant mostly for internal
16error reporting in the interpreter; third-party code is advised to access
17the :mod:`io` APIs instead.
Georg Brandl54a3faa2008-01-20 09:30:57 +000018
19
Victor Stinner474652f2020-08-13 22:11:50 +020020.. c:function:: PyObject* PyFile_FromFd(int fd, const char *name, const char *mode, int buffering, const char *encoding, const char *errors, const char *newline, int closefd)
Georg Brandl54a3faa2008-01-20 09:30:57 +000021
Antoine Pitrou04602882010-06-15 17:30:16 +000022 Create a Python file object from the file descriptor of an already
23 opened file *fd*. The arguments *name*, *encoding*, *errors* and *newline*
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020024 can be ``NULL`` to use the defaults; *buffering* can be *-1* to use the
Victor Stinner3603cc52010-08-13 13:34:52 +000025 default. *name* is ignored and kept for backward compatibility. Return
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020026 ``NULL`` on failure. For a more comprehensive description of the arguments,
Victor Stinner3603cc52010-08-13 13:34:52 +000027 please refer to the :func:`io.open` function documentation.
Georg Brandl54a3faa2008-01-20 09:30:57 +000028
29 .. warning::
30
Antoine Pitrou04602882010-06-15 17:30:16 +000031 Since Python streams have their own buffering layer, mixing them with
32 OS-level file descriptors can produce various issues (such as unexpected
33 ordering of data).
Georg Brandl54a3faa2008-01-20 09:30:57 +000034
Victor Stinner3603cc52010-08-13 13:34:52 +000035 .. versionchanged:: 3.2
36 Ignore *name* attribute.
37
Georg Brandl54a3faa2008-01-20 09:30:57 +000038
Georg Brandl60203b42010-10-06 10:11:56 +000039.. c:function:: int PyObject_AsFileDescriptor(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +000040
Georg Brandl60203b42010-10-06 10:11:56 +000041 Return the file descriptor associated with *p* as an :c:type:`int`. If the
Georg Brandl54a3faa2008-01-20 09:30:57 +000042 object is an integer, its value is returned. If not, the
Serhiy Storchaka0b68a2d2013-10-09 13:26:17 +030043 object's :meth:`~io.IOBase.fileno` method is called if it exists; the
44 method must return an integer, which is returned as the file descriptor
45 value. Sets an exception and returns ``-1`` on failure.
Georg Brandl54a3faa2008-01-20 09:30:57 +000046
47
Georg Brandl60203b42010-10-06 10:11:56 +000048.. c:function:: PyObject* PyFile_GetLine(PyObject *p, int n)
Georg Brandl54a3faa2008-01-20 09:30:57 +000049
50 .. index:: single: EOFError (built-in exception)
51
52 Equivalent to ``p.readline([n])``, this function reads one line from the
Serhiy Storchaka0b68a2d2013-10-09 13:26:17 +030053 object *p*. *p* may be a file object or any object with a
54 :meth:`~io.IOBase.readline`
Georg Brandl54a3faa2008-01-20 09:30:57 +000055 method. If *n* is ``0``, exactly one line is read, regardless of the length of
56 the line. If *n* is greater than ``0``, no more than *n* bytes will be read
57 from the file; a partial line can be returned. In both cases, an empty string
58 is returned if the end of the file is reached immediately. If *n* is less than
59 ``0``, however, one line is read regardless of length, but :exc:`EOFError` is
60 raised if the end of the file is reached immediately.
61
62
Steve Dowerb82e17e2019-05-23 08:45:22 -070063.. c:function:: int PyFile_SetOpenCodeHook(Py_OpenCodeHookFunction handler)
64
65 Overrides the normal behavior of :func:`io.open_code` to pass its parameter
66 through the provided handler.
67
68 The handler is a function of type :c:type:`PyObject *(\*)(PyObject *path,
69 void *userData)`, where *path* is guaranteed to be :c:type:`PyUnicodeObject`.
70
71 The *userData* pointer is passed into the hook function. Since hook
72 functions may be called from different runtimes, this pointer should not
73 refer directly to Python state.
74
75 As this hook is intentionally used during import, avoid importing new modules
76 during its execution unless they are known to be frozen or available in
77 ``sys.modules``.
78
79 Once a hook has been set, it cannot be removed or replaced, and later calls to
80 :c:func:`PyFile_SetOpenCodeHook` will fail. On failure, the function returns
81 -1 and sets an exception if the interpreter has been initialized.
82
83 This function is safe to call before :c:func:`Py_Initialize`.
84
Saiyang Gou3f7e9902020-10-20 12:23:15 -070085 .. audit-event:: setopencodehook "" c.PyFile_SetOpenCodeHook
86
Steve Dowerb82e17e2019-05-23 08:45:22 -070087 .. versionadded:: 3.8
88
89
90
Georg Brandl60203b42010-10-06 10:11:56 +000091.. c:function:: int PyFile_WriteObject(PyObject *obj, PyObject *p, int flags)
Georg Brandl54a3faa2008-01-20 09:30:57 +000092
93 .. index:: single: Py_PRINT_RAW
94
95 Write object *obj* to file object *p*. The only supported flag for *flags* is
96 :const:`Py_PRINT_RAW`; if given, the :func:`str` of the object is written
97 instead of the :func:`repr`. Return ``0`` on success or ``-1`` on failure; the
98 appropriate exception will be set.
99
100
Georg Brandl60203b42010-10-06 10:11:56 +0000101.. c:function:: int PyFile_WriteString(const char *s, PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000102
103 Write string *s* to file object *p*. Return ``0`` on success or ``-1`` on
104 failure; the appropriate exception will be set.