文件对象¶
These APIs are a minimal emulation of the Python 2 C API for built-in file
objects, which used to rely on the buffered I/O (FILE*) support
from the C standard library.  In Python 3, files and streams use the new
io module, which defines several layers over the low-level unbuffered
I/O of the operating system.  The functions described below are
convenience C wrappers over these new APIs, and meant mostly for internal
error reporting in the interpreter; third-party code is advised to access
the io APIs instead.
- 
PyObject *PyFile_FromFd(int fd, const char *name, const char *mode, int buffering, const char *encoding, const char *errors, const char *newline, int closefd)¶
- 返回值:新的引用。 Part of the Stable ABI.根据已打开文件 fd 的文件描述符创建一个 Python 文件对象。 参数 name, encoding, errors 和 newline 可以为 NULL表示使用默认值;buffering 可以为 -1 表示使用默认值。 name 会被忽略仅保留用于向下兼容。 失败时返回NULL。 有关参数的更全面描述,请参阅io.open()函数的文档。警告 由于Python流具有自己的缓冲层,因此将它们与 OS 级文件描述符混合会产生各种问题(例如数据的意外排序)。 在 3.2 版更改: 忽略 name 属性。 
- 
int PyObject_AsFileDescriptor(PyObject *p)¶
-  Part of the Stable ABI.Return the file descriptor associated with p as an int. If the object is an integer, its value is returned. If not, the object's fileno()method is called if it exists; the method must return an integer, which is returned as the file descriptor value. Sets an exception and returns-1on failure.
- 
PyObject *PyFile_GetLine(PyObject *p, int n)¶
- 返回值:新的引用。 Part of the Stable ABI.等价于 p.readline([n]),这个函数从对象 p 中读取一行。 p 可以是文件对象或具有readline()方法的任何对象。 如果 n 是0,则无论该行的长度如何,都会读取一行。 如果 n 大于``0``,则从文件中读取不超过 n 个字节;可以返回行的一部分。 在这两种情况下,如果立即到达文件末尾,则返回空字符串。 但是,如果 n 小于0,则无论长度如何都会读取一行,但是如果立即到达文件末尾,则引发EOFError。
- 
int PyFile_SetOpenCodeHook(Py_OpenCodeHookFunction handler)¶
- 重载 - io.open_code()的正常行为,将其形参通过所提供的处理程序来传递。- The handler is a function of type PyObject *(*)(PyObject *path, void *userData), where path is guaranteed to be - PyUnicodeObject.- userData 指针会被传入钩子函数。 因于钩子函数可能由不同的运行时调用,该指针不应直接指向 Python 状态。 - 鉴于这个钩子专门在导入期间使用的,请避免在新模块执行期间进行导入操作,除非已知它们为冻结状态或者是在 - sys.modules中可用。- 一旦钩子被设定,它就不能被移除或替换,之后对 - PyFile_SetOpenCodeHook()的调用也将失败,如果解释器已经被初始化,函数将返回 -1 并设置一个异常。- 此函数可以安全地在 - Py_Initialize()之前调用。- 引发一个 审计事件 - setopencodehook,不附带任何参数。- 3.8 新版功能. 
- 
int PyFile_WriteObject(PyObject *obj, PyObject *p, int flags)¶
-  Part of the Stable ABI.Write object obj to file object p. The only supported flag for flags is Py_PRINT_RAW; if given, thestr()of the object is written instead of therepr(). Return0on success or-1on failure; the appropriate exception will be set.
- 
int PyFile_WriteString(const char *s, PyObject *p)¶
-  Part of the Stable ABI.将字符串 s 写入文件对象 p。 成功返回 0失败返回-1;将设定相应的异常。