17db96d56Sopenharmony_ci.. highlight:: c 27db96d56Sopenharmony_ci 37db96d56Sopenharmony_ci 47db96d56Sopenharmony_ci.. _initialization: 57db96d56Sopenharmony_ci 67db96d56Sopenharmony_ci***************************************** 77db96d56Sopenharmony_ciInitialization, Finalization, and Threads 87db96d56Sopenharmony_ci***************************************** 97db96d56Sopenharmony_ci 107db96d56Sopenharmony_ciSee also :ref:`Python Initialization Configuration <init-config>`. 117db96d56Sopenharmony_ci 127db96d56Sopenharmony_ci.. _pre-init-safe: 137db96d56Sopenharmony_ci 147db96d56Sopenharmony_ciBefore Python Initialization 157db96d56Sopenharmony_ci============================ 167db96d56Sopenharmony_ci 177db96d56Sopenharmony_ciIn an application embedding Python, the :c:func:`Py_Initialize` function must 187db96d56Sopenharmony_cibe called before using any other Python/C API functions; with the exception of 197db96d56Sopenharmony_cia few functions and the :ref:`global configuration variables 207db96d56Sopenharmony_ci<global-conf-vars>`. 217db96d56Sopenharmony_ci 227db96d56Sopenharmony_ciThe following functions can be safely called before Python is initialized: 237db96d56Sopenharmony_ci 247db96d56Sopenharmony_ci* Configuration functions: 257db96d56Sopenharmony_ci 267db96d56Sopenharmony_ci * :c:func:`PyImport_AppendInittab` 277db96d56Sopenharmony_ci * :c:func:`PyImport_ExtendInittab` 287db96d56Sopenharmony_ci * :c:func:`PyInitFrozenExtensions` 297db96d56Sopenharmony_ci * :c:func:`PyMem_SetAllocator` 307db96d56Sopenharmony_ci * :c:func:`PyMem_SetupDebugHooks` 317db96d56Sopenharmony_ci * :c:func:`PyObject_SetArenaAllocator` 327db96d56Sopenharmony_ci * :c:func:`Py_SetPath` 337db96d56Sopenharmony_ci * :c:func:`Py_SetProgramName` 347db96d56Sopenharmony_ci * :c:func:`Py_SetPythonHome` 357db96d56Sopenharmony_ci * :c:func:`Py_SetStandardStreamEncoding` 367db96d56Sopenharmony_ci * :c:func:`PySys_AddWarnOption` 377db96d56Sopenharmony_ci * :c:func:`PySys_AddXOption` 387db96d56Sopenharmony_ci * :c:func:`PySys_ResetWarnOptions` 397db96d56Sopenharmony_ci 407db96d56Sopenharmony_ci* Informative functions: 417db96d56Sopenharmony_ci 427db96d56Sopenharmony_ci * :c:func:`Py_IsInitialized` 437db96d56Sopenharmony_ci * :c:func:`PyMem_GetAllocator` 447db96d56Sopenharmony_ci * :c:func:`PyObject_GetArenaAllocator` 457db96d56Sopenharmony_ci * :c:func:`Py_GetBuildInfo` 467db96d56Sopenharmony_ci * :c:func:`Py_GetCompiler` 477db96d56Sopenharmony_ci * :c:func:`Py_GetCopyright` 487db96d56Sopenharmony_ci * :c:func:`Py_GetPlatform` 497db96d56Sopenharmony_ci * :c:func:`Py_GetVersion` 507db96d56Sopenharmony_ci 517db96d56Sopenharmony_ci* Utilities: 527db96d56Sopenharmony_ci 537db96d56Sopenharmony_ci * :c:func:`Py_DecodeLocale` 547db96d56Sopenharmony_ci 557db96d56Sopenharmony_ci* Memory allocators: 567db96d56Sopenharmony_ci 577db96d56Sopenharmony_ci * :c:func:`PyMem_RawMalloc` 587db96d56Sopenharmony_ci * :c:func:`PyMem_RawRealloc` 597db96d56Sopenharmony_ci * :c:func:`PyMem_RawCalloc` 607db96d56Sopenharmony_ci * :c:func:`PyMem_RawFree` 617db96d56Sopenharmony_ci 627db96d56Sopenharmony_ci.. note:: 637db96d56Sopenharmony_ci 647db96d56Sopenharmony_ci The following functions **should not be called** before 657db96d56Sopenharmony_ci :c:func:`Py_Initialize`: :c:func:`Py_EncodeLocale`, :c:func:`Py_GetPath`, 667db96d56Sopenharmony_ci :c:func:`Py_GetPrefix`, :c:func:`Py_GetExecPrefix`, 677db96d56Sopenharmony_ci :c:func:`Py_GetProgramFullPath`, :c:func:`Py_GetPythonHome`, 687db96d56Sopenharmony_ci :c:func:`Py_GetProgramName` and :c:func:`PyEval_InitThreads`. 697db96d56Sopenharmony_ci 707db96d56Sopenharmony_ci 717db96d56Sopenharmony_ci.. _global-conf-vars: 727db96d56Sopenharmony_ci 737db96d56Sopenharmony_ciGlobal configuration variables 747db96d56Sopenharmony_ci============================== 757db96d56Sopenharmony_ci 767db96d56Sopenharmony_ciPython has variables for the global configuration to control different features 777db96d56Sopenharmony_ciand options. By default, these flags are controlled by :ref:`command line 787db96d56Sopenharmony_cioptions <using-on-interface-options>`. 797db96d56Sopenharmony_ci 807db96d56Sopenharmony_ciWhen a flag is set by an option, the value of the flag is the number of times 817db96d56Sopenharmony_cithat the option was set. For example, ``-b`` sets :c:data:`Py_BytesWarningFlag` 827db96d56Sopenharmony_cito 1 and ``-bb`` sets :c:data:`Py_BytesWarningFlag` to 2. 837db96d56Sopenharmony_ci 847db96d56Sopenharmony_ci.. c:var:: int Py_BytesWarningFlag 857db96d56Sopenharmony_ci 867db96d56Sopenharmony_ci Issue a warning when comparing :class:`bytes` or :class:`bytearray` with 877db96d56Sopenharmony_ci :class:`str` or :class:`bytes` with :class:`int`. Issue an error if greater 887db96d56Sopenharmony_ci or equal to ``2``. 897db96d56Sopenharmony_ci 907db96d56Sopenharmony_ci Set by the :option:`-b` option. 917db96d56Sopenharmony_ci 927db96d56Sopenharmony_ci.. c:var:: int Py_DebugFlag 937db96d56Sopenharmony_ci 947db96d56Sopenharmony_ci Turn on parser debugging output (for expert only, depending on compilation 957db96d56Sopenharmony_ci options). 967db96d56Sopenharmony_ci 977db96d56Sopenharmony_ci Set by the :option:`-d` option and the :envvar:`PYTHONDEBUG` environment 987db96d56Sopenharmony_ci variable. 997db96d56Sopenharmony_ci 1007db96d56Sopenharmony_ci.. c:var:: int Py_DontWriteBytecodeFlag 1017db96d56Sopenharmony_ci 1027db96d56Sopenharmony_ci If set to non-zero, Python won't try to write ``.pyc`` files on the 1037db96d56Sopenharmony_ci import of source modules. 1047db96d56Sopenharmony_ci 1057db96d56Sopenharmony_ci Set by the :option:`-B` option and the :envvar:`PYTHONDONTWRITEBYTECODE` 1067db96d56Sopenharmony_ci environment variable. 1077db96d56Sopenharmony_ci 1087db96d56Sopenharmony_ci.. c:var:: int Py_FrozenFlag 1097db96d56Sopenharmony_ci 1107db96d56Sopenharmony_ci Suppress error messages when calculating the module search path in 1117db96d56Sopenharmony_ci :c:func:`Py_GetPath`. 1127db96d56Sopenharmony_ci 1137db96d56Sopenharmony_ci Private flag used by ``_freeze_module`` and ``frozenmain`` programs. 1147db96d56Sopenharmony_ci 1157db96d56Sopenharmony_ci.. c:var:: int Py_HashRandomizationFlag 1167db96d56Sopenharmony_ci 1177db96d56Sopenharmony_ci Set to ``1`` if the :envvar:`PYTHONHASHSEED` environment variable is set to 1187db96d56Sopenharmony_ci a non-empty string. 1197db96d56Sopenharmony_ci 1207db96d56Sopenharmony_ci If the flag is non-zero, read the :envvar:`PYTHONHASHSEED` environment 1217db96d56Sopenharmony_ci variable to initialize the secret hash seed. 1227db96d56Sopenharmony_ci 1237db96d56Sopenharmony_ci.. c:var:: int Py_IgnoreEnvironmentFlag 1247db96d56Sopenharmony_ci 1257db96d56Sopenharmony_ci Ignore all :envvar:`PYTHON*` environment variables, e.g. 1267db96d56Sopenharmony_ci :envvar:`PYTHONPATH` and :envvar:`PYTHONHOME`, that might be set. 1277db96d56Sopenharmony_ci 1287db96d56Sopenharmony_ci Set by the :option:`-E` and :option:`-I` options. 1297db96d56Sopenharmony_ci 1307db96d56Sopenharmony_ci.. c:var:: int Py_InspectFlag 1317db96d56Sopenharmony_ci 1327db96d56Sopenharmony_ci When a script is passed as first argument or the :option:`-c` option is used, 1337db96d56Sopenharmony_ci enter interactive mode after executing the script or the command, even when 1347db96d56Sopenharmony_ci :data:`sys.stdin` does not appear to be a terminal. 1357db96d56Sopenharmony_ci 1367db96d56Sopenharmony_ci Set by the :option:`-i` option and the :envvar:`PYTHONINSPECT` environment 1377db96d56Sopenharmony_ci variable. 1387db96d56Sopenharmony_ci 1397db96d56Sopenharmony_ci.. c:var:: int Py_InteractiveFlag 1407db96d56Sopenharmony_ci 1417db96d56Sopenharmony_ci Set by the :option:`-i` option. 1427db96d56Sopenharmony_ci 1437db96d56Sopenharmony_ci.. c:var:: int Py_IsolatedFlag 1447db96d56Sopenharmony_ci 1457db96d56Sopenharmony_ci Run Python in isolated mode. In isolated mode :data:`sys.path` contains 1467db96d56Sopenharmony_ci neither the script's directory nor the user's site-packages directory. 1477db96d56Sopenharmony_ci 1487db96d56Sopenharmony_ci Set by the :option:`-I` option. 1497db96d56Sopenharmony_ci 1507db96d56Sopenharmony_ci .. versionadded:: 3.4 1517db96d56Sopenharmony_ci 1527db96d56Sopenharmony_ci.. c:var:: int Py_LegacyWindowsFSEncodingFlag 1537db96d56Sopenharmony_ci 1547db96d56Sopenharmony_ci If the flag is non-zero, use the ``mbcs`` encoding with ``replace`` error 1557db96d56Sopenharmony_ci handler, instead of the UTF-8 encoding with ``surrogatepass`` error handler, 1567db96d56Sopenharmony_ci for the :term:`filesystem encoding and error handler`. 1577db96d56Sopenharmony_ci 1587db96d56Sopenharmony_ci Set to ``1`` if the :envvar:`PYTHONLEGACYWINDOWSFSENCODING` environment 1597db96d56Sopenharmony_ci variable is set to a non-empty string. 1607db96d56Sopenharmony_ci 1617db96d56Sopenharmony_ci See :pep:`529` for more details. 1627db96d56Sopenharmony_ci 1637db96d56Sopenharmony_ci .. availability:: Windows. 1647db96d56Sopenharmony_ci 1657db96d56Sopenharmony_ci.. c:var:: int Py_LegacyWindowsStdioFlag 1667db96d56Sopenharmony_ci 1677db96d56Sopenharmony_ci If the flag is non-zero, use :class:`io.FileIO` instead of 1687db96d56Sopenharmony_ci :class:`WindowsConsoleIO` for :mod:`sys` standard streams. 1697db96d56Sopenharmony_ci 1707db96d56Sopenharmony_ci Set to ``1`` if the :envvar:`PYTHONLEGACYWINDOWSSTDIO` environment 1717db96d56Sopenharmony_ci variable is set to a non-empty string. 1727db96d56Sopenharmony_ci 1737db96d56Sopenharmony_ci See :pep:`528` for more details. 1747db96d56Sopenharmony_ci 1757db96d56Sopenharmony_ci .. availability:: Windows. 1767db96d56Sopenharmony_ci 1777db96d56Sopenharmony_ci.. c:var:: int Py_NoSiteFlag 1787db96d56Sopenharmony_ci 1797db96d56Sopenharmony_ci Disable the import of the module :mod:`site` and the site-dependent 1807db96d56Sopenharmony_ci manipulations of :data:`sys.path` that it entails. Also disable these 1817db96d56Sopenharmony_ci manipulations if :mod:`site` is explicitly imported later (call 1827db96d56Sopenharmony_ci :func:`site.main` if you want them to be triggered). 1837db96d56Sopenharmony_ci 1847db96d56Sopenharmony_ci Set by the :option:`-S` option. 1857db96d56Sopenharmony_ci 1867db96d56Sopenharmony_ci.. c:var:: int Py_NoUserSiteDirectory 1877db96d56Sopenharmony_ci 1887db96d56Sopenharmony_ci Don't add the :data:`user site-packages directory <site.USER_SITE>` to 1897db96d56Sopenharmony_ci :data:`sys.path`. 1907db96d56Sopenharmony_ci 1917db96d56Sopenharmony_ci Set by the :option:`-s` and :option:`-I` options, and the 1927db96d56Sopenharmony_ci :envvar:`PYTHONNOUSERSITE` environment variable. 1937db96d56Sopenharmony_ci 1947db96d56Sopenharmony_ci.. c:var:: int Py_OptimizeFlag 1957db96d56Sopenharmony_ci 1967db96d56Sopenharmony_ci Set by the :option:`-O` option and the :envvar:`PYTHONOPTIMIZE` environment 1977db96d56Sopenharmony_ci variable. 1987db96d56Sopenharmony_ci 1997db96d56Sopenharmony_ci.. c:var:: int Py_QuietFlag 2007db96d56Sopenharmony_ci 2017db96d56Sopenharmony_ci Don't display the copyright and version messages even in interactive mode. 2027db96d56Sopenharmony_ci 2037db96d56Sopenharmony_ci Set by the :option:`-q` option. 2047db96d56Sopenharmony_ci 2057db96d56Sopenharmony_ci .. versionadded:: 3.2 2067db96d56Sopenharmony_ci 2077db96d56Sopenharmony_ci.. c:var:: int Py_UnbufferedStdioFlag 2087db96d56Sopenharmony_ci 2097db96d56Sopenharmony_ci Force the stdout and stderr streams to be unbuffered. 2107db96d56Sopenharmony_ci 2117db96d56Sopenharmony_ci Set by the :option:`-u` option and the :envvar:`PYTHONUNBUFFERED` 2127db96d56Sopenharmony_ci environment variable. 2137db96d56Sopenharmony_ci 2147db96d56Sopenharmony_ci.. c:var:: int Py_VerboseFlag 2157db96d56Sopenharmony_ci 2167db96d56Sopenharmony_ci Print a message each time a module is initialized, showing the place 2177db96d56Sopenharmony_ci (filename or built-in module) from which it is loaded. If greater or equal 2187db96d56Sopenharmony_ci to ``2``, print a message for each file that is checked for when 2197db96d56Sopenharmony_ci searching for a module. Also provides information on module cleanup at exit. 2207db96d56Sopenharmony_ci 2217db96d56Sopenharmony_ci Set by the :option:`-v` option and the :envvar:`PYTHONVERBOSE` environment 2227db96d56Sopenharmony_ci variable. 2237db96d56Sopenharmony_ci 2247db96d56Sopenharmony_ci 2257db96d56Sopenharmony_ciInitializing and finalizing the interpreter 2267db96d56Sopenharmony_ci=========================================== 2277db96d56Sopenharmony_ci 2287db96d56Sopenharmony_ci 2297db96d56Sopenharmony_ci.. c:function:: void Py_Initialize() 2307db96d56Sopenharmony_ci 2317db96d56Sopenharmony_ci .. index:: 2327db96d56Sopenharmony_ci single: Py_SetProgramName() 2337db96d56Sopenharmony_ci single: PyEval_InitThreads() 2347db96d56Sopenharmony_ci single: modules (in module sys) 2357db96d56Sopenharmony_ci single: path (in module sys) 2367db96d56Sopenharmony_ci pair: module; builtins 2377db96d56Sopenharmony_ci pair: module; __main__ 2387db96d56Sopenharmony_ci pair: module; sys 2397db96d56Sopenharmony_ci triple: module; search; path 2407db96d56Sopenharmony_ci single: PySys_SetArgv() 2417db96d56Sopenharmony_ci single: PySys_SetArgvEx() 2427db96d56Sopenharmony_ci single: Py_FinalizeEx() 2437db96d56Sopenharmony_ci 2447db96d56Sopenharmony_ci Initialize the Python interpreter. In an application embedding Python, 2457db96d56Sopenharmony_ci this should be called before using any other Python/C API functions; see 2467db96d56Sopenharmony_ci :ref:`Before Python Initialization <pre-init-safe>` for the few exceptions. 2477db96d56Sopenharmony_ci 2487db96d56Sopenharmony_ci This initializes 2497db96d56Sopenharmony_ci the table of loaded modules (``sys.modules``), and creates the fundamental 2507db96d56Sopenharmony_ci modules :mod:`builtins`, :mod:`__main__` and :mod:`sys`. It also initializes 2517db96d56Sopenharmony_ci the module search path (``sys.path``). It does not set ``sys.argv``; use 2527db96d56Sopenharmony_ci :c:func:`PySys_SetArgvEx` for that. This is a no-op when called for a second time 2537db96d56Sopenharmony_ci (without calling :c:func:`Py_FinalizeEx` first). There is no return value; it is a 2547db96d56Sopenharmony_ci fatal error if the initialization fails. 2557db96d56Sopenharmony_ci 2567db96d56Sopenharmony_ci .. note:: 2577db96d56Sopenharmony_ci On Windows, changes the console mode from ``O_TEXT`` to ``O_BINARY``, which will 2587db96d56Sopenharmony_ci also affect non-Python uses of the console using the C Runtime. 2597db96d56Sopenharmony_ci 2607db96d56Sopenharmony_ci 2617db96d56Sopenharmony_ci.. c:function:: void Py_InitializeEx(int initsigs) 2627db96d56Sopenharmony_ci 2637db96d56Sopenharmony_ci This function works like :c:func:`Py_Initialize` if *initsigs* is ``1``. If 2647db96d56Sopenharmony_ci *initsigs* is ``0``, it skips initialization registration of signal handlers, which 2657db96d56Sopenharmony_ci might be useful when Python is embedded. 2667db96d56Sopenharmony_ci 2677db96d56Sopenharmony_ci 2687db96d56Sopenharmony_ci.. c:function:: int Py_IsInitialized() 2697db96d56Sopenharmony_ci 2707db96d56Sopenharmony_ci Return true (nonzero) when the Python interpreter has been initialized, false 2717db96d56Sopenharmony_ci (zero) if not. After :c:func:`Py_FinalizeEx` is called, this returns false until 2727db96d56Sopenharmony_ci :c:func:`Py_Initialize` is called again. 2737db96d56Sopenharmony_ci 2747db96d56Sopenharmony_ci 2757db96d56Sopenharmony_ci.. c:function:: int Py_FinalizeEx() 2767db96d56Sopenharmony_ci 2777db96d56Sopenharmony_ci Undo all initializations made by :c:func:`Py_Initialize` and subsequent use of 2787db96d56Sopenharmony_ci Python/C API functions, and destroy all sub-interpreters (see 2797db96d56Sopenharmony_ci :c:func:`Py_NewInterpreter` below) that were created and not yet destroyed since 2807db96d56Sopenharmony_ci the last call to :c:func:`Py_Initialize`. Ideally, this frees all memory 2817db96d56Sopenharmony_ci allocated by the Python interpreter. This is a no-op when called for a second 2827db96d56Sopenharmony_ci time (without calling :c:func:`Py_Initialize` again first). Normally the 2837db96d56Sopenharmony_ci return value is ``0``. If there were errors during finalization 2847db96d56Sopenharmony_ci (flushing buffered data), ``-1`` is returned. 2857db96d56Sopenharmony_ci 2867db96d56Sopenharmony_ci This function is provided for a number of reasons. An embedding application 2877db96d56Sopenharmony_ci might want to restart Python without having to restart the application itself. 2887db96d56Sopenharmony_ci An application that has loaded the Python interpreter from a dynamically 2897db96d56Sopenharmony_ci loadable library (or DLL) might want to free all memory allocated by Python 2907db96d56Sopenharmony_ci before unloading the DLL. During a hunt for memory leaks in an application a 2917db96d56Sopenharmony_ci developer might want to free all memory allocated by Python before exiting from 2927db96d56Sopenharmony_ci the application. 2937db96d56Sopenharmony_ci 2947db96d56Sopenharmony_ci **Bugs and caveats:** The destruction of modules and objects in modules is done 2957db96d56Sopenharmony_ci in random order; this may cause destructors (:meth:`__del__` methods) to fail 2967db96d56Sopenharmony_ci when they depend on other objects (even functions) or modules. Dynamically 2977db96d56Sopenharmony_ci loaded extension modules loaded by Python are not unloaded. Small amounts of 2987db96d56Sopenharmony_ci memory allocated by the Python interpreter may not be freed (if you find a leak, 2997db96d56Sopenharmony_ci please report it). Memory tied up in circular references between objects is not 3007db96d56Sopenharmony_ci freed. Some memory allocated by extension modules may not be freed. Some 3017db96d56Sopenharmony_ci extensions may not work properly if their initialization routine is called more 3027db96d56Sopenharmony_ci than once; this can happen if an application calls :c:func:`Py_Initialize` and 3037db96d56Sopenharmony_ci :c:func:`Py_FinalizeEx` more than once. 3047db96d56Sopenharmony_ci 3057db96d56Sopenharmony_ci .. audit-event:: cpython._PySys_ClearAuditHooks "" c.Py_FinalizeEx 3067db96d56Sopenharmony_ci 3077db96d56Sopenharmony_ci .. versionadded:: 3.6 3087db96d56Sopenharmony_ci 3097db96d56Sopenharmony_ci.. c:function:: void Py_Finalize() 3107db96d56Sopenharmony_ci 3117db96d56Sopenharmony_ci This is a backwards-compatible version of :c:func:`Py_FinalizeEx` that 3127db96d56Sopenharmony_ci disregards the return value. 3137db96d56Sopenharmony_ci 3147db96d56Sopenharmony_ci 3157db96d56Sopenharmony_ciProcess-wide parameters 3167db96d56Sopenharmony_ci======================= 3177db96d56Sopenharmony_ci 3187db96d56Sopenharmony_ci 3197db96d56Sopenharmony_ci.. c:function:: int Py_SetStandardStreamEncoding(const char *encoding, const char *errors) 3207db96d56Sopenharmony_ci 3217db96d56Sopenharmony_ci .. index:: 3227db96d56Sopenharmony_ci single: Py_Initialize() 3237db96d56Sopenharmony_ci single: main() 3247db96d56Sopenharmony_ci triple: stdin; stdout; sdterr 3257db96d56Sopenharmony_ci 3267db96d56Sopenharmony_ci This API is kept for backward compatibility: setting 3277db96d56Sopenharmony_ci :c:member:`PyConfig.stdio_encoding` and :c:member:`PyConfig.stdio_errors` 3287db96d56Sopenharmony_ci should be used instead, see :ref:`Python Initialization Configuration 3297db96d56Sopenharmony_ci <init-config>`. 3307db96d56Sopenharmony_ci 3317db96d56Sopenharmony_ci This function should be called before :c:func:`Py_Initialize`, if it is 3327db96d56Sopenharmony_ci called at all. It specifies which encoding and error handling to use 3337db96d56Sopenharmony_ci with standard IO, with the same meanings as in :func:`str.encode`. 3347db96d56Sopenharmony_ci 3357db96d56Sopenharmony_ci It overrides :envvar:`PYTHONIOENCODING` values, and allows embedding code 3367db96d56Sopenharmony_ci to control IO encoding when the environment variable does not work. 3377db96d56Sopenharmony_ci 3387db96d56Sopenharmony_ci *encoding* and/or *errors* may be ``NULL`` to use 3397db96d56Sopenharmony_ci :envvar:`PYTHONIOENCODING` and/or default values (depending on other 3407db96d56Sopenharmony_ci settings). 3417db96d56Sopenharmony_ci 3427db96d56Sopenharmony_ci Note that :data:`sys.stderr` always uses the "backslashreplace" error 3437db96d56Sopenharmony_ci handler, regardless of this (or any other) setting. 3447db96d56Sopenharmony_ci 3457db96d56Sopenharmony_ci If :c:func:`Py_FinalizeEx` is called, this function will need to be called 3467db96d56Sopenharmony_ci again in order to affect subsequent calls to :c:func:`Py_Initialize`. 3477db96d56Sopenharmony_ci 3487db96d56Sopenharmony_ci Returns ``0`` if successful, a nonzero value on error (e.g. calling after the 3497db96d56Sopenharmony_ci interpreter has already been initialized). 3507db96d56Sopenharmony_ci 3517db96d56Sopenharmony_ci .. versionadded:: 3.4 3527db96d56Sopenharmony_ci 3537db96d56Sopenharmony_ci .. deprecated:: 3.11 3547db96d56Sopenharmony_ci 3557db96d56Sopenharmony_ci 3567db96d56Sopenharmony_ci.. c:function:: void Py_SetProgramName(const wchar_t *name) 3577db96d56Sopenharmony_ci 3587db96d56Sopenharmony_ci .. index:: 3597db96d56Sopenharmony_ci single: Py_Initialize() 3607db96d56Sopenharmony_ci single: main() 3617db96d56Sopenharmony_ci single: Py_GetPath() 3627db96d56Sopenharmony_ci 3637db96d56Sopenharmony_ci This API is kept for backward compatibility: setting 3647db96d56Sopenharmony_ci :c:member:`PyConfig.program_name` should be used instead, see :ref:`Python 3657db96d56Sopenharmony_ci Initialization Configuration <init-config>`. 3667db96d56Sopenharmony_ci 3677db96d56Sopenharmony_ci This function should be called before :c:func:`Py_Initialize` is called for 3687db96d56Sopenharmony_ci the first time, if it is called at all. It tells the interpreter the value 3697db96d56Sopenharmony_ci of the ``argv[0]`` argument to the :c:func:`main` function of the program 3707db96d56Sopenharmony_ci (converted to wide characters). 3717db96d56Sopenharmony_ci This is used by :c:func:`Py_GetPath` and some other functions below to find 3727db96d56Sopenharmony_ci the Python run-time libraries relative to the interpreter executable. The 3737db96d56Sopenharmony_ci default value is ``'python'``. The argument should point to a 3747db96d56Sopenharmony_ci zero-terminated wide character string in static storage whose contents will not 3757db96d56Sopenharmony_ci change for the duration of the program's execution. No code in the Python 3767db96d56Sopenharmony_ci interpreter will change the contents of this storage. 3777db96d56Sopenharmony_ci 3787db96d56Sopenharmony_ci Use :c:func:`Py_DecodeLocale` to decode a bytes string to get a 3797db96d56Sopenharmony_ci :c:expr:`wchar_*` string. 3807db96d56Sopenharmony_ci 3817db96d56Sopenharmony_ci .. deprecated:: 3.11 3827db96d56Sopenharmony_ci 3837db96d56Sopenharmony_ci 3847db96d56Sopenharmony_ci.. c:function:: wchar* Py_GetProgramName() 3857db96d56Sopenharmony_ci 3867db96d56Sopenharmony_ci .. index:: single: Py_SetProgramName() 3877db96d56Sopenharmony_ci 3887db96d56Sopenharmony_ci Return the program name set with :c:func:`Py_SetProgramName`, or the default. 3897db96d56Sopenharmony_ci The returned string points into static storage; the caller should not modify its 3907db96d56Sopenharmony_ci value. 3917db96d56Sopenharmony_ci 3927db96d56Sopenharmony_ci This function should not be called before :c:func:`Py_Initialize`, otherwise 3937db96d56Sopenharmony_ci it returns ``NULL``. 3947db96d56Sopenharmony_ci 3957db96d56Sopenharmony_ci .. versionchanged:: 3.10 3967db96d56Sopenharmony_ci It now returns ``NULL`` if called before :c:func:`Py_Initialize`. 3977db96d56Sopenharmony_ci 3987db96d56Sopenharmony_ci 3997db96d56Sopenharmony_ci.. c:function:: wchar_t* Py_GetPrefix() 4007db96d56Sopenharmony_ci 4017db96d56Sopenharmony_ci Return the *prefix* for installed platform-independent files. This is derived 4027db96d56Sopenharmony_ci through a number of complicated rules from the program name set with 4037db96d56Sopenharmony_ci :c:func:`Py_SetProgramName` and some environment variables; for example, if the 4047db96d56Sopenharmony_ci program name is ``'/usr/local/bin/python'``, the prefix is ``'/usr/local'``. The 4057db96d56Sopenharmony_ci returned string points into static storage; the caller should not modify its 4067db96d56Sopenharmony_ci value. This corresponds to the :makevar:`prefix` variable in the top-level 4077db96d56Sopenharmony_ci :file:`Makefile` and the :option:`--prefix` argument to the :program:`configure` 4087db96d56Sopenharmony_ci script at build time. The value is available to Python code as ``sys.prefix``. 4097db96d56Sopenharmony_ci It is only useful on Unix. See also the next function. 4107db96d56Sopenharmony_ci 4117db96d56Sopenharmony_ci This function should not be called before :c:func:`Py_Initialize`, otherwise 4127db96d56Sopenharmony_ci it returns ``NULL``. 4137db96d56Sopenharmony_ci 4147db96d56Sopenharmony_ci .. versionchanged:: 3.10 4157db96d56Sopenharmony_ci It now returns ``NULL`` if called before :c:func:`Py_Initialize`. 4167db96d56Sopenharmony_ci 4177db96d56Sopenharmony_ci 4187db96d56Sopenharmony_ci.. c:function:: wchar_t* Py_GetExecPrefix() 4197db96d56Sopenharmony_ci 4207db96d56Sopenharmony_ci Return the *exec-prefix* for installed platform-*dependent* files. This is 4217db96d56Sopenharmony_ci derived through a number of complicated rules from the program name set with 4227db96d56Sopenharmony_ci :c:func:`Py_SetProgramName` and some environment variables; for example, if the 4237db96d56Sopenharmony_ci program name is ``'/usr/local/bin/python'``, the exec-prefix is 4247db96d56Sopenharmony_ci ``'/usr/local'``. The returned string points into static storage; the caller 4257db96d56Sopenharmony_ci should not modify its value. This corresponds to the :makevar:`exec_prefix` 4267db96d56Sopenharmony_ci variable in the top-level :file:`Makefile` and the ``--exec-prefix`` 4277db96d56Sopenharmony_ci argument to the :program:`configure` script at build time. The value is 4287db96d56Sopenharmony_ci available to Python code as ``sys.exec_prefix``. It is only useful on Unix. 4297db96d56Sopenharmony_ci 4307db96d56Sopenharmony_ci Background: The exec-prefix differs from the prefix when platform dependent 4317db96d56Sopenharmony_ci files (such as executables and shared libraries) are installed in a different 4327db96d56Sopenharmony_ci directory tree. In a typical installation, platform dependent files may be 4337db96d56Sopenharmony_ci installed in the :file:`/usr/local/plat` subtree while platform independent may 4347db96d56Sopenharmony_ci be installed in :file:`/usr/local`. 4357db96d56Sopenharmony_ci 4367db96d56Sopenharmony_ci Generally speaking, a platform is a combination of hardware and software 4377db96d56Sopenharmony_ci families, e.g. Sparc machines running the Solaris 2.x operating system are 4387db96d56Sopenharmony_ci considered the same platform, but Intel machines running Solaris 2.x are another 4397db96d56Sopenharmony_ci platform, and Intel machines running Linux are yet another platform. Different 4407db96d56Sopenharmony_ci major revisions of the same operating system generally also form different 4417db96d56Sopenharmony_ci platforms. Non-Unix operating systems are a different story; the installation 4427db96d56Sopenharmony_ci strategies on those systems are so different that the prefix and exec-prefix are 4437db96d56Sopenharmony_ci meaningless, and set to the empty string. Note that compiled Python bytecode 4447db96d56Sopenharmony_ci files are platform independent (but not independent from the Python version by 4457db96d56Sopenharmony_ci which they were compiled!). 4467db96d56Sopenharmony_ci 4477db96d56Sopenharmony_ci System administrators will know how to configure the :program:`mount` or 4487db96d56Sopenharmony_ci :program:`automount` programs to share :file:`/usr/local` between platforms 4497db96d56Sopenharmony_ci while having :file:`/usr/local/plat` be a different filesystem for each 4507db96d56Sopenharmony_ci platform. 4517db96d56Sopenharmony_ci 4527db96d56Sopenharmony_ci This function should not be called before :c:func:`Py_Initialize`, otherwise 4537db96d56Sopenharmony_ci it returns ``NULL``. 4547db96d56Sopenharmony_ci 4557db96d56Sopenharmony_ci .. versionchanged:: 3.10 4567db96d56Sopenharmony_ci It now returns ``NULL`` if called before :c:func:`Py_Initialize`. 4577db96d56Sopenharmony_ci 4587db96d56Sopenharmony_ci 4597db96d56Sopenharmony_ci.. c:function:: wchar_t* Py_GetProgramFullPath() 4607db96d56Sopenharmony_ci 4617db96d56Sopenharmony_ci .. index:: 4627db96d56Sopenharmony_ci single: Py_SetProgramName() 4637db96d56Sopenharmony_ci single: executable (in module sys) 4647db96d56Sopenharmony_ci 4657db96d56Sopenharmony_ci Return the full program name of the Python executable; this is computed as a 4667db96d56Sopenharmony_ci side-effect of deriving the default module search path from the program name 4677db96d56Sopenharmony_ci (set by :c:func:`Py_SetProgramName` above). The returned string points into 4687db96d56Sopenharmony_ci static storage; the caller should not modify its value. The value is available 4697db96d56Sopenharmony_ci to Python code as ``sys.executable``. 4707db96d56Sopenharmony_ci 4717db96d56Sopenharmony_ci This function should not be called before :c:func:`Py_Initialize`, otherwise 4727db96d56Sopenharmony_ci it returns ``NULL``. 4737db96d56Sopenharmony_ci 4747db96d56Sopenharmony_ci .. versionchanged:: 3.10 4757db96d56Sopenharmony_ci It now returns ``NULL`` if called before :c:func:`Py_Initialize`. 4767db96d56Sopenharmony_ci 4777db96d56Sopenharmony_ci 4787db96d56Sopenharmony_ci.. c:function:: wchar_t* Py_GetPath() 4797db96d56Sopenharmony_ci 4807db96d56Sopenharmony_ci .. index:: 4817db96d56Sopenharmony_ci triple: module; search; path 4827db96d56Sopenharmony_ci single: path (in module sys) 4837db96d56Sopenharmony_ci single: Py_SetPath() 4847db96d56Sopenharmony_ci 4857db96d56Sopenharmony_ci Return the default module search path; this is computed from the program name 4867db96d56Sopenharmony_ci (set by :c:func:`Py_SetProgramName` above) and some environment variables. 4877db96d56Sopenharmony_ci The returned string consists of a series of directory names separated by a 4887db96d56Sopenharmony_ci platform dependent delimiter character. The delimiter character is ``':'`` 4897db96d56Sopenharmony_ci on Unix and macOS, ``';'`` on Windows. The returned string points into 4907db96d56Sopenharmony_ci static storage; the caller should not modify its value. The list 4917db96d56Sopenharmony_ci :data:`sys.path` is initialized with this value on interpreter startup; it 4927db96d56Sopenharmony_ci can be (and usually is) modified later to change the search path for loading 4937db96d56Sopenharmony_ci modules. 4947db96d56Sopenharmony_ci 4957db96d56Sopenharmony_ci This function should not be called before :c:func:`Py_Initialize`, otherwise 4967db96d56Sopenharmony_ci it returns ``NULL``. 4977db96d56Sopenharmony_ci 4987db96d56Sopenharmony_ci .. XXX should give the exact rules 4997db96d56Sopenharmony_ci 5007db96d56Sopenharmony_ci .. versionchanged:: 3.10 5017db96d56Sopenharmony_ci It now returns ``NULL`` if called before :c:func:`Py_Initialize`. 5027db96d56Sopenharmony_ci 5037db96d56Sopenharmony_ci 5047db96d56Sopenharmony_ci.. c:function:: void Py_SetPath(const wchar_t *) 5057db96d56Sopenharmony_ci 5067db96d56Sopenharmony_ci .. index:: 5077db96d56Sopenharmony_ci triple: module; search; path 5087db96d56Sopenharmony_ci single: path (in module sys) 5097db96d56Sopenharmony_ci single: Py_GetPath() 5107db96d56Sopenharmony_ci 5117db96d56Sopenharmony_ci This API is kept for backward compatibility: setting 5127db96d56Sopenharmony_ci :c:member:`PyConfig.module_search_paths` and 5137db96d56Sopenharmony_ci :c:member:`PyConfig.module_search_paths_set` should be used instead, see 5147db96d56Sopenharmony_ci :ref:`Python Initialization Configuration <init-config>`. 5157db96d56Sopenharmony_ci 5167db96d56Sopenharmony_ci Set the default module search path. If this function is called before 5177db96d56Sopenharmony_ci :c:func:`Py_Initialize`, then :c:func:`Py_GetPath` won't attempt to compute a 5187db96d56Sopenharmony_ci default search path but uses the one provided instead. This is useful if 5197db96d56Sopenharmony_ci Python is embedded by an application that has full knowledge of the location 5207db96d56Sopenharmony_ci of all modules. The path components should be separated by the platform 5217db96d56Sopenharmony_ci dependent delimiter character, which is ``':'`` on Unix and macOS, ``';'`` 5227db96d56Sopenharmony_ci on Windows. 5237db96d56Sopenharmony_ci 5247db96d56Sopenharmony_ci This also causes :data:`sys.executable` to be set to the program 5257db96d56Sopenharmony_ci full path (see :c:func:`Py_GetProgramFullPath`) and for :data:`sys.prefix` and 5267db96d56Sopenharmony_ci :data:`sys.exec_prefix` to be empty. It is up to the caller to modify these 5277db96d56Sopenharmony_ci if required after calling :c:func:`Py_Initialize`. 5287db96d56Sopenharmony_ci 5297db96d56Sopenharmony_ci Use :c:func:`Py_DecodeLocale` to decode a bytes string to get a 5307db96d56Sopenharmony_ci :c:expr:`wchar_*` string. 5317db96d56Sopenharmony_ci 5327db96d56Sopenharmony_ci The path argument is copied internally, so the caller may free it after the 5337db96d56Sopenharmony_ci call completes. 5347db96d56Sopenharmony_ci 5357db96d56Sopenharmony_ci .. versionchanged:: 3.8 5367db96d56Sopenharmony_ci The program full path is now used for :data:`sys.executable`, instead 5377db96d56Sopenharmony_ci of the program name. 5387db96d56Sopenharmony_ci 5397db96d56Sopenharmony_ci .. deprecated:: 3.11 5407db96d56Sopenharmony_ci 5417db96d56Sopenharmony_ci 5427db96d56Sopenharmony_ci.. c:function:: const char* Py_GetVersion() 5437db96d56Sopenharmony_ci 5447db96d56Sopenharmony_ci Return the version of this Python interpreter. This is a string that looks 5457db96d56Sopenharmony_ci something like :: 5467db96d56Sopenharmony_ci 5477db96d56Sopenharmony_ci "3.0a5+ (py3k:63103M, May 12 2008, 00:53:55) \n[GCC 4.2.3]" 5487db96d56Sopenharmony_ci 5497db96d56Sopenharmony_ci .. index:: single: version (in module sys) 5507db96d56Sopenharmony_ci 5517db96d56Sopenharmony_ci The first word (up to the first space character) is the current Python version; 5527db96d56Sopenharmony_ci the first characters are the major and minor version separated by a 5537db96d56Sopenharmony_ci period. The returned string points into static storage; the caller should not 5547db96d56Sopenharmony_ci modify its value. The value is available to Python code as :data:`sys.version`. 5557db96d56Sopenharmony_ci 5567db96d56Sopenharmony_ci See also the :c:var:`Py_Version` constant. 5577db96d56Sopenharmony_ci 5587db96d56Sopenharmony_ci 5597db96d56Sopenharmony_ci.. c:function:: const char* Py_GetPlatform() 5607db96d56Sopenharmony_ci 5617db96d56Sopenharmony_ci .. index:: single: platform (in module sys) 5627db96d56Sopenharmony_ci 5637db96d56Sopenharmony_ci Return the platform identifier for the current platform. On Unix, this is 5647db96d56Sopenharmony_ci formed from the "official" name of the operating system, converted to lower 5657db96d56Sopenharmony_ci case, followed by the major revision number; e.g., for Solaris 2.x, which is 5667db96d56Sopenharmony_ci also known as SunOS 5.x, the value is ``'sunos5'``. On macOS, it is 5677db96d56Sopenharmony_ci ``'darwin'``. On Windows, it is ``'win'``. The returned string points into 5687db96d56Sopenharmony_ci static storage; the caller should not modify its value. The value is available 5697db96d56Sopenharmony_ci to Python code as ``sys.platform``. 5707db96d56Sopenharmony_ci 5717db96d56Sopenharmony_ci 5727db96d56Sopenharmony_ci.. c:function:: const char* Py_GetCopyright() 5737db96d56Sopenharmony_ci 5747db96d56Sopenharmony_ci Return the official copyright string for the current Python version, for example 5757db96d56Sopenharmony_ci 5767db96d56Sopenharmony_ci ``'Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam'`` 5777db96d56Sopenharmony_ci 5787db96d56Sopenharmony_ci .. index:: single: copyright (in module sys) 5797db96d56Sopenharmony_ci 5807db96d56Sopenharmony_ci The returned string points into static storage; the caller should not modify its 5817db96d56Sopenharmony_ci value. The value is available to Python code as ``sys.copyright``. 5827db96d56Sopenharmony_ci 5837db96d56Sopenharmony_ci 5847db96d56Sopenharmony_ci.. c:function:: const char* Py_GetCompiler() 5857db96d56Sopenharmony_ci 5867db96d56Sopenharmony_ci Return an indication of the compiler used to build the current Python version, 5877db96d56Sopenharmony_ci in square brackets, for example:: 5887db96d56Sopenharmony_ci 5897db96d56Sopenharmony_ci "[GCC 2.7.2.2]" 5907db96d56Sopenharmony_ci 5917db96d56Sopenharmony_ci .. index:: single: version (in module sys) 5927db96d56Sopenharmony_ci 5937db96d56Sopenharmony_ci The returned string points into static storage; the caller should not modify its 5947db96d56Sopenharmony_ci value. The value is available to Python code as part of the variable 5957db96d56Sopenharmony_ci ``sys.version``. 5967db96d56Sopenharmony_ci 5977db96d56Sopenharmony_ci 5987db96d56Sopenharmony_ci.. c:function:: const char* Py_GetBuildInfo() 5997db96d56Sopenharmony_ci 6007db96d56Sopenharmony_ci Return information about the sequence number and build date and time of the 6017db96d56Sopenharmony_ci current Python interpreter instance, for example :: 6027db96d56Sopenharmony_ci 6037db96d56Sopenharmony_ci "#67, Aug 1 1997, 22:34:28" 6047db96d56Sopenharmony_ci 6057db96d56Sopenharmony_ci .. index:: single: version (in module sys) 6067db96d56Sopenharmony_ci 6077db96d56Sopenharmony_ci The returned string points into static storage; the caller should not modify its 6087db96d56Sopenharmony_ci value. The value is available to Python code as part of the variable 6097db96d56Sopenharmony_ci ``sys.version``. 6107db96d56Sopenharmony_ci 6117db96d56Sopenharmony_ci 6127db96d56Sopenharmony_ci.. c:function:: void PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath) 6137db96d56Sopenharmony_ci 6147db96d56Sopenharmony_ci .. index:: 6157db96d56Sopenharmony_ci single: main() 6167db96d56Sopenharmony_ci single: Py_FatalError() 6177db96d56Sopenharmony_ci single: argv (in module sys) 6187db96d56Sopenharmony_ci 6197db96d56Sopenharmony_ci This API is kept for backward compatibility: setting 6207db96d56Sopenharmony_ci :c:member:`PyConfig.argv`, :c:member:`PyConfig.parse_argv` and 6217db96d56Sopenharmony_ci :c:member:`PyConfig.safe_path` should be used instead, see :ref:`Python 6227db96d56Sopenharmony_ci Initialization Configuration <init-config>`. 6237db96d56Sopenharmony_ci 6247db96d56Sopenharmony_ci Set :data:`sys.argv` based on *argc* and *argv*. These parameters are 6257db96d56Sopenharmony_ci similar to those passed to the program's :c:func:`main` function with the 6267db96d56Sopenharmony_ci difference that the first entry should refer to the script file to be 6277db96d56Sopenharmony_ci executed rather than the executable hosting the Python interpreter. If there 6287db96d56Sopenharmony_ci isn't a script that will be run, the first entry in *argv* can be an empty 6297db96d56Sopenharmony_ci string. If this function fails to initialize :data:`sys.argv`, a fatal 6307db96d56Sopenharmony_ci condition is signalled using :c:func:`Py_FatalError`. 6317db96d56Sopenharmony_ci 6327db96d56Sopenharmony_ci If *updatepath* is zero, this is all the function does. If *updatepath* 6337db96d56Sopenharmony_ci is non-zero, the function also modifies :data:`sys.path` according to the 6347db96d56Sopenharmony_ci following algorithm: 6357db96d56Sopenharmony_ci 6367db96d56Sopenharmony_ci - If the name of an existing script is passed in ``argv[0]``, the absolute 6377db96d56Sopenharmony_ci path of the directory where the script is located is prepended to 6387db96d56Sopenharmony_ci :data:`sys.path`. 6397db96d56Sopenharmony_ci - Otherwise (that is, if *argc* is ``0`` or ``argv[0]`` doesn't point 6407db96d56Sopenharmony_ci to an existing file name), an empty string is prepended to 6417db96d56Sopenharmony_ci :data:`sys.path`, which is the same as prepending the current working 6427db96d56Sopenharmony_ci directory (``"."``). 6437db96d56Sopenharmony_ci 6447db96d56Sopenharmony_ci Use :c:func:`Py_DecodeLocale` to decode a bytes string to get a 6457db96d56Sopenharmony_ci :c:expr:`wchar_*` string. 6467db96d56Sopenharmony_ci 6477db96d56Sopenharmony_ci See also :c:member:`PyConfig.orig_argv` and :c:member:`PyConfig.argv` 6487db96d56Sopenharmony_ci members of the :ref:`Python Initialization Configuration <init-config>`. 6497db96d56Sopenharmony_ci 6507db96d56Sopenharmony_ci .. note:: 6517db96d56Sopenharmony_ci It is recommended that applications embedding the Python interpreter 6527db96d56Sopenharmony_ci for purposes other than executing a single script pass ``0`` as *updatepath*, 6537db96d56Sopenharmony_ci and update :data:`sys.path` themselves if desired. 6547db96d56Sopenharmony_ci See `CVE-2008-5983 <https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-5983>`_. 6557db96d56Sopenharmony_ci 6567db96d56Sopenharmony_ci On versions before 3.1.3, you can achieve the same effect by manually 6577db96d56Sopenharmony_ci popping the first :data:`sys.path` element after having called 6587db96d56Sopenharmony_ci :c:func:`PySys_SetArgv`, for example using:: 6597db96d56Sopenharmony_ci 6607db96d56Sopenharmony_ci PyRun_SimpleString("import sys; sys.path.pop(0)\n"); 6617db96d56Sopenharmony_ci 6627db96d56Sopenharmony_ci .. versionadded:: 3.1.3 6637db96d56Sopenharmony_ci 6647db96d56Sopenharmony_ci .. XXX impl. doesn't seem consistent in allowing ``0``/``NULL`` for the params; 6657db96d56Sopenharmony_ci check w/ Guido. 6667db96d56Sopenharmony_ci 6677db96d56Sopenharmony_ci .. deprecated:: 3.11 6687db96d56Sopenharmony_ci 6697db96d56Sopenharmony_ci 6707db96d56Sopenharmony_ci.. c:function:: void PySys_SetArgv(int argc, wchar_t **argv) 6717db96d56Sopenharmony_ci 6727db96d56Sopenharmony_ci This API is kept for backward compatibility: setting 6737db96d56Sopenharmony_ci :c:member:`PyConfig.argv` and :c:member:`PyConfig.parse_argv` should be used 6747db96d56Sopenharmony_ci instead, see :ref:`Python Initialization Configuration <init-config>`. 6757db96d56Sopenharmony_ci 6767db96d56Sopenharmony_ci This function works like :c:func:`PySys_SetArgvEx` with *updatepath* set 6777db96d56Sopenharmony_ci to ``1`` unless the :program:`python` interpreter was started with the 6787db96d56Sopenharmony_ci :option:`-I`. 6797db96d56Sopenharmony_ci 6807db96d56Sopenharmony_ci Use :c:func:`Py_DecodeLocale` to decode a bytes string to get a 6817db96d56Sopenharmony_ci :c:expr:`wchar_*` string. 6827db96d56Sopenharmony_ci 6837db96d56Sopenharmony_ci See also :c:member:`PyConfig.orig_argv` and :c:member:`PyConfig.argv` 6847db96d56Sopenharmony_ci members of the :ref:`Python Initialization Configuration <init-config>`. 6857db96d56Sopenharmony_ci 6867db96d56Sopenharmony_ci .. versionchanged:: 3.4 The *updatepath* value depends on :option:`-I`. 6877db96d56Sopenharmony_ci 6887db96d56Sopenharmony_ci .. deprecated:: 3.11 6897db96d56Sopenharmony_ci 6907db96d56Sopenharmony_ci 6917db96d56Sopenharmony_ci.. c:function:: void Py_SetPythonHome(const wchar_t *home) 6927db96d56Sopenharmony_ci 6937db96d56Sopenharmony_ci This API is kept for backward compatibility: setting 6947db96d56Sopenharmony_ci :c:member:`PyConfig.home` should be used instead, see :ref:`Python 6957db96d56Sopenharmony_ci Initialization Configuration <init-config>`. 6967db96d56Sopenharmony_ci 6977db96d56Sopenharmony_ci Set the default "home" directory, that is, the location of the standard 6987db96d56Sopenharmony_ci Python libraries. See :envvar:`PYTHONHOME` for the meaning of the 6997db96d56Sopenharmony_ci argument string. 7007db96d56Sopenharmony_ci 7017db96d56Sopenharmony_ci The argument should point to a zero-terminated character string in static 7027db96d56Sopenharmony_ci storage whose contents will not change for the duration of the program's 7037db96d56Sopenharmony_ci execution. No code in the Python interpreter will change the contents of 7047db96d56Sopenharmony_ci this storage. 7057db96d56Sopenharmony_ci 7067db96d56Sopenharmony_ci Use :c:func:`Py_DecodeLocale` to decode a bytes string to get a 7077db96d56Sopenharmony_ci :c:expr:`wchar_*` string. 7087db96d56Sopenharmony_ci 7097db96d56Sopenharmony_ci .. deprecated:: 3.11 7107db96d56Sopenharmony_ci 7117db96d56Sopenharmony_ci 7127db96d56Sopenharmony_ci.. c:function:: wchar_t* Py_GetPythonHome() 7137db96d56Sopenharmony_ci 7147db96d56Sopenharmony_ci Return the default "home", that is, the value set by a previous call to 7157db96d56Sopenharmony_ci :c:func:`Py_SetPythonHome`, or the value of the :envvar:`PYTHONHOME` 7167db96d56Sopenharmony_ci environment variable if it is set. 7177db96d56Sopenharmony_ci 7187db96d56Sopenharmony_ci This function should not be called before :c:func:`Py_Initialize`, otherwise 7197db96d56Sopenharmony_ci it returns ``NULL``. 7207db96d56Sopenharmony_ci 7217db96d56Sopenharmony_ci .. versionchanged:: 3.10 7227db96d56Sopenharmony_ci It now returns ``NULL`` if called before :c:func:`Py_Initialize`. 7237db96d56Sopenharmony_ci 7247db96d56Sopenharmony_ci 7257db96d56Sopenharmony_ci.. _threads: 7267db96d56Sopenharmony_ci 7277db96d56Sopenharmony_ciThread State and the Global Interpreter Lock 7287db96d56Sopenharmony_ci============================================ 7297db96d56Sopenharmony_ci 7307db96d56Sopenharmony_ci.. index:: 7317db96d56Sopenharmony_ci single: global interpreter lock 7327db96d56Sopenharmony_ci single: interpreter lock 7337db96d56Sopenharmony_ci single: lock, interpreter 7347db96d56Sopenharmony_ci 7357db96d56Sopenharmony_ciThe Python interpreter is not fully thread-safe. In order to support 7367db96d56Sopenharmony_cimulti-threaded Python programs, there's a global lock, called the :term:`global 7377db96d56Sopenharmony_ciinterpreter lock` or :term:`GIL`, that must be held by the current thread before 7387db96d56Sopenharmony_ciit can safely access Python objects. Without the lock, even the simplest 7397db96d56Sopenharmony_cioperations could cause problems in a multi-threaded program: for example, when 7407db96d56Sopenharmony_citwo threads simultaneously increment the reference count of the same object, the 7417db96d56Sopenharmony_cireference count could end up being incremented only once instead of twice. 7427db96d56Sopenharmony_ci 7437db96d56Sopenharmony_ci.. index:: single: setswitchinterval() (in module sys) 7447db96d56Sopenharmony_ci 7457db96d56Sopenharmony_ciTherefore, the rule exists that only the thread that has acquired the 7467db96d56Sopenharmony_ci:term:`GIL` may operate on Python objects or call Python/C API functions. 7477db96d56Sopenharmony_ciIn order to emulate concurrency of execution, the interpreter regularly 7487db96d56Sopenharmony_citries to switch threads (see :func:`sys.setswitchinterval`). The lock is also 7497db96d56Sopenharmony_cireleased around potentially blocking I/O operations like reading or writing 7507db96d56Sopenharmony_cia file, so that other Python threads can run in the meantime. 7517db96d56Sopenharmony_ci 7527db96d56Sopenharmony_ci.. index:: 7537db96d56Sopenharmony_ci single: PyThreadState 7547db96d56Sopenharmony_ci single: PyThreadState 7557db96d56Sopenharmony_ci 7567db96d56Sopenharmony_ciThe Python interpreter keeps some thread-specific bookkeeping information 7577db96d56Sopenharmony_ciinside a data structure called :c:type:`PyThreadState`. There's also one 7587db96d56Sopenharmony_ciglobal variable pointing to the current :c:type:`PyThreadState`: it can 7597db96d56Sopenharmony_cibe retrieved using :c:func:`PyThreadState_Get`. 7607db96d56Sopenharmony_ci 7617db96d56Sopenharmony_ciReleasing the GIL from extension code 7627db96d56Sopenharmony_ci------------------------------------- 7637db96d56Sopenharmony_ci 7647db96d56Sopenharmony_ciMost extension code manipulating the :term:`GIL` has the following simple 7657db96d56Sopenharmony_cistructure:: 7667db96d56Sopenharmony_ci 7677db96d56Sopenharmony_ci Save the thread state in a local variable. 7687db96d56Sopenharmony_ci Release the global interpreter lock. 7697db96d56Sopenharmony_ci ... Do some blocking I/O operation ... 7707db96d56Sopenharmony_ci Reacquire the global interpreter lock. 7717db96d56Sopenharmony_ci Restore the thread state from the local variable. 7727db96d56Sopenharmony_ci 7737db96d56Sopenharmony_ciThis is so common that a pair of macros exists to simplify it:: 7747db96d56Sopenharmony_ci 7757db96d56Sopenharmony_ci Py_BEGIN_ALLOW_THREADS 7767db96d56Sopenharmony_ci ... Do some blocking I/O operation ... 7777db96d56Sopenharmony_ci Py_END_ALLOW_THREADS 7787db96d56Sopenharmony_ci 7797db96d56Sopenharmony_ci.. index:: 7807db96d56Sopenharmony_ci single: Py_BEGIN_ALLOW_THREADS 7817db96d56Sopenharmony_ci single: Py_END_ALLOW_THREADS 7827db96d56Sopenharmony_ci 7837db96d56Sopenharmony_ciThe :c:macro:`Py_BEGIN_ALLOW_THREADS` macro opens a new block and declares a 7847db96d56Sopenharmony_cihidden local variable; the :c:macro:`Py_END_ALLOW_THREADS` macro closes the 7857db96d56Sopenharmony_ciblock. 7867db96d56Sopenharmony_ci 7877db96d56Sopenharmony_ciThe block above expands to the following code:: 7887db96d56Sopenharmony_ci 7897db96d56Sopenharmony_ci PyThreadState *_save; 7907db96d56Sopenharmony_ci 7917db96d56Sopenharmony_ci _save = PyEval_SaveThread(); 7927db96d56Sopenharmony_ci ... Do some blocking I/O operation ... 7937db96d56Sopenharmony_ci PyEval_RestoreThread(_save); 7947db96d56Sopenharmony_ci 7957db96d56Sopenharmony_ci.. index:: 7967db96d56Sopenharmony_ci single: PyEval_RestoreThread() 7977db96d56Sopenharmony_ci single: PyEval_SaveThread() 7987db96d56Sopenharmony_ci 7997db96d56Sopenharmony_ciHere is how these functions work: the global interpreter lock is used to protect the pointer to the 8007db96d56Sopenharmony_cicurrent thread state. When releasing the lock and saving the thread state, 8017db96d56Sopenharmony_cithe current thread state pointer must be retrieved before the lock is released 8027db96d56Sopenharmony_ci(since another thread could immediately acquire the lock and store its own thread 8037db96d56Sopenharmony_cistate in the global variable). Conversely, when acquiring the lock and restoring 8047db96d56Sopenharmony_cithe thread state, the lock must be acquired before storing the thread state 8057db96d56Sopenharmony_cipointer. 8067db96d56Sopenharmony_ci 8077db96d56Sopenharmony_ci.. note:: 8087db96d56Sopenharmony_ci Calling system I/O functions is the most common use case for releasing 8097db96d56Sopenharmony_ci the GIL, but it can also be useful before calling long-running computations 8107db96d56Sopenharmony_ci which don't need access to Python objects, such as compression or 8117db96d56Sopenharmony_ci cryptographic functions operating over memory buffers. For example, the 8127db96d56Sopenharmony_ci standard :mod:`zlib` and :mod:`hashlib` modules release the GIL when 8137db96d56Sopenharmony_ci compressing or hashing data. 8147db96d56Sopenharmony_ci 8157db96d56Sopenharmony_ci 8167db96d56Sopenharmony_ci.. _gilstate: 8177db96d56Sopenharmony_ci 8187db96d56Sopenharmony_ciNon-Python created threads 8197db96d56Sopenharmony_ci-------------------------- 8207db96d56Sopenharmony_ci 8217db96d56Sopenharmony_ciWhen threads are created using the dedicated Python APIs (such as the 8227db96d56Sopenharmony_ci:mod:`threading` module), a thread state is automatically associated to them 8237db96d56Sopenharmony_ciand the code showed above is therefore correct. However, when threads are 8247db96d56Sopenharmony_cicreated from C (for example by a third-party library with its own thread 8257db96d56Sopenharmony_cimanagement), they don't hold the GIL, nor is there a thread state structure 8267db96d56Sopenharmony_cifor them. 8277db96d56Sopenharmony_ci 8287db96d56Sopenharmony_ciIf you need to call Python code from these threads (often this will be part 8297db96d56Sopenharmony_ciof a callback API provided by the aforementioned third-party library), 8307db96d56Sopenharmony_ciyou must first register these threads with the interpreter by 8317db96d56Sopenharmony_cicreating a thread state data structure, then acquiring the GIL, and finally 8327db96d56Sopenharmony_cistoring their thread state pointer, before you can start using the Python/C 8337db96d56Sopenharmony_ciAPI. When you are done, you should reset the thread state pointer, release 8347db96d56Sopenharmony_cithe GIL, and finally free the thread state data structure. 8357db96d56Sopenharmony_ci 8367db96d56Sopenharmony_ciThe :c:func:`PyGILState_Ensure` and :c:func:`PyGILState_Release` functions do 8377db96d56Sopenharmony_ciall of the above automatically. The typical idiom for calling into Python 8387db96d56Sopenharmony_cifrom a C thread is:: 8397db96d56Sopenharmony_ci 8407db96d56Sopenharmony_ci PyGILState_STATE gstate; 8417db96d56Sopenharmony_ci gstate = PyGILState_Ensure(); 8427db96d56Sopenharmony_ci 8437db96d56Sopenharmony_ci /* Perform Python actions here. */ 8447db96d56Sopenharmony_ci result = CallSomeFunction(); 8457db96d56Sopenharmony_ci /* evaluate result or handle exception */ 8467db96d56Sopenharmony_ci 8477db96d56Sopenharmony_ci /* Release the thread. No Python API allowed beyond this point. */ 8487db96d56Sopenharmony_ci PyGILState_Release(gstate); 8497db96d56Sopenharmony_ci 8507db96d56Sopenharmony_ciNote that the ``PyGILState_*`` functions assume there is only one global 8517db96d56Sopenharmony_ciinterpreter (created automatically by :c:func:`Py_Initialize`). Python 8527db96d56Sopenharmony_cisupports the creation of additional interpreters (using 8537db96d56Sopenharmony_ci:c:func:`Py_NewInterpreter`), but mixing multiple interpreters and the 8547db96d56Sopenharmony_ci``PyGILState_*`` API is unsupported. 8557db96d56Sopenharmony_ci 8567db96d56Sopenharmony_ci 8577db96d56Sopenharmony_ci.. _fork-and-threads: 8587db96d56Sopenharmony_ci 8597db96d56Sopenharmony_ciCautions about fork() 8607db96d56Sopenharmony_ci--------------------- 8617db96d56Sopenharmony_ci 8627db96d56Sopenharmony_ciAnother important thing to note about threads is their behaviour in the face 8637db96d56Sopenharmony_ciof the C :c:func:`fork` call. On most systems with :c:func:`fork`, after a 8647db96d56Sopenharmony_ciprocess forks only the thread that issued the fork will exist. This has a 8657db96d56Sopenharmony_ciconcrete impact both on how locks must be handled and on all stored state 8667db96d56Sopenharmony_ciin CPython's runtime. 8677db96d56Sopenharmony_ci 8687db96d56Sopenharmony_ciThe fact that only the "current" thread remains 8697db96d56Sopenharmony_cimeans any locks held by other threads will never be released. Python solves 8707db96d56Sopenharmony_cithis for :func:`os.fork` by acquiring the locks it uses internally before 8717db96d56Sopenharmony_cithe fork, and releasing them afterwards. In addition, it resets any 8727db96d56Sopenharmony_ci:ref:`lock-objects` in the child. When extending or embedding Python, there 8737db96d56Sopenharmony_ciis no way to inform Python of additional (non-Python) locks that need to be 8747db96d56Sopenharmony_ciacquired before or reset after a fork. OS facilities such as 8757db96d56Sopenharmony_ci:c:func:`pthread_atfork` would need to be used to accomplish the same thing. 8767db96d56Sopenharmony_ciAdditionally, when extending or embedding Python, calling :c:func:`fork` 8777db96d56Sopenharmony_cidirectly rather than through :func:`os.fork` (and returning to or calling 8787db96d56Sopenharmony_ciinto Python) may result in a deadlock by one of Python's internal locks 8797db96d56Sopenharmony_cibeing held by a thread that is defunct after the fork. 8807db96d56Sopenharmony_ci:c:func:`PyOS_AfterFork_Child` tries to reset the necessary locks, but is not 8817db96d56Sopenharmony_cialways able to. 8827db96d56Sopenharmony_ci 8837db96d56Sopenharmony_ciThe fact that all other threads go away also means that CPython's 8847db96d56Sopenharmony_ciruntime state there must be cleaned up properly, which :func:`os.fork` 8857db96d56Sopenharmony_cidoes. This means finalizing all other :c:type:`PyThreadState` objects 8867db96d56Sopenharmony_cibelonging to the current interpreter and all other 8877db96d56Sopenharmony_ci:c:type:`PyInterpreterState` objects. Due to this and the special 8887db96d56Sopenharmony_cinature of the :ref:`"main" interpreter <sub-interpreter-support>`, 8897db96d56Sopenharmony_ci:c:func:`fork` should only be called in that interpreter's "main" 8907db96d56Sopenharmony_cithread, where the CPython global runtime was originally initialized. 8917db96d56Sopenharmony_ciThe only exception is if :c:func:`exec` will be called immediately 8927db96d56Sopenharmony_ciafter. 8937db96d56Sopenharmony_ci 8947db96d56Sopenharmony_ci 8957db96d56Sopenharmony_ciHigh-level API 8967db96d56Sopenharmony_ci-------------- 8977db96d56Sopenharmony_ci 8987db96d56Sopenharmony_ciThese are the most commonly used types and functions when writing C extension 8997db96d56Sopenharmony_cicode, or when embedding the Python interpreter: 9007db96d56Sopenharmony_ci 9017db96d56Sopenharmony_ci.. c:type:: PyInterpreterState 9027db96d56Sopenharmony_ci 9037db96d56Sopenharmony_ci This data structure represents the state shared by a number of cooperating 9047db96d56Sopenharmony_ci threads. Threads belonging to the same interpreter share their module 9057db96d56Sopenharmony_ci administration and a few other internal items. There are no public members in 9067db96d56Sopenharmony_ci this structure. 9077db96d56Sopenharmony_ci 9087db96d56Sopenharmony_ci Threads belonging to different interpreters initially share nothing, except 9097db96d56Sopenharmony_ci process state like available memory, open file descriptors and such. The global 9107db96d56Sopenharmony_ci interpreter lock is also shared by all threads, regardless of to which 9117db96d56Sopenharmony_ci interpreter they belong. 9127db96d56Sopenharmony_ci 9137db96d56Sopenharmony_ci 9147db96d56Sopenharmony_ci.. c:type:: PyThreadState 9157db96d56Sopenharmony_ci 9167db96d56Sopenharmony_ci This data structure represents the state of a single thread. The only public 9177db96d56Sopenharmony_ci data member is :attr:`interp` (:c:expr:`PyInterpreterState *`), which points to 9187db96d56Sopenharmony_ci this thread's interpreter state. 9197db96d56Sopenharmony_ci 9207db96d56Sopenharmony_ci 9217db96d56Sopenharmony_ci.. c:function:: void PyEval_InitThreads() 9227db96d56Sopenharmony_ci 9237db96d56Sopenharmony_ci .. index:: 9247db96d56Sopenharmony_ci single: PyEval_AcquireThread() 9257db96d56Sopenharmony_ci single: PyEval_ReleaseThread() 9267db96d56Sopenharmony_ci single: PyEval_SaveThread() 9277db96d56Sopenharmony_ci single: PyEval_RestoreThread() 9287db96d56Sopenharmony_ci 9297db96d56Sopenharmony_ci Deprecated function which does nothing. 9307db96d56Sopenharmony_ci 9317db96d56Sopenharmony_ci In Python 3.6 and older, this function created the GIL if it didn't exist. 9327db96d56Sopenharmony_ci 9337db96d56Sopenharmony_ci .. versionchanged:: 3.9 9347db96d56Sopenharmony_ci The function now does nothing. 9357db96d56Sopenharmony_ci 9367db96d56Sopenharmony_ci .. versionchanged:: 3.7 9377db96d56Sopenharmony_ci This function is now called by :c:func:`Py_Initialize()`, so you don't 9387db96d56Sopenharmony_ci have to call it yourself anymore. 9397db96d56Sopenharmony_ci 9407db96d56Sopenharmony_ci .. versionchanged:: 3.2 9417db96d56Sopenharmony_ci This function cannot be called before :c:func:`Py_Initialize()` anymore. 9427db96d56Sopenharmony_ci 9437db96d56Sopenharmony_ci .. deprecated:: 3.9 9447db96d56Sopenharmony_ci 9457db96d56Sopenharmony_ci .. index:: pair: module; _thread 9467db96d56Sopenharmony_ci 9477db96d56Sopenharmony_ci 9487db96d56Sopenharmony_ci.. c:function:: int PyEval_ThreadsInitialized() 9497db96d56Sopenharmony_ci 9507db96d56Sopenharmony_ci Returns a non-zero value if :c:func:`PyEval_InitThreads` has been called. This 9517db96d56Sopenharmony_ci function can be called without holding the GIL, and therefore can be used to 9527db96d56Sopenharmony_ci avoid calls to the locking API when running single-threaded. 9537db96d56Sopenharmony_ci 9547db96d56Sopenharmony_ci .. versionchanged:: 3.7 9557db96d56Sopenharmony_ci The :term:`GIL` is now initialized by :c:func:`Py_Initialize()`. 9567db96d56Sopenharmony_ci 9577db96d56Sopenharmony_ci .. deprecated:: 3.9 9587db96d56Sopenharmony_ci 9597db96d56Sopenharmony_ci 9607db96d56Sopenharmony_ci.. c:function:: PyThreadState* PyEval_SaveThread() 9617db96d56Sopenharmony_ci 9627db96d56Sopenharmony_ci Release the global interpreter lock (if it has been created) and reset the 9637db96d56Sopenharmony_ci thread state to ``NULL``, returning the previous thread state (which is not 9647db96d56Sopenharmony_ci ``NULL``). If the lock has been created, the current thread must have 9657db96d56Sopenharmony_ci acquired it. 9667db96d56Sopenharmony_ci 9677db96d56Sopenharmony_ci 9687db96d56Sopenharmony_ci.. c:function:: void PyEval_RestoreThread(PyThreadState *tstate) 9697db96d56Sopenharmony_ci 9707db96d56Sopenharmony_ci Acquire the global interpreter lock (if it has been created) and set the 9717db96d56Sopenharmony_ci thread state to *tstate*, which must not be ``NULL``. If the lock has been 9727db96d56Sopenharmony_ci created, the current thread must not have acquired it, otherwise deadlock 9737db96d56Sopenharmony_ci ensues. 9747db96d56Sopenharmony_ci 9757db96d56Sopenharmony_ci .. note:: 9767db96d56Sopenharmony_ci Calling this function from a thread when the runtime is finalizing 9777db96d56Sopenharmony_ci will terminate the thread, even if the thread was not created by Python. 9787db96d56Sopenharmony_ci You can use :c:func:`_Py_IsFinalizing` or :func:`sys.is_finalizing` to 9797db96d56Sopenharmony_ci check if the interpreter is in process of being finalized before calling 9807db96d56Sopenharmony_ci this function to avoid unwanted termination. 9817db96d56Sopenharmony_ci 9827db96d56Sopenharmony_ci.. c:function:: PyThreadState* PyThreadState_Get() 9837db96d56Sopenharmony_ci 9847db96d56Sopenharmony_ci Return the current thread state. The global interpreter lock must be held. 9857db96d56Sopenharmony_ci When the current thread state is ``NULL``, this issues a fatal error (so that 9867db96d56Sopenharmony_ci the caller needn't check for ``NULL``). 9877db96d56Sopenharmony_ci 9887db96d56Sopenharmony_ci 9897db96d56Sopenharmony_ci.. c:function:: PyThreadState* PyThreadState_Swap(PyThreadState *tstate) 9907db96d56Sopenharmony_ci 9917db96d56Sopenharmony_ci Swap the current thread state with the thread state given by the argument 9927db96d56Sopenharmony_ci *tstate*, which may be ``NULL``. The global interpreter lock must be held 9937db96d56Sopenharmony_ci and is not released. 9947db96d56Sopenharmony_ci 9957db96d56Sopenharmony_ci 9967db96d56Sopenharmony_ciThe following functions use thread-local storage, and are not compatible 9977db96d56Sopenharmony_ciwith sub-interpreters: 9987db96d56Sopenharmony_ci 9997db96d56Sopenharmony_ci.. c:function:: PyGILState_STATE PyGILState_Ensure() 10007db96d56Sopenharmony_ci 10017db96d56Sopenharmony_ci Ensure that the current thread is ready to call the Python C API regardless 10027db96d56Sopenharmony_ci of the current state of Python, or of the global interpreter lock. This may 10037db96d56Sopenharmony_ci be called as many times as desired by a thread as long as each call is 10047db96d56Sopenharmony_ci matched with a call to :c:func:`PyGILState_Release`. In general, other 10057db96d56Sopenharmony_ci thread-related APIs may be used between :c:func:`PyGILState_Ensure` and 10067db96d56Sopenharmony_ci :c:func:`PyGILState_Release` calls as long as the thread state is restored to 10077db96d56Sopenharmony_ci its previous state before the Release(). For example, normal usage of the 10087db96d56Sopenharmony_ci :c:macro:`Py_BEGIN_ALLOW_THREADS` and :c:macro:`Py_END_ALLOW_THREADS` macros is 10097db96d56Sopenharmony_ci acceptable. 10107db96d56Sopenharmony_ci 10117db96d56Sopenharmony_ci The return value is an opaque "handle" to the thread state when 10127db96d56Sopenharmony_ci :c:func:`PyGILState_Ensure` was called, and must be passed to 10137db96d56Sopenharmony_ci :c:func:`PyGILState_Release` to ensure Python is left in the same state. Even 10147db96d56Sopenharmony_ci though recursive calls are allowed, these handles *cannot* be shared - each 10157db96d56Sopenharmony_ci unique call to :c:func:`PyGILState_Ensure` must save the handle for its call 10167db96d56Sopenharmony_ci to :c:func:`PyGILState_Release`. 10177db96d56Sopenharmony_ci 10187db96d56Sopenharmony_ci When the function returns, the current thread will hold the GIL and be able 10197db96d56Sopenharmony_ci to call arbitrary Python code. Failure is a fatal error. 10207db96d56Sopenharmony_ci 10217db96d56Sopenharmony_ci .. note:: 10227db96d56Sopenharmony_ci Calling this function from a thread when the runtime is finalizing 10237db96d56Sopenharmony_ci will terminate the thread, even if the thread was not created by Python. 10247db96d56Sopenharmony_ci You can use :c:func:`_Py_IsFinalizing` or :func:`sys.is_finalizing` to 10257db96d56Sopenharmony_ci check if the interpreter is in process of being finalized before calling 10267db96d56Sopenharmony_ci this function to avoid unwanted termination. 10277db96d56Sopenharmony_ci 10287db96d56Sopenharmony_ci.. c:function:: void PyGILState_Release(PyGILState_STATE) 10297db96d56Sopenharmony_ci 10307db96d56Sopenharmony_ci Release any resources previously acquired. After this call, Python's state will 10317db96d56Sopenharmony_ci be the same as it was prior to the corresponding :c:func:`PyGILState_Ensure` call 10327db96d56Sopenharmony_ci (but generally this state will be unknown to the caller, hence the use of the 10337db96d56Sopenharmony_ci GILState API). 10347db96d56Sopenharmony_ci 10357db96d56Sopenharmony_ci Every call to :c:func:`PyGILState_Ensure` must be matched by a call to 10367db96d56Sopenharmony_ci :c:func:`PyGILState_Release` on the same thread. 10377db96d56Sopenharmony_ci 10387db96d56Sopenharmony_ci 10397db96d56Sopenharmony_ci.. c:function:: PyThreadState* PyGILState_GetThisThreadState() 10407db96d56Sopenharmony_ci 10417db96d56Sopenharmony_ci Get the current thread state for this thread. May return ``NULL`` if no 10427db96d56Sopenharmony_ci GILState API has been used on the current thread. Note that the main thread 10437db96d56Sopenharmony_ci always has such a thread-state, even if no auto-thread-state call has been 10447db96d56Sopenharmony_ci made on the main thread. This is mainly a helper/diagnostic function. 10457db96d56Sopenharmony_ci 10467db96d56Sopenharmony_ci 10477db96d56Sopenharmony_ci.. c:function:: int PyGILState_Check() 10487db96d56Sopenharmony_ci 10497db96d56Sopenharmony_ci Return ``1`` if the current thread is holding the GIL and ``0`` otherwise. 10507db96d56Sopenharmony_ci This function can be called from any thread at any time. 10517db96d56Sopenharmony_ci Only if it has had its Python thread state initialized and currently is 10527db96d56Sopenharmony_ci holding the GIL will it return ``1``. 10537db96d56Sopenharmony_ci This is mainly a helper/diagnostic function. It can be useful 10547db96d56Sopenharmony_ci for example in callback contexts or memory allocation functions when 10557db96d56Sopenharmony_ci knowing that the GIL is locked can allow the caller to perform sensitive 10567db96d56Sopenharmony_ci actions or otherwise behave differently. 10577db96d56Sopenharmony_ci 10587db96d56Sopenharmony_ci .. versionadded:: 3.4 10597db96d56Sopenharmony_ci 10607db96d56Sopenharmony_ci 10617db96d56Sopenharmony_ciThe following macros are normally used without a trailing semicolon; look for 10627db96d56Sopenharmony_ciexample usage in the Python source distribution. 10637db96d56Sopenharmony_ci 10647db96d56Sopenharmony_ci 10657db96d56Sopenharmony_ci.. c:macro:: Py_BEGIN_ALLOW_THREADS 10667db96d56Sopenharmony_ci 10677db96d56Sopenharmony_ci This macro expands to ``{ PyThreadState *_save; _save = PyEval_SaveThread();``. 10687db96d56Sopenharmony_ci Note that it contains an opening brace; it must be matched with a following 10697db96d56Sopenharmony_ci :c:macro:`Py_END_ALLOW_THREADS` macro. See above for further discussion of this 10707db96d56Sopenharmony_ci macro. 10717db96d56Sopenharmony_ci 10727db96d56Sopenharmony_ci 10737db96d56Sopenharmony_ci.. c:macro:: Py_END_ALLOW_THREADS 10747db96d56Sopenharmony_ci 10757db96d56Sopenharmony_ci This macro expands to ``PyEval_RestoreThread(_save); }``. Note that it contains 10767db96d56Sopenharmony_ci a closing brace; it must be matched with an earlier 10777db96d56Sopenharmony_ci :c:macro:`Py_BEGIN_ALLOW_THREADS` macro. See above for further discussion of 10787db96d56Sopenharmony_ci this macro. 10797db96d56Sopenharmony_ci 10807db96d56Sopenharmony_ci 10817db96d56Sopenharmony_ci.. c:macro:: Py_BLOCK_THREADS 10827db96d56Sopenharmony_ci 10837db96d56Sopenharmony_ci This macro expands to ``PyEval_RestoreThread(_save);``: it is equivalent to 10847db96d56Sopenharmony_ci :c:macro:`Py_END_ALLOW_THREADS` without the closing brace. 10857db96d56Sopenharmony_ci 10867db96d56Sopenharmony_ci 10877db96d56Sopenharmony_ci.. c:macro:: Py_UNBLOCK_THREADS 10887db96d56Sopenharmony_ci 10897db96d56Sopenharmony_ci This macro expands to ``_save = PyEval_SaveThread();``: it is equivalent to 10907db96d56Sopenharmony_ci :c:macro:`Py_BEGIN_ALLOW_THREADS` without the opening brace and variable 10917db96d56Sopenharmony_ci declaration. 10927db96d56Sopenharmony_ci 10937db96d56Sopenharmony_ci 10947db96d56Sopenharmony_ciLow-level API 10957db96d56Sopenharmony_ci------------- 10967db96d56Sopenharmony_ci 10977db96d56Sopenharmony_ciAll of the following functions must be called after :c:func:`Py_Initialize`. 10987db96d56Sopenharmony_ci 10997db96d56Sopenharmony_ci.. versionchanged:: 3.7 11007db96d56Sopenharmony_ci :c:func:`Py_Initialize()` now initializes the :term:`GIL`. 11017db96d56Sopenharmony_ci 11027db96d56Sopenharmony_ci 11037db96d56Sopenharmony_ci.. c:function:: PyInterpreterState* PyInterpreterState_New() 11047db96d56Sopenharmony_ci 11057db96d56Sopenharmony_ci Create a new interpreter state object. The global interpreter lock need not 11067db96d56Sopenharmony_ci be held, but may be held if it is necessary to serialize calls to this 11077db96d56Sopenharmony_ci function. 11087db96d56Sopenharmony_ci 11097db96d56Sopenharmony_ci .. audit-event:: cpython.PyInterpreterState_New "" c.PyInterpreterState_New 11107db96d56Sopenharmony_ci 11117db96d56Sopenharmony_ci 11127db96d56Sopenharmony_ci.. c:function:: void PyInterpreterState_Clear(PyInterpreterState *interp) 11137db96d56Sopenharmony_ci 11147db96d56Sopenharmony_ci Reset all information in an interpreter state object. The global interpreter 11157db96d56Sopenharmony_ci lock must be held. 11167db96d56Sopenharmony_ci 11177db96d56Sopenharmony_ci .. audit-event:: cpython.PyInterpreterState_Clear "" c.PyInterpreterState_Clear 11187db96d56Sopenharmony_ci 11197db96d56Sopenharmony_ci 11207db96d56Sopenharmony_ci.. c:function:: void PyInterpreterState_Delete(PyInterpreterState *interp) 11217db96d56Sopenharmony_ci 11227db96d56Sopenharmony_ci Destroy an interpreter state object. The global interpreter lock need not be 11237db96d56Sopenharmony_ci held. The interpreter state must have been reset with a previous call to 11247db96d56Sopenharmony_ci :c:func:`PyInterpreterState_Clear`. 11257db96d56Sopenharmony_ci 11267db96d56Sopenharmony_ci 11277db96d56Sopenharmony_ci.. c:function:: PyThreadState* PyThreadState_New(PyInterpreterState *interp) 11287db96d56Sopenharmony_ci 11297db96d56Sopenharmony_ci Create a new thread state object belonging to the given interpreter object. 11307db96d56Sopenharmony_ci The global interpreter lock need not be held, but may be held if it is 11317db96d56Sopenharmony_ci necessary to serialize calls to this function. 11327db96d56Sopenharmony_ci 11337db96d56Sopenharmony_ci 11347db96d56Sopenharmony_ci.. c:function:: void PyThreadState_Clear(PyThreadState *tstate) 11357db96d56Sopenharmony_ci 11367db96d56Sopenharmony_ci Reset all information in a thread state object. The global interpreter lock 11377db96d56Sopenharmony_ci must be held. 11387db96d56Sopenharmony_ci 11397db96d56Sopenharmony_ci .. versionchanged:: 3.9 11407db96d56Sopenharmony_ci This function now calls the :c:member:`PyThreadState.on_delete` callback. 11417db96d56Sopenharmony_ci Previously, that happened in :c:func:`PyThreadState_Delete`. 11427db96d56Sopenharmony_ci 11437db96d56Sopenharmony_ci 11447db96d56Sopenharmony_ci.. c:function:: void PyThreadState_Delete(PyThreadState *tstate) 11457db96d56Sopenharmony_ci 11467db96d56Sopenharmony_ci Destroy a thread state object. The global interpreter lock need not be held. 11477db96d56Sopenharmony_ci The thread state must have been reset with a previous call to 11487db96d56Sopenharmony_ci :c:func:`PyThreadState_Clear`. 11497db96d56Sopenharmony_ci 11507db96d56Sopenharmony_ci 11517db96d56Sopenharmony_ci.. c:function:: void PyThreadState_DeleteCurrent(void) 11527db96d56Sopenharmony_ci 11537db96d56Sopenharmony_ci Destroy the current thread state and release the global interpreter lock. 11547db96d56Sopenharmony_ci Like :c:func:`PyThreadState_Delete`, the global interpreter lock need not 11557db96d56Sopenharmony_ci be held. The thread state must have been reset with a previous call 11567db96d56Sopenharmony_ci to :c:func:`PyThreadState_Clear`. 11577db96d56Sopenharmony_ci 11587db96d56Sopenharmony_ci 11597db96d56Sopenharmony_ci.. c:function:: PyFrameObject* PyThreadState_GetFrame(PyThreadState *tstate) 11607db96d56Sopenharmony_ci 11617db96d56Sopenharmony_ci Get the current frame of the Python thread state *tstate*. 11627db96d56Sopenharmony_ci 11637db96d56Sopenharmony_ci Return a :term:`strong reference`. Return ``NULL`` if no frame is currently 11647db96d56Sopenharmony_ci executing. 11657db96d56Sopenharmony_ci 11667db96d56Sopenharmony_ci See also :c:func:`PyEval_GetFrame`. 11677db96d56Sopenharmony_ci 11687db96d56Sopenharmony_ci *tstate* must not be ``NULL``. 11697db96d56Sopenharmony_ci 11707db96d56Sopenharmony_ci .. versionadded:: 3.9 11717db96d56Sopenharmony_ci 11727db96d56Sopenharmony_ci 11737db96d56Sopenharmony_ci.. c:function:: uint64_t PyThreadState_GetID(PyThreadState *tstate) 11747db96d56Sopenharmony_ci 11757db96d56Sopenharmony_ci Get the unique thread state identifier of the Python thread state *tstate*. 11767db96d56Sopenharmony_ci 11777db96d56Sopenharmony_ci *tstate* must not be ``NULL``. 11787db96d56Sopenharmony_ci 11797db96d56Sopenharmony_ci .. versionadded:: 3.9 11807db96d56Sopenharmony_ci 11817db96d56Sopenharmony_ci 11827db96d56Sopenharmony_ci.. c:function:: PyInterpreterState* PyThreadState_GetInterpreter(PyThreadState *tstate) 11837db96d56Sopenharmony_ci 11847db96d56Sopenharmony_ci Get the interpreter of the Python thread state *tstate*. 11857db96d56Sopenharmony_ci 11867db96d56Sopenharmony_ci *tstate* must not be ``NULL``. 11877db96d56Sopenharmony_ci 11887db96d56Sopenharmony_ci .. versionadded:: 3.9 11897db96d56Sopenharmony_ci 11907db96d56Sopenharmony_ci 11917db96d56Sopenharmony_ci.. c:function:: void PyThreadState_EnterTracing(PyThreadState *tstate) 11927db96d56Sopenharmony_ci 11937db96d56Sopenharmony_ci Suspend tracing and profiling in the Python thread state *tstate*. 11947db96d56Sopenharmony_ci 11957db96d56Sopenharmony_ci Resume them using the :c:func:`PyThreadState_LeaveTracing` function. 11967db96d56Sopenharmony_ci 11977db96d56Sopenharmony_ci .. versionadded:: 3.11 11987db96d56Sopenharmony_ci 11997db96d56Sopenharmony_ci 12007db96d56Sopenharmony_ci.. c:function:: void PyThreadState_LeaveTracing(PyThreadState *tstate) 12017db96d56Sopenharmony_ci 12027db96d56Sopenharmony_ci Resume tracing and profiling in the Python thread state *tstate* suspended 12037db96d56Sopenharmony_ci by the :c:func:`PyThreadState_EnterTracing` function. 12047db96d56Sopenharmony_ci 12057db96d56Sopenharmony_ci See also :c:func:`PyEval_SetTrace` and :c:func:`PyEval_SetProfile` 12067db96d56Sopenharmony_ci functions. 12077db96d56Sopenharmony_ci 12087db96d56Sopenharmony_ci .. versionadded:: 3.11 12097db96d56Sopenharmony_ci 12107db96d56Sopenharmony_ci 12117db96d56Sopenharmony_ci.. c:function:: PyInterpreterState* PyInterpreterState_Get(void) 12127db96d56Sopenharmony_ci 12137db96d56Sopenharmony_ci Get the current interpreter. 12147db96d56Sopenharmony_ci 12157db96d56Sopenharmony_ci Issue a fatal error if there no current Python thread state or no current 12167db96d56Sopenharmony_ci interpreter. It cannot return NULL. 12177db96d56Sopenharmony_ci 12187db96d56Sopenharmony_ci The caller must hold the GIL. 12197db96d56Sopenharmony_ci 12207db96d56Sopenharmony_ci .. versionadded:: 3.9 12217db96d56Sopenharmony_ci 12227db96d56Sopenharmony_ci 12237db96d56Sopenharmony_ci.. c:function:: int64_t PyInterpreterState_GetID(PyInterpreterState *interp) 12247db96d56Sopenharmony_ci 12257db96d56Sopenharmony_ci Return the interpreter's unique ID. If there was any error in doing 12267db96d56Sopenharmony_ci so then ``-1`` is returned and an error is set. 12277db96d56Sopenharmony_ci 12287db96d56Sopenharmony_ci The caller must hold the GIL. 12297db96d56Sopenharmony_ci 12307db96d56Sopenharmony_ci .. versionadded:: 3.7 12317db96d56Sopenharmony_ci 12327db96d56Sopenharmony_ci 12337db96d56Sopenharmony_ci.. c:function:: PyObject* PyInterpreterState_GetDict(PyInterpreterState *interp) 12347db96d56Sopenharmony_ci 12357db96d56Sopenharmony_ci Return a dictionary in which interpreter-specific data may be stored. 12367db96d56Sopenharmony_ci If this function returns ``NULL`` then no exception has been raised and 12377db96d56Sopenharmony_ci the caller should assume no interpreter-specific dict is available. 12387db96d56Sopenharmony_ci 12397db96d56Sopenharmony_ci This is not a replacement for :c:func:`PyModule_GetState()`, which 12407db96d56Sopenharmony_ci extensions should use to store interpreter-specific state information. 12417db96d56Sopenharmony_ci 12427db96d56Sopenharmony_ci .. versionadded:: 3.8 12437db96d56Sopenharmony_ci 12447db96d56Sopenharmony_ci.. c:type:: PyObject* (*_PyFrameEvalFunction)(PyThreadState *tstate, _PyInterpreterFrame *frame, int throwflag) 12457db96d56Sopenharmony_ci 12467db96d56Sopenharmony_ci Type of a frame evaluation function. 12477db96d56Sopenharmony_ci 12487db96d56Sopenharmony_ci The *throwflag* parameter is used by the ``throw()`` method of generators: 12497db96d56Sopenharmony_ci if non-zero, handle the current exception. 12507db96d56Sopenharmony_ci 12517db96d56Sopenharmony_ci .. versionchanged:: 3.9 12527db96d56Sopenharmony_ci The function now takes a *tstate* parameter. 12537db96d56Sopenharmony_ci 12547db96d56Sopenharmony_ci .. versionchanged:: 3.11 12557db96d56Sopenharmony_ci The *frame* parameter changed from ``PyFrameObject*`` to ``_PyInterpreterFrame*``. 12567db96d56Sopenharmony_ci 12577db96d56Sopenharmony_ci.. c:function:: _PyFrameEvalFunction _PyInterpreterState_GetEvalFrameFunc(PyInterpreterState *interp) 12587db96d56Sopenharmony_ci 12597db96d56Sopenharmony_ci Get the frame evaluation function. 12607db96d56Sopenharmony_ci 12617db96d56Sopenharmony_ci See the :pep:`523` "Adding a frame evaluation API to CPython". 12627db96d56Sopenharmony_ci 12637db96d56Sopenharmony_ci .. versionadded:: 3.9 12647db96d56Sopenharmony_ci 12657db96d56Sopenharmony_ci.. c:function:: void _PyInterpreterState_SetEvalFrameFunc(PyInterpreterState *interp, _PyFrameEvalFunction eval_frame) 12667db96d56Sopenharmony_ci 12677db96d56Sopenharmony_ci Set the frame evaluation function. 12687db96d56Sopenharmony_ci 12697db96d56Sopenharmony_ci See the :pep:`523` "Adding a frame evaluation API to CPython". 12707db96d56Sopenharmony_ci 12717db96d56Sopenharmony_ci .. versionadded:: 3.9 12727db96d56Sopenharmony_ci 12737db96d56Sopenharmony_ci 12747db96d56Sopenharmony_ci.. c:function:: PyObject* PyThreadState_GetDict() 12757db96d56Sopenharmony_ci 12767db96d56Sopenharmony_ci Return a dictionary in which extensions can store thread-specific state 12777db96d56Sopenharmony_ci information. Each extension should use a unique key to use to store state in 12787db96d56Sopenharmony_ci the dictionary. It is okay to call this function when no current thread state 12797db96d56Sopenharmony_ci is available. If this function returns ``NULL``, no exception has been raised and 12807db96d56Sopenharmony_ci the caller should assume no current thread state is available. 12817db96d56Sopenharmony_ci 12827db96d56Sopenharmony_ci 12837db96d56Sopenharmony_ci.. c:function:: int PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc) 12847db96d56Sopenharmony_ci 12857db96d56Sopenharmony_ci Asynchronously raise an exception in a thread. The *id* argument is the thread 12867db96d56Sopenharmony_ci id of the target thread; *exc* is the exception object to be raised. This 12877db96d56Sopenharmony_ci function does not steal any references to *exc*. To prevent naive misuse, you 12887db96d56Sopenharmony_ci must write your own C extension to call this. Must be called with the GIL held. 12897db96d56Sopenharmony_ci Returns the number of thread states modified; this is normally one, but will be 12907db96d56Sopenharmony_ci zero if the thread id isn't found. If *exc* is :const:`NULL`, the pending 12917db96d56Sopenharmony_ci exception (if any) for the thread is cleared. This raises no exceptions. 12927db96d56Sopenharmony_ci 12937db96d56Sopenharmony_ci .. versionchanged:: 3.7 12947db96d56Sopenharmony_ci The type of the *id* parameter changed from :c:expr:`long` to 12957db96d56Sopenharmony_ci :c:expr:`unsigned long`. 12967db96d56Sopenharmony_ci 12977db96d56Sopenharmony_ci.. c:function:: void PyEval_AcquireThread(PyThreadState *tstate) 12987db96d56Sopenharmony_ci 12997db96d56Sopenharmony_ci Acquire the global interpreter lock and set the current thread state to 13007db96d56Sopenharmony_ci *tstate*, which must not be ``NULL``. The lock must have been created earlier. 13017db96d56Sopenharmony_ci If this thread already has the lock, deadlock ensues. 13027db96d56Sopenharmony_ci 13037db96d56Sopenharmony_ci .. note:: 13047db96d56Sopenharmony_ci Calling this function from a thread when the runtime is finalizing 13057db96d56Sopenharmony_ci will terminate the thread, even if the thread was not created by Python. 13067db96d56Sopenharmony_ci You can use :c:func:`_Py_IsFinalizing` or :func:`sys.is_finalizing` to 13077db96d56Sopenharmony_ci check if the interpreter is in process of being finalized before calling 13087db96d56Sopenharmony_ci this function to avoid unwanted termination. 13097db96d56Sopenharmony_ci 13107db96d56Sopenharmony_ci .. versionchanged:: 3.8 13117db96d56Sopenharmony_ci Updated to be consistent with :c:func:`PyEval_RestoreThread`, 13127db96d56Sopenharmony_ci :c:func:`Py_END_ALLOW_THREADS`, and :c:func:`PyGILState_Ensure`, 13137db96d56Sopenharmony_ci and terminate the current thread if called while the interpreter is finalizing. 13147db96d56Sopenharmony_ci 13157db96d56Sopenharmony_ci :c:func:`PyEval_RestoreThread` is a higher-level function which is always 13167db96d56Sopenharmony_ci available (even when threads have not been initialized). 13177db96d56Sopenharmony_ci 13187db96d56Sopenharmony_ci 13197db96d56Sopenharmony_ci.. c:function:: void PyEval_ReleaseThread(PyThreadState *tstate) 13207db96d56Sopenharmony_ci 13217db96d56Sopenharmony_ci Reset the current thread state to ``NULL`` and release the global interpreter 13227db96d56Sopenharmony_ci lock. The lock must have been created earlier and must be held by the current 13237db96d56Sopenharmony_ci thread. The *tstate* argument, which must not be ``NULL``, is only used to check 13247db96d56Sopenharmony_ci that it represents the current thread state --- if it isn't, a fatal error is 13257db96d56Sopenharmony_ci reported. 13267db96d56Sopenharmony_ci 13277db96d56Sopenharmony_ci :c:func:`PyEval_SaveThread` is a higher-level function which is always 13287db96d56Sopenharmony_ci available (even when threads have not been initialized). 13297db96d56Sopenharmony_ci 13307db96d56Sopenharmony_ci 13317db96d56Sopenharmony_ci.. c:function:: void PyEval_AcquireLock() 13327db96d56Sopenharmony_ci 13337db96d56Sopenharmony_ci Acquire the global interpreter lock. The lock must have been created earlier. 13347db96d56Sopenharmony_ci If this thread already has the lock, a deadlock ensues. 13357db96d56Sopenharmony_ci 13367db96d56Sopenharmony_ci .. deprecated:: 3.2 13377db96d56Sopenharmony_ci This function does not update the current thread state. Please use 13387db96d56Sopenharmony_ci :c:func:`PyEval_RestoreThread` or :c:func:`PyEval_AcquireThread` 13397db96d56Sopenharmony_ci instead. 13407db96d56Sopenharmony_ci 13417db96d56Sopenharmony_ci .. note:: 13427db96d56Sopenharmony_ci Calling this function from a thread when the runtime is finalizing 13437db96d56Sopenharmony_ci will terminate the thread, even if the thread was not created by Python. 13447db96d56Sopenharmony_ci You can use :c:func:`_Py_IsFinalizing` or :func:`sys.is_finalizing` to 13457db96d56Sopenharmony_ci check if the interpreter is in process of being finalized before calling 13467db96d56Sopenharmony_ci this function to avoid unwanted termination. 13477db96d56Sopenharmony_ci 13487db96d56Sopenharmony_ci .. versionchanged:: 3.8 13497db96d56Sopenharmony_ci Updated to be consistent with :c:func:`PyEval_RestoreThread`, 13507db96d56Sopenharmony_ci :c:func:`Py_END_ALLOW_THREADS`, and :c:func:`PyGILState_Ensure`, 13517db96d56Sopenharmony_ci and terminate the current thread if called while the interpreter is finalizing. 13527db96d56Sopenharmony_ci 13537db96d56Sopenharmony_ci 13547db96d56Sopenharmony_ci.. c:function:: void PyEval_ReleaseLock() 13557db96d56Sopenharmony_ci 13567db96d56Sopenharmony_ci Release the global interpreter lock. The lock must have been created earlier. 13577db96d56Sopenharmony_ci 13587db96d56Sopenharmony_ci .. deprecated:: 3.2 13597db96d56Sopenharmony_ci This function does not update the current thread state. Please use 13607db96d56Sopenharmony_ci :c:func:`PyEval_SaveThread` or :c:func:`PyEval_ReleaseThread` 13617db96d56Sopenharmony_ci instead. 13627db96d56Sopenharmony_ci 13637db96d56Sopenharmony_ci 13647db96d56Sopenharmony_ci.. _sub-interpreter-support: 13657db96d56Sopenharmony_ci 13667db96d56Sopenharmony_ciSub-interpreter support 13677db96d56Sopenharmony_ci======================= 13687db96d56Sopenharmony_ci 13697db96d56Sopenharmony_ciWhile in most uses, you will only embed a single Python interpreter, there 13707db96d56Sopenharmony_ciare cases where you need to create several independent interpreters in the 13717db96d56Sopenharmony_cisame process and perhaps even in the same thread. Sub-interpreters allow 13727db96d56Sopenharmony_ciyou to do that. 13737db96d56Sopenharmony_ci 13747db96d56Sopenharmony_ciThe "main" interpreter is the first one created when the runtime initializes. 13757db96d56Sopenharmony_ciIt is usually the only Python interpreter in a process. Unlike sub-interpreters, 13767db96d56Sopenharmony_cithe main interpreter has unique process-global responsibilities like signal 13777db96d56Sopenharmony_cihandling. It is also responsible for execution during runtime initialization and 13787db96d56Sopenharmony_ciis usually the active interpreter during runtime finalization. The 13797db96d56Sopenharmony_ci:c:func:`PyInterpreterState_Main` function returns a pointer to its state. 13807db96d56Sopenharmony_ci 13817db96d56Sopenharmony_ciYou can switch between sub-interpreters using the :c:func:`PyThreadState_Swap` 13827db96d56Sopenharmony_cifunction. You can create and destroy them using the following functions: 13837db96d56Sopenharmony_ci 13847db96d56Sopenharmony_ci 13857db96d56Sopenharmony_ci.. c:function:: PyThreadState* Py_NewInterpreter() 13867db96d56Sopenharmony_ci 13877db96d56Sopenharmony_ci .. index:: 13887db96d56Sopenharmony_ci pair: module; builtins 13897db96d56Sopenharmony_ci pair: module; __main__ 13907db96d56Sopenharmony_ci pair: module; sys 13917db96d56Sopenharmony_ci single: stdout (in module sys) 13927db96d56Sopenharmony_ci single: stderr (in module sys) 13937db96d56Sopenharmony_ci single: stdin (in module sys) 13947db96d56Sopenharmony_ci 13957db96d56Sopenharmony_ci Create a new sub-interpreter. This is an (almost) totally separate environment 13967db96d56Sopenharmony_ci for the execution of Python code. In particular, the new interpreter has 13977db96d56Sopenharmony_ci separate, independent versions of all imported modules, including the 13987db96d56Sopenharmony_ci fundamental modules :mod:`builtins`, :mod:`__main__` and :mod:`sys`. The 13997db96d56Sopenharmony_ci table of loaded modules (``sys.modules``) and the module search path 14007db96d56Sopenharmony_ci (``sys.path``) are also separate. The new environment has no ``sys.argv`` 14017db96d56Sopenharmony_ci variable. It has new standard I/O stream file objects ``sys.stdin``, 14027db96d56Sopenharmony_ci ``sys.stdout`` and ``sys.stderr`` (however these refer to the same underlying 14037db96d56Sopenharmony_ci file descriptors). 14047db96d56Sopenharmony_ci 14057db96d56Sopenharmony_ci The return value points to the first thread state created in the new 14067db96d56Sopenharmony_ci sub-interpreter. This thread state is made in the current thread state. 14077db96d56Sopenharmony_ci Note that no actual thread is created; see the discussion of thread states 14087db96d56Sopenharmony_ci below. If creation of the new interpreter is unsuccessful, ``NULL`` is 14097db96d56Sopenharmony_ci returned; no exception is set since the exception state is stored in the 14107db96d56Sopenharmony_ci current thread state and there may not be a current thread state. (Like all 14117db96d56Sopenharmony_ci other Python/C API functions, the global interpreter lock must be held before 14127db96d56Sopenharmony_ci calling this function and is still held when it returns; however, unlike most 14137db96d56Sopenharmony_ci other Python/C API functions, there needn't be a current thread state on 14147db96d56Sopenharmony_ci entry.) 14157db96d56Sopenharmony_ci 14167db96d56Sopenharmony_ci .. index:: 14177db96d56Sopenharmony_ci single: Py_FinalizeEx() 14187db96d56Sopenharmony_ci single: Py_Initialize() 14197db96d56Sopenharmony_ci 14207db96d56Sopenharmony_ci Extension modules are shared between (sub-)interpreters as follows: 14217db96d56Sopenharmony_ci 14227db96d56Sopenharmony_ci * For modules using multi-phase initialization, 14237db96d56Sopenharmony_ci e.g. :c:func:`PyModule_FromDefAndSpec`, a separate module object is 14247db96d56Sopenharmony_ci created and initialized for each interpreter. 14257db96d56Sopenharmony_ci Only C-level static and global variables are shared between these 14267db96d56Sopenharmony_ci module objects. 14277db96d56Sopenharmony_ci 14287db96d56Sopenharmony_ci * For modules using single-phase initialization, 14297db96d56Sopenharmony_ci e.g. :c:func:`PyModule_Create`, the first time a particular extension 14307db96d56Sopenharmony_ci is imported, it is initialized normally, and a (shallow) copy of its 14317db96d56Sopenharmony_ci module's dictionary is squirreled away. 14327db96d56Sopenharmony_ci When the same extension is imported by another (sub-)interpreter, a new 14337db96d56Sopenharmony_ci module is initialized and filled with the contents of this copy; the 14347db96d56Sopenharmony_ci extension's ``init`` function is not called. 14357db96d56Sopenharmony_ci Objects in the module's dictionary thus end up shared across 14367db96d56Sopenharmony_ci (sub-)interpreters, which might cause unwanted behavior (see 14377db96d56Sopenharmony_ci `Bugs and caveats`_ below). 14387db96d56Sopenharmony_ci 14397db96d56Sopenharmony_ci Note that this is different from what happens when an extension is 14407db96d56Sopenharmony_ci imported after the interpreter has been completely re-initialized by 14417db96d56Sopenharmony_ci calling :c:func:`Py_FinalizeEx` and :c:func:`Py_Initialize`; in that 14427db96d56Sopenharmony_ci case, the extension's ``initmodule`` function *is* called again. 14437db96d56Sopenharmony_ci As with multi-phase initialization, this means that only C-level static 14447db96d56Sopenharmony_ci and global variables are shared between these modules. 14457db96d56Sopenharmony_ci 14467db96d56Sopenharmony_ci .. index:: single: close() (in module os) 14477db96d56Sopenharmony_ci 14487db96d56Sopenharmony_ci 14497db96d56Sopenharmony_ci.. c:function:: void Py_EndInterpreter(PyThreadState *tstate) 14507db96d56Sopenharmony_ci 14517db96d56Sopenharmony_ci .. index:: single: Py_FinalizeEx() 14527db96d56Sopenharmony_ci 14537db96d56Sopenharmony_ci Destroy the (sub-)interpreter represented by the given thread state. The given 14547db96d56Sopenharmony_ci thread state must be the current thread state. See the discussion of thread 14557db96d56Sopenharmony_ci states below. When the call returns, the current thread state is ``NULL``. All 14567db96d56Sopenharmony_ci thread states associated with this interpreter are destroyed. (The global 14577db96d56Sopenharmony_ci interpreter lock must be held before calling this function and is still held 14587db96d56Sopenharmony_ci when it returns.) :c:func:`Py_FinalizeEx` will destroy all sub-interpreters that 14597db96d56Sopenharmony_ci haven't been explicitly destroyed at that point. 14607db96d56Sopenharmony_ci 14617db96d56Sopenharmony_ci 14627db96d56Sopenharmony_ciBugs and caveats 14637db96d56Sopenharmony_ci---------------- 14647db96d56Sopenharmony_ci 14657db96d56Sopenharmony_ciBecause sub-interpreters (and the main interpreter) are part of the same 14667db96d56Sopenharmony_ciprocess, the insulation between them isn't perfect --- for example, using 14677db96d56Sopenharmony_cilow-level file operations like :func:`os.close` they can 14687db96d56Sopenharmony_ci(accidentally or maliciously) affect each other's open files. Because of the 14697db96d56Sopenharmony_ciway extensions are shared between (sub-)interpreters, some extensions may not 14707db96d56Sopenharmony_ciwork properly; this is especially likely when using single-phase initialization 14717db96d56Sopenharmony_cior (static) global variables. 14727db96d56Sopenharmony_ciIt is possible to insert objects created in one sub-interpreter into 14737db96d56Sopenharmony_cia namespace of another (sub-)interpreter; this should be avoided if possible. 14747db96d56Sopenharmony_ci 14757db96d56Sopenharmony_ciSpecial care should be taken to avoid sharing user-defined functions, 14767db96d56Sopenharmony_cimethods, instances or classes between sub-interpreters, since import 14777db96d56Sopenharmony_cioperations executed by such objects may affect the wrong (sub-)interpreter's 14787db96d56Sopenharmony_cidictionary of loaded modules. It is equally important to avoid sharing 14797db96d56Sopenharmony_ciobjects from which the above are reachable. 14807db96d56Sopenharmony_ci 14817db96d56Sopenharmony_ciAlso note that combining this functionality with ``PyGILState_*`` APIs 14827db96d56Sopenharmony_ciis delicate, because these APIs assume a bijection between Python thread states 14837db96d56Sopenharmony_ciand OS-level threads, an assumption broken by the presence of sub-interpreters. 14847db96d56Sopenharmony_ciIt is highly recommended that you don't switch sub-interpreters between a pair 14857db96d56Sopenharmony_ciof matching :c:func:`PyGILState_Ensure` and :c:func:`PyGILState_Release` calls. 14867db96d56Sopenharmony_ciFurthermore, extensions (such as :mod:`ctypes`) using these APIs to allow calling 14877db96d56Sopenharmony_ciof Python code from non-Python created threads will probably be broken when using 14887db96d56Sopenharmony_cisub-interpreters. 14897db96d56Sopenharmony_ci 14907db96d56Sopenharmony_ci 14917db96d56Sopenharmony_ciAsynchronous Notifications 14927db96d56Sopenharmony_ci========================== 14937db96d56Sopenharmony_ci 14947db96d56Sopenharmony_ciA mechanism is provided to make asynchronous notifications to the main 14957db96d56Sopenharmony_ciinterpreter thread. These notifications take the form of a function 14967db96d56Sopenharmony_cipointer and a void pointer argument. 14977db96d56Sopenharmony_ci 14987db96d56Sopenharmony_ci 14997db96d56Sopenharmony_ci.. c:function:: int Py_AddPendingCall(int (*func)(void *), void *arg) 15007db96d56Sopenharmony_ci 15017db96d56Sopenharmony_ci .. index:: single: Py_AddPendingCall() 15027db96d56Sopenharmony_ci 15037db96d56Sopenharmony_ci Schedule a function to be called from the main interpreter thread. On 15047db96d56Sopenharmony_ci success, ``0`` is returned and *func* is queued for being called in the 15057db96d56Sopenharmony_ci main thread. On failure, ``-1`` is returned without setting any exception. 15067db96d56Sopenharmony_ci 15077db96d56Sopenharmony_ci When successfully queued, *func* will be *eventually* called from the 15087db96d56Sopenharmony_ci main interpreter thread with the argument *arg*. It will be called 15097db96d56Sopenharmony_ci asynchronously with respect to normally running Python code, but with 15107db96d56Sopenharmony_ci both these conditions met: 15117db96d56Sopenharmony_ci 15127db96d56Sopenharmony_ci * on a :term:`bytecode` boundary; 15137db96d56Sopenharmony_ci * with the main thread holding the :term:`global interpreter lock` 15147db96d56Sopenharmony_ci (*func* can therefore use the full C API). 15157db96d56Sopenharmony_ci 15167db96d56Sopenharmony_ci *func* must return ``0`` on success, or ``-1`` on failure with an exception 15177db96d56Sopenharmony_ci set. *func* won't be interrupted to perform another asynchronous 15187db96d56Sopenharmony_ci notification recursively, but it can still be interrupted to switch 15197db96d56Sopenharmony_ci threads if the global interpreter lock is released. 15207db96d56Sopenharmony_ci 15217db96d56Sopenharmony_ci This function doesn't need a current thread state to run, and it doesn't 15227db96d56Sopenharmony_ci need the global interpreter lock. 15237db96d56Sopenharmony_ci 15247db96d56Sopenharmony_ci To call this function in a subinterpreter, the caller must hold the GIL. 15257db96d56Sopenharmony_ci Otherwise, the function *func* can be scheduled to be called from the wrong 15267db96d56Sopenharmony_ci interpreter. 15277db96d56Sopenharmony_ci 15287db96d56Sopenharmony_ci .. warning:: 15297db96d56Sopenharmony_ci This is a low-level function, only useful for very special cases. 15307db96d56Sopenharmony_ci There is no guarantee that *func* will be called as quick as 15317db96d56Sopenharmony_ci possible. If the main thread is busy executing a system call, 15327db96d56Sopenharmony_ci *func* won't be called before the system call returns. This 15337db96d56Sopenharmony_ci function is generally **not** suitable for calling Python code from 15347db96d56Sopenharmony_ci arbitrary C threads. Instead, use the :ref:`PyGILState API<gilstate>`. 15357db96d56Sopenharmony_ci 15367db96d56Sopenharmony_ci .. versionchanged:: 3.9 15377db96d56Sopenharmony_ci If this function is called in a subinterpreter, the function *func* is 15387db96d56Sopenharmony_ci now scheduled to be called from the subinterpreter, rather than being 15397db96d56Sopenharmony_ci called from the main interpreter. Each subinterpreter now has its own 15407db96d56Sopenharmony_ci list of scheduled calls. 15417db96d56Sopenharmony_ci 15427db96d56Sopenharmony_ci .. versionadded:: 3.1 15437db96d56Sopenharmony_ci 15447db96d56Sopenharmony_ci.. _profiling: 15457db96d56Sopenharmony_ci 15467db96d56Sopenharmony_ciProfiling and Tracing 15477db96d56Sopenharmony_ci===================== 15487db96d56Sopenharmony_ci 15497db96d56Sopenharmony_ci.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org> 15507db96d56Sopenharmony_ci 15517db96d56Sopenharmony_ci 15527db96d56Sopenharmony_ciThe Python interpreter provides some low-level support for attaching profiling 15537db96d56Sopenharmony_ciand execution tracing facilities. These are used for profiling, debugging, and 15547db96d56Sopenharmony_cicoverage analysis tools. 15557db96d56Sopenharmony_ci 15567db96d56Sopenharmony_ciThis C interface allows the profiling or tracing code to avoid the overhead of 15577db96d56Sopenharmony_cicalling through Python-level callable objects, making a direct C function call 15587db96d56Sopenharmony_ciinstead. The essential attributes of the facility have not changed; the 15597db96d56Sopenharmony_ciinterface allows trace functions to be installed per-thread, and the basic 15607db96d56Sopenharmony_cievents reported to the trace function are the same as had been reported to the 15617db96d56Sopenharmony_ciPython-level trace functions in previous versions. 15627db96d56Sopenharmony_ci 15637db96d56Sopenharmony_ci 15647db96d56Sopenharmony_ci.. c:type:: int (*Py_tracefunc)(PyObject *obj, PyFrameObject *frame, int what, PyObject *arg) 15657db96d56Sopenharmony_ci 15667db96d56Sopenharmony_ci The type of the trace function registered using :c:func:`PyEval_SetProfile` and 15677db96d56Sopenharmony_ci :c:func:`PyEval_SetTrace`. The first parameter is the object passed to the 15687db96d56Sopenharmony_ci registration function as *obj*, *frame* is the frame object to which the event 15697db96d56Sopenharmony_ci pertains, *what* is one of the constants :const:`PyTrace_CALL`, 15707db96d56Sopenharmony_ci :const:`PyTrace_EXCEPTION`, :const:`PyTrace_LINE`, :const:`PyTrace_RETURN`, 15717db96d56Sopenharmony_ci :const:`PyTrace_C_CALL`, :const:`PyTrace_C_EXCEPTION`, :const:`PyTrace_C_RETURN`, 15727db96d56Sopenharmony_ci or :const:`PyTrace_OPCODE`, and *arg* depends on the value of *what*: 15737db96d56Sopenharmony_ci 15747db96d56Sopenharmony_ci +------------------------------+----------------------------------------+ 15757db96d56Sopenharmony_ci | Value of *what* | Meaning of *arg* | 15767db96d56Sopenharmony_ci +==============================+========================================+ 15777db96d56Sopenharmony_ci | :const:`PyTrace_CALL` | Always :c:data:`Py_None`. | 15787db96d56Sopenharmony_ci +------------------------------+----------------------------------------+ 15797db96d56Sopenharmony_ci | :const:`PyTrace_EXCEPTION` | Exception information as returned by | 15807db96d56Sopenharmony_ci | | :func:`sys.exc_info`. | 15817db96d56Sopenharmony_ci +------------------------------+----------------------------------------+ 15827db96d56Sopenharmony_ci | :const:`PyTrace_LINE` | Always :c:data:`Py_None`. | 15837db96d56Sopenharmony_ci +------------------------------+----------------------------------------+ 15847db96d56Sopenharmony_ci | :const:`PyTrace_RETURN` | Value being returned to the caller, | 15857db96d56Sopenharmony_ci | | or ``NULL`` if caused by an exception. | 15867db96d56Sopenharmony_ci +------------------------------+----------------------------------------+ 15877db96d56Sopenharmony_ci | :const:`PyTrace_C_CALL` | Function object being called. | 15887db96d56Sopenharmony_ci +------------------------------+----------------------------------------+ 15897db96d56Sopenharmony_ci | :const:`PyTrace_C_EXCEPTION` | Function object being called. | 15907db96d56Sopenharmony_ci +------------------------------+----------------------------------------+ 15917db96d56Sopenharmony_ci | :const:`PyTrace_C_RETURN` | Function object being called. | 15927db96d56Sopenharmony_ci +------------------------------+----------------------------------------+ 15937db96d56Sopenharmony_ci | :const:`PyTrace_OPCODE` | Always :c:data:`Py_None`. | 15947db96d56Sopenharmony_ci +------------------------------+----------------------------------------+ 15957db96d56Sopenharmony_ci 15967db96d56Sopenharmony_ci.. c:var:: int PyTrace_CALL 15977db96d56Sopenharmony_ci 15987db96d56Sopenharmony_ci The value of the *what* parameter to a :c:type:`Py_tracefunc` function when a new 15997db96d56Sopenharmony_ci call to a function or method is being reported, or a new entry into a generator. 16007db96d56Sopenharmony_ci Note that the creation of the iterator for a generator function is not reported 16017db96d56Sopenharmony_ci as there is no control transfer to the Python bytecode in the corresponding 16027db96d56Sopenharmony_ci frame. 16037db96d56Sopenharmony_ci 16047db96d56Sopenharmony_ci 16057db96d56Sopenharmony_ci.. c:var:: int PyTrace_EXCEPTION 16067db96d56Sopenharmony_ci 16077db96d56Sopenharmony_ci The value of the *what* parameter to a :c:type:`Py_tracefunc` function when an 16087db96d56Sopenharmony_ci exception has been raised. The callback function is called with this value for 16097db96d56Sopenharmony_ci *what* when after any bytecode is processed after which the exception becomes 16107db96d56Sopenharmony_ci set within the frame being executed. The effect of this is that as exception 16117db96d56Sopenharmony_ci propagation causes the Python stack to unwind, the callback is called upon 16127db96d56Sopenharmony_ci return to each frame as the exception propagates. Only trace functions receives 16137db96d56Sopenharmony_ci these events; they are not needed by the profiler. 16147db96d56Sopenharmony_ci 16157db96d56Sopenharmony_ci 16167db96d56Sopenharmony_ci.. c:var:: int PyTrace_LINE 16177db96d56Sopenharmony_ci 16187db96d56Sopenharmony_ci The value passed as the *what* parameter to a :c:type:`Py_tracefunc` function 16197db96d56Sopenharmony_ci (but not a profiling function) when a line-number event is being reported. 16207db96d56Sopenharmony_ci It may be disabled for a frame by setting :attr:`f_trace_lines` to *0* on that frame. 16217db96d56Sopenharmony_ci 16227db96d56Sopenharmony_ci 16237db96d56Sopenharmony_ci.. c:var:: int PyTrace_RETURN 16247db96d56Sopenharmony_ci 16257db96d56Sopenharmony_ci The value for the *what* parameter to :c:type:`Py_tracefunc` functions when a 16267db96d56Sopenharmony_ci call is about to return. 16277db96d56Sopenharmony_ci 16287db96d56Sopenharmony_ci 16297db96d56Sopenharmony_ci.. c:var:: int PyTrace_C_CALL 16307db96d56Sopenharmony_ci 16317db96d56Sopenharmony_ci The value for the *what* parameter to :c:type:`Py_tracefunc` functions when a C 16327db96d56Sopenharmony_ci function is about to be called. 16337db96d56Sopenharmony_ci 16347db96d56Sopenharmony_ci 16357db96d56Sopenharmony_ci.. c:var:: int PyTrace_C_EXCEPTION 16367db96d56Sopenharmony_ci 16377db96d56Sopenharmony_ci The value for the *what* parameter to :c:type:`Py_tracefunc` functions when a C 16387db96d56Sopenharmony_ci function has raised an exception. 16397db96d56Sopenharmony_ci 16407db96d56Sopenharmony_ci 16417db96d56Sopenharmony_ci.. c:var:: int PyTrace_C_RETURN 16427db96d56Sopenharmony_ci 16437db96d56Sopenharmony_ci The value for the *what* parameter to :c:type:`Py_tracefunc` functions when a C 16447db96d56Sopenharmony_ci function has returned. 16457db96d56Sopenharmony_ci 16467db96d56Sopenharmony_ci 16477db96d56Sopenharmony_ci.. c:var:: int PyTrace_OPCODE 16487db96d56Sopenharmony_ci 16497db96d56Sopenharmony_ci The value for the *what* parameter to :c:type:`Py_tracefunc` functions (but not 16507db96d56Sopenharmony_ci profiling functions) when a new opcode is about to be executed. This event is 16517db96d56Sopenharmony_ci not emitted by default: it must be explicitly requested by setting 16527db96d56Sopenharmony_ci :attr:`f_trace_opcodes` to *1* on the frame. 16537db96d56Sopenharmony_ci 16547db96d56Sopenharmony_ci 16557db96d56Sopenharmony_ci.. c:function:: void PyEval_SetProfile(Py_tracefunc func, PyObject *obj) 16567db96d56Sopenharmony_ci 16577db96d56Sopenharmony_ci Set the profiler function to *func*. The *obj* parameter is passed to the 16587db96d56Sopenharmony_ci function as its first parameter, and may be any Python object, or ``NULL``. If 16597db96d56Sopenharmony_ci the profile function needs to maintain state, using a different value for *obj* 16607db96d56Sopenharmony_ci for each thread provides a convenient and thread-safe place to store it. The 16617db96d56Sopenharmony_ci profile function is called for all monitored events except :const:`PyTrace_LINE` 16627db96d56Sopenharmony_ci :const:`PyTrace_OPCODE` and :const:`PyTrace_EXCEPTION`. 16637db96d56Sopenharmony_ci 16647db96d56Sopenharmony_ci See also the :func:`sys.setprofile` function. 16657db96d56Sopenharmony_ci 16667db96d56Sopenharmony_ci The caller must hold the :term:`GIL`. 16677db96d56Sopenharmony_ci 16687db96d56Sopenharmony_ci 16697db96d56Sopenharmony_ci.. c:function:: void PyEval_SetTrace(Py_tracefunc func, PyObject *obj) 16707db96d56Sopenharmony_ci 16717db96d56Sopenharmony_ci Set the tracing function to *func*. This is similar to 16727db96d56Sopenharmony_ci :c:func:`PyEval_SetProfile`, except the tracing function does receive line-number 16737db96d56Sopenharmony_ci events and per-opcode events, but does not receive any event related to C function 16747db96d56Sopenharmony_ci objects being called. Any trace function registered using :c:func:`PyEval_SetTrace` 16757db96d56Sopenharmony_ci will not receive :const:`PyTrace_C_CALL`, :const:`PyTrace_C_EXCEPTION` or 16767db96d56Sopenharmony_ci :const:`PyTrace_C_RETURN` as a value for the *what* parameter. 16777db96d56Sopenharmony_ci 16787db96d56Sopenharmony_ci See also the :func:`sys.settrace` function. 16797db96d56Sopenharmony_ci 16807db96d56Sopenharmony_ci The caller must hold the :term:`GIL`. 16817db96d56Sopenharmony_ci 16827db96d56Sopenharmony_ci 16837db96d56Sopenharmony_ci.. _advanced-debugging: 16847db96d56Sopenharmony_ci 16857db96d56Sopenharmony_ciAdvanced Debugger Support 16867db96d56Sopenharmony_ci========================= 16877db96d56Sopenharmony_ci 16887db96d56Sopenharmony_ci.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org> 16897db96d56Sopenharmony_ci 16907db96d56Sopenharmony_ci 16917db96d56Sopenharmony_ciThese functions are only intended to be used by advanced debugging tools. 16927db96d56Sopenharmony_ci 16937db96d56Sopenharmony_ci 16947db96d56Sopenharmony_ci.. c:function:: PyInterpreterState* PyInterpreterState_Head() 16957db96d56Sopenharmony_ci 16967db96d56Sopenharmony_ci Return the interpreter state object at the head of the list of all such objects. 16977db96d56Sopenharmony_ci 16987db96d56Sopenharmony_ci 16997db96d56Sopenharmony_ci.. c:function:: PyInterpreterState* PyInterpreterState_Main() 17007db96d56Sopenharmony_ci 17017db96d56Sopenharmony_ci Return the main interpreter state object. 17027db96d56Sopenharmony_ci 17037db96d56Sopenharmony_ci 17047db96d56Sopenharmony_ci.. c:function:: PyInterpreterState* PyInterpreterState_Next(PyInterpreterState *interp) 17057db96d56Sopenharmony_ci 17067db96d56Sopenharmony_ci Return the next interpreter state object after *interp* from the list of all 17077db96d56Sopenharmony_ci such objects. 17087db96d56Sopenharmony_ci 17097db96d56Sopenharmony_ci 17107db96d56Sopenharmony_ci.. c:function:: PyThreadState * PyInterpreterState_ThreadHead(PyInterpreterState *interp) 17117db96d56Sopenharmony_ci 17127db96d56Sopenharmony_ci Return the pointer to the first :c:type:`PyThreadState` object in the list of 17137db96d56Sopenharmony_ci threads associated with the interpreter *interp*. 17147db96d56Sopenharmony_ci 17157db96d56Sopenharmony_ci 17167db96d56Sopenharmony_ci.. c:function:: PyThreadState* PyThreadState_Next(PyThreadState *tstate) 17177db96d56Sopenharmony_ci 17187db96d56Sopenharmony_ci Return the next thread state object after *tstate* from the list of all such 17197db96d56Sopenharmony_ci objects belonging to the same :c:type:`PyInterpreterState` object. 17207db96d56Sopenharmony_ci 17217db96d56Sopenharmony_ci 17227db96d56Sopenharmony_ci.. _thread-local-storage: 17237db96d56Sopenharmony_ci 17247db96d56Sopenharmony_ciThread Local Storage Support 17257db96d56Sopenharmony_ci============================ 17267db96d56Sopenharmony_ci 17277db96d56Sopenharmony_ci.. sectionauthor:: Masayuki Yamamoto <ma3yuki.8mamo10@gmail.com> 17287db96d56Sopenharmony_ci 17297db96d56Sopenharmony_ciThe Python interpreter provides low-level support for thread-local storage 17307db96d56Sopenharmony_ci(TLS) which wraps the underlying native TLS implementation to support the 17317db96d56Sopenharmony_ciPython-level thread local storage API (:class:`threading.local`). The 17327db96d56Sopenharmony_ciCPython C level APIs are similar to those offered by pthreads and Windows: 17337db96d56Sopenharmony_ciuse a thread key and functions to associate a :c:expr:`void*` value per 17347db96d56Sopenharmony_cithread. 17357db96d56Sopenharmony_ci 17367db96d56Sopenharmony_ciThe GIL does *not* need to be held when calling these functions; they supply 17377db96d56Sopenharmony_citheir own locking. 17387db96d56Sopenharmony_ci 17397db96d56Sopenharmony_ciNote that :file:`Python.h` does not include the declaration of the TLS APIs, 17407db96d56Sopenharmony_ciyou need to include :file:`pythread.h` to use thread-local storage. 17417db96d56Sopenharmony_ci 17427db96d56Sopenharmony_ci.. note:: 17437db96d56Sopenharmony_ci None of these API functions handle memory management on behalf of the 17447db96d56Sopenharmony_ci :c:expr:`void*` values. You need to allocate and deallocate them yourself. 17457db96d56Sopenharmony_ci If the :c:expr:`void*` values happen to be :c:expr:`PyObject*`, these 17467db96d56Sopenharmony_ci functions don't do refcount operations on them either. 17477db96d56Sopenharmony_ci 17487db96d56Sopenharmony_ci.. _thread-specific-storage-api: 17497db96d56Sopenharmony_ci 17507db96d56Sopenharmony_ciThread Specific Storage (TSS) API 17517db96d56Sopenharmony_ci--------------------------------- 17527db96d56Sopenharmony_ci 17537db96d56Sopenharmony_ciTSS API is introduced to supersede the use of the existing TLS API within the 17547db96d56Sopenharmony_ciCPython interpreter. This API uses a new type :c:type:`Py_tss_t` instead of 17557db96d56Sopenharmony_ci:c:expr:`int` to represent thread keys. 17567db96d56Sopenharmony_ci 17577db96d56Sopenharmony_ci.. versionadded:: 3.7 17587db96d56Sopenharmony_ci 17597db96d56Sopenharmony_ci.. seealso:: "A New C-API for Thread-Local Storage in CPython" (:pep:`539`) 17607db96d56Sopenharmony_ci 17617db96d56Sopenharmony_ci 17627db96d56Sopenharmony_ci.. c:type:: Py_tss_t 17637db96d56Sopenharmony_ci 17647db96d56Sopenharmony_ci This data structure represents the state of a thread key, the definition of 17657db96d56Sopenharmony_ci which may depend on the underlying TLS implementation, and it has an 17667db96d56Sopenharmony_ci internal field representing the key's initialization state. There are no 17677db96d56Sopenharmony_ci public members in this structure. 17687db96d56Sopenharmony_ci 17697db96d56Sopenharmony_ci When :ref:`Py_LIMITED_API <stable>` is not defined, static allocation of 17707db96d56Sopenharmony_ci this type by :c:macro:`Py_tss_NEEDS_INIT` is allowed. 17717db96d56Sopenharmony_ci 17727db96d56Sopenharmony_ci 17737db96d56Sopenharmony_ci.. c:macro:: Py_tss_NEEDS_INIT 17747db96d56Sopenharmony_ci 17757db96d56Sopenharmony_ci This macro expands to the initializer for :c:type:`Py_tss_t` variables. 17767db96d56Sopenharmony_ci Note that this macro won't be defined with :ref:`Py_LIMITED_API <stable>`. 17777db96d56Sopenharmony_ci 17787db96d56Sopenharmony_ci 17797db96d56Sopenharmony_ciDynamic Allocation 17807db96d56Sopenharmony_ci~~~~~~~~~~~~~~~~~~ 17817db96d56Sopenharmony_ci 17827db96d56Sopenharmony_ciDynamic allocation of the :c:type:`Py_tss_t`, required in extension modules 17837db96d56Sopenharmony_cibuilt with :ref:`Py_LIMITED_API <stable>`, where static allocation of this type 17847db96d56Sopenharmony_ciis not possible due to its implementation being opaque at build time. 17857db96d56Sopenharmony_ci 17867db96d56Sopenharmony_ci 17877db96d56Sopenharmony_ci.. c:function:: Py_tss_t* PyThread_tss_alloc() 17887db96d56Sopenharmony_ci 17897db96d56Sopenharmony_ci Return a value which is the same state as a value initialized with 17907db96d56Sopenharmony_ci :c:macro:`Py_tss_NEEDS_INIT`, or ``NULL`` in the case of dynamic allocation 17917db96d56Sopenharmony_ci failure. 17927db96d56Sopenharmony_ci 17937db96d56Sopenharmony_ci 17947db96d56Sopenharmony_ci.. c:function:: void PyThread_tss_free(Py_tss_t *key) 17957db96d56Sopenharmony_ci 17967db96d56Sopenharmony_ci Free the given *key* allocated by :c:func:`PyThread_tss_alloc`, after 17977db96d56Sopenharmony_ci first calling :c:func:`PyThread_tss_delete` to ensure any associated 17987db96d56Sopenharmony_ci thread locals have been unassigned. This is a no-op if the *key* 17997db96d56Sopenharmony_ci argument is ``NULL``. 18007db96d56Sopenharmony_ci 18017db96d56Sopenharmony_ci .. note:: 18027db96d56Sopenharmony_ci A freed key becomes a dangling pointer. You should reset the key to 18037db96d56Sopenharmony_ci ``NULL``. 18047db96d56Sopenharmony_ci 18057db96d56Sopenharmony_ci 18067db96d56Sopenharmony_ciMethods 18077db96d56Sopenharmony_ci~~~~~~~ 18087db96d56Sopenharmony_ci 18097db96d56Sopenharmony_ciThe parameter *key* of these functions must not be ``NULL``. Moreover, the 18107db96d56Sopenharmony_cibehaviors of :c:func:`PyThread_tss_set` and :c:func:`PyThread_tss_get` are 18117db96d56Sopenharmony_ciundefined if the given :c:type:`Py_tss_t` has not been initialized by 18127db96d56Sopenharmony_ci:c:func:`PyThread_tss_create`. 18137db96d56Sopenharmony_ci 18147db96d56Sopenharmony_ci 18157db96d56Sopenharmony_ci.. c:function:: int PyThread_tss_is_created(Py_tss_t *key) 18167db96d56Sopenharmony_ci 18177db96d56Sopenharmony_ci Return a non-zero value if the given :c:type:`Py_tss_t` has been initialized 18187db96d56Sopenharmony_ci by :c:func:`PyThread_tss_create`. 18197db96d56Sopenharmony_ci 18207db96d56Sopenharmony_ci 18217db96d56Sopenharmony_ci.. c:function:: int PyThread_tss_create(Py_tss_t *key) 18227db96d56Sopenharmony_ci 18237db96d56Sopenharmony_ci Return a zero value on successful initialization of a TSS key. The behavior 18247db96d56Sopenharmony_ci is undefined if the value pointed to by the *key* argument is not 18257db96d56Sopenharmony_ci initialized by :c:macro:`Py_tss_NEEDS_INIT`. This function can be called 18267db96d56Sopenharmony_ci repeatedly on the same key -- calling it on an already initialized key is a 18277db96d56Sopenharmony_ci no-op and immediately returns success. 18287db96d56Sopenharmony_ci 18297db96d56Sopenharmony_ci 18307db96d56Sopenharmony_ci.. c:function:: void PyThread_tss_delete(Py_tss_t *key) 18317db96d56Sopenharmony_ci 18327db96d56Sopenharmony_ci Destroy a TSS key to forget the values associated with the key across all 18337db96d56Sopenharmony_ci threads, and change the key's initialization state to uninitialized. A 18347db96d56Sopenharmony_ci destroyed key is able to be initialized again by 18357db96d56Sopenharmony_ci :c:func:`PyThread_tss_create`. This function can be called repeatedly on 18367db96d56Sopenharmony_ci the same key -- calling it on an already destroyed key is a no-op. 18377db96d56Sopenharmony_ci 18387db96d56Sopenharmony_ci 18397db96d56Sopenharmony_ci.. c:function:: int PyThread_tss_set(Py_tss_t *key, void *value) 18407db96d56Sopenharmony_ci 18417db96d56Sopenharmony_ci Return a zero value to indicate successfully associating a :c:expr:`void*` 18427db96d56Sopenharmony_ci value with a TSS key in the current thread. Each thread has a distinct 18437db96d56Sopenharmony_ci mapping of the key to a :c:expr:`void*` value. 18447db96d56Sopenharmony_ci 18457db96d56Sopenharmony_ci 18467db96d56Sopenharmony_ci.. c:function:: void* PyThread_tss_get(Py_tss_t *key) 18477db96d56Sopenharmony_ci 18487db96d56Sopenharmony_ci Return the :c:expr:`void*` value associated with a TSS key in the current 18497db96d56Sopenharmony_ci thread. This returns ``NULL`` if no value is associated with the key in the 18507db96d56Sopenharmony_ci current thread. 18517db96d56Sopenharmony_ci 18527db96d56Sopenharmony_ci 18537db96d56Sopenharmony_ci.. _thread-local-storage-api: 18547db96d56Sopenharmony_ci 18557db96d56Sopenharmony_ciThread Local Storage (TLS) API 18567db96d56Sopenharmony_ci------------------------------ 18577db96d56Sopenharmony_ci 18587db96d56Sopenharmony_ci.. deprecated:: 3.7 18597db96d56Sopenharmony_ci This API is superseded by 18607db96d56Sopenharmony_ci :ref:`Thread Specific Storage (TSS) API <thread-specific-storage-api>`. 18617db96d56Sopenharmony_ci 18627db96d56Sopenharmony_ci.. note:: 18637db96d56Sopenharmony_ci This version of the API does not support platforms where the native TLS key 18647db96d56Sopenharmony_ci is defined in a way that cannot be safely cast to ``int``. On such platforms, 18657db96d56Sopenharmony_ci :c:func:`PyThread_create_key` will return immediately with a failure status, 18667db96d56Sopenharmony_ci and the other TLS functions will all be no-ops on such platforms. 18677db96d56Sopenharmony_ci 18687db96d56Sopenharmony_ciDue to the compatibility problem noted above, this version of the API should not 18697db96d56Sopenharmony_cibe used in new code. 18707db96d56Sopenharmony_ci 18717db96d56Sopenharmony_ci.. c:function:: int PyThread_create_key() 18727db96d56Sopenharmony_ci.. c:function:: void PyThread_delete_key(int key) 18737db96d56Sopenharmony_ci.. c:function:: int PyThread_set_key_value(int key, void *value) 18747db96d56Sopenharmony_ci.. c:function:: void* PyThread_get_key_value(int key) 18757db96d56Sopenharmony_ci.. c:function:: void PyThread_delete_key_value(int key) 18767db96d56Sopenharmony_ci.. c:function:: void PyThread_ReInitTLS() 18777db96d56Sopenharmony_ci 1878