파일 객체

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)
Return value: New reference. Part of the Stable ABI.

이미 열려있는 파일의 파일 기술자 fd로 파이썬 파일 객체를 만듭니다. 인자 name, encoding, errorsnewline은 기본값을 사용하기 위해 NULL 일 수 있습니다; buffering은 기본값을 사용하기 위해 -1 일 수 있습니다. name은 무시되고, 이전 버전과의 호환성을 위해 유지됩니다. 실패 시 NULL을 반환합니다. 인자에 대한 더 자세한 설명은 io.open() 함수 설명서를 참조하십시오.

경고

파이썬 스트림이 자체적인 버퍼링 계층을 가지고 있으므로, 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 -1 on failure.

PyObject *PyFile_GetLine(PyObject *p, int n)
Return value: New reference. Part of the Stable ABI.

p.readline([n])과 동등합니다. 이 함수는 객체 p에서 한 줄을 읽습니다. p는 파일 객체나 readline() 메서드가 있는 임의의 객체일 수 있습니다. n0이면, 줄의 길이와 관계없이 정확히 한 줄을 읽습니다. n0보다 크면, n 바이트 이상을 파일에서 읽지 않습니다; 불완전한 줄이 반환될 수 있습니다. 두 경우 모두, 파일 끝에 즉시 도달하면 빈 문자열이 반환됩니다. 그러나 n0보다 작으면, 길이와 관계없이 한 줄을 읽지만, 파일 끝에 즉시 도달하면 EOFError가 발생합니다.

int PyFile_SetOpenCodeHook(Py_OpenCodeHookFunction handler)

제공된 handler를 통해 매개 변수를 전달하도록 io.open_code()의 일반적인 동작을 재정의합니다.

The handler is a function of type:

type Py_OpenCodeHookFunction

Equivalent of PyObject *(*)(PyObject *path, void *userData), where path is guaranteed to be PyUnicodeObject.

userData 포인터는 훅 함수로 전달됩니다. 훅 함수는 다른 런타임에서 호출될 수 있으므로, 이 포인터는 파이썬 상태를 직접 참조하면 안 됩니다.

이 훅은 의도적으로 임포트 중에 사용되므로, 고정되었거나(frozen) 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, the str() of the object is written instead of the repr(). Return 0 on success or -1 on failure; the appropriate exception will be set.

int PyFile_WriteString(const char *s, PyObject *p)
Part of the Stable ABI.

문자열 s를 파일 객체 p에 씁니다. 성공하면 0을 반환하고, 실패하면 -1을 반환합니다; 적절한 예외가 설정됩니다.