# 初期化 (initialization)、終了処理 (finalization)、スレッド

See Python Initialization Configuration for details on how to configure the interpreter prior to initialization.

## Python 初期化以前

Python が埋め込まれているアプリケーションでは、他の Python/C API 関数を使う前に `Py_Initialize()` 関数を呼ばなければなりません。これには例外として、いくつかの関数と グローバルな設定変数 があります。

次の関数は Python の初期化の前でも安全に呼び出せます:

- Functions that initialize the interpreter:

  - `Py_Initialize()`

  - `Py_InitializeEx()`

  - `Py_InitializeFromConfig()`

  - `Py_BytesMain()`

  - `Py_Main()`

  - the runtime pre-initialization functions covered in Python 初期化設定

- 設定関数:

  - `PyImport_AppendInittab()`

  - `PyImport_ExtendInittab()`

  - `PyInitFrozenExtensions()`

  - `PyMem_SetAllocator()`

  - `PyMem_SetupDebugHooks()`

  - `PyObject_SetArenaAllocator()`

  - `Py_SetProgramName()`

  - `Py_SetPythonHome()`

  - `PySys_ResetWarnOptions()`

  - the configuration functions covered in Python 初期化設定

- 情報取得の関数:

  - `Py_IsInitialized()`

  - `PyMem_GetAllocator()`

  - `PyObject_GetArenaAllocator()`

  - `Py_GetBuildInfo()`

  - `Py_GetCompiler()`

  - `Py_GetCopyright()`

  - `Py_GetPlatform()`

  - `Py_GetVersion()`

  - `Py_IsInitialized()`

- ユーティリティ:

  - `Py_DecodeLocale()`

  - the status reporting and utility functions covered in Python 初期化設定

- メモリアロケータ:

  - `PyMem_RawMalloc()`

  - `PyMem_RawRealloc()`

  - `PyMem_RawCalloc()`

  - `PyMem_RawFree()`

- Synchronization:

  - `PyMutex_Lock()`

  - `PyMutex_Unlock()`

注釈

Despite their apparent similarity to some of the functions listed above, the following functions should not be called before the interpreter has been initialized: `Py_EncodeLocale()`, `Py_GetPath()`, `Py_GetPrefix()`, `Py_GetExecPrefix()`, `Py_GetProgramFullPath()`, `Py_GetPythonHome()`, `Py_GetProgramName()`, `PyEval_InitThreads()`, and `Py_RunMain()`.

## グローバルな設定変数

Python には、様々な機能やオプションを制御するグローバルな設定のための変数があります。 デフォルトでは、これらのフラグは コマンドラインオプション で制御されます。

オプションでフラグがセットされると、フラグの値はそのオプションがセットされた回数になります。 例えば、 `-b` では `Py_BytesWarningFlag` が 1 に設定され、 `-bb` では `Py_BytesWarningFlag` が 2 に設定されます。

int Py_BytesWarningFlag

  This API is kept for backward compatibility: setting `PyConfig.bytes_warning` should be used instead, see Python Initialization Configuration.

  `bytes` または `bytearray` を `str` と比較した場合、または、 `bytes` を `int` と比較した場合に警告を発生させます。 `2` 以上の値を設定している場合は、エラーを発生させます。

  `-b` オプションで設定します。

  Deprecated since version 3.12, will be removed in version 3.15.

int Py_DebugFlag

  This API is kept for backward compatibility: setting `PyConfig.parser_debug` should be used instead, see Python Initialization Configuration.

  パーサーのデバッグ出力を有効にします。(専門家専用です。コンパイルオプションに依存します)。

  `-d` オプションと `PYTHONDEBUG` 環境変数で設定します。

  Deprecated since version 3.12, will be removed in version 3.15.

int Py_DontWriteBytecodeFlag

  This API is kept for backward compatibility: setting `PyConfig.write_bytecode` should be used instead, see Python Initialization Configuration.

  非ゼロに設定した場合、Python はソースモジュールのインポート時に `.pyc` ファイルの作成を試みません。

  `-B` オプションと `PYTHONDONTWRITEBYTECODE` 環境変数で設定します。

  Deprecated since version 3.12, will be removed in version 3.15.

int Py_FrozenFlag

  This API is kept for backward compatibility: setting `PyConfig.pathconfig_warnings` should be used instead, see Python Initialization Configuration.

  `Py_GetPath()` の中でモジュール検索パスを割り出しているときのエラーメッセージを抑制します。

  `_freeze_module` プログラムと `frozenmain` プログラムが使用する非公開フラグです。

  Deprecated since version 3.12, will be removed in version 3.15.

int Py_HashRandomizationFlag

  This API is kept for backward compatibility: setting `PyConfig.hash_seed` and `PyConfig.use_hash_seed` should be used instead, see Python Initialization Configuration.

  `PYTHONHASHSEED` 環境変数が空でない文字列に設定された場合に、 `1` が設定されます。

  フラグがゼロでない場合、 `PYTHONHASHSEED` 環境変数を読みシークレットハッシュシードを初期化します。

  Deprecated since version 3.12, will be removed in version 3.15.

int Py_IgnoreEnvironmentFlag

  This API is kept for backward compatibility: setting `PyConfig.use_environment` should be used instead, see Python Initialization Configuration.

  Ignore all `PYTHON*` environment variables, e.g. `PYTHONPATH` and `PYTHONHOME`, that might be set.

  `-E` オプションと `-I` オプションで設定します。

  Deprecated since version 3.12, will be removed in version 3.15.

int Py_InspectFlag

  This API is kept for backward compatibility: setting `PyConfig.inspect` should be used instead, see Python Initialization Configuration.

  最初の引数にスクリプトが指定されたときや `-c` オプションが利用された際に、 `sys.stdin` がターミナルに出力されないときであっても、スクリプトかコマンドを実行した後にインタラクティブモードに入ります。

  `-i` オプションと `PYTHONINSPECT` 環境変数で設定します。

  Deprecated since version 3.12, will be removed in version 3.15.

int Py_InteractiveFlag

  This API is kept for backward compatibility: setting `PyConfig.interactive` should be used instead, see Python Initialization Configuration.

  `-i` オプションで設定します。

  Deprecated since version 3.12, will be removed in version 3.15.

int Py_IsolatedFlag

  This API is kept for backward compatibility: setting `PyConfig.isolated` should be used instead, see Python Initialization Configuration.

  Python を隔離モードで実行します。 隔離モードでは `sys.path` はスクリプトのディレクトリやユーザのサイトパッケージのディレクトリを含みません。

  `-I` オプションで設定します。

  Added in version 3.4.

  Deprecated since version 3.12, will be removed in version 3.15.

int Py_LegacyWindowsFSEncodingFlag

  This API is kept for backward compatibility: setting `PyPreConfig.legacy_windows_fs_encoding` should be used instead, see Python Initialization Configuration.

  If the flag is non-zero, use the `mbcs` encoding with `replace` error handler, instead of the UTF-8 encoding with `surrogatepass` error handler, for the filesystem encoding and error handler.

  `PYTHONLEGACYWINDOWSFSENCODING` 環境変数が空でない文字列に設定された場合に、 `1` に設定されます。

  より詳しくは PEP 529 を参照してください。

  Availability: Windows.

  Deprecated since version 3.12, will be removed in version 3.15.

int Py_LegacyWindowsStdioFlag

  This API is kept for backward compatibility: setting `PyConfig.legacy_windows_stdio` should be used instead, see Python Initialization Configuration.

  If the flag is non-zero, use `io.FileIO` instead of `io._WindowsConsoleIO` for `sys` standard streams.

  `PYTHONLEGACYWINDOWSSTDIO` 環境変数が空でない文字列に設定された場合に、 `1` に設定されます。

  より詳しくは PEP 528 を参照してください。

  Availability: Windows.

  Deprecated since version 3.12, will be removed in version 3.15.

int Py_NoSiteFlag

  This API is kept for backward compatibility: setting `PyConfig.site_import` should be used instead, see Python Initialization Configuration.

  `site` モジュールの import と、そのモジュールが行なっていた site ごとの `sys.path` への操作を無効にします。後で `site` を明示的に import しても、これらの操作は実行されません (実行したい場合は、 `site.main()` を呼び出してください)。

  `-S` オプションで設定します。

  Deprecated since version 3.12, will be removed in version 3.15.

int Py_NoUserSiteDirectory

  This API is kept for backward compatibility: setting `PyConfig.user_site_directory` should be used instead, see Python Initialization Configuration.

  `ユーザのサイトパッケージのディレクトリ` を `sys.path` に追加しません。

  `-s` オプション、 `-I` 、 `PYTHONNOUSERSITE` 環境変数で設定します。

  Deprecated since version 3.12, will be removed in version 3.15.

int Py_OptimizeFlag

  This API is kept for backward compatibility: setting `PyConfig.optimization_level` should be used instead, see Python Initialization Configuration.

  `-O` オプションと `PYTHONOPTIMIZE` 環境変数で設定します。

  Deprecated since version 3.12, will be removed in version 3.15.

int Py_QuietFlag

  This API is kept for backward compatibility: setting `PyConfig.quiet` should be used instead, see Python Initialization Configuration.

  インタラクティブモードでも copyright とバージョンのメッセージを表示しません。

  `-q` オプションで設定します。

  Added in version 3.2.

  Deprecated since version 3.12, will be removed in version 3.15.

int Py_UnbufferedStdioFlag

  This API is kept for backward compatibility: setting `PyConfig.buffered_stdio` should be used instead, see Python Initialization Configuration.

  標準出力と標準エラーをバッファリングしないように強制します。

  `-u` オプションと `PYTHONUNBUFFERED` 環境変数で設定します。

  Deprecated since version 3.12, will be removed in version 3.15.

int Py_VerboseFlag

  This API is kept for backward compatibility: setting `PyConfig.verbose` should be used instead, see Python Initialization Configuration.

  モジュールが初期化されるたびにメッセージを出力し、それがどこ (ファイル名やビルトインモジュール) からロードされたのかを表示します。 値が 2 以上の場合は、モジュールを検索するときにチェックしたファイルごとにメッセージを出力します。また、終了時のモジュールクリーンアップに関する情報も提供します。

  `-v` オプションと `PYTHONVERBOSE` 環境変数で設定します。

  Deprecated since version 3.12, will be removed in version 3.15.

