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

PyObject *PyOS_FSPath(PyObject *path)
    *戻り値: 新しい参照。** 次に属します: Stable 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.

   Added in version 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()
    * 次に属します: Stable ABI on platforms with fork() (バージョン
   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()".

   Added in version 3.7.

void PyOS_AfterFork_Parent()
    * 次に属します: Stable ABI on platforms with fork() (バージョン
   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()".

   Added in version 3.7.

void PyOS_AfterFork_Child()
    * 次に属します: Stable 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()".

   Added in version 3.7.

   参考:

     "os.register_at_fork()" を利用すると "PyOS_BeforeFork()"、
     "PyOS_AfterFork_Parent()" "PyOS_AfterFork_Child()" によって呼び出
     されるカスタムのPython関数を登録できます。

void PyOS_AfterFork()
    * 次に属します: Stable ABI on platforms with fork().*

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

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

int PyOS_CheckStack()
    * 次に属します: Stable 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)
    * 次に属します: Stable ABI.*

PyOS_sighandler_t PyOS_getsig(int i)
    * 次に属します: 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 PyOS_setsig(int i, PyOS_sighandler_t h)
    * 次に属します: 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!

int PyOS_InterruptOccurred(void)
    * 次に属します: Stable 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)
    * 次に属します: Stable 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.

   *ファイルシステムのエンコーディングとエラーハンドラ* からバイト文字
   列をデコードします。エラーハンドラが 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()" 関数。

   Added in version 3.5.

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

   バージョン 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)
    * 次に属します: Stable ABI (バージョン 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()"
       関数。

   Added in version 3.5.

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

   バージョン 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.

   *path* must be a "str" object, a "bytes" object, or a *path-like
   object*.

   On success, return the new file pointer. On error, set an exception
   and return "NULL".

   The file must be closed by "Py_fclose()" rather than calling
   directly "fclose()".

   The file descriptor is created non-inheritable (**PEP 446**).

   The caller must have an *attached thread state*.

   Added in version 3.14.

int Py_fclose(FILE *file)

   Close a file that was opened by "Py_fopen()".

   On success, return "0". On error, return "EOF" and "errno" is set
   to indicate the error. In either case, any further access
   (including another call to "Py_fclose()") to the stream results in
   undefined behavior.

   Added in version 3.14.


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

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

PyObject *PySys_GetObject(const char *name)
    *戻り値: 借用参照。** 次に属します: Stable ABI.*

   "sys" モジュールの *name* オブジェクトを返すか、存在しなければ例外
   を設定せずに "NULL" を返します。

int PySys_SetObject(const char *name, PyObject *v)
    * 次に属します: Stable ABI.*

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

void PySys_ResetWarnOptions()
    * 次に属します: Stable ABI.*

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

   Deprecated since version 3.13, will be removed in version 3.15:
   Clear "sys.warnoptions" and "warnings.filters" instead.

void PySys_WriteStdout(const char *format, ...)
    * 次に属します: 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, ...)
    * 次に属します: Stable ABI.*

   "PySys_WriteStdout()" と同じですが、 "sys.stderr" もしくは *stderr*
   に出力します。

void PySys_FormatStdout(const char *format, ...)
    * 次に属します: Stable ABI.*

   PySys_WriteStdout() に似た関数ですが、 "PyUnicode_FromFormatV()" を
   使ってメッセージをフォーマットし、メッセージを任意の長さに切り詰め
   たりはしません。

   Added in version 3.2.

void PySys_FormatStderr(const char *format, ...)
    * 次に属します: Stable ABI.*

   "PySys_FormatStdout()" と同じですが、 "sys.stderr" もしくは
   *stderr* に出力します。

   Added in version 3.2.

PyObject *PySys_GetXOptions()
    *戻り値: 借用参照。** 次に属します: Stable ABI (バージョン 3.7 よ
   り).*

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

   Added in version 3.2.

int PySys_Audit(const char *event, const char *format, ...)
    * 次に属します: Stable ABI (バージョン 3.13 より).*

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

   The *event* string argument must not be *NULL*.

   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 must not be used. It 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.

   See also "PySys_AuditTuple()".

   Added in version 3.8.

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

int PySys_AuditTuple(const char *event, PyObject *args)
    * 次に属します: Stable ABI (バージョン 3.13 より).*

   Similar to "PySys_Audit()", but pass arguments as a Python object.
   *args* must be a "tuple". To pass no arguments, *args* can be
   *NULL*.

   Added in version 3.13.

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 always called with an *attached thread state*
   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.

   If the interpreter is initialized, this function raises an auditing
   event "sys.addaudithook" with no arguments. If any existing hooks
   raise an exception derived from "Exception", the new hook will not
   be added and the exception is cleared. As a result, callers cannot
   assume that their hook has been added unless they control all
   existing hooks.

   typedef int (*Py_AuditHookFunction)(const char *event, PyObject *args, void *userData)

      The type of the hook function. *event* is the C string event
      argument passed to "PySys_Audit()" or "PySys_AuditTuple()".
      *args* is guaranteed to be a "PyTupleObject". *userData* is the
      argument passed to PySys_AddAuditHook().

   Added in version 3.8.


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

void Py_FatalError(const char *message)
    * 次に属します: Stable ABI.*

   Print a fatal error message and kill the process.  No cleanup is
   performed. This function should only be invoked when a condition is
   detected that would make it dangerous to continue using the Python
   interpreter; e.g., when the object administration appears to be
   corrupted.  On Unix, the standard C library function "abort()" is
   called which will attempt to produce a "core" file.

   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)
    * 次に属します: Stable ABI.*

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

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

int Py_AtExit(void (*func)())
    * 次に属します: Stable ABI.*

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

   参考: "PyUnstable_AtExit()" for passing a "void *data" argument.
