新聞中心
操作系統(tǒng)實用工具
PyObject *PyOS_FSPath(PyObject *path)

站在用戶的角度思考問題,與客戶深入溝通,找到城東網(wǎng)站設計與城東網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設計與互聯(lián)網(wǎng)技術結合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:網(wǎng)站制作、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、域名申請、虛擬主機、企業(yè)郵箱。業(yè)務覆蓋城東地區(qū)。
Return value: New reference. Part of the Stable ABI since version 3.6.
返回 path 在文件系統(tǒng)中的表示形式。 如果該對象是一個 str 或 bytes 對象,則它的引用計數(shù)將會增加。 如果該對象實現(xiàn)了 os.PathLike 接口,則只要它是一個 str 或 bytes 對象就將返回 __fspath__()。 在其他情況下將引發(fā) TypeError 并返回 NULL。
3.6 新版功能.
int Py_FdIsInteractive(FILE *fp, const char *filename)
如果名稱為 filename 的標準 I/O 文件 fp 被確認為可交互的則返回真(非零)值。 isatty(fileno(fp)) 為真值的文件均屬于這種情況。 如果全局旗標 Py_InteractiveFlag 為真值,此函數(shù)在 filename 指針為 NULL 或者其名稱等于字符串 ' 或 '???' 時也將返回真值。
void PyOS_BeforeFork()
Part of the Stable ABI on platforms with fork() since version 3.7.
在進程分叉之前準備某些內部狀態(tài)的函數(shù)。 此函數(shù)應當在調用 fork() 或者任何類似的克隆當前進程的函數(shù)之前被調用。 只適用于定義了 fork() 的系統(tǒng)。
警告
C fork() 調用應當只在 “main” 線程 (位于 “main” 解釋器) 中進行。 對于 PyOS_BeforeFork() 來說也是如此。
3.7 新版功能.
void PyOS_AfterFork_Parent()
Part of the Stable ABI on platforms with fork() since version 3.7.
在進程分叉之后更新某些內部狀態(tài)的函數(shù)。 此函數(shù)應當在調用 fork() 或任何類似的克隆當前進程的函數(shù)之后被調用,無論進程克隆是否成功。 只適用于定義了 fork() 的系統(tǒng)。
警告
C fork() 調用應當只在 “main” 線程 (位于 “main” 解釋器) 中進行。 對于 PyOS_AfterFork_Parent() 來說也是如此。
3.7 新版功能.
void PyOS_AfterFork_Child()
Part of the Stable ABI on platforms with fork() since version 3.7.
在進程分叉之后更新內部解釋器狀態(tài)的函數(shù)。 此函數(shù)必須在調用 fork() 或任何類似的克隆當前進程的函數(shù)之后在子進程中被調用,如果該進程有機會回調到 python 解釋器的話。 只適用于定義了 fork() 的系統(tǒng)。
警告
C fork() 調用應當只在 “main” 線程 (位于 “main” 解釋器) 中進行。 對于 PyOS_AfterFork_Child() 來說也是如此。
3.7 新版功能.
參見
os.register_at_fork() 允許注冊可被 PyOS_BeforeFork(), PyOS_AfterFork_Parent() 和 PyOS_AfterFork_Child() 調用的自定義 Python 函數(shù)。
void PyOS_AfterFork()
Part of the Stable ABI on platforms with fork().
在進程分叉之后更新某些內部狀態(tài)的函數(shù);如果要繼續(xù)使用 Python 解釋器則此函數(shù)應當在新進程中被調用。 如果已將一個新的可執(zhí)行文件載入到新進程中,則不需要調用此函數(shù)。
3.7 版后已移除: 此函數(shù)已被 PyOS_AfterFork_Child() 取代。
int PyOS_CheckStack()
Part of the Stable ABI on platforms with USE_STACKCHECK since version 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.
PyOS_sighandler_t PyOS_getsig(int i)
Part of the Stable 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 is a typedef alias for void (*)(int).
PyOS_sighandler_t PyOS_setsig(int i, PyOS_sighandler_t h)
Part of the Stable 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! PyOS_sighandler_t is a typedef alias for void (*)(int).
wchar_t *Py_DecodeLocale(const char *arg, size_t *size)
Part of the Stable ABI since version 3.7.
警告
此函數(shù)不應當被直接調用:請使用 PyConfig API 以及可確保 對 Python 進行預初始化 的 PyConfig_SetBytesString() 函數(shù)。
此函數(shù)不可在This function must not be called before 對 Python 進行預初始化 之前被調用以便正確地配置 LC_CTYPE 語言區(qū)域:請參閱 Py_PreInitialize() 函數(shù)。
使用 filesystem encoding and error handler 來解碼一個字節(jié)串。 如果錯誤處理句柄為 surrogateescape 錯誤處理句柄,則不可解碼的字節(jié)將被解碼為 U+DC80..U+DCFF 范圍內的字符;而如果一個字節(jié)序列可被解碼為代理字符,則其中的字節(jié)會使用 surrogateescape 錯誤處理句柄來轉義而不是解碼它們。
返回一個指向新分配的由寬字符組成的字符串的指針,使用 PyMem_RawFree() 來釋放內存。 如果 size 不為 NULL,則將排除了 null 字符的寬字符數(shù)量寫入到 *size
在解碼錯誤或內存分配錯誤時返回 NULL。 如果 size 不為 NULL,則 *size 將在內存錯誤時設為 (size_t)-1 或在解碼錯誤時設為 (size_t)-2。
filesystem encoding and error handler 是由 PyConfig_Read() 來選擇的: 參見 PyConfig 的 filesystem_encoding 和 filesystem_errors 等成員。
解碼錯誤絕對不應當發(fā)生,除非 C 庫有程序缺陷。
請使用 Py_EncodeLocale() 函數(shù)來將字符串編碼回字節(jié)串。
參見
PyUnicode_DecodeFSDefaultAndSize() 和 PyUnicode_DecodeLocaleAndSize() 函數(shù)。
3.5 新版功能.
在 3.7 版更改: 現(xiàn)在此函數(shù)在 Python UTF-8 模式 下將使用 UTF-8 編碼格式。
在 3.8 版更改: 現(xiàn)在如果在 Windows 上 Py_LegacyWindowsFSEncodingFlag 為零則此函數(shù)將使用 UTF-8 編碼格式;
char *Py_EncodeLocale(const wchar_t *text, size_t *error_pos)
Part of the Stable ABI since version 3.7.
將一個由寬字符組成的字符串編碼為 filesystem encoding and error handler。 如果錯誤處理句柄為 surrogateescape 錯誤處理句柄,則在 U+DC80..U+DCFF 范圍內的代理字符會被轉換為字節(jié)值 0x80..0xFF。
返回一個指向新分配的字節(jié)串的指針,使用 PyMem_Free() 來釋放內存。 當發(fā)生編碼錯誤或內存分配錯誤時返回 NULL。
如果 error_pos 不為 NULL,則成功時會將 *error_pos 設為 (size_t)-1,或是在發(fā)生編碼錯誤時設為無效字符的索引號。
filesystem encoding and error handler 是由 PyConfig_Read() 來選擇的: 參見 PyConfig 的 filesystem_encoding 和 filesystem_errors 等成員。
請使用 Py_DecodeLocale() 函數(shù)來將字節(jié)串解碼回由寬字符組成的字符串。
警告
此函數(shù)不可在This function must not be called before 對 Python 進行預初始化 之前被調用以便正確地配置 LC_CTYPE 語言區(qū)域:請參閱 Py_PreInitialize() 函數(shù)。
參見
PyUnicode_EncodeFSDefault() 和 PyUnicode_EncodeLocale() 函數(shù)。
3.5 新版功能.
在 3.7 版更改: 現(xiàn)在此函數(shù)在 Python UTF-8 模式 下將使用 UTF-8 編碼格式。
在 3.8 版更改: 現(xiàn)在如果在 Windows 上 Py_LegacyWindowsFSEncodingFlag 為零則此函數(shù)將使用 UTF-8 編碼格式。
系統(tǒng)功能
這些是使來自 sys 模塊的功能可以讓 C 代碼訪問的工具函數(shù)。 它們都可用于當前解釋器線程的 sys 模塊的字典,該字典包含在內部線程狀態(tài)結構體中。
PyObject *PySys_GetObject(const char *name)
Return value: Borrowed reference. Part of the Stable ABI.
返回來自 sys 模塊的對象 name 或者如果它不存在則返回 NULL,并且不會設置異常。
int PySys_SetObject(const char *name, PyObject *v)
Part of the Stable ABI.
將 sys 模塊中的 name 設為 v 除非 v 為 NULL,在此情況下 name 將從 sys 模塊中被刪除。 成功時返回 0,發(fā)生錯誤時返回 -1。
void PySys_ResetWarnOptions()
Part of the Stable ABI.
將 sys.warnoptions 重置為空列表。 此函數(shù)可在 Py_Initialize() 之前被調用。
void PySys_AddWarnOption(const wchar_t *s)
Part of the Stable ABI.
This API is kept for backward compatibility: setting PyConfig.warnoptions should be used instead, see Python Initialization Configuration.
將 s 添加到 sys.warnoptions。 此函數(shù)必須在 Py_Initialize() 之前被調用以便影響警告過濾器列表。
3.11 版后已移除.
void PySys_AddWarnOptionUnicode(PyObject *unicode)
Part of the Stable ABI.
This API is kept for backward compatibility: setting PyConfig.warnoptions should be used instead, see Python Initialization Configuration.
將 unicode 添加到 sys.warnoptions。
注意:目前此函數(shù)不可在 CPython 實現(xiàn)之外使用,因為它必須在 Py_Initialize() 中的 warnings 顯式導入之前被調用,但是要等運行時已初始化到足以允許創(chuàng)建 Unicode 對象時才能被調用。
3.11 版后已移除.
void PySys_SetPath(const wchar_t *path)
Part of the Stable ABI.
This API is kept for backward compatibility: setting PyConfig.module_search_paths and PyConfig.module_search_paths_set should be used instead, see Python Initialization Configuration.
將 sys.path 設為由在 path 中找到的路徑組成的列表對象,該參數(shù)應為使用特定平臺的搜索路徑分隔符 (在 Unix 上為 :,在 Windows 上為 ;) 分隔的路徑的列表。
3.11 版后已移除.
void PySys_WriteStdout(const char *format, …)
Part of the Stable ABI.
將以 format 描述的輸出字符串寫入到 sys.stdout。 不會引發(fā)任何異常,即使發(fā)生了截斷(見下文)。
format 應當將已格式化的輸出字符串的總大小限制在 1000 字節(jié)以下 — 超過 1000 字節(jié)后,輸出字符串會被截斷。 特別地,這意味著不應出現(xiàn)不受限制的 “%s” 格式;它們應當使用 “%.
如果發(fā)生了錯誤,sys.stdout 會被清空,已格式化的消息將被寫入到真正的 (C 層級) stdout。
void PySys_WriteStderr(const char *format, …)
Part of the Stable ABI.
類似 PySys_WriteStdout(),但改為寫入到 sys.stderr 或 stderr。
void PySys_FormatStdout(const char *format, …)
Part of the Stable ABI.
類似 PySys_WriteStdout() 的函數(shù)將會使用 PyUnicode_FromFormatV() 來格式化消息并且不會將消息截短至任意長度。
3.2 新版功能.
void PySys_FormatStderr(const char *format, …)
Part of the Stable ABI.
類似 PySys_FormatStdout(),但改為寫入到 sys.stderr 或 stderr。
3.2 新版功能.
void PySys_AddXOption(const wchar_t *s)
Part of the Stable ABI since version 3.7.
This API is kept for backward compatibility: setting PyConfig.xoptions should be used instead, see Python Initialization Configuration.
將 s 解析為一個由 -X 選項組成的集合并將它們添加到 PySys_GetXOptions() 所返回的當前選項映射。 此函數(shù)可以在 Py_Initialize() 之前被調用。
3.2 新版功能.
3.11 版后已移除.
PyObject *PySys_GetXOptions()
Return value: Borrowed reference. Part of the Stable ABI since version 3.7.
返回當前 -X 選項的字典,類似于 sys._xoptions。 發(fā)生錯誤時,將返回 NULL 并設置一個異常。
3.2 新版功能.
int PySys_Audit(const char *event, const char *format, …)
引發(fā)一個審計事件并附帶任何激活的鉤子。 成功時返回零值或在失敗時返回非零值并設置一個異常。
如果已添加了任何鉤子,則將使用 format 和其他參數(shù)來構造一個用入傳入的元組。 除 N 以外,在 Py_BuildValue() 中使用的格式字符均可使用。 如果構建的值不是一個元組,它將被添加到一個單元素元組中。 (格式選項 N 會消耗一個引用,但是由于沒有辦法知道此函數(shù)的參數(shù)是否將被消耗,因此使用它可能導致引用泄漏。)
請注意 # 格式字符應當總是被當作 Py_ssize_t 來處理,無論是否定義了 PY_SSIZE_T_CLEAN。
sys.audit() 會執(zhí)行與來自 Python 代碼的函數(shù)相同的操作。
3.8 新版功能.
在 3.8.2 版更改: 要求 Py_ssize_t 用于 # 格式字符。 在此之前,會引發(fā)一個不可避免的棄用警告。
int PySys_AddAuditHook(Py_AuditHookFunction hook, void *userData)
將可調用對象 hook 添加到激活的審計鉤子列表。 在成功時返回零而在失敗時返回非零值。 如果運行時已經(jīng)被初始化,還會在失敗時設置一個錯誤。 通過此 API 添加的鉤子會針對在運行時創(chuàng)建的所有解釋器被調用。
userData 指針會被傳入鉤子函數(shù)。 因于鉤子函數(shù)可能由不同的運行時調用,該指針不應直接指向 Python 狀態(tài)。
此函數(shù)可在 Py_Initialize() 之前被安全地調用。 如果在運行時初始化之后被調用,現(xiàn)有的審計鉤子將得到通知并可能通過引發(fā)一個從 Exception 子類化的錯誤靜默地放棄操作(其他錯誤將不會被靜默)。
The hook function is of type int (*)(const char *event, PyObject *args, void *userData), where args is guaranteed to be a PyTupleObject. The hook function is always called with the GIL held by the Python interpreter that raised the event.
請參閱 PEP 578 了解有關審計的詳細描述。 在運行時和標準庫中會引發(fā)審計事件的函數(shù)清單見 審計事件表。 更多細節(jié)見每個函數(shù)的文檔。
引發(fā)一個 審計事件 sys.addaudithook,沒有附帶參數(shù)。
3.8 新版功能.
過程控制
void Py_FatalError(const char *message)
Part of the Stable ABI.
打印一個致命錯誤消息并殺掉進程。 不會執(zhí)行任何清理。 此函數(shù)應當僅在檢測到可能令繼續(xù)使用 Python 解釋器變得危險的條件時被發(fā)起調用;例如,當對象管理已被破壞的時候。 在 Unix 上,標準 C 庫函數(shù) abort() 會被調用并將由它來嘗試產(chǎn)生一個 core 文件。
The Py_FatalError() function is replaced with a macro which logs automatically the name of the current function, unless the Py_LIMITED_API macro is defined.
在 3.9 版更改: 自動記錄函數(shù)名稱。
void Py_Exit(int status)
Part of the Stable ABI.
退出當前進程。 這將調用 Py_FinalizeEx() 然后再調用標準 C 庫函數(shù) exit(status)。 如果 Py_FinalizeEx() 提示錯誤,退出狀態(tài)將被設為 120。
在 3.6 版更改: 來自最終化的錯誤不會再被忽略。
int Py_AtExit(void (*func)())
Part of the Stable ABI.
注冊一個由 Py_FinalizeEx() 調用的清理函數(shù)。 調用清理函數(shù)將不傳入任何參數(shù)且不應返回任何值。 最多可以注冊32 個清理函數(shù)。 當注冊成功時,Py_AtExit() 將返回 0;失敗時,它將返回 -1。 最后注冊的清理函數(shù)會最先被調用。 每個清理函數(shù)將至多被調用一次。 由于 Python 的內部最終化將在清理函數(shù)之前完成,因此 Python API 不應被 func 調用。
分享文章:創(chuàng)新互聯(lián)Python教程:操作系統(tǒng)實用工具
網(wǎng)站URL:http://m.fisionsoft.com.cn/article/dhchsoo.html


咨詢
建站咨詢
