| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1 | .. highlightlang:: c | 
|  | 2 |  | 
|  | 3 | .. _fileobjects: | 
|  | 4 |  | 
|  | 5 | File Objects | 
|  | 6 | ------------ | 
|  | 7 |  | 
|  | 8 | .. index:: object: file | 
|  | 9 |  | 
| Antoine Pitrou | 0460288 | 2010-06-15 17:30:16 +0000 | [diff] [blame] | 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. | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 18 |  | 
|  | 19 |  | 
| Antoine Pitrou | 5d8a6c8 | 2010-06-15 17:00:21 +0000 | [diff] [blame] | 20 | .. cfunction:: PyFile_FromFd(int fd, char *name, char *mode, int buffering, char *encoding, char *errors, char *newline, int closefd) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 21 |  | 
| Antoine Pitrou | 0460288 | 2010-06-15 17:30:16 +0000 | [diff] [blame] | 22 | Create a Python file object from the file descriptor of an already | 
|  | 23 | opened file *fd*.  The arguments *name*, *encoding*, *errors* and *newline* | 
| Antoine Pitrou | 5d8a6c8 | 2010-06-15 17:00:21 +0000 | [diff] [blame] | 24 | can be *NULL* to use the defaults; *buffering* can be *-1* to use the | 
| Victor Stinner | 3603cc5 | 2010-08-13 13:34:52 +0000 | [diff] [blame] | 25 | default. *name* is ignored and kept for backward compatibility. Return | 
|  | 26 | *NULL* on failure. For a more comprehensive description of the arguments, | 
|  | 27 | please refer to the :func:`io.open` function documentation. | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 28 |  | 
|  | 29 | .. warning:: | 
|  | 30 |  | 
| Antoine Pitrou | 0460288 | 2010-06-15 17:30:16 +0000 | [diff] [blame] | 31 | 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 Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 34 |  | 
| Victor Stinner | 3603cc5 | 2010-08-13 13:34:52 +0000 | [diff] [blame] | 35 | .. versionchanged:: 3.2 | 
|  | 36 | Ignore *name* attribute. | 
|  | 37 |  | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 38 |  | 
|  | 39 | .. cfunction:: int PyObject_AsFileDescriptor(PyObject *p) | 
|  | 40 |  | 
|  | 41 | Return the file descriptor associated with *p* as an :ctype:`int`.  If the | 
|  | 42 | object is an integer, its value is returned.  If not, the | 
|  | 43 | object's :meth:`fileno` method is called if it exists; the method must return | 
|  | 44 | an integer, which is returned as the file descriptor value.  Sets an | 
|  | 45 | exception and returns ``-1`` on failure. | 
|  | 46 |  | 
|  | 47 |  | 
|  | 48 | .. cfunction:: PyObject* PyFile_GetLine(PyObject *p, int n) | 
|  | 49 |  | 
|  | 50 | .. index:: single: EOFError (built-in exception) | 
|  | 51 |  | 
|  | 52 | Equivalent to ``p.readline([n])``, this function reads one line from the | 
|  | 53 | object *p*.  *p* may be a file object or any object with a :meth:`readline` | 
|  | 54 | method.  If *n* is ``0``, exactly one line is read, regardless of the length of | 
|  | 55 | the line.  If *n* is greater than ``0``, no more than *n* bytes will be read | 
|  | 56 | from the file; a partial line can be returned.  In both cases, an empty string | 
|  | 57 | is returned if the end of the file is reached immediately.  If *n* is less than | 
|  | 58 | ``0``, however, one line is read regardless of length, but :exc:`EOFError` is | 
|  | 59 | raised if the end of the file is reached immediately. | 
|  | 60 |  | 
|  | 61 |  | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 62 | .. cfunction:: int PyFile_WriteObject(PyObject *obj, PyObject *p, int flags) | 
|  | 63 |  | 
|  | 64 | .. index:: single: Py_PRINT_RAW | 
|  | 65 |  | 
|  | 66 | Write object *obj* to file object *p*.  The only supported flag for *flags* is | 
|  | 67 | :const:`Py_PRINT_RAW`; if given, the :func:`str` of the object is written | 
|  | 68 | instead of the :func:`repr`.  Return ``0`` on success or ``-1`` on failure; the | 
|  | 69 | appropriate exception will be set. | 
|  | 70 |  | 
|  | 71 |  | 
|  | 72 | .. cfunction:: int PyFile_WriteString(const char *s, PyObject *p) | 
|  | 73 |  | 
|  | 74 | Write string *s* to file object *p*.  Return ``0`` on success or ``-1`` on | 
|  | 75 | failure; the appropriate exception will be set. |