## インタープリタの初期化と終了処理

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

  Python インタープリタを初期化します。 Python の埋め込みを行うアプリケーションでは、他のあらゆる Python/C API を使用するよりも前にこの関数を呼び出さなければなりません。いくつかの例外については Python 初期化以前 を参照してください。

  This initializes the table of loaded modules (`sys.modules`), and creates the fundamental modules `builtins`, `__main__` and `sys`. It also initializes the module search path (`sys.path`). It does not set `sys.argv`; use the Python Initialization Configuration API for that. This is a no-op when called for a second time (without calling `Py_FinalizeEx()` first). There is no return value; it is a fatal error if the initialization fails.

  Use `Py_InitializeFromConfig()` to customize the Python Initialization Configuration.

  注釈

  Windows では `O_TEXT` から `O_BINARY` へコンソールモードが変更されますが、これはその C ランタイムを使っているコンソールでの Python 以外の使い勝手にも影響を及ぼします。

void Py_InitializeEx(int initsigs)
  次に属します: Stable ABI.

  This function works like `Py_Initialize()` if initsigs is `1`. If initsigs is `0`, it skips initialization registration of signal handlers, which may be useful when CPython is embedded as part of a larger application.

  Use `Py_InitializeFromConfig()` to customize the Python Initialization Configuration.

PyStatus Py_InitializeFromConfig(const PyConfig *config)

  Initialize Python from config configuration, as described in Initialization with PyConfig.

  See the Python 初期化設定 section for details on pre-initializing the interpreter, populating the runtime configuration structure, and querying the returned status structure.

int Py_IsInitialized()
  次に属します: Stable ABI.

  Python インタプリタが初期化済みであれば真(非ゼロ)を、さもなければ偽(ゼロ)を返します。`Py_FinalizeEx()` を呼び出した後は、`Py_Initialize()` を再び呼び出すまで、この関数は偽を返します。

int Py_IsFinalizing()
  次に属します: Stable ABI (バージョン 3.13 より).

  Return true (non-zero) if the main Python interpreter is shutting down. Return false (zero) otherwise.

  Added in version 3.13.

int Py_FinalizeEx()
  次に属します: Stable ABI (バージョン 3.6 より).

  Undo all initializations made by `Py_Initialize()` and subsequent use of Python/C API functions, and destroy all sub-interpreters (see `Py_NewInterpreter()` below) that were created and not yet destroyed since the last call to `Py_Initialize()`. This is a no-op when called for a second time (without calling `Py_Initialize()` again first).

  Since this is the reverse of `Py_Initialize()`, it should be called in the same thread with the same interpreter active. That means the main thread and the main interpreter. This should never be called while `Py_RunMain()` is running.

  Normally the return value is `0`. If there were errors during finalization (flushing buffered data), `-1` is returned.

  Note that Python will do a best effort at freeing all memory allocated by the Python interpreter. Therefore, any C-Extension should make sure to correctly clean up all of the preveiously allocated PyObjects before using them in subsequent calls to `Py_Initialize()`. Otherwise it could introduce vulnerabilities and incorrect behavior.

  この関数が提供されている理由はいくつかあります。Python の埋め込みを行っているアプリケーションでは、アプリケーションを再起動することなく Python を再起動したいことがあります。また、動的ロード可能イブラリ (あるいは DLL) から Python インタプリタをロードするアプリケーションでは、DLL をアンロードする前に Python が確保したメモリを全て解放したいと考えるかもしれません。アプリケーション内で起きているメモリリークを追跡する際に、開発者は Python が確保したメモリをアプリケーションの終了前に解放させたいと思う場合もあります。

  Bugs and caveats: The destruction of modules and objects in modules is done in random order; this may cause destructors (`__del__()` methods) to fail when they depend on other objects (even functions) or modules. Dynamically loaded extension modules loaded by Python are not unloaded. Small amounts of memory allocated by the Python interpreter may not be freed (if you find a leak, please report it). Memory tied up in circular references between objects is not freed. Interned strings will all be deallocated regardless of their reference count. Some memory allocated by extension modules may not be freed. Some extensions may not work properly if their initialization routine is called more than once; this can happen if an application calls `Py_Initialize()` and `Py_FinalizeEx()` more than once. `Py_FinalizeEx()` must not be called recursively from within itself. Therefore, it must not be called by any code that may be run as part of the interpreter shutdown process, such as `atexit` handlers, object finalizers, or any code that may be run while flushing the stdout and stderr files.

  引数無しで 監査イベント `cpython._PySys_ClearAuditHooks` を送出します。

  Added in version 3.6.

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

  この関数は `Py_FinalizeEx()` の後方互換性バージョンで、戻り値がありません。

int Py_BytesMain(int argc, char **argv)
  次に属します: Stable ABI (バージョン 3.8 より).

  Similar to `Py_Main()` but argv is an array of bytes strings, allowing the calling application to delegate the text decoding step to the CPython runtime.

  Added in version 3.8.

int Py_Main(int argc, wchar_t **argv)
  次に属します: Stable ABI.

  The main program for the standard interpreter, encapsulating a full initialization/finalization cycle, as well as additional behaviour to implement reading configurations settings from the environment and command line, and then executing `__main__` in accordance with コマンドライン.

  This is made available for programs which wish to support the full CPython command line interface, rather than just embedding a Python runtime in a larger application.

  The argc and argv parameters are similar to those which are passed to a C program's `main()` function, except that the argv entries are first converted to `wchar_t` using `Py_DecodeLocale()`. It is also important to note that the argument list entries may be modified to point to strings other than those passed in (however, the contents of the strings pointed to by the argument list are not modified).

  The return value is `2` if the argument list does not represent a valid Python command line, and otherwise the same as `Py_RunMain()`.

  In terms of the CPython runtime configuration APIs documented in the runtime configuration section (and without accounting for error handling), `Py_Main` is approximately equivalent to:

      PyConfig config;
      PyConfig_InitPythonConfig(&config);
      PyConfig_SetArgv(&config, argc, argv);
      Py_InitializeFromConfig(&config);
      PyConfig_Clear(&config);

      Py_RunMain();

  In normal usage, an embedding application will call this function instead of calling `Py_Initialize()`, `Py_InitializeEx()` or `Py_InitializeFromConfig()` directly, and all settings will be applied as described elsewhere in this documentation. If this function is instead called after a preceding runtime initialization API call, then exactly which environmental and command line configuration settings will be updated is version dependent (as it depends on which settings correctly support being modified after they have already been set once when the runtime was first initialized).

int Py_RunMain(void)

  Executes the main module in a fully configured CPython runtime.

  Executes the command (`PyConfig.run_command`), the script (`PyConfig.run_filename`) or the module (`PyConfig.run_module`) specified on the command line or in the configuration. If none of these values are set, runs the interactive Python prompt (REPL) using the `__main__` module's global namespace.

  If `PyConfig.inspect` is not set (the default), the return value will be `0` if the interpreter exits normally (that is, without raising an exception), the exit status of an unhandled `SystemExit`, or `1` for any other unhandled exception.

  If `PyConfig.inspect` is set (such as when the `-i` option is used), rather than returning when the interpreter exits, execution will instead resume in an interactive Python prompt (REPL) using the `__main__` module's global namespace. If the interpreter exited with an exception, it is immediately raised in the REPL session. The function return value is then determined by the way the REPL session terminates: `0`, `1`, or the status of a `SystemExit`, as specified above.

  This function always finalizes the Python interpreter before it returns.

  See Python Configuration for an example of a customized Python that always runs in isolated mode using `Py_RunMain()`.

int PyUnstable_AtExit(PyInterpreterState *interp, void (*func)(void*), void *data)
  これは Unstable APIです。マイナーリリースで予告なく変更されることがあります。

  Register an `atexit` callback for the target interpreter interp. This is similar to `Py_AtExit()`, but takes an explicit interpreter and data pointer for the callback.

  There must be an attached thread state for interp.

  Added in version 3.13.

## プロセスワイドのパラメータ

void Py_SetProgramName(const wchar_t *name)
  次に属します: Stable ABI.

  This API is kept for backward compatibility: setting `PyConfig.program_name` should be used instead, see Python Initialization Configuration.

  この関数を呼び出すなら、最初に `Py_Initialize()` を呼び出すよりも前に呼び出さなければなりません。この関数はインタプリタにプログラムの `main()` 関数に指定した `argv[0]` 引数の値を教えます (ワイドキャラクタに変換されます)。この引数値は、 `Py_GetPath()` や、以下に示すその他の関数が、インタプリタの実行可能形式から Python ランタイムライブラリへの相対パスを取得するために使われます。デフォルトの値は `'python'` です。引数はゼロ終端されたワイドキャラクタ文字列で、静的な記憶領域に入っていなければならず、その内容はプログラムの実行中に変更してはなりません。 Python インタプリタ内のコードで、この記憶領域の内容を変更するものは一切ありません。

  Use `Py_DecodeLocale()` to decode a bytes string to get a wchar_t* string.

  Deprecated since version 3.11, will be removed in version 3.15.

wchar_t *Py_GetProgramName()
  次に属します: Stable ABI.

  Return the program name set with `PyConfig.program_name`, or the default. The returned string points into static storage; the caller should not modify its value.

  This function should not be called before `Py_Initialize()`, otherwise it returns `NULL`.

  バージョン 3.10 で変更: It now returns `NULL` if called before `Py_Initialize()`.

  Deprecated since version 3.13, will be removed in version 3.15: Use `PyConfig_Get("executable")` (`sys.executable`) instead.

wchar_t *Py_GetPrefix()
  次に属します: Stable ABI.

  Return the prefix for installed platform-independent files. This is derived through a number of complicated rules from the program name set with `PyConfig.program_name` and some environment variables; for example, if the program name is `'/usr/local/bin/python'`, the prefix is `'/usr/local'`. The returned string points into static storage; the caller should not modify its value. This corresponds to the prefix variable in the top-level `Makefile` and the `--prefix` argument to the configure script at build time. The value is available to Python code as `sys.base_prefix`. It is only useful on Unix. See also the next function.

  This function should not be called before `Py_Initialize()`, otherwise it returns `NULL`.

  バージョン 3.10 で変更: It now returns `NULL` if called before `Py_Initialize()`.

  Deprecated since version 3.13, will be removed in version 3.15: Use `PyConfig_Get("base_prefix")` (`sys.base_prefix`) instead. Use `PyConfig_Get("prefix")` (`sys.prefix`) if virtual environments need to be handled.

