/Doc/c-api/file.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 168 lines · 97 code · 71 blank · 0 comment · 0 complexity · 5196075dc45a906d28cde7cbcf7d6d77 MD5 · raw file

  1. .. highlightlang:: c
  2. .. _fileobjects:
  3. File Objects
  4. ------------
  5. .. index:: object: file
  6. Python's built-in file objects are implemented entirely on the :ctype:`FILE\*`
  7. support from the C standard library. This is an implementation detail and may
  8. change in future releases of Python.
  9. .. ctype:: PyFileObject
  10. This subtype of :ctype:`PyObject` represents a Python file object.
  11. .. cvar:: PyTypeObject PyFile_Type
  12. .. index:: single: FileType (in module types)
  13. This instance of :ctype:`PyTypeObject` represents the Python file type. This is
  14. exposed to Python programs as ``file`` and ``types.FileType``.
  15. .. cfunction:: int PyFile_Check(PyObject *p)
  16. Return true if its argument is a :ctype:`PyFileObject` or a subtype of
  17. :ctype:`PyFileObject`.
  18. .. versionchanged:: 2.2
  19. Allowed subtypes to be accepted.
  20. .. cfunction:: int PyFile_CheckExact(PyObject *p)
  21. Return true if its argument is a :ctype:`PyFileObject`, but not a subtype of
  22. :ctype:`PyFileObject`.
  23. .. versionadded:: 2.2
  24. .. cfunction:: PyObject* PyFile_FromString(char *filename, char *mode)
  25. .. index:: single: fopen()
  26. On success, return a new file object that is opened on the file given by
  27. *filename*, with a file mode given by *mode*, where *mode* has the same
  28. semantics as the standard C routine :cfunc:`fopen`. On failure, return *NULL*.
  29. .. cfunction:: PyObject* PyFile_FromFile(FILE *fp, char *name, char *mode, int (*close)(FILE*))
  30. Create a new :ctype:`PyFileObject` from the already-open standard C file
  31. pointer, *fp*. The function *close* will be called when the file should be
  32. closed. Return *NULL* on failure.
  33. .. cfunction:: FILE* PyFile_AsFile(PyObject \*p)
  34. Return the file object associated with *p* as a :ctype:`FILE\*`.
  35. If the caller will ever use the returned :ctype:`FILE\*` object while
  36. the GIL is released it must also call the :cfunc:`PyFile_IncUseCount` and
  37. :cfunc:`PyFile_DecUseCount` functions described below as appropriate.
  38. .. cfunction:: void PyFile_IncUseCount(PyFileObject \*p)
  39. Increments the PyFileObject's internal use count to indicate
  40. that the underlying :ctype:`FILE\*` is being used.
  41. This prevents Python from calling f_close() on it from another thread.
  42. Callers of this must call :cfunc:`PyFile_DecUseCount` when they are
  43. finished with the :ctype:`FILE\*`. Otherwise the file object will
  44. never be closed by Python.
  45. The GIL must be held while calling this function.
  46. The suggested use is to call this after :cfunc:`PyFile_AsFile` just before
  47. you release the GIL.
  48. .. versionadded:: 2.6
  49. .. cfunction:: void PyFile_DecUseCount(PyFileObject \*p)
  50. Decrements the PyFileObject's internal unlocked_count member to
  51. indicate that the caller is done with its own use of the :ctype:`FILE\*`.
  52. This may only be called to undo a prior call to :cfunc:`PyFile_IncUseCount`.
  53. The GIL must be held while calling this function.
  54. .. versionadded:: 2.6
  55. .. cfunction:: PyObject* PyFile_GetLine(PyObject *p, int n)
  56. .. index:: single: EOFError (built-in exception)
  57. Equivalent to ``p.readline([n])``, this function reads one line from the
  58. object *p*. *p* may be a file object or any object with a :meth:`readline`
  59. method. If *n* is ``0``, exactly one line is read, regardless of the length of
  60. the line. If *n* is greater than ``0``, no more than *n* bytes will be read
  61. from the file; a partial line can be returned. In both cases, an empty string
  62. is returned if the end of the file is reached immediately. If *n* is less than
  63. ``0``, however, one line is read regardless of length, but :exc:`EOFError` is
  64. raised if the end of the file is reached immediately.
  65. .. cfunction:: PyObject* PyFile_Name(PyObject *p)
  66. Return the name of the file specified by *p* as a string object.
  67. .. cfunction:: void PyFile_SetBufSize(PyFileObject *p, int n)
  68. .. index:: single: setvbuf()
  69. Available on systems with :cfunc:`setvbuf` only. This should only be called
  70. immediately after file object creation.
  71. .. cfunction:: int PyFile_SetEncoding(PyFileObject *p, const char *enc)
  72. Set the file's encoding for Unicode output to *enc*. Return 1 on success and 0
  73. on failure.
  74. .. versionadded:: 2.3
  75. .. cfunction:: int PyFile_SetEncodingAndErrors(PyFileObject *p, const char *enc, *errors)
  76. Set the file's encoding for Unicode output to *enc*, and its error
  77. mode to *err*. Return 1 on success and 0 on failure.
  78. .. versionadded:: 2.6
  79. .. cfunction:: int PyFile_SoftSpace(PyObject *p, int newflag)
  80. .. index:: single: softspace (file attribute)
  81. This function exists for internal use by the interpreter. Set the
  82. :attr:`softspace` attribute of *p* to *newflag* and return the previous value.
  83. *p* does not have to be a file object for this function to work properly; any
  84. object is supported (thought its only interesting if the :attr:`softspace`
  85. attribute can be set). This function clears any errors, and will return ``0``
  86. as the previous value if the attribute either does not exist or if there were
  87. errors in retrieving it. There is no way to detect errors from this function,
  88. but doing so should not be needed.
  89. .. cfunction:: int PyFile_WriteObject(PyObject *obj, PyObject *p, int flags)
  90. .. index:: single: Py_PRINT_RAW
  91. Write object *obj* to file object *p*. The only supported flag for *flags* is
  92. :const:`Py_PRINT_RAW`; if given, the :func:`str` of the object is written
  93. instead of the :func:`repr`. Return ``0`` on success or ``-1`` on failure; the
  94. appropriate exception will be set.
  95. .. cfunction:: int PyFile_WriteString(const char *s, PyObject *p)
  96. Write string *s* to file object *p*. Return ``0`` on success or ``-1`` on
  97. failure; the appropriate exception will be set.