オペレーティングシステム関連のユーティリティ
********************************************

PyObject *PyOS_FSPath(PyObject *path)
    *Return value: New reference.** Part of the Stable ABI since
   version 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)

   *filename* という名前の標準 I/O ファイル *fp* が対話的
   (interactive) であると考えられる場合に真 (非ゼロ) を返します。これ
   は "isatty(fileno(fp))" が真になるファイルの場合です。グローバルな
   フラグ "Py_InteractiveFlag" が真の場合には、 *filename* ポインタが
   "NULL" か、名前が "'<stdin>'" または "'???'" のいずれかに等しい場合
   にも真を返します。

void PyOS_BeforeFork()
    * Part of the Stable ABI on platforms with fork() since version
   3.7.*

   プロセスがフォークする前に、いくつかの内部状態を準備するための関数
   です。"fork()" や現在のプロセスを複製するその他の類似の関数を呼び出
   す前にこの関数を呼びださなければなりません。"fork()" が定義されてい
   るシステムでのみ利用できます。

   警告:

     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()
    * Part of the Stable ABI on platforms with fork() since version
   3.7.*

   プロセスがフォークした後に内部状態を更新するための関数です。
   "fork()" や、現在のプロセスを複製するその他の類似の関数を呼び出した
   後に、プロセスの複製が成功したかどうかにかかわらず、親プロセスから
   この関数を呼び出さなければなりません。"fork()" が定義されているシス
   テムでのみ利用できます。

   警告:

     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()
    * Part of the Stable ABI on platforms with fork() since version
   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()" を利用すると "PyOS_BeforeFork()"、
     "PyOS_AfterFork_Parent()" "PyOS_AfterFork_Child()" によって呼び出
     されるカスタムのPython関数を登録できます。

void PyOS_AfterFork()
    * Part of the Stable ABI on platforms with fork().*

   プロセスが fork した後の内部状態を更新するための関数です; fork 後
   Python インタプリタを使い続ける場合、新たなプロセス内でこの関数を呼
   び出さねばなりません。新たなプロセスに新たな実行可能物をロードする
   場合、この関数を呼び出す必要はありません。

   バージョン 3.7 で非推奨: この関数は "PyOS_AfterFork_Child()" によっ
   て置き換えられました。

int PyOS_CheckStack()
    * Part of the Stable ABI on platforms with USE_STACKCHECK since
   version 3.7.*

   インタプリタがスタック空間を使い尽くしたときに真を返します。このチ
   ェック関数には信頼性がありますが、 "USE_STACKCHECK" が定義されてい
   る場合 (現状では Microsoft Visual C++ コンパイラでビルドした
   Windows 版) にしか利用できません . "USE_STACKCHECK" は自動的に定義
   されます; 自前のコードでこの定義を変更してはなりません。

PyOS_sighandler_t PyOS_getsig(int i)
    * Part of the Stable ABI.*

   シグナル *i* に対する現在のシグナルハンドラを返します。この関数は
   "sigaction()" または "signal()" のいずれかに対する薄いラッパーです
   。 "sigaction()" や "signal()" を直接呼び出してはなりません!
   "PyOS_sighandler_t" は "void (*)(int)" の typedef  による別名です。

PyOS_sighandler_t PyOS_setsig(int i, PyOS_sighandler_t h)
    * Part of the Stable ABI.*

   シグナル *i* に対する現在のシグナルハンドラを *h* に設定します; 以
   前のシグナルハンドラを返します。この関数は "sigaction()" または
   "signal()" のいずれかに対する薄いラッパーです。 "sigaction()" や
   "signal()" を直接呼び出してはなりません!  "PyOS_sighandler_t" は
   "void (*)(int)" の typedef  による別名です。

