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 | |
| 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 |
Benjamin Peterson | 487b9dc | 2008-08-23 21:04:47 +0000 | [diff] [blame] | 12 | change in future releases of Python. The ``PyFile_`` APIs are a wrapper over |
| 13 | the :mod:`io` module. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 14 | |
| 15 | |
Antoine Pitrou | 5d8a6c8 | 2010-06-15 17:00:21 +0000 | [diff] [blame^] | 16 | .. 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] | 17 | |
| 18 | Create a new :ctype:`PyFileObject` from the file descriptor of an already |
Antoine Pitrou | 5d8a6c8 | 2010-06-15 17:00:21 +0000 | [diff] [blame^] | 19 | 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 Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 23 | |
| 24 | .. warning:: |
| 25 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 26 | Take care when you are mixing streams and descriptors! For more |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 27 | 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 Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 54 | .. 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. |