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 | |
| 16 | .. cfunction:: PyFile_FromFd(int fd, char *name, char *mode, int buffering, char *encoding, char *newline, int closefd) |
| 17 | |
| 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. |
| 22 | |
| 23 | .. warning:: |
| 24 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 25 | Take care when you are mixing streams and descriptors! For more |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 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>`_. |
| 28 | |
| 29 | |
| 30 | .. cfunction:: int PyObject_AsFileDescriptor(PyObject *p) |
| 31 | |
| 32 | Return the file descriptor associated with *p* as an :ctype:`int`. If the |
| 33 | object is an integer, its value is returned. If not, the |
| 34 | object's :meth:`fileno` method is called if it exists; the method must return |
| 35 | an integer, which is returned as the file descriptor value. Sets an |
| 36 | exception and returns ``-1`` on failure. |
| 37 | |
| 38 | |
| 39 | .. cfunction:: PyObject* PyFile_GetLine(PyObject *p, int n) |
| 40 | |
| 41 | .. index:: single: EOFError (built-in exception) |
| 42 | |
| 43 | Equivalent to ``p.readline([n])``, this function reads one line from the |
| 44 | object *p*. *p* may be a file object or any object with a :meth:`readline` |
| 45 | method. If *n* is ``0``, exactly one line is read, regardless of the length of |
| 46 | the line. If *n* is greater than ``0``, no more than *n* bytes will be read |
| 47 | from the file; a partial line can be returned. In both cases, an empty string |
| 48 | is returned if the end of the file is reached immediately. If *n* is less than |
| 49 | ``0``, however, one line is read regardless of length, but :exc:`EOFError` is |
| 50 | raised if the end of the file is reached immediately. |
| 51 | |
| 52 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 53 | .. cfunction:: int PyFile_WriteObject(PyObject *obj, PyObject *p, int flags) |
| 54 | |
| 55 | .. index:: single: Py_PRINT_RAW |
| 56 | |
| 57 | Write object *obj* to file object *p*. The only supported flag for *flags* is |
| 58 | :const:`Py_PRINT_RAW`; if given, the :func:`str` of the object is written |
| 59 | instead of the :func:`repr`. Return ``0`` on success or ``-1`` on failure; the |
| 60 | appropriate exception will be set. |
| 61 | |
| 62 | |
| 63 | .. cfunction:: int PyFile_WriteString(const char *s, PyObject *p) |
| 64 | |
| 65 | Write string *s* to file object *p*. Return ``0`` on success or ``-1`` on |
| 66 | failure; the appropriate exception will be set. |