wchar_t *Py_GetExecPrefix()
  次に属します: Stable ABI.

  Return the exec-prefix for installed platform-dependent files. This is derived through a number of complicated rules from the program name set with `PyConfig.program_name` and some environment variables; for example, if the program name is `'/usr/local/bin/python'`, the exec-prefix is `'/usr/local'`. The returned string points into static storage; the caller should not modify its value. This corresponds to the exec_prefix variable in the top-level `Makefile` and the `--exec-prefix` argument to the configure script at build time. The value is available to Python code as `sys.base_exec_prefix`. It is only useful on Unix.

  背景: プラットフォーム依存のファイル (実行形式や共有ライブラリ) が別のディレクトリツリー内にインストールされている場合、 exec-prefix は prefix と異なります。典型的なインストール形態では、プラットフォーム非依存のファイルが `/usr/local` に収められる一方、プラットフォーム依存のファイルは `/usr/local/plat` サブツリーに収められます。

  一般的に、プラットフォームとは、ハードウェアとソフトウェアファミリの組み合わせを指します。例えば、Solaris 2.x を動作させている Sparc マシンは全て同じプラットフォームであるとみなしますが、Solaris 2.x を動作させている Intel マシンは違うプラットフォームになりますし、同じ Intel マシンでも Linux を動作させているならまた別のプラットフォームです。一般的には、同じオペレーティングシステムでも、メジャーリビジョンの違うものは異なるプラットフォームです。非 Unix のオペレーティングシステムの場合は話はまた別です; 非 Unix のシステムでは、インストール方法はとても異なっていて、prefix や exec-prefix には意味がなく、空文字列が設定されています。コンパイル済みの Python バイトコードはプラットフォームに依存しないので注意してください (ただし、どのバージョンの Python でコンパイルされたかには依存します!)。

  システム管理者は、 mount や automount プログラムを使って、各プラットフォーム用の `/usr/local/plat` を異なったファイルシステムに置き、プラットフォーム間で `/usr/local` を共有するための設定方法を知っているでしょう。

  This function should not be called before `Py_Initialize()`, otherwise it returns `NULL`.

  バージョン 3.10 で変更: It now returns `NULL` if called before `Py_Initialize()`.

  Deprecated since version 3.13, will be removed in version 3.15: Use `PyConfig_Get("base_exec_prefix")` (`sys.base_exec_prefix`) instead. Use `PyConfig_Get("exec_prefix")` (`sys.exec_prefix`) if virtual environments need to be handled.

wchar_t *Py_GetProgramFullPath()
  次に属します: Stable ABI.

  Return the full program name of the Python executable; this is computed as a side-effect of deriving the default module search path from the program name (set by `PyConfig.program_name`). The returned string points into static storage; the caller should not modify its value. The value is available to Python code as `sys.executable`.

  This function should not be called before `Py_Initialize()`, otherwise it returns `NULL`.

  バージョン 3.10 で変更: It now returns `NULL` if called before `Py_Initialize()`.

  Deprecated since version 3.13, will be removed in version 3.15: Use `PyConfig_Get("executable")` (`sys.executable`) instead.

wchar_t *Py_GetPath()
  次に属します: Stable ABI.

  Return the default module search path; this is computed from the program name (set by `PyConfig.program_name`) and some environment variables. The returned string consists of a series of directory names separated by a platform dependent delimiter character. The delimiter character is `':'` on Unix and macOS, `';'` on Windows. The returned string points into static storage; the caller should not modify its value. The list `sys.path` is initialized with this value on interpreter startup; it can be (and usually is) modified later to change the search path for loading modules.

  This function should not be called before `Py_Initialize()`, otherwise it returns `NULL`.

  バージョン 3.10 で変更: It now returns `NULL` if called before `Py_Initialize()`.

  Deprecated since version 3.13, will be removed in version 3.15: Use `PyConfig_Get("module_search_paths")` (`sys.path`) instead.

const char *Py_GetVersion()
  次に属します: Stable ABI.

  Python インタプリタのバージョンを返します。バージョンは、次のような形式の文字列です

      "3.0a5+ (py3k:63103M, May 12 2008, 00:53:55) \n[GCC 4.2.3]"

  第一ワード (最初のスペース文字まで) は、現在の Python のバージョンです; 最初の文字は、ピリオドで区切られたメジャーバージョンとマイナーバージョンです。関数が返す文字列ポインタは静的な記憶領域を返します; 関数の呼び出し側はこの値を変更できません。この値は Python コードからは `sys.version` として利用できます。

  See also the `Py_Version` constant.

const char *Py_GetPlatform()
  次に属します: Stable ABI.

  現在のプラットフォームのプラットフォーム識別文字列を返します。Unixでは、オペレーティングシステムの "公式の" 名前を小文字に変換し、後ろにメジャーリビジョン番号を付けた構成になっています。例えば Solaris 2.x は、SunOS 5.x, としても知られていますが、`'sunos5'` になります。macOS では `'darwin'` です。Windows では `'win'` です。関数が返す文字列ポインタは静的な記憶領域を返します; 関数の呼び出し側はこの値を変更できません。この値は Python コードからは `sys.platform` として利用できます。

const char *Py_GetCopyright()
  次に属します: Stable ABI.

  現在の Python バージョンに対する公式の著作権表示文字列を返します。例えば

  `'Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam'`

  関数が返す文字列ポインタは静的な記憶領域を返します; 関数の呼び出し側はこの値を変更できません。この値は Python コードからは `sys.copyright` として利用できます。

const char *Py_GetCompiler()
  次に属します: Stable ABI.

  現在使っているバージョンの Python をビルドする際に用いたコンパイラを示す文字列を、角括弧で囲った文字列を返します。例えば:

      "[GCC 2.7.2.2]"

  関数が返す文字列ポインタは静的な記憶領域を返します; 関数の呼び出し側はこの値を変更できません。この値は Python コードからは `sys.version` の一部として取り出せます。

const char *Py_GetBuildInfo()
  次に属します: Stable ABI.

  現在使っている Python インタプリタインスタンスの、シーケンス番号とビルド日時に関する情報を返します。例えば

      "#67, Aug  1 1997, 22:34:28"

  関数が返す文字列ポインタは静的な記憶領域を返します; 関数の呼び出し側はこの値を変更できません。この値は Python コードからは `sys.version` の一部として取り出せます。

void PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)
  次に属します: Stable ABI.

  This API is kept for backward compatibility: setting `PyConfig.argv`, `PyConfig.parse_argv` and `PyConfig.safe_path` should be used instead, see Python Initialization Configuration.

  argc および argv に基づいて `sys.argv` を設定します。これらの引数はプログラムの `main()` に渡した引数に似ていますが、最初の要素が Python インタプリタの宿主となっている実行形式の名前ではなく、実行されるスクリプト名を参照しなければならない点が違います。実行するスクリプトがない場合、 argv の最初の要素は空文字列にしてもかまいません。この関数が `sys.argv` の初期化に失敗した場合、致命的エラーを `Py_FatalError()` で知らせます。

  updatepath が 0 の場合、ここまでの動作がこの関数がすることの全てです。 updatepath が 0 でない場合、この関数は `sys.path` を以下のアルゴリズムに基づいて修正します:

  - 存在するスクリプトの名前が `argv[0]` に渡された場合、そのスクリプトがある場所の絶対パスを `sys.path` の先頭に追加します。

  - それ以外の場合 (argc が `0` だったり、 `argv[0]` が存在するファイル名を指していない場合)、 `sys.path` の先頭に空の文字列を追加します。 これは現在の作業ディレクトリ (`"."`) を先頭に追加するのと同じです。

  Use `Py_DecodeLocale()` to decode a bytes string to get a wchar_t* string.

  See also `PyConfig.orig_argv` and `PyConfig.argv` members of the Python Initialization Configuration.

  注釈

  It is recommended that applications embedding the Python interpreter for purposes other than executing a single script pass `0` as updatepath, and update `sys.path` themselves if desired. See CVE 2008-5983.

  3.1.3 より前のバージョンでは、 `PySys_SetArgv()` の呼び出しが完了した後に `sys.path` の先頭の要素を取り出すことで、同じ効果が得られます。例えばこのように使います:

      PyRun_SimpleString("import sys; sys.path.pop(0)\n");

  Added in version 3.1.3.

  Deprecated since version 3.11, will be removed in version 3.15.

void PySys_SetArgv(int argc, wchar_t **argv)
  次に属します: Stable ABI.

  This API is kept for backward compatibility: setting `PyConfig.argv` and `PyConfig.parse_argv` should be used instead, see Python Initialization Configuration.

  この関数は、 python インタプリタが `-I` オプション付きで実行されている場合を除き `PySys_SetArgvEx()` の updatepath に `1` を設定したのと同じように動作します。

  Use `Py_DecodeLocale()` to decode a bytes string to get a wchar_t* string.

  See also `PyConfig.orig_argv` and `PyConfig.argv` members of the Python Initialization Configuration.

  バージョン 3.4 で変更: updatepath の値は `-I` オプションに依存します。

  Deprecated since version 3.11, will be removed in version 3.15.

void Py_SetPythonHome(const wchar_t *home)
  次に属します: Stable ABI.

  This API is kept for backward compatibility: setting `PyConfig.home` should be used instead, see Python Initialization Configuration.

  Python の標準ライブラリがある、デフォルトの "home" ディレクトリを設定します。引数の文字列の意味については `PYTHONHOME` を参照してください。

  引数は静的なストレージに置かれてプログラム実行中に書き換えられないようなゼロ終端の文字列であるべきです。Python インタプリタはこのストレージの内容を変更しません。

  Use `Py_DecodeLocale()` to decode a bytes string to get a wchar_t* string.

  Deprecated since version 3.11, will be removed in version 3.15.

wchar_t *Py_GetPythonHome()
  次に属します: Stable ABI.

  Return the default "home", that is, the value set by `PyConfig.home`, or the value of the `PYTHONHOME` environment variable if it is set.

  This function should not be called before `Py_Initialize()`, otherwise it returns `NULL`.

  バージョン 3.10 で変更: It now returns `NULL` if called before `Py_Initialize()`.

  Deprecated since version 3.13, will be removed in version 3.15: Use `PyConfig_Get("home")` or the `PYTHONHOME` environment variable instead.

## スレッド状態 (thread state) とグローバルインタプリタロック (global interpreter lock)

Unless on a free-threaded build of CPython, the Python interpreter is not fully thread-safe. In order to support multi-threaded Python programs, there's a global lock, called the global interpreter lock or GIL, that must be held by the current thread before it can safely access Python objects. Without the lock, even the simplest operations could cause problems in a multi-threaded program: for example, when two threads simultaneously increment the reference count of the same object, the reference count could end up being incremented only once instead of twice.

このため、 GIL を獲得したスレッドだけが Python オブジェクトを操作したり、 Python/C API 関数を呼び出したりできるというルールがあります。並行処理をエミュレートするために、インタプリタは定期的にロックを解放したり獲得したりします。 (`sys.setswitchinterval()` を参照) このロックはブロックが起こりうる I/O 操作の付近でも解放・獲得され、 I/O を要求するスレッドが I/O 操作の完了を待つ間、他のスレッドが動作できるようにしています。

The Python interpreter keeps some thread-specific bookkeeping information inside a data structure called `PyThreadState`, known as a thread state. Each OS thread has a thread-local pointer to a `PyThreadState`; a thread state referenced by this pointer is considered to be attached.

