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

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


咨詢(xún)
建站咨詢(xún)
