作業系統工具

PyObject *PyOS_FSPath(PyObject *path)
回傳值:新的參照。穩定 ABI 的一部分 自 3.6 版本開始.

Return the file system representation for path. If the object is a str or bytes object, then a new strong reference is returned. If the object implements the os.PathLike interface, then __fspath__() is returned as long as it is a str or bytes object. Otherwise TypeError is raised and NULL is returned.

在 3.6 版被加入.

int Py_FdIsInteractive(FILE *fp, const char *filename)

Return true (nonzero) if the standard I/O file fp with name filename is deemed interactive. This is the case for files for which isatty(fileno(fp)) is true. If the PyConfig.interactive is non-zero, this function also returns true if the filename pointer is NULL or if the name is equal to one of the strings '<stdin>' or '???'.

This function must not be called before Python is initialized.

void PyOS_BeforeFork()
穩定 ABI 的一部分 on platforms with fork() 自 3.7 版本開始.

Function to prepare some internal state before a process fork. This should be called before calling fork() or any similar function that clones the current process. Only available on systems where fork() is defined.

警告

The C fork() call should only be made from the "main" thread (of the "main" interpreter). The same is true for PyOS_BeforeFork().

在 3.7 版被加入.

void PyOS_AfterFork_Parent()
穩定 ABI 的一部分 on platforms with fork() 自 3.7 版本開始.

Function to update some internal state after a process fork. This should be called from the parent process after calling fork() or any similar function that clones the current process, regardless of whether process cloning was successful. Only available on systems where fork() is defined.

警告

The C fork() call should only be made from the "main" thread (of the "main" interpreter). The same is true for PyOS_AfterFork_Parent().

在 3.7 版被加入.

void PyOS_AfterFork_Child()
穩定 ABI 的一部分 on platforms with fork() 自 3.7 版本開始.

Function to update internal interpreter state after a process fork. This must be called from the child process after calling fork(), or any similar function that clones the current process, if there is any chance the process will call back into the Python interpreter. Only available on systems where fork() is defined.

警告

The C fork() call should only be made from the "main" thread (of the "main" interpreter). The same is true for PyOS_AfterFork_Child().

在 3.7 版被加入.

也參考

os.register_at_fork() allows registering custom Python functions to be called by PyOS_BeforeFork(), PyOS_AfterFork_Parent() and PyOS_AfterFork_Child().

void PyOS_AfterFork()
穩定 ABI 的一部分 on platforms with fork().

Function to update some internal state after a process fork; this should be called in the new process if the Python interpreter will continue to be used. If a new executable is loaded into the new process, this function does not need to be called.

在 3.7 版之後被棄用: This function is superseded by PyOS_AfterFork_Child().

int PyOS_CheckStack()
穩定 ABI 的一部分 on platforms with USE_STACKCHECK 自 3.7 版本開始.

Return true when the interpreter runs out of stack space. This is a reliable check, but is only available when USE_STACKCHECK is defined (currently on certain versions of Windows using the Microsoft Visual C++ compiler). USE_STACKCHECK will be defined automatically; you should never change the definition in your own code.

typedef void (*PyOS_sighandler_t)(int)
穩定 ABI 的一部分.
PyOS_sighandler_t PyOS_getsig(int i)
穩定 ABI 的一部分.

Return the current signal handler for signal i. This is a thin wrapper around either sigaction() or signal(). Do not call those functions directly!

PyOS_sighandler_t PyOS_setsig(int i, PyOS_sighandler_t h)
穩定 ABI 的一部分.

Set the signal handler for signal i to be h; return the old signal handler. This is a thin wrapper around either sigaction() or signal(). Do not call those functions directly!

int PyOS_InterruptOccurred(void)
穩定 ABI 的一部分.

Check if a SIGINT signal has been received.

Returns 1 if a SIGINT has occurred and clears the signal flag, or 0 otherwise.

In most cases, you should prefer PyErr_CheckSignals() over this function. PyErr_CheckSignals() invokes the appropriate signal handlers for all pending signals, allowing Python code to handle the signal properly. This function only detects SIGINT and does not invoke any Python signal handlers.

This function is async-signal-safe and this function cannot fail. The caller must hold an attached thread state.

wchar_t *Py_DecodeLocale(const char *arg, size_t *size)
穩定 ABI 的一部分 自 3.7 版本開始.

警告

This function should not be called directly: use the PyConfig API with the PyConfig_SetBytesString() function which ensures that Python is preinitialized.

This function must not be called before Python is preinitialized and so that the LC_CTYPE locale is properly configured: see the Py_PreInitialize() function.

Decode a byte string from the filesystem encoding and error handler. If the error handler is surrogateescape error handler, undecodable bytes are decoded as characters in range U+DC80..U+DCFF; and if a byte sequence can be decoded as a surrogate character, the bytes are escaped using the surrogateescape error handler instead of decoding them.

Return a pointer to a newly allocated wide character string, use PyMem_RawFree() to free the memory. If size is not NULL, write the number of wide characters excluding the null character into *size

Return NULL on decoding error or memory allocation error. If size is not NULL, *size is set to (size_t)-1 on memory error or set to (size_t)-2 on decoding error.

The filesystem encoding and error handler are selected by PyConfig_Read(): see filesystem_encoding and filesystem_errors members of PyConfig.

Decoding errors should never happen, unless there is a bug in the C library.

Use the Py_EncodeLocale() function to encode the character string back to a byte string.

在 3.5 版被加入.

在 3.7 版的變更: The function now uses the UTF-8 encoding in the Python UTF-8 Mode.

在 3.8 版的變更: The function now uses the UTF-8 encoding on Windows if PyPreConfig.legacy_windows_fs_encoding is zero;

char *Py_EncodeLocale(const wchar_t *text, size_t *error_pos)
穩定 ABI 的一部分 自 3.7 版本開始.

Encode a wide character string to the filesystem encoding and error handler. If the error handler is surrogateescape error handler, surrogate characters in the range U+DC80..U+DCFF are converted to bytes 0x80..0xFF.

Return a pointer to a newly allocated byte string, use PyMem_Free() to free the memory. Return NULL on encoding error or memory allocation error.

If error_pos is not NULL, *error_pos is set to (size_t)-1 on success, or set to the index of the invalid character on encoding error.

The filesystem encoding and error handler are selected by PyConfig_Read(): see filesystem_encoding and filesystem_errors members of PyConfig.

Use the Py_DecodeLocale() function to decode the bytes string back to a wide character string.

警告

This function must not be called before Python is preinitialized and so that the LC_CTYPE locale is properly configured: see the Py_PreInitialize() function.

在 3.5 版被加入.

在 3.7 版的變更: The function now uses the UTF-8 encoding in the Python UTF-8 Mode.

在 3.8 版的變更: The function now uses the UTF-8 encoding on Windows if PyPreConfig.legacy_windows_fs_encoding is zero.

FILE *Py_fopen(PyObject *path, const char *mode)

Similar to fopen(), but path is a Python object and an exception is set on error.