A thread can only have one attached thread state at a time. An attached thread state is typically analogous with holding the GIL, except on free-threaded builds. On builds with the GIL enabled, attaching a thread state will block until the GIL can be acquired. However, even on builds with the GIL disabled, it is still required to have an attached thread state to call most of the C API.

In general, there will always be an attached thread state when using Python's C API. Only in some specific cases (such as in a `Py_BEGIN_ALLOW_THREADS` block) will the thread not have an attached thread state. If uncertain, check if `PyThreadState_GetUnchecked()` returns `NULL`.

### Detaching the thread state from extension code

Most extension code manipulating the thread state has the following simple structure:

    Save the thread state in a local variable.
    ... Do some blocking I/O operation ...
    Restore the thread state from the local variable.

この構造は非常に一般的なので、作業を単純にするために2つのマクロが用意されています:

    Py_BEGIN_ALLOW_THREADS
    ... Do some blocking I/O operation ...
    Py_END_ALLOW_THREADS

`Py_BEGIN_ALLOW_THREADS` マクロは新たなブロックを開始し、隠しローカル変数を宣言します; `Py_END_ALLOW_THREADS` はブロックを閉じます。

上のブロックは次のコードに展開されます:

    PyThreadState *_save;

    _save = PyEval_SaveThread();
    ... Do some blocking I/O operation ...
    PyEval_RestoreThread(_save);

Here is how these functions work:

The attached thread state holds the GIL for the entire interpreter. When detaching the attached thread state, the GIL is released, allowing other threads to attach a thread state to their own thread, thus getting the GIL and can start executing. The pointer to the prior attached thread state is stored as a local variable. Upon reaching `Py_END_ALLOW_THREADS`, the thread state that was previously attached is passed to `PyEval_RestoreThread()`. This function will block until another releases its thread state, thus allowing the old thread state to get re-attached and the C API can be called again.

For free-threaded builds, the GIL is normally out of the question, but detaching the thread state is still required for blocking I/O and long operations. The difference is that threads don't have to wait for the GIL to be released to attach their thread state, allowing true multi-core parallelism.

注釈

Calling system I/O functions is the most common use case for detaching the thread state, but it can also be useful before calling long-running computations which don't need access to Python objects, such as compression or cryptographic functions operating over memory buffers. For example, the standard `zlib` and `hashlib` modules detach the thread state when compressing or hashing data.

### Python 以外で作られたスレッド

When threads are created using the dedicated Python APIs (such as the `threading` module), a thread state is automatically associated to them and the code showed above is therefore correct. However, when threads are created from C (for example by a third-party library with its own thread management), they don't hold the GIL, because they don't have an attached thread state.

If you need to call Python code from these threads (often this will be part of a callback API provided by the aforementioned third-party library), you must first register these threads with the interpreter by creating an attached thread state before you can start using the Python/C API. When you are done, you should detach the thread state, and finally free it.

`PyGILState_Ensure()` と `PyGILState_Release()` はこの処理を自動的に行います。 Cのスレッドから Python を呼び出す典型的な方法は以下のとおりです:

    PyGILState_STATE gstate;
    gstate = PyGILState_Ensure();

    /* Perform Python actions here. */
    result = CallSomeFunction();
    /* evaluate result or handle exception */

    /* Release the thread. No Python API allowed beyond this point. */
    PyGILState_Release(gstate);

Note that the `PyGILState_*` functions assume there is only one global interpreter (created automatically by `Py_Initialize()`). Python supports the creation of additional interpreters (using `Py_NewInterpreter()`), but mixing multiple interpreters and the `PyGILState_*` API is unsupported. This is because `PyGILState_Ensure()` and similar functions default to attaching a thread state for the main interpreter, meaning that the thread can't safely interact with the calling subinterpreter.

### Supporting subinterpreters in non-Python threads

If you would like to support subinterpreters with non-Python created threads, you must use the `PyThreadState_*` API instead of the traditional `PyGILState_*` API.

In particular, you must store the interpreter state from the calling function and pass it to `PyThreadState_New()`, which will ensure that the thread state is targeting the correct interpreter:

    /* The return value of PyInterpreterState_Get() from the
       function that created this thread. */
    PyInterpreterState *interp = ThreadData->interp;
    PyThreadState *tstate = PyThreadState_New(interp);
    PyThreadState_Swap(tstate);

    /* GIL of the subinterpreter is now held.
       Perform Python actions here. */
    result = CallSomeFunction();
    /* evaluate result or handle exception */

    /* Destroy the thread state. No Python API allowed beyond this point. */
    PyThreadState_Clear(tstate);
    PyThreadState_DeleteCurrent();

### Cautions about fork()

Another important thing to note about threads is their behaviour in the face of the C `fork()` call. On most systems with `fork()`, after a process forks only the thread that issued the fork will exist. This has a concrete impact both on how locks must be handled and on all stored state in CPython's runtime.

The fact that only the "current" thread remains means any locks held by other threads will never be released. Python solves this for `os.fork()` by acquiring the locks it uses internally before the fork, and releasing them afterwards. In addition, it resets any Lock objects in the child. When extending or embedding Python, there is no way to inform Python of additional (non-Python) locks that need to be acquired before or reset after a fork. OS facilities such as `pthread_atfork()` would need to be used to accomplish the same thing. Additionally, when extending or embedding Python, calling `fork()` directly rather than through `os.fork()` (and returning to or calling into Python) may result in a deadlock by one of Python's internal locks being held by a thread that is defunct after the fork. `PyOS_AfterFork_Child()` tries to reset the necessary locks, but is not always able to.

The fact that all other threads go away also means that CPython's runtime state there must be cleaned up properly, which `os.fork()` does. This means finalizing all other `PyThreadState` objects belonging to the current interpreter and all other `PyInterpreterState` objects. Due to this and the special nature of the "main" interpreter, `fork()` should only be called in that interpreter's "main" thread, where the CPython global runtime was originally initialized. The only exception is if `exec()` will be called immediately after.

### Cautions regarding runtime finalization

In the late stage of interpreter shutdown, after attempting to wait for non-daemon threads to exit (though this can be interrupted by `KeyboardInterrupt`) and running the `atexit` functions, the runtime is marked as finalizing: `Py_IsFinalizing()` and `sys.is_finalizing()` return true. At this point, only the finalization thread that initiated finalization (typically the main thread) is allowed to acquire the GIL.

If any thread, other than the finalization thread, attempts to attach a thread state during finalization, either explicitly or implicitly, the thread enters a permanently blocked state where it remains until the program exits. In most cases this is harmless, but this can result in deadlock if a later stage of finalization attempts to acquire a lock owned by the blocked thread, or otherwise waits on the blocked thread.

Gross? Yes. This prevents random crashes and/or unexpectedly skipped C++ finalizations further up the call stack when such threads were forcibly exited here in CPython 3.13 and earlier. The CPython runtime thread state C APIs have never had any error reporting or handling expectations at thread state attachment time that would've allowed for graceful exit from this situation. Changing that would require new stable C APIs and rewriting the majority of C code in the CPython ecosystem to use those with error handling.

### 高レベルAPI

C拡張を書いたりPythonインタプリタを埋め込むときに最も一般的に使われる型や関数は次のとおりです:

type PyInterpreterState
  次に属します: Limited API (不透明な構造体として).

  このデータ構造体は、協調動作する多数のスレッド間で共有されている状態を表現します。同じインタプリタに属するスレッドはモジュール管理情報やその他いくつかの内部的な情報を共有しています。この構造体には公開 (public) のメンバはありません。

  異なるインタプリタに属するスレッド間では、利用可能なメモリ、開かれているファイルデスクリプタなどといったプロセス状態を除いて、初期状態では何も共有されていません。GILもまた、スレッドがどのインタプリタに属しているかに関わらずすべてのスレッドで共有されています。

  バージョン 3.12 で変更: PEP 684 introduced the possibility of a per-interpreter GIL. See `Py_NewInterpreterFromConfig()`.

type PyThreadState
  次に属します: Limited API (不透明な構造体として).

  This data structure represents the state of a single thread. The only public data member is:

  PyInterpreterState *interp

    This thread's interpreter state.

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

  Deprecated function which does nothing.

  In Python 3.6 and older, this function created the GIL if it didn't exist.

  バージョン 3.9 で変更: The function now does nothing.

  バージョン 3.7 で変更: この関数は `Py_Initialize()` から呼び出されるようになり、わざわざ呼び出す必要はもう無くなりました。

  バージョン 3.2 で変更: この関数は `Py_Initialize()` より前に呼び出すことができなくなりました。

  バージョン 3.9 で非推奨.

PyThreadState *PyEval_SaveThread()
  次に属します: Stable ABI.

  Detach the attached thread state and return it. The thread will have no thread state upon returning.

void PyEval_RestoreThread(PyThreadState *tstate)
  次に属します: Stable ABI.

  Set the attached thread state to tstate. The passed thread state should not be attached, otherwise deadlock ensues. tstate will be attached upon returning.

  注釈

  Calling this function from a thread when the runtime is finalizing will hang the thread until the program exits, even if the thread was not created by Python. Refer to Cautions regarding runtime finalization for more details.

  バージョン 3.14 で変更: Hangs the current thread, rather than terminating it, if called while the interpreter is finalizing.

