Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 1 | .. highlightlang:: c |
| 2 | |
| 3 | .. _fileobjects: |
| 4 | |
| 5 | File Objects |
| 6 | ------------ |
| 7 | |
| 8 | .. index:: object: file |
| 9 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 10 | Python's built-in file objects are implemented entirely on the :c:type:`FILE\*` |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 11 | support from the C standard library. This is an implementation detail and may |
| 12 | change in future releases of Python. |
| 13 | |
| 14 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 15 | .. c:type:: PyFileObject |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 16 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 17 | This subtype of :c:type:`PyObject` represents a Python file object. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 18 | |
| 19 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 20 | .. c:var:: PyTypeObject PyFile_Type |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 21 | |
| 22 | .. index:: single: FileType (in module types) |
| 23 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 24 | This instance of :c:type:`PyTypeObject` represents the Python file type. This is |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 25 | exposed to Python programs as ``file`` and ``types.FileType``. |
| 26 | |
| 27 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 28 | .. c:function:: int PyFile_Check(PyObject *p) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 29 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 30 | Return true if its argument is a :c:type:`PyFileObject` or a subtype of |
| 31 | :c:type:`PyFileObject`. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 32 | |
| 33 | .. versionchanged:: 2.2 |
| 34 | Allowed subtypes to be accepted. |
| 35 | |
| 36 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 37 | .. c:function:: int PyFile_CheckExact(PyObject *p) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 38 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 39 | Return true if its argument is a :c:type:`PyFileObject`, but not a subtype of |
| 40 | :c:type:`PyFileObject`. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 41 | |
| 42 | .. versionadded:: 2.2 |
| 43 | |
| 44 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 45 | .. c:function:: PyObject* PyFile_FromString(char *filename, char *mode) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 46 | |
| 47 | .. index:: single: fopen() |
| 48 | |
| 49 | On success, return a new file object that is opened on the file given by |
| 50 | *filename*, with a file mode given by *mode*, where *mode* has the same |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 51 | semantics as the standard C routine :c:func:`fopen`. On failure, return *NULL*. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 52 | |
| 53 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 54 | .. c:function:: PyObject* PyFile_FromFile(FILE *fp, char *name, char *mode, int (*close)(FILE*)) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 55 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 56 | Create a new :c:type:`PyFileObject` from the already-open standard C file |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 57 | pointer, *fp*. The function *close* will be called when the file should be |
Victor Stinner | 63c22fa | 2011-09-23 19:37:03 +0200 | [diff] [blame] | 58 | closed. Return *NULL* and close the file using *close* on failure. |
| 59 | *close* is optional and can be set to *NULL*. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 60 | |
| 61 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 62 | .. c:function:: FILE* PyFile_AsFile(PyObject \*p) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 63 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 64 | Return the file object associated with *p* as a :c:type:`FILE\*`. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 65 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 66 | If the caller will ever use the returned :c:type:`FILE\*` object while |
| 67 | the :term:`GIL` is released it must also call the :c:func:`PyFile_IncUseCount` and |
| 68 | :c:func:`PyFile_DecUseCount` functions described below as appropriate. |
Gregory P. Smith | aa63d0d | 2008-04-06 23:11:17 +0000 | [diff] [blame] | 69 | |
| 70 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 71 | .. c:function:: void PyFile_IncUseCount(PyFileObject \*p) |
Gregory P. Smith | aa63d0d | 2008-04-06 23:11:17 +0000 | [diff] [blame] | 72 | |
| 73 | Increments the PyFileObject's internal use count to indicate |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 74 | that the underlying :c:type:`FILE\*` is being used. |
Gregory P. Smith | aa63d0d | 2008-04-06 23:11:17 +0000 | [diff] [blame] | 75 | This prevents Python from calling f_close() on it from another thread. |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 76 | Callers of this must call :c:func:`PyFile_DecUseCount` when they are |
| 77 | finished with the :c:type:`FILE\*`. Otherwise the file object will |
Gregory P. Smith | aa63d0d | 2008-04-06 23:11:17 +0000 | [diff] [blame] | 78 | never be closed by Python. |
| 79 | |
Georg Brandl | 63e284d | 2010-10-15 17:52:59 +0000 | [diff] [blame] | 80 | The :term:`GIL` must be held while calling this function. |
Gregory P. Smith | aa63d0d | 2008-04-06 23:11:17 +0000 | [diff] [blame] | 81 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 82 | The suggested use is to call this after :c:func:`PyFile_AsFile` and before |
Georg Brandl | 63e284d | 2010-10-15 17:52:59 +0000 | [diff] [blame] | 83 | you release the GIL:: |
| 84 | |
| 85 | FILE *fp = PyFile_AsFile(p); |
| 86 | PyFile_IncUseCount(p); |
| 87 | /* ... */ |
| 88 | Py_BEGIN_ALLOW_THREADS |
| 89 | do_something(fp); |
| 90 | Py_END_ALLOW_THREADS |
| 91 | /* ... */ |
| 92 | PyFile_DecUseCount(p); |
Gregory P. Smith | aa63d0d | 2008-04-06 23:11:17 +0000 | [diff] [blame] | 93 | |
| 94 | .. versionadded:: 2.6 |
| 95 | |
| 96 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 97 | .. c:function:: void PyFile_DecUseCount(PyFileObject \*p) |
Gregory P. Smith | aa63d0d | 2008-04-06 23:11:17 +0000 | [diff] [blame] | 98 | |
| 99 | Decrements the PyFileObject's internal unlocked_count member to |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 100 | indicate that the caller is done with its own use of the :c:type:`FILE\*`. |
| 101 | This may only be called to undo a prior call to :c:func:`PyFile_IncUseCount`. |
Gregory P. Smith | aa63d0d | 2008-04-06 23:11:17 +0000 | [diff] [blame] | 102 | |
Georg Brandl | 63e284d | 2010-10-15 17:52:59 +0000 | [diff] [blame] | 103 | The :term:`GIL` must be held while calling this function (see the example |
| 104 | above). |
Gregory P. Smith | aa63d0d | 2008-04-06 23:11:17 +0000 | [diff] [blame] | 105 | |
| 106 | .. versionadded:: 2.6 |
| 107 | |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 108 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 109 | .. c:function:: PyObject* PyFile_GetLine(PyObject *p, int n) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 110 | |
| 111 | .. index:: single: EOFError (built-in exception) |
| 112 | |
| 113 | Equivalent to ``p.readline([n])``, this function reads one line from the |
| 114 | object *p*. *p* may be a file object or any object with a :meth:`readline` |
| 115 | method. If *n* is ``0``, exactly one line is read, regardless of the length of |
| 116 | the line. If *n* is greater than ``0``, no more than *n* bytes will be read |
| 117 | from the file; a partial line can be returned. In both cases, an empty string |
| 118 | is returned if the end of the file is reached immediately. If *n* is less than |
| 119 | ``0``, however, one line is read regardless of length, but :exc:`EOFError` is |
| 120 | raised if the end of the file is reached immediately. |
| 121 | |
| 122 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 123 | .. c:function:: PyObject* PyFile_Name(PyObject *p) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 124 | |
| 125 | Return the name of the file specified by *p* as a string object. |
| 126 | |
| 127 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 128 | .. c:function:: void PyFile_SetBufSize(PyFileObject *p, int n) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 129 | |
| 130 | .. index:: single: setvbuf() |
| 131 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 132 | Available on systems with :c:func:`setvbuf` only. This should only be called |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 133 | immediately after file object creation. |
| 134 | |
| 135 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 136 | .. c:function:: int PyFile_SetEncoding(PyFileObject *p, const char *enc) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 137 | |
| 138 | Set the file's encoding for Unicode output to *enc*. Return 1 on success and 0 |
| 139 | on failure. |
| 140 | |
| 141 | .. versionadded:: 2.3 |
| 142 | |
| 143 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 144 | .. c:function:: int PyFile_SetEncodingAndErrors(PyFileObject *p, const char *enc, *errors) |
Martin v. Löwis | 9981589 | 2008-06-01 07:20:46 +0000 | [diff] [blame] | 145 | |
| 146 | Set the file's encoding for Unicode output to *enc*, and its error |
| 147 | mode to *err*. Return 1 on success and 0 on failure. |
| 148 | |
| 149 | .. versionadded:: 2.6 |
| 150 | |
| 151 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 152 | .. c:function:: int PyFile_SoftSpace(PyObject *p, int newflag) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 153 | |
| 154 | .. index:: single: softspace (file attribute) |
| 155 | |
| 156 | This function exists for internal use by the interpreter. Set the |
| 157 | :attr:`softspace` attribute of *p* to *newflag* and return the previous value. |
| 158 | *p* does not have to be a file object for this function to work properly; any |
| 159 | object is supported (thought its only interesting if the :attr:`softspace` |
| 160 | attribute can be set). This function clears any errors, and will return ``0`` |
| 161 | as the previous value if the attribute either does not exist or if there were |
| 162 | errors in retrieving it. There is no way to detect errors from this function, |
| 163 | but doing so should not be needed. |
| 164 | |
| 165 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 166 | .. c:function:: int PyFile_WriteObject(PyObject *obj, PyObject *p, int flags) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 167 | |
| 168 | .. index:: single: Py_PRINT_RAW |
| 169 | |
| 170 | Write object *obj* to file object *p*. The only supported flag for *flags* is |
| 171 | :const:`Py_PRINT_RAW`; if given, the :func:`str` of the object is written |
| 172 | instead of the :func:`repr`. Return ``0`` on success or ``-1`` on failure; the |
| 173 | appropriate exception will be set. |
| 174 | |
| 175 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 176 | .. c:function:: int PyFile_WriteString(const char *s, PyObject *p) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 177 | |
| 178 | Write string *s* to file object *p*. Return ``0`` on success or ``-1`` on |
| 179 | failure; the appropriate exception will be set. |