wchar_t *Py_DecodeLocale(const char *arg, size_t *size)
    * Part of the Stable ABI since version 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.

   *ファイルシステムのエンコーディングとエラーハンドラ* からバイト文字
   列をデコードします。エラーハンドラが surrogateescape エラーハンドラ
   なら、 デコードできないバイトは U+DC80 から U+DCFF までの範囲の文字
   としてデコードされ、バイト列がサロゲート文字としてデコードできる場
   合は、デコードするのではなく surrogateescape エラーハンドラを使って
   バイト列がエスケープされます。

   新しくメモリ確保されたワイドキャラクター文字列へのポインタを返しま
   す。 このメモリを解放するのには "PyMem_RawFree()" を使ってください
   。 引数 size が "NULL" でない場合は、 null 文字以外のワイドキャラク
   ターの数を "*size" へ書き込みます。

   デコードもしくはメモリ確保でエラーが起きると "NULL" を返します。
   *size* が "NULL" でない場合は、メモリエラーのときは "(size_t)-1" を
   、デコードでのエラーのときは "(size_t)-2" を "*size" に設定します。

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

   C ライブラリーにバグがない限り、デコードでのエラーは起こりえません
   。

   キャラクター文字列をバイト文字列に戻すには "Py_EncodeLocale()" 関数
   を使ってください。

   参考:

     "PyUnicode_DecodeFSDefaultAndSize()" および
     "PyUnicode_DecodeLocaleAndSize()" 関数。

   バージョン 3.5 で追加.

   バージョン 3.7 で変更: この関数は、Python UTF-8 Mode ではUTF-8エン
   コーディングを利用するようになりました。

   バージョン 3.8 で変更: The function now uses the UTF-8 encoding on
   Windows if "Py_LegacyWindowsFSEncodingFlag" is zero;

char *Py_EncodeLocale(const wchar_t *text, size_t *error_pos)
    * Part of the Stable ABI since version 3.7.*

   ワイドキャラクター文字列を *ファイルシステムのエンコーディングとエ
   ラーハンドラ* にエンコードします。エラーハンドラが surrogateescape
   エラーハンドラ なら、 U+DC80 から U+DCFF までの範囲のサロゲート文字
   は 0x80 から 0xFF までのバイトに変換されます。

   新しくメモリ確保されたバイト文字列へのポインタを返します。 このメモ
   リを解放するのには "PyMem_Free()" を使ってください。 エンコードエラ
   ーかメモリ確保エラーのときは "NULL" を返します。

   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".

   バイト文字列をワイドキャラクター文字列に戻すには
   "Py_DecodeLocale()" 関数を使ってください。

   警告:

     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.

   参考: "PyUnicode_EncodeFSDefault()" および "PyUnicode_EncodeLocale()"
       関数。

   バージョン 3.5 で追加.

   バージョン 3.7 で変更: この関数は、Python UTF-8 Mode ではUTF-8エン
   コーディングを利用するようになりました。

   バージョン 3.8 で変更: The function now uses the UTF-8 encoding on
   Windows if "Py_LegacyWindowsFSEncodingFlag" is zero.


システム関数
************

"sys" モジュールが提供している機能にCのコードからアクセスする関数です
。すべての関数は現在のインタプリタスレッドの "sys" モジュールの辞書に
対して動作します。この辞書は内部のスレッド状態構造体に格納されています
。

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.*

   *v* が "NULL" で無い場合、 "sys" モジュールの *name* に *v* を設定
   します。 *v* が "NULL" なら、 sys モジュールから *name* を削除しま
   す。成功したら "0" を、エラー時は "-1" を返します。

void PySys_ResetWarnOptions()
    * Part of the Stable ABI.*

   Reset "sys.warnoptions" to an empty list. This function may be
   called prior to "Py_Initialize()".

void PySys_AddWarnOption(const wchar_t *s)
    * Part of the Stable ABI.*

   Append *s* to "sys.warnoptions". This function must be called prior
   to "Py_Initialize()" in order to affect the warnings filter list.

void PySys_AddWarnOptionUnicode(PyObject *unicode)
    * Part of the Stable ABI.*

   "sys.warnoptions" に *unicode* を追加します。

   Note: this function is not currently usable from outside the
   CPython implementation, as it must be called prior to the implicit
   import of "warnings" in "Py_Initialize()" to be effective, but
   can't be called until enough of the runtime has been initialized to
   permit the creation of Unicode objects.

void PySys_SetPath(const wchar_t *path)
    * Part of the Stable ABI.*

   "sys.path" を *path* に含まれるパスの、リストオブジェクトに設定しま
   す。 *path* はプラットフォームの検索パスデリミタ(Unixでは ":",
   Windows では ";") で区切られたパスのリストでなければなりません。