PyThreadState *PyThreadState_Get()
  次に属します: Stable ABI.

  Return the attached thread state. If the thread has no attached thread state, (such as when inside of `Py_BEGIN_ALLOW_THREADS` block), then this issues a fatal error (so that the caller needn't check for `NULL`).

  See also `PyThreadState_GetUnchecked()`.

PyThreadState *PyThreadState_GetUnchecked()

  Similar to `PyThreadState_Get()`, but don't kill the process with a fatal error if it is NULL. The caller is responsible to check if the result is NULL.

  Added in version 3.13: In Python 3.5 to 3.12, the function was private and known as `_PyThreadState_UncheckedGet()`.

PyThreadState *PyThreadState_Swap(PyThreadState *tstate)
  次に属します: Stable ABI.

  Set the attached thread state to tstate, and return the thread state that was attached prior to calling.

  This function is safe to call without an attached thread state; it will simply return `NULL` indicating that there was no prior thread state.

  参考

  `PyEval_ReleaseThread()`

  注釈

  Similar to `PyGILState_Ensure()`, this function will hang the thread if the runtime is finalizing.

以下の関数はスレッドローカルストレージを利用していて、サブインタプリタとの互換性がありません:

type PyGILState_STATE
  次に属します: Stable ABI.

  The type of the value returned by `PyGILState_Ensure()` and passed to `PyGILState_Release()`.

  enumerator PyGILState_LOCKED

    The GIL was already held when `PyGILState_Ensure()` was called.

  enumerator PyGILState_UNLOCKED

    The GIL was not held when `PyGILState_Ensure()` was called.

PyGILState_STATE PyGILState_Ensure()
  次に属します: Stable ABI.

  Ensure that the current thread is ready to call the Python C API regardless of the current state of Python, or of the attached thread state. This may be called as many times as desired by a thread as long as each call is matched with a call to `PyGILState_Release()`. In general, other thread-related APIs may be used between `PyGILState_Ensure()` and `PyGILState_Release()` calls as long as the thread state is restored to its previous state before the Release(). For example, normal usage of the `Py_BEGIN_ALLOW_THREADS` and `Py_END_ALLOW_THREADS` macros is acceptable.

  The return value is an opaque "handle" to the attached thread state when `PyGILState_Ensure()` was called, and must be passed to `PyGILState_Release()` to ensure Python is left in the same state. Even though recursive calls are allowed, these handles cannot be shared - each unique call to `PyGILState_Ensure()` must save the handle for its call to `PyGILState_Release()`.

  When the function returns, there will be an attached thread state and the thread will be able to call arbitrary Python code. Failure is a fatal error.

  警告

  Calling this function when the runtime is finalizing is unsafe. Doing so will either hang the thread until the program ends, or fully crash the interpreter in rare cases. Refer to Cautions regarding runtime finalization for more details.

  バージョン 3.14 で変更: Hangs the current thread, rather than terminating it, if called while the interpreter is finalizing.

void PyGILState_Release(PyGILState_STATE)
  次に属します: Stable ABI.

  獲得したすべてのリソースを解放します。この関数を呼び出すと、Pythonの状態は対応する `PyGILState_Ensure()` を呼び出す前と同じとなります (通常、この状態は呼び出し元でははわかりませんので、GILState APIを利用するようにしてください)。

  `PyGILState_Ensure()` を呼び出す場合は、必ず同一スレッド内で対応する `PyGILState_Release()` を呼び出してください。

PyThreadState *PyGILState_GetThisThreadState()
  次に属します: Stable ABI.

  Get the attached thread state for this thread. May return `NULL` if no GILState API has been used on the current thread. Note that the main thread always has such a thread-state, even if no auto-thread-state call has been made on the main thread. This is mainly a helper/diagnostic function.

  注釈

  This function may return non-`NULL` even when the thread state is detached. Prefer `PyThreadState_Get()` or `PyThreadState_GetUnchecked()` for most cases.

  参考

  `PyThreadState_Get()`

int PyGILState_Check()

  Return `1` if the current thread is holding the GIL and `0` otherwise. This function can be called from any thread at any time. Only if it has had its thread state initialized via `PyGILState_Ensure()` will it return `1`. This is mainly a helper/diagnostic function. It can be useful for example in callback contexts or memory allocation functions when knowing that the GIL is locked can allow the caller to perform sensitive actions or otherwise behave differently.

  注釈

  If the current Python process has ever created a subinterpreter, this function will always return `1`. Prefer `PyThreadState_GetUnchecked()` for most cases.

  Added in version 3.4.

以下のマクロは、通常末尾にセミコロンを付けずに使います; Python ソース配布物内の使用例を見てください。

Py_BEGIN_ALLOW_THREADS
  次に属します: Stable ABI.

  このマクロを展開すると `{ PyThreadState *_save; _save = PyEval_SaveThread();` になります。マクロに開き波括弧が入っていることに注意してください; この波括弧は後で `Py_END_ALLOW_THREADS` マクロと対応させなければなりません。マクロについての詳しい議論は上記を参照してください。

Py_END_ALLOW_THREADS
  次に属します: Stable ABI.

  このマクロを展開すると `PyEval_RestoreThread(_save); }` になります。マクロに開き波括弧が入っていることに注意してください; この波括弧は事前の `Py_BEGIN_ALLOW_THREADS` マクロと対応していなければなりません。マクロについての詳しい議論は上記を参照してください。

Py_BLOCK_THREADS
  次に属します: Stable ABI.

  このマクロを展開すると `PyEval_RestoreThread(_save);` になります: 閉じ波括弧のない `Py_END_ALLOW_THREADS` と同じです。

Py_UNBLOCK_THREADS
  次に属します: Stable ABI.

  このマクロを展開すると `_save = PyEval_SaveThread();` になります: 開き波括弧のない `Py_BEGIN_ALLOW_THREADS` と同じです。

### 低レベルAPI

次の全ての関数は `Py_Initialize()` の後に呼び出さなければなりません。

バージョン 3.7 で変更: `Py_Initialize()` now initializes the GIL and sets an attached thread state.

PyInterpreterState *PyInterpreterState_New()
  次に属します: Stable ABI.

  Create a new interpreter state object. An attached thread state is not needed, but may optionally exist if it is necessary to serialize calls to this function.

  引数無しで 監査イベント `cpython.PyInterpreterState_New` を送出します。

void PyInterpreterState_Clear(PyInterpreterState *interp)
  次に属します: Stable ABI.

  Reset all information in an interpreter state object. There must be an attached thread state for the interpreter.

  引数無しで 監査イベント `cpython.PyInterpreterState_Clear` を送出します。

void PyInterpreterState_Delete(PyInterpreterState *interp)
  次に属します: Stable ABI.

  Destroy an interpreter state object. There should not be an attached thread state for the target interpreter. The interpreter state must have been reset with a previous call to `PyInterpreterState_Clear()`.

PyThreadState *PyThreadState_New(PyInterpreterState *interp)
  次に属します: Stable ABI.

  Create a new thread state object belonging to the given interpreter object. An attached thread state is not needed.

void PyThreadState_Clear(PyThreadState *tstate)
  次に属します: Stable ABI.

  Reset all information in a thread state object. tstate must be attached

  バージョン 3.9 で変更: This function now calls the `PyThreadState.on_delete` callback. Previously, that happened in `PyThreadState_Delete()`.

  バージョン 3.13 で変更: The `PyThreadState.on_delete` callback was removed.

void PyThreadState_Delete(PyThreadState *tstate)
  次に属します: Stable ABI.

  Destroy a thread state object. tstate should not be attached to any thread. tstate must have been reset with a previous call to `PyThreadState_Clear()`.

void PyThreadState_DeleteCurrent(void)

  Detach the attached thread state (which must have been reset with a previous call to `PyThreadState_Clear()`) and then destroy it.

  No thread state will be attached upon returning.

PyFrameObject *PyThreadState_GetFrame(PyThreadState *tstate)
  次に属します: Stable ABI (バージョン 3.10 より).

  Get the current frame of the Python thread state tstate.

  Return a strong reference. Return `NULL` if no frame is currently executing.

  See also `PyEval_GetFrame()`.

  tstate must not be `NULL`, and must be attached.

  Added in version 3.9.

uint64_t PyThreadState_GetID(PyThreadState *tstate)
  次に属します: Stable ABI (バージョン 3.10 より).

  Get the unique thread state identifier of the Python thread state tstate.

  tstate must not be `NULL`, and must be attached.

  Added in version 3.9.

PyInterpreterState *PyThreadState_GetInterpreter(PyThreadState *tstate)
  次に属します: Stable ABI (バージョン 3.10 より).

  Get the interpreter of the Python thread state tstate.

  tstate must not be `NULL`, and must be attached.

  Added in version 3.9.

void PyThreadState_EnterTracing(PyThreadState *tstate)

  Suspend tracing and profiling in the Python thread state tstate.

  Resume them using the `PyThreadState_LeaveTracing()` function.

  Added in version 3.11.

void PyThreadState_LeaveTracing(PyThreadState *tstate)

  Resume tracing and profiling in the Python thread state tstate suspended by the `PyThreadState_EnterTracing()` function.

  See also `PyEval_SetTrace()` and `PyEval_SetProfile()` functions.

  Added in version 3.11.

PyInterpreterState *PyInterpreterState_Get(void)
  次に属します: Stable ABI (バージョン 3.9 より).

  Get the current interpreter.

  Issue a fatal error if there no attached thread state. It cannot return NULL.

  Added in version 3.9.

int64_t PyInterpreterState_GetID(PyInterpreterState *interp)
  次に属します: Stable ABI (バージョン 3.7 より).

  インタプリタの一意な ID を返します。 処理中に何かエラーが起きたら、 `-1` が返され、エラーがセットされます。

  The caller must have an attached thread state.

  Added in version 3.7.

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

  インタプリタ固有のデータを保持している辞書を返します。 この関数が `NULL` を返した場合は、ここまでで例外は送出されておらず、呼び出し側はインタプリタ固有の辞書は利用できないと考えなければなりません。

  この関数は `PyModule_GetState()` を置き換えるものではなく、拡張モジュールがインタプリタ固有の状態情報を格納するのに使うべきものです。

  The returned dictionary is borrowed from the interpreter and is valid until interpreter shutdown.

  Added in version 3.8.

typedef PyObject *(*_PyFrameEvalFunction)(PyThreadState *tstate, _PyInterpreterFrame *frame, int throwflag)

  Type of a frame evaluation function.

  The throwflag parameter is used by the `throw()` method of generators: if non-zero, handle the current exception.

  バージョン 3.9 で変更: The function now takes a tstate parameter.

  バージョン 3.11 で変更: The frame parameter changed from `PyFrameObject*` to `_PyInterpreterFrame*`.

_PyFrameEvalFunction _PyInterpreterState_GetEvalFrameFunc(PyInterpreterState *interp)

  Get the frame evaluation function.

  See the PEP 523 "Adding a frame evaluation API to CPython".

  Added in version 3.9.

void _PyInterpreterState_SetEvalFrameFunc(PyInterpreterState *interp, _PyFrameEvalFunction eval_frame)

  Set the frame evaluation function.

  See the PEP 523 "Adding a frame evaluation API to CPython".

  Added in version 3.9.

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

  Return a dictionary in which extensions can store thread-specific state information. Each extension should use a unique key to use to store state in the dictionary. It is okay to call this function when no thread state is attached. If this function returns `NULL`, no exception has been raised and the caller should assume no thread state is attached.

int PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc)
  次に属します: Stable ABI.

  Asynchronously raise an exception in a thread. The id argument is the thread id of the target thread; exc is the exception object to be raised. This function does not steal any references to exc. To prevent naive misuse, you must write your own C extension to call this. Must be called with an attached thread state. Returns the number of thread states modified; this is normally one, but will be zero if the thread id isn't found. If exc is `NULL`, the pending exception (if any) for the thread is cleared. This raises no exceptions.

  バージョン 3.7 で変更: id 引数の型が long から unsigned long へ変更されました。

