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 |
| 12 | change in future releases of Python. |
| 13 | |
| 14 | |
| 15 | .. ctype:: PyFileObject |
| 16 | |
| 17 | This subtype of :ctype:`PyObject` represents a Python file object. |
| 18 | |
| 19 | |
| 20 | .. cvar:: PyTypeObject PyFile_Type |
| 21 | |
| 22 | .. index:: single: FileType (in module types) |
| 23 | |
| 24 | This instance of :ctype:`PyTypeObject` represents the Python file type. This is |
| 25 | exposed to Python programs as ``file`` and ``types.FileType``. |
| 26 | |
| 27 | |
| 28 | .. cfunction:: int PyFile_Check(PyObject *p) |
| 29 | |
| 30 | Return true if its argument is a :ctype:`PyFileObject` or a subtype of |
| 31 | :ctype:`PyFileObject`. |
| 32 | |
| 33 | |
| 34 | .. cfunction:: int PyFile_CheckExact(PyObject *p) |
| 35 | |
| 36 | Return true if its argument is a :ctype:`PyFileObject`, but not a subtype of |
| 37 | :ctype:`PyFileObject`. |
| 38 | |
| 39 | |
| 40 | .. cfunction:: PyFile_FromFd(int fd, char *name, char *mode, int buffering, char *encoding, char *newline, int closefd) |
| 41 | |
| 42 | Create a new :ctype:`PyFileObject` from the file descriptor of an already |
| 43 | opened file *fd*. The arguments *name*, *encoding* and *newline* can be |
| 44 | *NULL* to use the defaults; *buffering* can be *-1* to use the default. |
| 45 | Return *NULL* on failure. |
| 46 | |
| 47 | .. warning:: |
| 48 | |
| 49 | Take care when you are mixing streams and descriptors! For more |
| 50 | information, see `the GNU C Library docs |
| 51 | <http://www.gnu.org/software/libc/manual/html_node/Stream_002fDescriptor-Precautions.html#Stream_002fDescriptor-Precautions>`_. |
| 52 | |
| 53 | |
| 54 | .. cfunction:: int PyObject_AsFileDescriptor(PyObject *p) |
| 55 | |
| 56 | Return the file descriptor associated with *p* as an :ctype:`int`. If the |
| 57 | object is an integer, its value is returned. If not, the |
| 58 | object's :meth:`fileno` method is called if it exists; the method must return |
| 59 | an integer, which is returned as the file descriptor value. Sets an |
| 60 | exception and returns ``-1`` on failure. |
| 61 | |
| 62 | |
| 63 | .. cfunction:: PyObject* PyFile_GetLine(PyObject *p, int n) |
| 64 | |
| 65 | .. index:: single: EOFError (built-in exception) |
| 66 | |
| 67 | Equivalent to ``p.readline([n])``, this function reads one line from the |
| 68 | object *p*. *p* may be a file object or any object with a :meth:`readline` |
| 69 | method. If *n* is ``0``, exactly one line is read, regardless of the length of |
| 70 | the line. If *n* is greater than ``0``, no more than *n* bytes will be read |
| 71 | from the file; a partial line can be returned. In both cases, an empty string |
| 72 | is returned if the end of the file is reached immediately. If *n* is less than |
| 73 | ``0``, however, one line is read regardless of length, but :exc:`EOFError` is |
| 74 | raised if the end of the file is reached immediately. |
| 75 | |
| 76 | |
| 77 | .. cfunction:: PyObject* PyFile_Name(PyObject *p) |
| 78 | |
| 79 | Return the name of the file specified by *p* as a string object. |
| 80 | |
| 81 | |
| 82 | .. cfunction:: void PyFile_SetBufSize(PyFileObject *p, int n) |
| 83 | |
| 84 | .. index:: single: setvbuf() |
| 85 | |
| 86 | Available on systems with :cfunc:`setvbuf` only. This should only be called |
| 87 | immediately after file object creation. |
| 88 | |
| 89 | |
| 90 | .. cfunction:: int PyFile_SetEncoding(PyFileObject *p, const char *enc) |
| 91 | |
| 92 | Set the file's encoding for Unicode output to *enc*. Return 1 on success and 0 |
| 93 | on failure. |
| 94 | |
| 95 | |
| 96 | .. cfunction:: int PyFile_SoftSpace(PyObject *p, int newflag) |
| 97 | |
| 98 | .. index:: single: softspace (file attribute) |
| 99 | |
| 100 | This function exists for internal use by the interpreter. Set the |
| 101 | :attr:`softspace` attribute of *p* to *newflag* and return the previous value. |
| 102 | *p* does not have to be a file object for this function to work properly; any |
| 103 | object is supported (thought its only interesting if the :attr:`softspace` |
| 104 | attribute can be set). This function clears any errors, and will return ``0`` |
| 105 | as the previous value if the attribute either does not exist or if there were |
| 106 | errors in retrieving it. There is no way to detect errors from this function, |
| 107 | but doing so should not be needed. |
| 108 | |
| 109 | |
| 110 | .. cfunction:: int PyFile_WriteObject(PyObject *obj, PyObject *p, int flags) |
| 111 | |
| 112 | .. index:: single: Py_PRINT_RAW |
| 113 | |
| 114 | Write object *obj* to file object *p*. The only supported flag for *flags* is |
| 115 | :const:`Py_PRINT_RAW`; if given, the :func:`str` of the object is written |
| 116 | instead of the :func:`repr`. Return ``0`` on success or ``-1`` on failure; the |
| 117 | appropriate exception will be set. |
| 118 | |
| 119 | |
| 120 | .. cfunction:: int PyFile_WriteString(const char *s, PyObject *p) |
| 121 | |
| 122 | Write string *s* to file object *p*. Return ``0`` on success or ``-1`` on |
| 123 | failure; the appropriate exception will be set. |