void PySys_WriteStdout(const char *format, ...)
    * Part of the Stable ABI.*

   *format* で指定された出力文字列を "sys.stdout" に出力します。切り詰
   めが起こった場合を含め、例外は一切発生しません (後述)。

   *format* は、フォーマット後の出力文字列のトータルの大きさを1000バイ
   ト以下に抑えるべきです。-- 1000 バイト以降の出力文字列は切り詰めら
   れます。特に、制限のない "%s" フォーマットを使うべきではありません
   。"%.<N>s" のようにして N に10進数の値を指定し、<N> + その他のフォ
   ーマット後の最大サイズが1000を超えないように設定するべきです。同じ
   ように "%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() に似た関数ですが、 "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.*

   Parse *s* as a set of "-X" options and add them to the current
   options mapping as returned by "PySys_GetXOptions()". This function
   may be called prior to "Py_Initialize()".

   バージョン 3.2 で追加.

PyObject *PySys_GetXOptions()
    *Return value: Borrowed reference.** Part of the Stable ABI since
   version 3.7.*

   "sys._xoptions" と同様、 "-X" オプションの現在の辞書を返します。エ
   ラーが起きると、 "NULL" が返され、例外がセットされます。

   バージョン 3.2 で追加.

int PySys_Audit(const char *event, const char *format, ...)

   Raise an auditing event with any active hooks. Return zero for
   success and non-zero with an exception set on failure.

   If any hooks have been added, *format* and other arguments will be
   used to construct a tuple to pass. Apart from "N", the same format
   characters as used in "Py_BuildValue()" are available. If the built
   value is not a tuple, it will be added into a single-element tuple.
   (The "N" format option consumes a reference, but since there is no
   way to know whether arguments to this function will be consumed,
   using it may cause reference leaks.)

   Note that "#" format characters should always be treated as
   "Py_ssize_t", regardless of whether "PY_SSIZE_T_CLEAN" was defined.

   "sys.audit()" performs the same function from Python code.

   バージョン 3.8 で追加.

   バージョン 3.8.2 で変更: Require "Py_ssize_t" for "#" format
   characters. Previously, an unavoidable deprecation warning was
   raised.

int PySys_AddAuditHook(Py_AuditHookFunction hook, void *userData)

   Append the callable *hook* to the list of active auditing hooks.
   Return zero on success and non-zero on failure. If the runtime has
   been initialized, also set an error on failure. Hooks added through
   this API are called for all interpreters created by the runtime.

   *userData* ポインタはフック関数に渡されます。 フック関数は別なラン
   タイムから呼び出されるかもしれないので、このポインタは直接 Python
   の状態を参照すべきではありません。

   This function is safe to call before "Py_Initialize()". When called
   after runtime initialization, existing audit hooks are notified and
   may silently abort the operation by raising an error subclassed
   from "Exception" (other errors will not be silenced).

   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.

   See **PEP 578** for a detailed description of auditing.  Functions
   in the runtime and standard library that raise events are listed in
   the audit events table. Details are in each function's
   documentation.

   引数無しで 監査イベント "sys.addaudithook" を送出します。

   バージョン 3.8 で追加.


プロセス制御
************

void Py_FatalError(const char *message)
    * Part of the Stable ABI.*

   致命的エラーメッセージ (fatal error message) を出力してプロセスを強
   制終了 (kill) します。後始末処理は行われません。この関数は、Python
   インタプリタを使い続けるのが危険であるような状況が検出されたとき;
   例えば、オブジェクト管理が崩壊していると思われるときにのみ、呼び出
   されるようにしなければなりません。Unixでは、標準 C ライブラリ関数
   "abort()" を呼び出して "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 で変更: Log the function name automatically.

void Py_Exit(int status)
    * Part of the Stable ABI.*

   現在のプロセスを終了します。"Py_FinalizeEx()" を呼び出した後、標準C
   ライブラリ関数の "exit(status)" を呼び出します。"Py_FinalizeEx()"
   がエラーになった場合、終了ステータスは 120に設定されます。

   バージョン 3.6 で変更: 終了処理のエラーは無視されなくなりました。

int Py_AtExit(void (*func)())
    * Part of the Stable ABI.*

   "Py_FinalizeEx()" から呼び出される後始末処理を行う関数 (cleanup
   function) を登録します。後始末関数は引数無しで呼び出され、値を返し
   ません。最大で 32 の後始末処理関数を登録できます。登録に成功すると
   、 "Py_AtExit()" は "0" を返します;  失敗すると "-1" を返します。最
   後に登録した後始末処理関数から先に呼び出されます。各関数は高々一度
   しか呼び出されません。 Python の内部的な終了処理は後始末処理関数よ
   り以前に完了しているので、 *func* からはいかなる Python API も呼び
   出してはなりません。