void PyEval_AcquireThread(PyThreadState *tstate)
  次に属します: Stable ABI.

  Attach tstate to the current thread, which must not be `NULL` or already attached.

  The calling thread must not already have an attached thread state.

  注釈

  Calling this function from a thread when the runtime is finalizing will hang the thread until the program exits, even if the thread was not created by Python. Refer to Cautions regarding runtime finalization for more details.

  バージョン 3.8 で変更: Updated to be consistent with `PyEval_RestoreThread()`, `Py_END_ALLOW_THREADS()`, and `PyGILState_Ensure()`, and terminate the current thread if called while the interpreter is finalizing.

  バージョン 3.14 で変更: Hangs the current thread, rather than terminating it, if called while the interpreter is finalizing.

  `PyEval_RestoreThread()` はいつでも (スレッドが初期化されたいないときでも) 利用可能な高レベル関数です。

void PyEval_ReleaseThread(PyThreadState *tstate)
  次に属します: Stable ABI.

  Detach the attached thread state. The tstate argument, which must not be `NULL`, is only used to check that it represents the attached thread state --- if it isn't, a fatal error is reported.

  `PyEval_SaveThread()` はより高レベルな関数で常に (スレッドが初期化されていないときでも) 利用できます。

## サブインタプリタサポート

ほとんどの場合は埋め込む Python インタプリタは1つだけですが、いくつかの場合に同一プロセス内、あるいは同一スレッド内で、複数の独立したインタプリタを作成する必要があります。 これを可能にするのがサブインタプリタです。

"メイン" インタプリタとは、ランタイムが初期化を行ったときに最初に作成されたインタプリタのことです。 サブインタプリタと違い、メインインタプリタにはシグナルハンドリングのような、プロセス全域で唯一な責務があります。 メインインタプリタにはランタイムの初期化中の処理実行という責務もあり、通常はランタイムの終了処理中に動いているランタイムでもあります。 `PyInterpreterState_Main()` 関数は、メインインタプリタの状態へのポインタを返します。

サブインタプリタを切り替えが `PyThreadState_Swap()` 関数でできます。 次の関数を使ってサブインタプリタの作成と削除が行えます:

type PyInterpreterConfig

  Structure containing most parameters to configure a sub-interpreter. Its values are used only in `Py_NewInterpreterFromConfig()` and never modified by the runtime.

  Added in version 3.12.

  構造体フィールド:

  int use_main_obmalloc

    If this is `0` then the sub-interpreter will use its own "object" allocator state. Otherwise it will use (share) the main interpreter's.

    If this is `0` then `check_multi_interp_extensions` must be `1` (non-zero). If this is `1` then `gil` must not be `PyInterpreterConfig_OWN_GIL`.

  int allow_fork

    If this is `0` then the runtime will not support forking the process in any thread where the sub-interpreter is currently active. Otherwise fork is unrestricted.

    Note that the `subprocess` module still works when fork is disallowed.

  int allow_exec

    If this is `0` then the runtime will not support replacing the current process via exec (e.g. `os.execv()`) in any thread where the sub-interpreter is currently active. Otherwise exec is unrestricted.

    Note that the `subprocess` module still works when exec is disallowed.

  int allow_threads

    If this is `0` then the sub-interpreter's `threading` module won't create threads. Otherwise threads are allowed.

  int allow_daemon_threads

    If this is `0` then the sub-interpreter's `threading` module won't create daemon threads. Otherwise daemon threads are allowed (as long as `allow_threads` is non-zero).

  int check_multi_interp_extensions

    If this is `0` then all extension modules may be imported, including legacy (single-phase init) modules, in any thread where the sub-interpreter is currently active. Otherwise only multi-phase init extension modules (see PEP 489) may be imported. (Also see `Py_mod_multiple_interpreters`.)

    This must be `1` (non-zero) if `use_main_obmalloc` is `0`.

  int gil

    This determines the operation of the GIL for the sub-interpreter. It may be one of the following:

    PyInterpreterConfig_DEFAULT_GIL

      Use the default selection (`PyInterpreterConfig_SHARED_GIL`).

    PyInterpreterConfig_SHARED_GIL

      Use (share) the main interpreter's GIL.

    PyInterpreterConfig_OWN_GIL

      Use the sub-interpreter's own GIL.

    If this is `PyInterpreterConfig_OWN_GIL` then `PyInterpreterConfig.use_main_obmalloc` must be `0`.

PyStatus Py_NewInterpreterFromConfig(PyThreadState **tstate_p, const PyInterpreterConfig *config)

  新しいサブインタプリタ (sub-interpreter) を生成します。サブインタプリタとは、(ほぼ完全に) 個別に分割された Python コードの実行環境です。特に、新しいサブインタプリタは、 import されるモジュール全てについて個別のバージョンを持ち、これには基盤となるモジュール `builtins`, `__main__` および `sys` も含まれます。ロード済みのモジュールからなるテーブル (`sys.modules`) およびモジュール検索パス (`sys.path`) もサブインタプリタ毎に別個のものになります。新たなサブインタプリタ環境には `sys.argv` 変数がありません。また、サブインタプリタは新たな標準 I/O ストリーム `sys.stdin`, `sys.stdout`, `sys.stderr` を持ちます (とはいえ、これらのストリームは根底にある同じファイル記述子を参照しています)。

  The given config controls the options with which the interpreter is initialized.

  Upon success, tstate_p will be set to the first thread state created in the new sub-interpreter. This thread state is attached. Note that no actual thread is created; see the discussion of thread states below. If creation of the new interpreter is unsuccessful, tstate_p is set to `NULL`; no exception is set since the exception state is stored in the attached thread state, which might not exist.

  Like all other Python/C API functions, an attached thread state must be present before calling this function, but it might be detached upon returning. On success, the returned thread state will be attached. If the sub-interpreter is created with its own GIL then the attached thread state of the calling interpreter will be detached. When the function returns, the new interpreter's thread state will be attached to the current thread and the previous interpreter's attached thread state will remain detached.

  Added in version 3.12.

  Sub-interpreters are most effective when isolated from each other, with certain functionality restricted:

      PyInterpreterConfig config = {
          .use_main_obmalloc = 0,
          .allow_fork = 0,
          .allow_exec = 0,
          .allow_threads = 1,
          .allow_daemon_threads = 0,
          .check_multi_interp_extensions = 1,
          .gil = PyInterpreterConfig_OWN_GIL,
      };
      PyThreadState *tstate = NULL;
      PyStatus status = Py_NewInterpreterFromConfig(&tstate, &config);
      if (PyStatus_Exception(status)) {
          Py_ExitStatusException(status);
      }

  Note that the config is used only briefly and does not get modified. During initialization the config's values are converted into various `PyInterpreterState` values. A read-only copy of the config may be stored internally on the `PyInterpreterState`.

  Extension modules are shared between (sub-)interpreters as follows:

  - For modules using multi-phase initialization, e.g. `PyModule_FromDefAndSpec()`, a separate module object is created and initialized for each interpreter. Only C-level static and global variables are shared between these module objects.

  - For modules using single-phase initialization, e.g. `PyModule_Create()`, the first time a particular extension is imported, it is initialized normally, and a (shallow) copy of its module's dictionary is squirreled away. When the same extension is imported by another (sub-)interpreter, a new module is initialized and filled with the contents of this copy; the extension's `init` function is not called. Objects in the module's dictionary thus end up shared across (sub-)interpreters, which might cause unwanted behavior (see Bugs and caveats below).

    Note that this is different from what happens when an extension is imported after the interpreter has been completely re-initialized by calling `Py_FinalizeEx()` and `Py_Initialize()`; in that case, the extension's `initmodule` function is called again. As with multi-phase initialization, this means that only C-level static and global variables are shared between these modules.

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

  Create a new sub-interpreter. This is essentially just a wrapper around `Py_NewInterpreterFromConfig()` with a config that preserves the existing behavior. The result is an unisolated sub-interpreter that shares the main interpreter's GIL, allows fork/exec, allows daemon threads, and allows single-phase init modules.

void Py_EndInterpreter(PyThreadState *tstate)
  次に属します: Stable ABI.

  Destroy the (sub-)interpreter represented by the given thread state. The given thread state must be attached. When the call returns, there will be no attached thread state. All thread states associated with this interpreter are destroyed.

  `Py_FinalizeEx()` will destroy all sub-interpreters that haven't been explicitly destroyed at that point.

### A Per-Interpreter GIL

Using `Py_NewInterpreterFromConfig()` you can create a sub-interpreter that is completely isolated from other interpreters, including having its own GIL. The most important benefit of this isolation is that such an interpreter can execute Python code without being blocked by other interpreters or blocking any others. Thus a single Python process can truly take advantage of multiple CPU cores when running Python code. The isolation also encourages a different approach to concurrency than that of just using threads. (See PEP 554 and PEP 684.)

Using an isolated interpreter requires vigilance in preserving that isolation. That especially means not sharing any objects or mutable state without guarantees about thread-safety. Even objects that are otherwise immutable (e.g. `None`, `(1, 5)`) can't normally be shared because of the refcount. One simple but less-efficient approach around this is to use a global lock around all use of some state (or object). Alternately, effectively immutable objects (like integers or strings) can be made safe in spite of their refcounts by making them immortal. In fact, this has been done for the builtin singletons, small integers, and a number of other builtin objects.

If you preserve isolation then you will have access to proper multi-core computing without the complications that come with free-threading. Failure to preserve isolation will expose you to the full consequences of free-threading, including races and hard-to-debug crashes.

Aside from that, one of the main challenges of using multiple isolated interpreters is how to communicate between them safely (not break isolation) and efficiently. The runtime and stdlib do not provide any standard approach to this yet. A future stdlib module would help mitigate the effort of preserving isolation and expose effective tools for communicating (and sharing) data between interpreters.

Added in version 3.12.

### バグと注意事項

Because sub-interpreters (and the main interpreter) are part of the same process, the insulation between them isn't perfect --- for example, using low-level file operations like `os.close()` they can (accidentally or maliciously) affect each other's open files. Because of the way extensions are shared between (sub-)interpreters, some extensions may not work properly; this is especially likely when using single-phase initialization or (static) global variables. It is possible to insert objects created in one sub-interpreter into a namespace of another (sub-)interpreter; this should be avoided if possible.

Special care should be taken to avoid sharing user-defined functions, methods, instances or classes between sub-interpreters, since import operations executed by such objects may affect the wrong (sub-)interpreter's dictionary of loaded modules. It is equally important to avoid sharing objects from which the above are reachable.

サブインタプリタを `PyGILState_*` API と組み合わせるのが難しいことにも注意してください。これらのAPIはPythonのスレッド状態とOSレベルスレッドが1対1で対応していることを前提にしていて、サブインタプリタが存在するとその前提が崩れるからです。対応する `PyGILState_Ensure()` と `PyGILState_Release()` の呼び出しのペアの間では、サブインタプリタの切り替えを行わないことを強く推奨します。さらに、(`ctypes` のような)これらのAPIを使ってPythonの外で作られたスレッドから Pythonコードを実行している拡張モジュールはサブインタプリタを使うと壊れる可能性があります。

## 非同期通知

インタプリタのメインスレッドに非同期な通知を行うために提供されている仕組みです。これらの通知は関数ポインタと void ポインタ引数という形態を取ります。

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

  インタプリタのメインスレッドから関数が呼び出される予定を組みます。成功すると `0` が返り、func はメインスレッドの呼び出しキューに詰められます。失敗すると、例外をセットせずに `-1` が返ります。

  無事にキューに詰められると、func は いつかは必ず インタプリタのメインスレッドから、arg を引数として呼び出されます。この関数は、通常の実行中の Python コードに対して非同期に呼び出されますが、次の両方の条件に合致したときに呼び出されます:

  - bytecode 境界上にいるとき、

  - with the main thread holding an attached thread state (func can therefore use the full C API).

  func must return `0` on success, or `-1` on failure with an exception set. func won't be interrupted to perform another asynchronous notification recursively, but it can still be interrupted to switch threads if the thread state is detached.

  This function doesn't need an attached thread state. However, to call this function in a subinterpreter, the caller must have an attached thread state. Otherwise, the function func can be scheduled to be called from the wrong interpreter.

  警告

  これは、非常に特別な場合にのみ役立つ、低レベルな関数です。 func が可能な限り早く呼び出される保証はありません。メインスレッドがシステムコールを実行するのに忙しい場合は、 func はシステムコールが返ってくるまで呼び出されないでしょう。この関数は一般的には、任意の C スレッドから Python コードを呼び出すのには 向きません 。これの代わりに、 PyGILState API を使用してください。

  Added in version 3.1.

  バージョン 3.9 で変更: If this function is called in a subinterpreter, the function func is now scheduled to be called from the subinterpreter, rather than being called from the main interpreter. Each subinterpreter now has its own list of scheduled calls.

  バージョン 3.12 で変更: This function now always schedules func to be run in the main interpreter.

## プロファイルとトレース (profiling and tracing)

Python インタプリタは、プロファイル: 分析 (profile) や実行のトレース: 追跡 (trace) といった機能を組み込むために低水準のサポートを提供しています。このサポートは、プロファイルやデバッグ、適用範囲分析 (coverage analysis) ツールなどに使われます。

この C インターフェースは、プロファイルやトレース作業時に、Python レベルの呼び出し可能オブジェクトが呼び出されることによるオーバヘッドを避け、直接 C 関数呼び出しが行えるようにしています。プロファイルやトレース機能の本質的な特性は変わっていません; インターフェースではトレース関数をスレッドごとにインストールでき、トレース関数に報告される基本イベント (basic event) は以前のバージョンにおいて Python レベルのトレース関数で報告されていたものと同じです。

typedef int (*Py_tracefunc)(PyObject *obj, PyFrameObject *frame, int what, PyObject *arg)

  The type of the trace function registered using `PyEval_SetProfile()` and `PyEval_SetTrace()`. The first parameter is the object passed to the registration function as obj, frame is the frame object to which the event pertains, what is one of the constants `PyTrace_CALL`, `PyTrace_EXCEPTION`, `PyTrace_LINE`, `PyTrace_RETURN`, `PyTrace_C_CALL`, `PyTrace_C_EXCEPTION`, `PyTrace_C_RETURN`, or `PyTrace_OPCODE`, and arg depends on the value of what:

  what の値 | arg の意味
  --- | ---
  `PyTrace_CALL` | 常に `Py_None` 。
  `PyTrace_EXCEPTION` | `sys.exc_info()` の返す例外情報です。
  `PyTrace_LINE` | 常に `Py_None` 。
  `PyTrace_RETURN` | 呼び出し側に返される予定の値か、例外によって関数を抜ける場合は `NULL` です。
  `PyTrace_C_CALL` | 呼び出される関数オブジェクト。
  `PyTrace_C_EXCEPTION` | 呼び出される関数オブジェクト。
  `PyTrace_C_RETURN` | 呼び出される関数オブジェクト。
  `PyTrace_OPCODE` | 常に `Py_None` 。

int PyTrace_CALL

  関数やメソッドが新たに呼び出されたり、ジェネレータが新たなエントリの処理に入ったことを報告する際の、 `Py_tracefunc` の what の値です。イテレータやジェネレータ関数の生成は、対応するフレーム内の Python バイトコードに制御の委譲 (control transfer) が起こらないため報告されないので注意してください。

int PyTrace_EXCEPTION

  例外が送出された際の `Py_tracefunc` の what の値です。現在実行されているフレームで例外がセットされ、何らかのバイトコードが処理された後に、 what にこの値がセットされた状態でコールバック関数が呼び出されます。この結果、例外の伝播によって Python が呼び出しスタックを逆戻りする際に、各フレームから処理が戻るごとにコールバック関数が呼び出されます。トレース関数だけがこれらのイベントを受け取ります; プロファイラはこの種のイベントを必要としません。

int PyTrace_LINE

  The value passed as the what parameter to a `Py_tracefunc` function (but not a profiling function) when a line-number event is being reported. It may be disabled for a frame by setting `f_trace_lines` to 0 on that frame.

int PyTrace_RETURN

  呼び出しが返るときに `Py_tracefunc` 関数に what 引数として渡す値です。

int PyTrace_C_CALL

  C関数を呼び出す直前に `Py_tracefunc` 関数の what 引数として渡す値です。

int PyTrace_C_EXCEPTION

  C関数が例外を送出したときに `Py_tracefunc` 関数の what 引数として渡す値です。

int PyTrace_C_RETURN

  C関数から戻るときに `Py_tracefunc` 関数の what 引数として渡す値です。

int PyTrace_OPCODE

  The value for the what parameter to `Py_tracefunc` functions (but not profiling functions) when a new opcode is about to be executed. This event is not emitted by default: it must be explicitly requested by setting `f_trace_opcodes` to 1 on the frame.

void PyEval_SetProfile(Py_tracefunc func, PyObject *obj)

  Set the profiler function to func. The obj parameter is passed to the function as its first parameter, and may be any Python object, or `NULL`. If the profile function needs to maintain state, using a different value for obj for each thread provides a convenient and thread-safe place to store it. The profile function is called for all monitored events except `PyTrace_LINE` `PyTrace_OPCODE` and `PyTrace_EXCEPTION`.

  `sys.setprofile()` 関数も参照してください。

  The caller must have an attached thread state.

void PyEval_SetProfileAllThreads(Py_tracefunc func, PyObject *obj)

  Like `PyEval_SetProfile()` but sets the profile function in all running threads belonging to the current interpreter instead of the setting it only on the current thread.

  The caller must have an attached thread state.

  As `PyEval_SetProfile()`, this function ignores any exceptions raised while setting the profile functions in all threads.

Added in version 3.12.

void PyEval_SetTrace(Py_tracefunc func, PyObject *obj)

  Set the tracing function to func. This is similar to `PyEval_SetProfile()`, except the tracing function does receive line-number events and per-opcode events, but does not receive any event related to C function objects being called. Any trace function registered using `PyEval_SetTrace()` will not receive `PyTrace_C_CALL`, `PyTrace_C_EXCEPTION` or `PyTrace_C_RETURN` as a value for the what parameter.

  `sys.settrace()` 関数も参照してください。

  The caller must have an attached thread state.

void PyEval_SetTraceAllThreads(Py_tracefunc func, PyObject *obj)

  Like `PyEval_SetTrace()` but sets the tracing function in all running threads belonging to the current interpreter instead of the setting it only on the current thread.

  The caller must have an attached thread state.

  As `PyEval_SetTrace()`, this function ignores any exceptions raised while setting the trace functions in all threads.

Added in version 3.12.

## Reference tracing

Added in version 3.13.

typedef int (*PyRefTracer)(PyObject*, int event, void *data)

  The type of the trace function registered using `PyRefTracer_SetTracer()`. The first parameter is a Python object that has been just created (when event is set to `PyRefTracer_CREATE`) or about to be destroyed (when event is set to `PyRefTracer_DESTROY`). The data argument is the opaque pointer that was provided when `PyRefTracer_SetTracer()` was called.

Added in version 3.13.

int PyRefTracer_CREATE

  The value for the event parameter to `PyRefTracer` functions when a Python object has been created.

int PyRefTracer_DESTROY

  The value for the event parameter to `PyRefTracer` functions when a Python object has been destroyed.

int PyRefTracer_SetTracer(PyRefTracer tracer, void *data)

  Register a reference tracer function. The function will be called when a new Python has been created or when an object is going to be destroyed. If data is provided it must be an opaque pointer that will be provided when the tracer function is called. Return `0` on success. Set an exception and return `-1` on error.

  Not that tracer functions must not create Python objects inside or otherwise the call will be re-entrant. The tracer also must not clear any existing exception or set an exception. A thread state will be active every time the tracer function is called.

  There must be an attached thread state when calling this function.

Added in version 3.13.

PyRefTracer PyRefTracer_GetTracer(void **data)

  Get the registered reference tracer function and the value of the opaque data pointer that was registered when `PyRefTracer_SetTracer()` was called. If no tracer was registered this function will return NULL and will set the data pointer to NULL.

  There must be an attached thread state when calling this function.

Added in version 3.13.

## 高度なデバッガサポート (advanced debugger support)

以下の関数は高度なデバッグツールでの使用のためだけのものです。

PyInterpreterState *PyInterpreterState_Head()

  インタプリタ状態オブジェクトからなるリストのうち、先頭にあるものを返します。

PyInterpreterState *PyInterpreterState_Main()

  メインインタプリタの状態オブジェクトを返します。

PyInterpreterState *PyInterpreterState_Next(PyInterpreterState *interp)

  インタプリタ状態オブジェクトからなるリストのうち、interp の次にあるものを返します。

PyThreadState *PyInterpreterState_ThreadHead(PyInterpreterState *interp)

  インタプリタ interp に関連付けられているスレッドからなるリストのうち、先頭にある `PyThreadState` オブジェクトを返します。

PyThreadState *PyThreadState_Next(PyThreadState *tstate)

  tstate と同じ `PyInterpreterState` オブジェクトに属しているスレッド状態オブジェクトのうち、 tstate の次にあるものを返します。

## スレッドローカルストレージのサポート

Python インタプリタは、スレッドローカルストレージ (thread-local storage, TLS) の低レベルサポートを提供していて、ネイティブの TLS 実装を内部にラップして Python レベルのスレッドローカルストレージ API (`threading.local`) をサポートしています。 CPython の C レベル API は pthreads や Windows で与えられる TLS と同様です: スレッドキーとスレッドごとに void* 値を関係付ける関数を使います。

A thread state does not need to be attached when calling these functions; they suppl their own locking.

`Python.h` は TLS API の宣言を include せず、スレッドローカルストレージを使うには `pythread.h` を include する必要があることに注意してください。

注釈

この API 関数はどれも void* 値の代わりにメモリ管理を行うことはしません。 メモリの確保と解放は自前で行う必要があります。 void* 値がたまたま PyObject* だった場合は、 API 関数はそれぞれの値の参照カウントの操作は行いません。

### スレッド固有ストレージ (Thread Specific Storage, TSS) API

TSS API は、 CPython インタプリタに含まれている既存の TLS API を置き換えるために導入されました。 この API は、スレッドキーの表現に int の代わりに新しい型 `Py_tss_t` を使います。

Added in version 3.7.

参考

"CPython のスレッドローカルストレージのための新しい C API" (PEP 539)

type Py_tss_t

  このデータ構造体はスレッドキーの状態を表現しています。この構造体の定義は、根底の TLS 実装に依存し、キーの初期化状態を表現する内部フィールドを持ちます。 この構造体には公開 (public) のメンバはありません。

  Py_LIMITED_API が定義されていないときは、この型の `Py_tss_NEEDS_INIT` による静的メモリ確保ができます。

Py_tss_NEEDS_INIT

  このマクロは `Py_tss_t` 変数の初期化子に展開されます。 このマクロは Py_LIMITED_API があるときは定義されません。

#### 動的メモリ確保

動的な `Py_tss_t` のメモリ確保は Py_LIMITED_API でビルドされた拡張モジュールで必要になりますが、その実装がビルド時に不透明なために、この型の静的なメモリ確保は不可能です。

Py_tss_t *PyThread_tss_alloc()
  次に属します: Stable ABI (バージョン 3.7 より).

  Return a value which is the same state as a value initialized with `Py_tss_NEEDS_INIT`, or `NULL` in the case of dynamic allocation failure.

void PyThread_tss_free(Py_tss_t *key)
  次に属します: Stable ABI (バージョン 3.7 より).

  Free the given key allocated by `PyThread_tss_alloc()`, after first calling `PyThread_tss_delete()` to ensure any associated thread locals have been unassigned. This is a no-op if the key argument is `NULL`.

  注釈

  A freed key becomes a dangling pointer. You should reset the key to `NULL`.

#### メソッド

The parameter key of these functions must not be `NULL`. Moreover, the behaviors of `PyThread_tss_set()` and `PyThread_tss_get()` are undefined if the given `Py_tss_t` has not been initialized by `PyThread_tss_create()`.

int PyThread_tss_is_created(Py_tss_t *key)
  次に属します: Stable ABI (バージョン 3.7 より).

  Return a non-zero value if the given `Py_tss_t` has been initialized by `PyThread_tss_create()`.

int PyThread_tss_create(Py_tss_t *key)
  次に属します: Stable ABI (バージョン 3.7 より).

  Return a zero value on successful initialization of a TSS key. The behavior is undefined if the value pointed to by the key argument is not initialized by `Py_tss_NEEDS_INIT`. This function can be called repeatedly on the same key -- calling it on an already initialized key is a no-op and immediately returns success.

void PyThread_tss_delete(Py_tss_t *key)
  次に属します: Stable ABI (バージョン 3.7 より).

  Destroy a TSS key to forget the values associated with the key across all threads, and change the key's initialization state to uninitialized. A destroyed key is able to be initialized again by `PyThread_tss_create()`. This function can be called repeatedly on the same key -- calling it on an already destroyed key is a no-op.

int PyThread_tss_set(Py_tss_t *key, void *value)
  次に属します: Stable ABI (バージョン 3.7 より).

  Return a zero value to indicate successfully associating a void* value with a TSS key in the current thread. Each thread has a distinct mapping of the key to a void* value.

void *PyThread_tss_get(Py_tss_t *key)
  次に属します: Stable ABI (バージョン 3.7 より).

  Return the void* value associated with a TSS key in the current thread. This returns `NULL` if no value is associated with the key in the current thread.

### スレッドローカルストレージ (TLS) API

バージョン 3.7 で非推奨: This API is superseded by Thread Specific Storage (TSS) API.

注釈

This version of the API does not support platforms where the native TLS key is defined in a way that cannot be safely cast to `int`. On such platforms, `PyThread_create_key()` will return immediately with a failure status, and the other TLS functions will all be no-ops on such platforms.

前述の互換性の問題により、このバージョンのAPIは新規のコードで利用すべきではありません。

int PyThread_create_key()
  次に属します: Stable ABI.

void PyThread_delete_key(int key)
  次に属します: Stable ABI.

int PyThread_set_key_value(int key, void *value)
  次に属します: Stable ABI.

void *PyThread_get_key_value(int key)
  次に属します: Stable ABI.

void PyThread_delete_key_value(int key)
  次に属します: Stable ABI.

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

## 同期プリミティブ

The C-API provides a basic mutual exclusion lock.

type PyMutex

  A mutual exclusion lock. The `PyMutex` should be initialized to zero to represent the unlocked state. For example:

      PyMutex mutex = {0};

  Instances of `PyMutex` should not be copied or moved. Both the contents and address of a `PyMutex` are meaningful, and it must remain at a fixed, writable location in memory.

  注釈

  A `PyMutex` currently occupies one byte, but the size should be considered unstable. The size may change in future Python releases without a deprecation period.

  Added in version 3.13.

void PyMutex_Lock(PyMutex *m)

  Lock mutex m. If another thread has already locked it, the calling thread will block until the mutex is unlocked. While blocked, the thread will temporarily detach the thread state if one exists.

  Added in version 3.13.

void PyMutex_Unlock(PyMutex *m)

  Unlock mutex m. The mutex must be locked --- otherwise, the function will issue a fatal error.

  Added in version 3.13.

int PyMutex_IsLocked(PyMutex *m)

  Returns non-zero if the mutex m is currently locked, zero otherwise.

  注釈

  This function is intended for use in assertions and debugging only and should not be used to make concurrency control decisions, as the lock state may change immediately after the check.

  Added in version 3.14.

### Python Critical Section API

The critical section API provides a deadlock avoidance layer on top of per-object locks for free-threaded CPython. They are intended to replace reliance on the global interpreter lock, and are no-ops in versions of Python with the global interpreter lock.

Critical sections are intended to be used for custom types implemented in C-API extensions. They should generally not be used with built-in types like `list` and `dict` because their public C-APIs already use critical sections internally, with the notable exception of `PyDict_Next()`, which requires critical section to be acquired externally.

Critical sections avoid deadlocks by implicitly suspending active critical sections, hence, they do not provide exclusive access such as provided by traditional locks like `PyMutex`. When a critical section is started, the per-object lock for the object is acquired. If the code executed inside the critical section calls C-API functions then it can suspend the critical section thereby releasing the per-object lock, so other threads can acquire the per-object lock for the same object.

Variants that accept `PyMutex` pointers rather than Python objects are also available. Use these variants to start a critical section in a situation where there is no `PyObject` -- for example, when working with a C type that does not extend or wrap `PyObject` but still needs to call into the C API in a manner that might lead to deadlocks.

The functions and structs used by the macros are exposed for cases where C macros are not available. They should only be used as in the given macro expansions. Note that the sizes and contents of the structures may change in future Python versions.

注釈

Operations that need to lock two objects at once must use `Py_BEGIN_CRITICAL_SECTION2`. You cannot use nested critical sections to lock more than one object at once, because the inner critical section may suspend the outer critical sections. This API does not provide a way to lock more than two objects at once.

使用例:

    static PyObject *
    set_field(MyObject *self, PyObject *value)
    {
       Py_BEGIN_CRITICAL_SECTION(self);
       Py_SETREF(self->field, Py_XNewRef(value));
       Py_END_CRITICAL_SECTION();
       Py_RETURN_NONE;
    }

In the above example, `Py_SETREF` calls `Py_DECREF`, which can call arbitrary code through an object's deallocation function. The critical section API avoids potential deadlocks due to reentrancy and lock ordering by allowing the runtime to temporarily suspend the critical section if the code triggered by the finalizer blocks and calls `PyEval_SaveThread()`.

Py_BEGIN_CRITICAL_SECTION(op)

  Acquires the per-object lock for the object op and begins a critical section.

  In the free-threaded build, this macro expands to:

      {
          PyCriticalSection _py_cs;
          PyCriticalSection_Begin(&_py_cs, (PyObject*)(op))

  In the default build, this macro expands to `{`.

  Added in version 3.13.

Py_BEGIN_CRITICAL_SECTION_MUTEX(m)

  Locks the mutex m and begins a critical section.

  In the free-threaded build, this macro expands to:

      {
           PyCriticalSection _py_cs;
           PyCriticalSection_BeginMutex(&_py_cs, m)

  Note that unlike `Py_BEGIN_CRITICAL_SECTION`, there is no cast for the argument of the macro - it must be a `PyMutex` pointer.

  On the default build, this macro expands to `{`.

  Added in version 3.14.

Py_END_CRITICAL_SECTION()

  Ends the critical section and releases the per-object lock.

  In the free-threaded build, this macro expands to:

          PyCriticalSection_End(&_py_cs);
      }

  In the default build, this macro expands to `}`.

  Added in version 3.13.

Py_BEGIN_CRITICAL_SECTION2(a, b)

  Acquires the per-objects locks for the objects a and b and begins a critical section. The locks are acquired in a consistent order (lowest address first) to avoid lock ordering deadlocks.

  In the free-threaded build, this macro expands to:

      {
          PyCriticalSection2 _py_cs2;
          PyCriticalSection2_Begin(&_py_cs2, (PyObject*)(a), (PyObject*)(b))

  In the default build, this macro expands to `{`.

  Added in version 3.13.

Py_BEGIN_CRITICAL_SECTION2_MUTEX(m1, m2)

  Locks the mutexes m1 and m2 and begins a critical section.

  In the free-threaded build, this macro expands to:

      {
           PyCriticalSection2 _py_cs2;
           PyCriticalSection2_BeginMutex(&_py_cs2, m1, m2)

  Note that unlike `Py_BEGIN_CRITICAL_SECTION2`, there is no cast for the arguments of the macro - they must be `PyMutex` pointers.

  On the default build, this macro expands to `{`.

  Added in version 3.14.

Py_END_CRITICAL_SECTION2()

  Ends the critical section and releases the per-object locks.

  In the free-threaded build, this macro expands to:

          PyCriticalSection2_End(&_py_cs2);
      }

  In the default build, this macro expands to `}`.

  Added in version 3.13.