Stéphane Wirtel | cbb6484 | 2019-05-17 11:55:34 +0200 | [diff] [blame] | 1 | .. highlight:: c |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2 | |
| 3 | |
| 4 | .. _initialization: |
| 5 | |
| 6 | ***************************************** |
| 7 | Initialization, Finalization, and Threads |
| 8 | ***************************************** |
| 9 | |
Victor Stinner | 1beb7c3 | 2019-08-23 17:59:12 +0100 | [diff] [blame] | 10 | See also :ref:`Python Initialization Configuration <init-config>`. |
| 11 | |
Victor Stinner | 84c4b19 | 2017-11-24 22:30:27 +0100 | [diff] [blame] | 12 | .. _pre-init-safe: |
| 13 | |
| 14 | Before Python Initialization |
| 15 | ============================ |
| 16 | |
| 17 | In an application embedding Python, the :c:func:`Py_Initialize` function must |
| 18 | be called before using any other Python/C API functions; with the exception of |
| 19 | a few functions and the :ref:`global configuration variables |
| 20 | <global-conf-vars>`. |
| 21 | |
| 22 | The following functions can be safely called before Python is initialized: |
| 23 | |
| 24 | * Configuration functions: |
| 25 | |
| 26 | * :c:func:`PyImport_AppendInittab` |
| 27 | * :c:func:`PyImport_ExtendInittab` |
| 28 | * :c:func:`PyInitFrozenExtensions` |
| 29 | * :c:func:`PyMem_SetAllocator` |
| 30 | * :c:func:`PyMem_SetupDebugHooks` |
| 31 | * :c:func:`PyObject_SetArenaAllocator` |
| 32 | * :c:func:`Py_SetPath` |
| 33 | * :c:func:`Py_SetProgramName` |
| 34 | * :c:func:`Py_SetPythonHome` |
| 35 | * :c:func:`Py_SetStandardStreamEncoding` |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 36 | * :c:func:`PySys_AddWarnOption` |
| 37 | * :c:func:`PySys_AddXOption` |
| 38 | * :c:func:`PySys_ResetWarnOptions` |
Victor Stinner | 84c4b19 | 2017-11-24 22:30:27 +0100 | [diff] [blame] | 39 | |
| 40 | * Informative functions: |
| 41 | |
Nick Coghlan | ddbb978 | 2019-03-30 21:24:05 +1000 | [diff] [blame] | 42 | * :c:func:`Py_IsInitialized` |
Victor Stinner | 84c4b19 | 2017-11-24 22:30:27 +0100 | [diff] [blame] | 43 | * :c:func:`PyMem_GetAllocator` |
| 44 | * :c:func:`PyObject_GetArenaAllocator` |
| 45 | * :c:func:`Py_GetBuildInfo` |
| 46 | * :c:func:`Py_GetCompiler` |
| 47 | * :c:func:`Py_GetCopyright` |
| 48 | * :c:func:`Py_GetPlatform` |
Victor Stinner | 84c4b19 | 2017-11-24 22:30:27 +0100 | [diff] [blame] | 49 | * :c:func:`Py_GetVersion` |
| 50 | |
| 51 | * Utilities: |
| 52 | |
| 53 | * :c:func:`Py_DecodeLocale` |
| 54 | |
| 55 | * Memory allocators: |
| 56 | |
| 57 | * :c:func:`PyMem_RawMalloc` |
| 58 | * :c:func:`PyMem_RawRealloc` |
| 59 | * :c:func:`PyMem_RawCalloc` |
| 60 | * :c:func:`PyMem_RawFree` |
| 61 | |
| 62 | .. note:: |
| 63 | |
| 64 | The following functions **should not be called** before |
| 65 | :c:func:`Py_Initialize`: :c:func:`Py_EncodeLocale`, :c:func:`Py_GetPath`, |
Victor Stinner | b4d1e1f | 2017-11-30 22:05:00 +0100 | [diff] [blame] | 66 | :c:func:`Py_GetPrefix`, :c:func:`Py_GetExecPrefix`, |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 67 | :c:func:`Py_GetProgramFullPath`, :c:func:`Py_GetPythonHome`, |
| 68 | :c:func:`Py_GetProgramName` and :c:func:`PyEval_InitThreads`. |
Victor Stinner | 84c4b19 | 2017-11-24 22:30:27 +0100 | [diff] [blame] | 69 | |
| 70 | |
| 71 | .. _global-conf-vars: |
| 72 | |
| 73 | Global configuration variables |
| 74 | ============================== |
| 75 | |
| 76 | Python has variables for the global configuration to control different features |
| 77 | and options. By default, these flags are controlled by :ref:`command line |
| 78 | options <using-on-interface-options>`. |
| 79 | |
| 80 | When a flag is set by an option, the value of the flag is the number of times |
| 81 | that the option was set. For example, ``-b`` sets :c:data:`Py_BytesWarningFlag` |
| 82 | to 1 and ``-bb`` sets :c:data:`Py_BytesWarningFlag` to 2. |
| 83 | |
Victor Stinner | 474652f | 2020-08-13 22:11:50 +0200 | [diff] [blame] | 84 | .. c:var:: int Py_BytesWarningFlag |
Victor Stinner | 84c4b19 | 2017-11-24 22:30:27 +0100 | [diff] [blame] | 85 | |
| 86 | Issue a warning when comparing :class:`bytes` or :class:`bytearray` with |
| 87 | :class:`str` or :class:`bytes` with :class:`int`. Issue an error if greater |
| 88 | or equal to ``2``. |
| 89 | |
| 90 | Set by the :option:`-b` option. |
| 91 | |
Victor Stinner | 474652f | 2020-08-13 22:11:50 +0200 | [diff] [blame] | 92 | .. c:var:: int Py_DebugFlag |
Victor Stinner | 84c4b19 | 2017-11-24 22:30:27 +0100 | [diff] [blame] | 93 | |
| 94 | Turn on parser debugging output (for expert only, depending on compilation |
| 95 | options). |
| 96 | |
| 97 | Set by the :option:`-d` option and the :envvar:`PYTHONDEBUG` environment |
| 98 | variable. |
| 99 | |
Victor Stinner | 474652f | 2020-08-13 22:11:50 +0200 | [diff] [blame] | 100 | .. c:var:: int Py_DontWriteBytecodeFlag |
Victor Stinner | 84c4b19 | 2017-11-24 22:30:27 +0100 | [diff] [blame] | 101 | |
| 102 | If set to non-zero, Python won't try to write ``.pyc`` files on the |
| 103 | import of source modules. |
| 104 | |
| 105 | Set by the :option:`-B` option and the :envvar:`PYTHONDONTWRITEBYTECODE` |
| 106 | environment variable. |
| 107 | |
Victor Stinner | 474652f | 2020-08-13 22:11:50 +0200 | [diff] [blame] | 108 | .. c:var:: int Py_FrozenFlag |
Victor Stinner | 84c4b19 | 2017-11-24 22:30:27 +0100 | [diff] [blame] | 109 | |
| 110 | Suppress error messages when calculating the module search path in |
| 111 | :c:func:`Py_GetPath`. |
| 112 | |
| 113 | Private flag used by ``_freeze_importlib`` and ``frozenmain`` programs. |
| 114 | |
Victor Stinner | 474652f | 2020-08-13 22:11:50 +0200 | [diff] [blame] | 115 | .. c:var:: int Py_HashRandomizationFlag |
Victor Stinner | 84c4b19 | 2017-11-24 22:30:27 +0100 | [diff] [blame] | 116 | |
| 117 | Set to ``1`` if the :envvar:`PYTHONHASHSEED` environment variable is set to |
| 118 | a non-empty string. |
| 119 | |
| 120 | If the flag is non-zero, read the :envvar:`PYTHONHASHSEED` environment |
| 121 | variable to initialize the secret hash seed. |
| 122 | |
Victor Stinner | 474652f | 2020-08-13 22:11:50 +0200 | [diff] [blame] | 123 | .. c:var:: int Py_IgnoreEnvironmentFlag |
Victor Stinner | 84c4b19 | 2017-11-24 22:30:27 +0100 | [diff] [blame] | 124 | |
| 125 | Ignore all :envvar:`PYTHON*` environment variables, e.g. |
| 126 | :envvar:`PYTHONPATH` and :envvar:`PYTHONHOME`, that might be set. |
| 127 | |
| 128 | Set by the :option:`-E` and :option:`-I` options. |
| 129 | |
Victor Stinner | 474652f | 2020-08-13 22:11:50 +0200 | [diff] [blame] | 130 | .. c:var:: int Py_InspectFlag |
Victor Stinner | 84c4b19 | 2017-11-24 22:30:27 +0100 | [diff] [blame] | 131 | |
| 132 | When a script is passed as first argument or the :option:`-c` option is used, |
| 133 | enter interactive mode after executing the script or the command, even when |
| 134 | :data:`sys.stdin` does not appear to be a terminal. |
| 135 | |
| 136 | Set by the :option:`-i` option and the :envvar:`PYTHONINSPECT` environment |
| 137 | variable. |
| 138 | |
Victor Stinner | 474652f | 2020-08-13 22:11:50 +0200 | [diff] [blame] | 139 | .. c:var:: int Py_InteractiveFlag |
Victor Stinner | 84c4b19 | 2017-11-24 22:30:27 +0100 | [diff] [blame] | 140 | |
| 141 | Set by the :option:`-i` option. |
| 142 | |
Victor Stinner | 474652f | 2020-08-13 22:11:50 +0200 | [diff] [blame] | 143 | .. c:var:: int Py_IsolatedFlag |
Victor Stinner | 84c4b19 | 2017-11-24 22:30:27 +0100 | [diff] [blame] | 144 | |
| 145 | Run Python in isolated mode. In isolated mode :data:`sys.path` contains |
| 146 | neither the script's directory nor the user's site-packages directory. |
| 147 | |
| 148 | Set by the :option:`-I` option. |
| 149 | |
| 150 | .. versionadded:: 3.4 |
| 151 | |
Victor Stinner | 474652f | 2020-08-13 22:11:50 +0200 | [diff] [blame] | 152 | .. c:var:: int Py_LegacyWindowsFSEncodingFlag |
Victor Stinner | 84c4b19 | 2017-11-24 22:30:27 +0100 | [diff] [blame] | 153 | |
Victor Stinner | 4b9aad4 | 2020-11-02 16:49:54 +0100 | [diff] [blame] | 154 | If the flag is non-zero, use the ``mbcs`` encoding with ``replace`` error |
| 155 | handler, instead of the UTF-8 encoding with ``surrogatepass`` error handler, |
| 156 | for the :term:`filesystem encoding and error handler`. |
Victor Stinner | 84c4b19 | 2017-11-24 22:30:27 +0100 | [diff] [blame] | 157 | |
| 158 | Set to ``1`` if the :envvar:`PYTHONLEGACYWINDOWSFSENCODING` environment |
| 159 | variable is set to a non-empty string. |
| 160 | |
| 161 | See :pep:`529` for more details. |
| 162 | |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 163 | .. availability:: Windows. |
Victor Stinner | 84c4b19 | 2017-11-24 22:30:27 +0100 | [diff] [blame] | 164 | |
Victor Stinner | 474652f | 2020-08-13 22:11:50 +0200 | [diff] [blame] | 165 | .. c:var:: int Py_LegacyWindowsStdioFlag |
Victor Stinner | 84c4b19 | 2017-11-24 22:30:27 +0100 | [diff] [blame] | 166 | |
| 167 | If the flag is non-zero, use :class:`io.FileIO` instead of |
| 168 | :class:`WindowsConsoleIO` for :mod:`sys` standard streams. |
| 169 | |
| 170 | Set to ``1`` if the :envvar:`PYTHONLEGACYWINDOWSSTDIO` environment |
| 171 | variable is set to a non-empty string. |
| 172 | |
| 173 | See :pep:`528` for more details. |
| 174 | |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 175 | .. availability:: Windows. |
Victor Stinner | 84c4b19 | 2017-11-24 22:30:27 +0100 | [diff] [blame] | 176 | |
Victor Stinner | 474652f | 2020-08-13 22:11:50 +0200 | [diff] [blame] | 177 | .. c:var:: int Py_NoSiteFlag |
Victor Stinner | 84c4b19 | 2017-11-24 22:30:27 +0100 | [diff] [blame] | 178 | |
| 179 | Disable the import of the module :mod:`site` and the site-dependent |
| 180 | manipulations of :data:`sys.path` that it entails. Also disable these |
| 181 | manipulations if :mod:`site` is explicitly imported later (call |
| 182 | :func:`site.main` if you want them to be triggered). |
| 183 | |
| 184 | Set by the :option:`-S` option. |
| 185 | |
Victor Stinner | 474652f | 2020-08-13 22:11:50 +0200 | [diff] [blame] | 186 | .. c:var:: int Py_NoUserSiteDirectory |
Victor Stinner | 84c4b19 | 2017-11-24 22:30:27 +0100 | [diff] [blame] | 187 | |
| 188 | Don't add the :data:`user site-packages directory <site.USER_SITE>` to |
| 189 | :data:`sys.path`. |
| 190 | |
| 191 | Set by the :option:`-s` and :option:`-I` options, and the |
| 192 | :envvar:`PYTHONNOUSERSITE` environment variable. |
| 193 | |
Victor Stinner | 474652f | 2020-08-13 22:11:50 +0200 | [diff] [blame] | 194 | .. c:var:: int Py_OptimizeFlag |
Victor Stinner | 84c4b19 | 2017-11-24 22:30:27 +0100 | [diff] [blame] | 195 | |
| 196 | Set by the :option:`-O` option and the :envvar:`PYTHONOPTIMIZE` environment |
| 197 | variable. |
| 198 | |
Victor Stinner | 474652f | 2020-08-13 22:11:50 +0200 | [diff] [blame] | 199 | .. c:var:: int Py_QuietFlag |
Victor Stinner | 84c4b19 | 2017-11-24 22:30:27 +0100 | [diff] [blame] | 200 | |
| 201 | Don't display the copyright and version messages even in interactive mode. |
| 202 | |
| 203 | Set by the :option:`-q` option. |
| 204 | |
| 205 | .. versionadded:: 3.2 |
| 206 | |
Victor Stinner | 474652f | 2020-08-13 22:11:50 +0200 | [diff] [blame] | 207 | .. c:var:: int Py_UnbufferedStdioFlag |
Victor Stinner | 84c4b19 | 2017-11-24 22:30:27 +0100 | [diff] [blame] | 208 | |
| 209 | Force the stdout and stderr streams to be unbuffered. |
| 210 | |
| 211 | Set by the :option:`-u` option and the :envvar:`PYTHONUNBUFFERED` |
| 212 | environment variable. |
| 213 | |
Victor Stinner | 474652f | 2020-08-13 22:11:50 +0200 | [diff] [blame] | 214 | .. c:var:: int Py_VerboseFlag |
Victor Stinner | 84c4b19 | 2017-11-24 22:30:27 +0100 | [diff] [blame] | 215 | |
| 216 | Print a message each time a module is initialized, showing the place |
| 217 | (filename or built-in module) from which it is loaded. If greater or equal |
| 218 | to ``2``, print a message for each file that is checked for when |
| 219 | searching for a module. Also provides information on module cleanup at exit. |
| 220 | |
| 221 | Set by the :option:`-v` option and the :envvar:`PYTHONVERBOSE` environment |
| 222 | variable. |
| 223 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 224 | |
Antoine Pitrou | 8b50b83 | 2011-01-15 11:57:42 +0000 | [diff] [blame] | 225 | Initializing and finalizing the interpreter |
| 226 | =========================================== |
| 227 | |
| 228 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 229 | .. c:function:: void Py_Initialize() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 230 | |
| 231 | .. index:: |
| 232 | single: Py_SetProgramName() |
| 233 | single: PyEval_InitThreads() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 234 | single: modules (in module sys) |
| 235 | single: path (in module sys) |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 236 | module: builtins |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 237 | module: __main__ |
| 238 | module: sys |
| 239 | triple: module; search; path |
| 240 | single: PySys_SetArgv() |
Antoine Pitrou | f978fac | 2010-05-21 17:25:34 +0000 | [diff] [blame] | 241 | single: PySys_SetArgvEx() |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 242 | single: Py_FinalizeEx() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 243 | |
Victor Stinner | 84c4b19 | 2017-11-24 22:30:27 +0100 | [diff] [blame] | 244 | Initialize the Python interpreter. In an application embedding Python, |
| 245 | this should be called before using any other Python/C API functions; see |
| 246 | :ref:`Before Python Initialization <pre-init-safe>` for the few exceptions. |
| 247 | |
| 248 | This initializes |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 249 | the table of loaded modules (``sys.modules``), and creates the fundamental |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 250 | modules :mod:`builtins`, :mod:`__main__` and :mod:`sys`. It also initializes |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 251 | the module search path (``sys.path``). It does not set ``sys.argv``; use |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 252 | :c:func:`PySys_SetArgvEx` for that. This is a no-op when called for a second time |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 253 | (without calling :c:func:`Py_FinalizeEx` first). There is no return value; it is a |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 254 | fatal error if the initialization fails. |
| 255 | |
Steve Dower | de02b08 | 2016-09-09 11:46:37 -0700 | [diff] [blame] | 256 | .. note:: |
| 257 | On Windows, changes the console mode from ``O_TEXT`` to ``O_BINARY``, which will |
| 258 | also affect non-Python uses of the console using the C Runtime. |
| 259 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 260 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 261 | .. c:function:: void Py_InitializeEx(int initsigs) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 262 | |
Serhiy Storchaka | 1ecf7d2 | 2016-10-27 21:41:19 +0300 | [diff] [blame] | 263 | This function works like :c:func:`Py_Initialize` if *initsigs* is ``1``. If |
| 264 | *initsigs* is ``0``, it skips initialization registration of signal handlers, which |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 265 | might be useful when Python is embedded. |
| 266 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 267 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 268 | .. c:function:: int Py_IsInitialized() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 269 | |
| 270 | Return true (nonzero) when the Python interpreter has been initialized, false |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 271 | (zero) if not. After :c:func:`Py_FinalizeEx` is called, this returns false until |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 272 | :c:func:`Py_Initialize` is called again. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 273 | |
| 274 | |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 275 | .. c:function:: int Py_FinalizeEx() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 276 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 277 | Undo all initializations made by :c:func:`Py_Initialize` and subsequent use of |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 278 | Python/C API functions, and destroy all sub-interpreters (see |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 279 | :c:func:`Py_NewInterpreter` below) that were created and not yet destroyed since |
| 280 | the last call to :c:func:`Py_Initialize`. Ideally, this frees all memory |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 281 | allocated by the Python interpreter. This is a no-op when called for a second |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 282 | time (without calling :c:func:`Py_Initialize` again first). Normally the |
Serhiy Storchaka | 5bb0005 | 2018-02-09 13:31:19 +0200 | [diff] [blame] | 283 | return value is ``0``. If there were errors during finalization |
| 284 | (flushing buffered data), ``-1`` is returned. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 285 | |
| 286 | This function is provided for a number of reasons. An embedding application |
| 287 | might want to restart Python without having to restart the application itself. |
| 288 | An application that has loaded the Python interpreter from a dynamically |
| 289 | loadable library (or DLL) might want to free all memory allocated by Python |
| 290 | before unloading the DLL. During a hunt for memory leaks in an application a |
| 291 | developer might want to free all memory allocated by Python before exiting from |
| 292 | the application. |
| 293 | |
| 294 | **Bugs and caveats:** The destruction of modules and objects in modules is done |
| 295 | in random order; this may cause destructors (:meth:`__del__` methods) to fail |
| 296 | when they depend on other objects (even functions) or modules. Dynamically |
| 297 | loaded extension modules loaded by Python are not unloaded. Small amounts of |
| 298 | memory allocated by the Python interpreter may not be freed (if you find a leak, |
| 299 | please report it). Memory tied up in circular references between objects is not |
| 300 | freed. Some memory allocated by extension modules may not be freed. Some |
| 301 | extensions may not work properly if their initialization routine is called more |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 302 | than once; this can happen if an application calls :c:func:`Py_Initialize` and |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 303 | :c:func:`Py_FinalizeEx` more than once. |
| 304 | |
Christian Heimes | ed4b321 | 2019-09-12 15:13:02 +0200 | [diff] [blame] | 305 | .. audit-event:: cpython._PySys_ClearAuditHooks "" c.Py_FinalizeEx |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 306 | |
Christian Heimes | ed4b321 | 2019-09-12 15:13:02 +0200 | [diff] [blame] | 307 | .. versionadded:: 3.6 |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 308 | |
| 309 | .. c:function:: void Py_Finalize() |
| 310 | |
| 311 | This is a backwards-compatible version of :c:func:`Py_FinalizeEx` that |
| 312 | disregards the return value. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 313 | |
| 314 | |
Antoine Pitrou | 8b50b83 | 2011-01-15 11:57:42 +0000 | [diff] [blame] | 315 | Process-wide parameters |
| 316 | ======================= |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 317 | |
| 318 | |
Serhiy Storchaka | 03863d2 | 2015-06-21 17:11:21 +0300 | [diff] [blame] | 319 | .. c:function:: int Py_SetStandardStreamEncoding(const char *encoding, const char *errors) |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 320 | |
| 321 | .. index:: |
| 322 | single: Py_Initialize() |
| 323 | single: main() |
| 324 | triple: stdin; stdout; sdterr |
| 325 | |
Nick Coghlan | 1805a62 | 2013-10-18 23:11:47 +1000 | [diff] [blame] | 326 | This function should be called before :c:func:`Py_Initialize`, if it is |
| 327 | called at all. It specifies which encoding and error handling to use |
| 328 | with standard IO, with the same meanings as in :func:`str.encode`. |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 329 | |
| 330 | It overrides :envvar:`PYTHONIOENCODING` values, and allows embedding code |
Nick Coghlan | 1805a62 | 2013-10-18 23:11:47 +1000 | [diff] [blame] | 331 | to control IO encoding when the environment variable does not work. |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 332 | |
Serhiy Storchaka | e835b31 | 2019-10-30 21:37:16 +0200 | [diff] [blame] | 333 | *encoding* and/or *errors* may be ``NULL`` to use |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 334 | :envvar:`PYTHONIOENCODING` and/or default values (depending on other |
| 335 | settings). |
| 336 | |
| 337 | Note that :data:`sys.stderr` always uses the "backslashreplace" error |
| 338 | handler, regardless of this (or any other) setting. |
| 339 | |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 340 | If :c:func:`Py_FinalizeEx` is called, this function will need to be called |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 341 | again in order to affect subsequent calls to :c:func:`Py_Initialize`. |
| 342 | |
Serhiy Storchaka | 1ecf7d2 | 2016-10-27 21:41:19 +0300 | [diff] [blame] | 343 | Returns ``0`` if successful, a nonzero value on error (e.g. calling after the |
Nick Coghlan | 1805a62 | 2013-10-18 23:11:47 +1000 | [diff] [blame] | 344 | interpreter has already been initialized). |
| 345 | |
| 346 | .. versionadded:: 3.4 |
Nick Coghlan | 7d270ee | 2013-10-17 22:35:35 +1000 | [diff] [blame] | 347 | |
| 348 | |
Serhiy Storchaka | 4ae06c5 | 2017-12-12 13:55:04 +0200 | [diff] [blame] | 349 | .. c:function:: void Py_SetProgramName(const wchar_t *name) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 350 | |
| 351 | .. index:: |
| 352 | single: Py_Initialize() |
| 353 | single: main() |
| 354 | single: Py_GetPath() |
| 355 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 356 | This function should be called before :c:func:`Py_Initialize` is called for |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 357 | the first time, if it is called at all. It tells the interpreter the value |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 358 | of the ``argv[0]`` argument to the :c:func:`main` function of the program |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 359 | (converted to wide characters). |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 360 | This is used by :c:func:`Py_GetPath` and some other functions below to find |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 361 | the Python run-time libraries relative to the interpreter executable. The |
| 362 | default value is ``'python'``. The argument should point to a |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 363 | zero-terminated wide character string in static storage whose contents will not |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 364 | change for the duration of the program's execution. No code in the Python |
| 365 | interpreter will change the contents of this storage. |
| 366 | |
Victor Stinner | 25e014b | 2014-08-01 12:28:49 +0200 | [diff] [blame] | 367 | Use :c:func:`Py_DecodeLocale` to decode a bytes string to get a |
| 368 | :c:type:`wchar_*` string. |
| 369 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 370 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 371 | .. c:function:: wchar* Py_GetProgramName() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 372 | |
| 373 | .. index:: single: Py_SetProgramName() |
| 374 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 375 | Return the program name set with :c:func:`Py_SetProgramName`, or the default. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 376 | The returned string points into static storage; the caller should not modify its |
| 377 | value. |
| 378 | |
Victor Stinner | ace3f9a | 2020-11-10 21:10:22 +0100 | [diff] [blame] | 379 | This function should not be called before :c:func:`Py_Initialize`, otherwise |
| 380 | it returns ``NULL``. |
| 381 | |
| 382 | .. versionchanged:: 3.10 |
| 383 | It now returns ``NULL`` if called before :c:func:`Py_Initialize`. |
| 384 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 385 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 386 | .. c:function:: wchar_t* Py_GetPrefix() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 387 | |
| 388 | Return the *prefix* for installed platform-independent files. This is derived |
| 389 | through a number of complicated rules from the program name set with |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 390 | :c:func:`Py_SetProgramName` and some environment variables; for example, if the |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 391 | program name is ``'/usr/local/bin/python'``, the prefix is ``'/usr/local'``. The |
| 392 | returned string points into static storage; the caller should not modify its |
| 393 | value. This corresponds to the :makevar:`prefix` variable in the top-level |
Éric Araujo | 37b5f9e | 2011-09-01 03:19:30 +0200 | [diff] [blame] | 394 | :file:`Makefile` and the ``--prefix`` argument to the :program:`configure` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 395 | script at build time. The value is available to Python code as ``sys.prefix``. |
| 396 | It is only useful on Unix. See also the next function. |
| 397 | |
Victor Stinner | ace3f9a | 2020-11-10 21:10:22 +0100 | [diff] [blame] | 398 | This function should not be called before :c:func:`Py_Initialize`, otherwise |
| 399 | it returns ``NULL``. |
| 400 | |
| 401 | .. versionchanged:: 3.10 |
| 402 | It now returns ``NULL`` if called before :c:func:`Py_Initialize`. |
| 403 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 404 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 405 | .. c:function:: wchar_t* Py_GetExecPrefix() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 406 | |
| 407 | Return the *exec-prefix* for installed platform-*dependent* files. This is |
| 408 | derived through a number of complicated rules from the program name set with |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 409 | :c:func:`Py_SetProgramName` and some environment variables; for example, if the |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 410 | program name is ``'/usr/local/bin/python'``, the exec-prefix is |
| 411 | ``'/usr/local'``. The returned string points into static storage; the caller |
| 412 | should not modify its value. This corresponds to the :makevar:`exec_prefix` |
Éric Araujo | 37b5f9e | 2011-09-01 03:19:30 +0200 | [diff] [blame] | 413 | variable in the top-level :file:`Makefile` and the ``--exec-prefix`` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 414 | argument to the :program:`configure` script at build time. The value is |
| 415 | available to Python code as ``sys.exec_prefix``. It is only useful on Unix. |
| 416 | |
| 417 | Background: The exec-prefix differs from the prefix when platform dependent |
| 418 | files (such as executables and shared libraries) are installed in a different |
| 419 | directory tree. In a typical installation, platform dependent files may be |
| 420 | installed in the :file:`/usr/local/plat` subtree while platform independent may |
| 421 | be installed in :file:`/usr/local`. |
| 422 | |
| 423 | Generally speaking, a platform is a combination of hardware and software |
| 424 | families, e.g. Sparc machines running the Solaris 2.x operating system are |
| 425 | considered the same platform, but Intel machines running Solaris 2.x are another |
| 426 | platform, and Intel machines running Linux are yet another platform. Different |
| 427 | major revisions of the same operating system generally also form different |
| 428 | platforms. Non-Unix operating systems are a different story; the installation |
| 429 | strategies on those systems are so different that the prefix and exec-prefix are |
| 430 | meaningless, and set to the empty string. Note that compiled Python bytecode |
| 431 | files are platform independent (but not independent from the Python version by |
| 432 | which they were compiled!). |
| 433 | |
| 434 | System administrators will know how to configure the :program:`mount` or |
| 435 | :program:`automount` programs to share :file:`/usr/local` between platforms |
| 436 | while having :file:`/usr/local/plat` be a different filesystem for each |
| 437 | platform. |
| 438 | |
Victor Stinner | ace3f9a | 2020-11-10 21:10:22 +0100 | [diff] [blame] | 439 | This function should not be called before :c:func:`Py_Initialize`, otherwise |
| 440 | it returns ``NULL``. |
| 441 | |
| 442 | .. versionchanged:: 3.10 |
| 443 | It now returns ``NULL`` if called before :c:func:`Py_Initialize`. |
| 444 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 445 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 446 | .. c:function:: wchar_t* Py_GetProgramFullPath() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 447 | |
| 448 | .. index:: |
| 449 | single: Py_SetProgramName() |
| 450 | single: executable (in module sys) |
| 451 | |
| 452 | Return the full program name of the Python executable; this is computed as a |
| 453 | side-effect of deriving the default module search path from the program name |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 454 | (set by :c:func:`Py_SetProgramName` above). The returned string points into |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 455 | static storage; the caller should not modify its value. The value is available |
| 456 | to Python code as ``sys.executable``. |
| 457 | |
Victor Stinner | ace3f9a | 2020-11-10 21:10:22 +0100 | [diff] [blame] | 458 | This function should not be called before :c:func:`Py_Initialize`, otherwise |
| 459 | it returns ``NULL``. |
| 460 | |
| 461 | .. versionchanged:: 3.10 |
| 462 | It now returns ``NULL`` if called before :c:func:`Py_Initialize`. |
| 463 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 464 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 465 | .. c:function:: wchar_t* Py_GetPath() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 466 | |
| 467 | .. index:: |
| 468 | triple: module; search; path |
| 469 | single: path (in module sys) |
Kristján Valur Jónsson | 3b69db2 | 2010-09-27 05:32:54 +0000 | [diff] [blame] | 470 | single: Py_SetPath() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 471 | |
Benjamin Peterson | 46a9900 | 2010-01-09 18:45:30 +0000 | [diff] [blame] | 472 | Return the default module search path; this is computed from the program name |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 473 | (set by :c:func:`Py_SetProgramName` above) and some environment variables. |
Benjamin Peterson | 46a9900 | 2010-01-09 18:45:30 +0000 | [diff] [blame] | 474 | The returned string consists of a series of directory names separated by a |
| 475 | platform dependent delimiter character. The delimiter character is ``':'`` |
| 476 | on Unix and Mac OS X, ``';'`` on Windows. The returned string points into |
| 477 | static storage; the caller should not modify its value. The list |
| 478 | :data:`sys.path` is initialized with this value on interpreter startup; it |
| 479 | can be (and usually is) modified later to change the search path for loading |
| 480 | modules. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 481 | |
Victor Stinner | ace3f9a | 2020-11-10 21:10:22 +0100 | [diff] [blame] | 482 | This function should not be called before :c:func:`Py_Initialize`, otherwise |
| 483 | it returns ``NULL``. |
| 484 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 485 | .. XXX should give the exact rules |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 486 | |
Victor Stinner | ace3f9a | 2020-11-10 21:10:22 +0100 | [diff] [blame] | 487 | .. versionchanged:: 3.10 |
| 488 | It now returns ``NULL`` if called before :c:func:`Py_Initialize`. |
| 489 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 490 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 491 | .. c:function:: void Py_SetPath(const wchar_t *) |
Kristján Valur Jónsson | 3b69db2 | 2010-09-27 05:32:54 +0000 | [diff] [blame] | 492 | |
| 493 | .. index:: |
| 494 | triple: module; search; path |
| 495 | single: path (in module sys) |
| 496 | single: Py_GetPath() |
| 497 | |
| 498 | Set the default module search path. If this function is called before |
Georg Brandl | fa4f7f9 | 2010-10-06 10:14:08 +0000 | [diff] [blame] | 499 | :c:func:`Py_Initialize`, then :c:func:`Py_GetPath` won't attempt to compute a |
| 500 | default search path but uses the one provided instead. This is useful if |
| 501 | Python is embedded by an application that has full knowledge of the location |
Georg Brandl | e8ea355 | 2014-10-11 14:36:02 +0200 | [diff] [blame] | 502 | of all modules. The path components should be separated by the platform |
| 503 | dependent delimiter character, which is ``':'`` on Unix and Mac OS X, ``';'`` |
| 504 | on Windows. |
Kristján Valur Jónsson | 3b69db2 | 2010-09-27 05:32:54 +0000 | [diff] [blame] | 505 | |
Victor Stinner | 1ce152a | 2019-09-24 17:44:15 +0200 | [diff] [blame] | 506 | This also causes :data:`sys.executable` to be set to the program |
| 507 | full path (see :c:func:`Py_GetProgramFullPath`) and for :data:`sys.prefix` and |
Georg Brandl | fa4f7f9 | 2010-10-06 10:14:08 +0000 | [diff] [blame] | 508 | :data:`sys.exec_prefix` to be empty. It is up to the caller to modify these |
| 509 | if required after calling :c:func:`Py_Initialize`. |
| 510 | |
Victor Stinner | 25e014b | 2014-08-01 12:28:49 +0200 | [diff] [blame] | 511 | Use :c:func:`Py_DecodeLocale` to decode a bytes string to get a |
| 512 | :c:type:`wchar_*` string. |
| 513 | |
Benjamin Peterson | b33bb89 | 2014-12-24 10:49:11 -0600 | [diff] [blame] | 514 | The path argument is copied internally, so the caller may free it after the |
| 515 | call completes. |
| 516 | |
Victor Stinner | 1ce152a | 2019-09-24 17:44:15 +0200 | [diff] [blame] | 517 | .. versionchanged:: 3.8 |
| 518 | The program full path is now used for :data:`sys.executable`, instead |
| 519 | of the program name. |
| 520 | |
Kristján Valur Jónsson | 3b69db2 | 2010-09-27 05:32:54 +0000 | [diff] [blame] | 521 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 522 | .. c:function:: const char* Py_GetVersion() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 523 | |
| 524 | Return the version of this Python interpreter. This is a string that looks |
| 525 | something like :: |
| 526 | |
Georg Brandl | e6bcc91 | 2008-05-12 18:05:20 +0000 | [diff] [blame] | 527 | "3.0a5+ (py3k:63103M, May 12 2008, 00:53:55) \n[GCC 4.2.3]" |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 528 | |
| 529 | .. index:: single: version (in module sys) |
| 530 | |
| 531 | The first word (up to the first space character) is the current Python version; |
| 532 | the first three characters are the major and minor version separated by a |
| 533 | period. The returned string points into static storage; the caller should not |
Georg Brandl | e6bcc91 | 2008-05-12 18:05:20 +0000 | [diff] [blame] | 534 | modify its value. The value is available to Python code as :data:`sys.version`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 535 | |
| 536 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 537 | .. c:function:: const char* Py_GetPlatform() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 538 | |
| 539 | .. index:: single: platform (in module sys) |
| 540 | |
| 541 | Return the platform identifier for the current platform. On Unix, this is |
| 542 | formed from the "official" name of the operating system, converted to lower |
| 543 | case, followed by the major revision number; e.g., for Solaris 2.x, which is |
| 544 | also known as SunOS 5.x, the value is ``'sunos5'``. On Mac OS X, it is |
| 545 | ``'darwin'``. On Windows, it is ``'win'``. The returned string points into |
| 546 | static storage; the caller should not modify its value. The value is available |
| 547 | to Python code as ``sys.platform``. |
| 548 | |
| 549 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 550 | .. c:function:: const char* Py_GetCopyright() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 551 | |
| 552 | Return the official copyright string for the current Python version, for example |
| 553 | |
| 554 | ``'Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam'`` |
| 555 | |
| 556 | .. index:: single: copyright (in module sys) |
| 557 | |
| 558 | The returned string points into static storage; the caller should not modify its |
| 559 | value. The value is available to Python code as ``sys.copyright``. |
| 560 | |
| 561 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 562 | .. c:function:: const char* Py_GetCompiler() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 563 | |
| 564 | Return an indication of the compiler used to build the current Python version, |
| 565 | in square brackets, for example:: |
| 566 | |
| 567 | "[GCC 2.7.2.2]" |
| 568 | |
| 569 | .. index:: single: version (in module sys) |
| 570 | |
| 571 | The returned string points into static storage; the caller should not modify its |
| 572 | value. The value is available to Python code as part of the variable |
| 573 | ``sys.version``. |
| 574 | |
| 575 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 576 | .. c:function:: const char* Py_GetBuildInfo() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 577 | |
| 578 | Return information about the sequence number and build date and time of the |
| 579 | current Python interpreter instance, for example :: |
| 580 | |
| 581 | "#67, Aug 1 1997, 22:34:28" |
| 582 | |
| 583 | .. index:: single: version (in module sys) |
| 584 | |
| 585 | The returned string points into static storage; the caller should not modify its |
| 586 | value. The value is available to Python code as part of the variable |
| 587 | ``sys.version``. |
| 588 | |
| 589 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 590 | .. c:function:: void PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 591 | |
| 592 | .. index:: |
| 593 | single: main() |
| 594 | single: Py_FatalError() |
| 595 | single: argv (in module sys) |
| 596 | |
Benjamin Peterson | 5c6d787 | 2009-02-06 02:40:07 +0000 | [diff] [blame] | 597 | Set :data:`sys.argv` based on *argc* and *argv*. These parameters are |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 598 | similar to those passed to the program's :c:func:`main` function with the |
Benjamin Peterson | 5c6d787 | 2009-02-06 02:40:07 +0000 | [diff] [blame] | 599 | difference that the first entry should refer to the script file to be |
| 600 | executed rather than the executable hosting the Python interpreter. If there |
| 601 | isn't a script that will be run, the first entry in *argv* can be an empty |
| 602 | string. If this function fails to initialize :data:`sys.argv`, a fatal |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 603 | condition is signalled using :c:func:`Py_FatalError`. |
Benjamin Peterson | 5c6d787 | 2009-02-06 02:40:07 +0000 | [diff] [blame] | 604 | |
Antoine Pitrou | f978fac | 2010-05-21 17:25:34 +0000 | [diff] [blame] | 605 | If *updatepath* is zero, this is all the function does. If *updatepath* |
| 606 | is non-zero, the function also modifies :data:`sys.path` according to the |
| 607 | following algorithm: |
| 608 | |
| 609 | - If the name of an existing script is passed in ``argv[0]``, the absolute |
| 610 | path of the directory where the script is located is prepended to |
| 611 | :data:`sys.path`. |
Serhiy Storchaka | 1ecf7d2 | 2016-10-27 21:41:19 +0300 | [diff] [blame] | 612 | - Otherwise (that is, if *argc* is ``0`` or ``argv[0]`` doesn't point |
Antoine Pitrou | f978fac | 2010-05-21 17:25:34 +0000 | [diff] [blame] | 613 | to an existing file name), an empty string is prepended to |
| 614 | :data:`sys.path`, which is the same as prepending the current working |
| 615 | directory (``"."``). |
| 616 | |
Victor Stinner | 25e014b | 2014-08-01 12:28:49 +0200 | [diff] [blame] | 617 | Use :c:func:`Py_DecodeLocale` to decode a bytes string to get a |
| 618 | :c:type:`wchar_*` string. |
| 619 | |
Antoine Pitrou | f978fac | 2010-05-21 17:25:34 +0000 | [diff] [blame] | 620 | .. note:: |
| 621 | It is recommended that applications embedding the Python interpreter |
Serhiy Storchaka | 1ecf7d2 | 2016-10-27 21:41:19 +0300 | [diff] [blame] | 622 | for purposes other than executing a single script pass ``0`` as *updatepath*, |
Antoine Pitrou | f978fac | 2010-05-21 17:25:34 +0000 | [diff] [blame] | 623 | and update :data:`sys.path` themselves if desired. |
Serhiy Storchaka | 6dff020 | 2016-05-07 10:49:07 +0300 | [diff] [blame] | 624 | See `CVE-2008-5983 <https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-5983>`_. |
Antoine Pitrou | f978fac | 2010-05-21 17:25:34 +0000 | [diff] [blame] | 625 | |
| 626 | On versions before 3.1.3, you can achieve the same effect by manually |
| 627 | popping the first :data:`sys.path` element after having called |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 628 | :c:func:`PySys_SetArgv`, for example using:: |
Antoine Pitrou | f978fac | 2010-05-21 17:25:34 +0000 | [diff] [blame] | 629 | |
| 630 | PyRun_SimpleString("import sys; sys.path.pop(0)\n"); |
| 631 | |
| 632 | .. versionadded:: 3.1.3 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 633 | |
Serhiy Storchaka | 1ecf7d2 | 2016-10-27 21:41:19 +0300 | [diff] [blame] | 634 | .. XXX impl. doesn't seem consistent in allowing ``0``/``NULL`` for the params; |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 635 | check w/ Guido. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 636 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 637 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 638 | .. c:function:: void PySys_SetArgv(int argc, wchar_t **argv) |
Antoine Pitrou | f978fac | 2010-05-21 17:25:34 +0000 | [diff] [blame] | 639 | |
Christian Heimes | ad73a9c | 2013-08-10 16:36:18 +0200 | [diff] [blame] | 640 | This function works like :c:func:`PySys_SetArgvEx` with *updatepath* set |
Serhiy Storchaka | 1ecf7d2 | 2016-10-27 21:41:19 +0300 | [diff] [blame] | 641 | to ``1`` unless the :program:`python` interpreter was started with the |
Christian Heimes | ad73a9c | 2013-08-10 16:36:18 +0200 | [diff] [blame] | 642 | :option:`-I`. |
| 643 | |
Victor Stinner | 25e014b | 2014-08-01 12:28:49 +0200 | [diff] [blame] | 644 | Use :c:func:`Py_DecodeLocale` to decode a bytes string to get a |
| 645 | :c:type:`wchar_*` string. |
| 646 | |
Christian Heimes | ad73a9c | 2013-08-10 16:36:18 +0200 | [diff] [blame] | 647 | .. versionchanged:: 3.4 The *updatepath* value depends on :option:`-I`. |
Antoine Pitrou | f978fac | 2010-05-21 17:25:34 +0000 | [diff] [blame] | 648 | |
| 649 | |
Serhiy Storchaka | 4ae06c5 | 2017-12-12 13:55:04 +0200 | [diff] [blame] | 650 | .. c:function:: void Py_SetPythonHome(const wchar_t *home) |
Benjamin Peterson | 5c6d787 | 2009-02-06 02:40:07 +0000 | [diff] [blame] | 651 | |
| 652 | Set the default "home" directory, that is, the location of the standard |
Georg Brandl | de0ab5e | 2010-12-02 18:02:01 +0000 | [diff] [blame] | 653 | Python libraries. See :envvar:`PYTHONHOME` for the meaning of the |
| 654 | argument string. |
| 655 | |
Benjamin Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame] | 656 | The argument should point to a zero-terminated character string in static |
| 657 | storage whose contents will not change for the duration of the program's |
| 658 | execution. No code in the Python interpreter will change the contents of |
| 659 | this storage. |
Benjamin Peterson | 5c6d787 | 2009-02-06 02:40:07 +0000 | [diff] [blame] | 660 | |
Victor Stinner | 25e014b | 2014-08-01 12:28:49 +0200 | [diff] [blame] | 661 | Use :c:func:`Py_DecodeLocale` to decode a bytes string to get a |
| 662 | :c:type:`wchar_*` string. |
| 663 | |
Benjamin Peterson | 5c6d787 | 2009-02-06 02:40:07 +0000 | [diff] [blame] | 664 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 665 | .. c:function:: w_char* Py_GetPythonHome() |
Benjamin Peterson | 5c6d787 | 2009-02-06 02:40:07 +0000 | [diff] [blame] | 666 | |
| 667 | Return the default "home", that is, the value set by a previous call to |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 668 | :c:func:`Py_SetPythonHome`, or the value of the :envvar:`PYTHONHOME` |
Benjamin Peterson | 5c6d787 | 2009-02-06 02:40:07 +0000 | [diff] [blame] | 669 | environment variable if it is set. |
| 670 | |
Victor Stinner | ace3f9a | 2020-11-10 21:10:22 +0100 | [diff] [blame] | 671 | This function should not be called before :c:func:`Py_Initialize`, otherwise |
| 672 | it returns ``NULL``. |
| 673 | |
| 674 | .. versionchanged:: 3.10 |
| 675 | It now returns ``NULL`` if called before :c:func:`Py_Initialize`. |
| 676 | |
Benjamin Peterson | 5c6d787 | 2009-02-06 02:40:07 +0000 | [diff] [blame] | 677 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 678 | .. _threads: |
| 679 | |
| 680 | Thread State and the Global Interpreter Lock |
| 681 | ============================================ |
| 682 | |
| 683 | .. index:: |
| 684 | single: global interpreter lock |
| 685 | single: interpreter lock |
| 686 | single: lock, interpreter |
| 687 | |
Georg Brandl | f285bcc | 2010-10-19 21:07:16 +0000 | [diff] [blame] | 688 | The Python interpreter is not fully thread-safe. In order to support |
Antoine Pitrou | bedd2c2 | 2011-01-15 12:54:19 +0000 | [diff] [blame] | 689 | multi-threaded Python programs, there's a global lock, called the :term:`global |
| 690 | interpreter lock` or :term:`GIL`, that must be held by the current thread before |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 691 | it can safely access Python objects. Without the lock, even the simplest |
| 692 | operations could cause problems in a multi-threaded program: for example, when |
| 693 | two threads simultaneously increment the reference count of the same object, the |
| 694 | reference count could end up being incremented only once instead of twice. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 695 | |
Antoine Pitrou | bedd2c2 | 2011-01-15 12:54:19 +0000 | [diff] [blame] | 696 | .. index:: single: setswitchinterval() (in module sys) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 697 | |
Antoine Pitrou | bedd2c2 | 2011-01-15 12:54:19 +0000 | [diff] [blame] | 698 | Therefore, the rule exists that only the thread that has acquired the |
| 699 | :term:`GIL` may operate on Python objects or call Python/C API functions. |
| 700 | In order to emulate concurrency of execution, the interpreter regularly |
| 701 | tries to switch threads (see :func:`sys.setswitchinterval`). The lock is also |
| 702 | released around potentially blocking I/O operations like reading or writing |
| 703 | a file, so that other Python threads can run in the meantime. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 704 | |
| 705 | .. index:: |
| 706 | single: PyThreadState |
| 707 | single: PyThreadState |
| 708 | |
Antoine Pitrou | bedd2c2 | 2011-01-15 12:54:19 +0000 | [diff] [blame] | 709 | The Python interpreter keeps some thread-specific bookkeeping information |
| 710 | inside a data structure called :c:type:`PyThreadState`. There's also one |
| 711 | global variable pointing to the current :c:type:`PyThreadState`: it can |
| 712 | be retrieved using :c:func:`PyThreadState_Get`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 713 | |
Antoine Pitrou | bedd2c2 | 2011-01-15 12:54:19 +0000 | [diff] [blame] | 714 | Releasing the GIL from extension code |
| 715 | ------------------------------------- |
| 716 | |
| 717 | Most extension code manipulating the :term:`GIL` has the following simple |
| 718 | structure:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 719 | |
| 720 | Save the thread state in a local variable. |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 721 | Release the global interpreter lock. |
Antoine Pitrou | bedd2c2 | 2011-01-15 12:54:19 +0000 | [diff] [blame] | 722 | ... Do some blocking I/O operation ... |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 723 | Reacquire the global interpreter lock. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 724 | Restore the thread state from the local variable. |
| 725 | |
| 726 | This is so common that a pair of macros exists to simplify it:: |
| 727 | |
| 728 | Py_BEGIN_ALLOW_THREADS |
Antoine Pitrou | bedd2c2 | 2011-01-15 12:54:19 +0000 | [diff] [blame] | 729 | ... Do some blocking I/O operation ... |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 730 | Py_END_ALLOW_THREADS |
| 731 | |
| 732 | .. index:: |
| 733 | single: Py_BEGIN_ALLOW_THREADS |
| 734 | single: Py_END_ALLOW_THREADS |
| 735 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 736 | The :c:macro:`Py_BEGIN_ALLOW_THREADS` macro opens a new block and declares a |
| 737 | hidden local variable; the :c:macro:`Py_END_ALLOW_THREADS` macro closes the |
Victor Stinner | 2914bb3 | 2018-01-29 11:57:45 +0100 | [diff] [blame] | 738 | block. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 739 | |
Victor Stinner | 2914bb3 | 2018-01-29 11:57:45 +0100 | [diff] [blame] | 740 | The block above expands to the following code:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 741 | |
| 742 | PyThreadState *_save; |
| 743 | |
| 744 | _save = PyEval_SaveThread(); |
Victor Stinner | 2914bb3 | 2018-01-29 11:57:45 +0100 | [diff] [blame] | 745 | ... Do some blocking I/O operation ... |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 746 | PyEval_RestoreThread(_save); |
| 747 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 748 | .. index:: |
| 749 | single: PyEval_RestoreThread() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 750 | single: PyEval_SaveThread() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 751 | |
Antoine Pitrou | bedd2c2 | 2011-01-15 12:54:19 +0000 | [diff] [blame] | 752 | Here is how these functions work: the global interpreter lock is used to protect the pointer to the |
| 753 | current thread state. When releasing the lock and saving the thread state, |
| 754 | the current thread state pointer must be retrieved before the lock is released |
| 755 | (since another thread could immediately acquire the lock and store its own thread |
| 756 | state in the global variable). Conversely, when acquiring the lock and restoring |
| 757 | the thread state, the lock must be acquired before storing the thread state |
| 758 | pointer. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 759 | |
Antoine Pitrou | bedd2c2 | 2011-01-15 12:54:19 +0000 | [diff] [blame] | 760 | .. note:: |
| 761 | Calling system I/O functions is the most common use case for releasing |
| 762 | the GIL, but it can also be useful before calling long-running computations |
| 763 | which don't need access to Python objects, such as compression or |
| 764 | cryptographic functions operating over memory buffers. For example, the |
| 765 | standard :mod:`zlib` and :mod:`hashlib` modules release the GIL when |
| 766 | compressing or hashing data. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 767 | |
Antoine Pitrou | 1a67bee | 2013-09-30 21:35:44 +0200 | [diff] [blame] | 768 | |
| 769 | .. _gilstate: |
| 770 | |
Antoine Pitrou | bedd2c2 | 2011-01-15 12:54:19 +0000 | [diff] [blame] | 771 | Non-Python created threads |
| 772 | -------------------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 773 | |
Antoine Pitrou | bedd2c2 | 2011-01-15 12:54:19 +0000 | [diff] [blame] | 774 | When threads are created using the dedicated Python APIs (such as the |
| 775 | :mod:`threading` module), a thread state is automatically associated to them |
| 776 | and the code showed above is therefore correct. However, when threads are |
| 777 | created from C (for example by a third-party library with its own thread |
| 778 | management), they don't hold the GIL, nor is there a thread state structure |
| 779 | for them. |
| 780 | |
| 781 | If you need to call Python code from these threads (often this will be part |
| 782 | of a callback API provided by the aforementioned third-party library), |
| 783 | you must first register these threads with the interpreter by |
| 784 | creating a thread state data structure, then acquiring the GIL, and finally |
| 785 | storing their thread state pointer, before you can start using the Python/C |
| 786 | API. When you are done, you should reset the thread state pointer, release |
| 787 | the GIL, and finally free the thread state data structure. |
| 788 | |
| 789 | The :c:func:`PyGILState_Ensure` and :c:func:`PyGILState_Release` functions do |
| 790 | all of the above automatically. The typical idiom for calling into Python |
| 791 | from a C thread is:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 792 | |
| 793 | PyGILState_STATE gstate; |
| 794 | gstate = PyGILState_Ensure(); |
| 795 | |
Antoine Pitrou | bedd2c2 | 2011-01-15 12:54:19 +0000 | [diff] [blame] | 796 | /* Perform Python actions here. */ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 797 | result = CallSomeFunction(); |
Antoine Pitrou | bedd2c2 | 2011-01-15 12:54:19 +0000 | [diff] [blame] | 798 | /* evaluate result or handle exception */ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 799 | |
| 800 | /* Release the thread. No Python API allowed beyond this point. */ |
| 801 | PyGILState_Release(gstate); |
| 802 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 803 | Note that the :c:func:`PyGILState_\*` functions assume there is only one global |
Antoine Pitrou | bedd2c2 | 2011-01-15 12:54:19 +0000 | [diff] [blame] | 804 | interpreter (created automatically by :c:func:`Py_Initialize`). Python |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 805 | supports the creation of additional interpreters (using |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 806 | :c:func:`Py_NewInterpreter`), but mixing multiple interpreters and the |
| 807 | :c:func:`PyGILState_\*` API is unsupported. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 808 | |
Eric Snow | 73cdb0c | 2019-11-15 13:28:54 -0800 | [diff] [blame] | 809 | |
| 810 | .. _fork-and-threads: |
| 811 | |
| 812 | Cautions about fork() |
| 813 | --------------------- |
| 814 | |
Benjamin Peterson | 0df35a9 | 2009-10-04 20:32:25 +0000 | [diff] [blame] | 815 | Another important thing to note about threads is their behaviour in the face |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 816 | of the C :c:func:`fork` call. On most systems with :c:func:`fork`, after a |
Eric Snow | 73cdb0c | 2019-11-15 13:28:54 -0800 | [diff] [blame] | 817 | process forks only the thread that issued the fork will exist. This has a |
| 818 | concrete impact both on how locks must be handled and on all stored state |
| 819 | in CPython's runtime. |
| 820 | |
| 821 | The fact that only the "current" thread remains |
Benjamin Peterson | 0df35a9 | 2009-10-04 20:32:25 +0000 | [diff] [blame] | 822 | means any locks held by other threads will never be released. Python solves |
| 823 | this for :func:`os.fork` by acquiring the locks it uses internally before |
| 824 | the fork, and releasing them afterwards. In addition, it resets any |
| 825 | :ref:`lock-objects` in the child. When extending or embedding Python, there |
| 826 | is no way to inform Python of additional (non-Python) locks that need to be |
| 827 | acquired before or reset after a fork. OS facilities such as |
Ezio Melotti | 861d27f | 2011-04-20 21:32:40 +0300 | [diff] [blame] | 828 | :c:func:`pthread_atfork` would need to be used to accomplish the same thing. |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 829 | Additionally, when extending or embedding Python, calling :c:func:`fork` |
Benjamin Peterson | 0df35a9 | 2009-10-04 20:32:25 +0000 | [diff] [blame] | 830 | directly rather than through :func:`os.fork` (and returning to or calling |
| 831 | into Python) may result in a deadlock by one of Python's internal locks |
| 832 | being held by a thread that is defunct after the fork. |
Antoine Pitrou | f7ecfac | 2017-05-28 11:35:14 +0200 | [diff] [blame] | 833 | :c:func:`PyOS_AfterFork_Child` tries to reset the necessary locks, but is not |
Benjamin Peterson | 0df35a9 | 2009-10-04 20:32:25 +0000 | [diff] [blame] | 834 | always able to. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 835 | |
Eric Snow | 73cdb0c | 2019-11-15 13:28:54 -0800 | [diff] [blame] | 836 | The fact that all other threads go away also means that CPython's |
| 837 | runtime state there must be cleaned up properly, which :func:`os.fork` |
| 838 | does. This means finalizing all other :c:type:`PyThreadState` objects |
| 839 | belonging to the current interpreter and all other |
| 840 | :c:type:`PyInterpreterState` objects. Due to this and the special |
| 841 | nature of the :ref:`"main" interpreter <sub-interpreter-support>`, |
| 842 | :c:func:`fork` should only be called in that interpreter's "main" |
| 843 | thread, where the CPython global runtime was originally initialized. |
| 844 | The only exception is if :c:func:`exec` will be called immediately |
| 845 | after. |
| 846 | |
Antoine Pitrou | 8b50b83 | 2011-01-15 11:57:42 +0000 | [diff] [blame] | 847 | |
Antoine Pitrou | bedd2c2 | 2011-01-15 12:54:19 +0000 | [diff] [blame] | 848 | High-level API |
| 849 | -------------- |
| 850 | |
| 851 | These are the most commonly used types and functions when writing C extension |
| 852 | code, or when embedding the Python interpreter: |
| 853 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 854 | .. c:type:: PyInterpreterState |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 855 | |
| 856 | This data structure represents the state shared by a number of cooperating |
| 857 | threads. Threads belonging to the same interpreter share their module |
| 858 | administration and a few other internal items. There are no public members in |
| 859 | this structure. |
| 860 | |
| 861 | Threads belonging to different interpreters initially share nothing, except |
| 862 | process state like available memory, open file descriptors and such. The global |
| 863 | interpreter lock is also shared by all threads, regardless of to which |
| 864 | interpreter they belong. |
| 865 | |
| 866 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 867 | .. c:type:: PyThreadState |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 868 | |
| 869 | This data structure represents the state of a single thread. The only public |
Victor Stinner | 474652f | 2020-08-13 22:11:50 +0200 | [diff] [blame] | 870 | data member is :attr:`interp` (:c:type:`PyInterpreterState *`), which points to |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 871 | this thread's interpreter state. |
| 872 | |
| 873 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 874 | .. c:function:: void PyEval_InitThreads() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 875 | |
| 876 | .. index:: |
Antoine Pitrou | f5cf435 | 2011-01-15 14:31:49 +0000 | [diff] [blame] | 877 | single: PyEval_AcquireThread() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 878 | single: PyEval_ReleaseThread() |
| 879 | single: PyEval_SaveThread() |
| 880 | single: PyEval_RestoreThread() |
| 881 | |
Victor Stinner | b4698ec | 2020-03-10 01:28:54 +0100 | [diff] [blame] | 882 | Deprecated function which does nothing. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 883 | |
Victor Stinner | b4698ec | 2020-03-10 01:28:54 +0100 | [diff] [blame] | 884 | In Python 3.6 and older, this function created the GIL if it didn't exist. |
| 885 | |
| 886 | .. versionchanged:: 3.9 |
| 887 | The function now does nothing. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 888 | |
Victor Stinner | 2914bb3 | 2018-01-29 11:57:45 +0100 | [diff] [blame] | 889 | .. versionchanged:: 3.7 |
| 890 | This function is now called by :c:func:`Py_Initialize()`, so you don't |
| 891 | have to call it yourself anymore. |
| 892 | |
Antoine Pitrou | 9bb9877 | 2011-03-15 20:22:50 +0100 | [diff] [blame] | 893 | .. versionchanged:: 3.2 |
| 894 | This function cannot be called before :c:func:`Py_Initialize()` anymore. |
| 895 | |
Victor Stinner | b4698ec | 2020-03-10 01:28:54 +0100 | [diff] [blame] | 896 | .. deprecated-removed:: 3.9 3.11 |
| 897 | |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 898 | .. index:: module: _thread |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 899 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 900 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 901 | .. c:function:: int PyEval_ThreadsInitialized() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 902 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 903 | Returns a non-zero value if :c:func:`PyEval_InitThreads` has been called. This |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 904 | function can be called without holding the GIL, and therefore can be used to |
Victor Stinner | 2914bb3 | 2018-01-29 11:57:45 +0100 | [diff] [blame] | 905 | avoid calls to the locking API when running single-threaded. |
| 906 | |
| 907 | .. versionchanged:: 3.7 |
| 908 | The :term:`GIL` is now initialized by :c:func:`Py_Initialize()`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 909 | |
Victor Stinner | b4698ec | 2020-03-10 01:28:54 +0100 | [diff] [blame] | 910 | .. deprecated-removed:: 3.9 3.11 |
| 911 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 912 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 913 | .. c:function:: PyThreadState* PyEval_SaveThread() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 914 | |
Zackery Spytz | eef0596 | 2018-09-29 10:07:11 -0600 | [diff] [blame] | 915 | Release the global interpreter lock (if it has been created) and reset the |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 916 | thread state to ``NULL``, returning the previous thread state (which is not |
| 917 | ``NULL``). If the lock has been created, the current thread must have |
Zackery Spytz | eef0596 | 2018-09-29 10:07:11 -0600 | [diff] [blame] | 918 | acquired it. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 919 | |
| 920 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 921 | .. c:function:: void PyEval_RestoreThread(PyThreadState *tstate) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 922 | |
Zackery Spytz | eef0596 | 2018-09-29 10:07:11 -0600 | [diff] [blame] | 923 | Acquire the global interpreter lock (if it has been created) and set the |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 924 | thread state to *tstate*, which must not be ``NULL``. If the lock has been |
Zackery Spytz | eef0596 | 2018-09-29 10:07:11 -0600 | [diff] [blame] | 925 | created, the current thread must not have acquired it, otherwise deadlock |
| 926 | ensues. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 927 | |
Pablo Galindo | fde9b33 | 2019-04-13 17:23:24 +0100 | [diff] [blame] | 928 | .. note:: |
| 929 | Calling this function from a thread when the runtime is finalizing |
| 930 | will terminate the thread, even if the thread was not created by Python. |
| 931 | You can use :c:func:`_Py_IsFinalizing` or :func:`sys.is_finalizing` to |
| 932 | check if the interpreter is in process of being finalized before calling |
| 933 | this function to avoid unwanted termination. |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 934 | |
Antoine Pitrou | bedd2c2 | 2011-01-15 12:54:19 +0000 | [diff] [blame] | 935 | .. c:function:: PyThreadState* PyThreadState_Get() |
| 936 | |
| 937 | Return the current thread state. The global interpreter lock must be held. |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 938 | When the current thread state is ``NULL``, this issues a fatal error (so that |
| 939 | the caller needn't check for ``NULL``). |
Antoine Pitrou | bedd2c2 | 2011-01-15 12:54:19 +0000 | [diff] [blame] | 940 | |
| 941 | |
| 942 | .. c:function:: PyThreadState* PyThreadState_Swap(PyThreadState *tstate) |
| 943 | |
| 944 | Swap the current thread state with the thread state given by the argument |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 945 | *tstate*, which may be ``NULL``. The global interpreter lock must be held |
Antoine Pitrou | bedd2c2 | 2011-01-15 12:54:19 +0000 | [diff] [blame] | 946 | and is not released. |
| 947 | |
| 948 | |
Antoine Pitrou | bedd2c2 | 2011-01-15 12:54:19 +0000 | [diff] [blame] | 949 | The following functions use thread-local storage, and are not compatible |
| 950 | with sub-interpreters: |
| 951 | |
| 952 | .. c:function:: PyGILState_STATE PyGILState_Ensure() |
| 953 | |
| 954 | Ensure that the current thread is ready to call the Python C API regardless |
| 955 | of the current state of Python, or of the global interpreter lock. This may |
| 956 | be called as many times as desired by a thread as long as each call is |
| 957 | matched with a call to :c:func:`PyGILState_Release`. In general, other |
| 958 | thread-related APIs may be used between :c:func:`PyGILState_Ensure` and |
| 959 | :c:func:`PyGILState_Release` calls as long as the thread state is restored to |
| 960 | its previous state before the Release(). For example, normal usage of the |
| 961 | :c:macro:`Py_BEGIN_ALLOW_THREADS` and :c:macro:`Py_END_ALLOW_THREADS` macros is |
| 962 | acceptable. |
| 963 | |
| 964 | The return value is an opaque "handle" to the thread state when |
| 965 | :c:func:`PyGILState_Ensure` was called, and must be passed to |
| 966 | :c:func:`PyGILState_Release` to ensure Python is left in the same state. Even |
| 967 | though recursive calls are allowed, these handles *cannot* be shared - each |
| 968 | unique call to :c:func:`PyGILState_Ensure` must save the handle for its call |
| 969 | to :c:func:`PyGILState_Release`. |
| 970 | |
| 971 | When the function returns, the current thread will hold the GIL and be able |
| 972 | to call arbitrary Python code. Failure is a fatal error. |
| 973 | |
Pablo Galindo | fde9b33 | 2019-04-13 17:23:24 +0100 | [diff] [blame] | 974 | .. note:: |
| 975 | Calling this function from a thread when the runtime is finalizing |
| 976 | will terminate the thread, even if the thread was not created by Python. |
| 977 | You can use :c:func:`_Py_IsFinalizing` or :func:`sys.is_finalizing` to |
| 978 | check if the interpreter is in process of being finalized before calling |
| 979 | this function to avoid unwanted termination. |
Antoine Pitrou | bedd2c2 | 2011-01-15 12:54:19 +0000 | [diff] [blame] | 980 | |
| 981 | .. c:function:: void PyGILState_Release(PyGILState_STATE) |
| 982 | |
| 983 | Release any resources previously acquired. After this call, Python's state will |
| 984 | be the same as it was prior to the corresponding :c:func:`PyGILState_Ensure` call |
| 985 | (but generally this state will be unknown to the caller, hence the use of the |
| 986 | GILState API). |
| 987 | |
| 988 | Every call to :c:func:`PyGILState_Ensure` must be matched by a call to |
| 989 | :c:func:`PyGILState_Release` on the same thread. |
| 990 | |
| 991 | |
Eli Bendersky | 0813168 | 2012-06-03 08:07:47 +0300 | [diff] [blame] | 992 | .. c:function:: PyThreadState* PyGILState_GetThisThreadState() |
Sandro Tosi | 61baee0 | 2011-08-08 00:16:54 +0200 | [diff] [blame] | 993 | |
| 994 | Get the current thread state for this thread. May return ``NULL`` if no |
| 995 | GILState API has been used on the current thread. Note that the main thread |
| 996 | always has such a thread-state, even if no auto-thread-state call has been |
| 997 | made on the main thread. This is mainly a helper/diagnostic function. |
| 998 | |
| 999 | |
Kristján Valur Jónsson | 684cd0e | 2013-03-23 03:36:16 -0700 | [diff] [blame] | 1000 | .. c:function:: int PyGILState_Check() |
| 1001 | |
Serhiy Storchaka | 1ecf7d2 | 2016-10-27 21:41:19 +0300 | [diff] [blame] | 1002 | Return ``1`` if the current thread is holding the GIL and ``0`` otherwise. |
Kristján Valur Jónsson | 684cd0e | 2013-03-23 03:36:16 -0700 | [diff] [blame] | 1003 | This function can be called from any thread at any time. |
| 1004 | Only if it has had its Python thread state initialized and currently is |
Serhiy Storchaka | 1ecf7d2 | 2016-10-27 21:41:19 +0300 | [diff] [blame] | 1005 | holding the GIL will it return ``1``. |
Kristján Valur Jónsson | 684cd0e | 2013-03-23 03:36:16 -0700 | [diff] [blame] | 1006 | This is mainly a helper/diagnostic function. It can be useful |
| 1007 | for example in callback contexts or memory allocation functions when |
| 1008 | knowing that the GIL is locked can allow the caller to perform sensitive |
| 1009 | actions or otherwise behave differently. |
| 1010 | |
Kristján Valur Jónsson | 34870c4 | 2013-03-23 03:56:16 -0700 | [diff] [blame] | 1011 | .. versionadded:: 3.4 |
| 1012 | |
Kristján Valur Jónsson | 684cd0e | 2013-03-23 03:36:16 -0700 | [diff] [blame] | 1013 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1014 | The following macros are normally used without a trailing semicolon; look for |
| 1015 | example usage in the Python source distribution. |
| 1016 | |
| 1017 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1018 | .. c:macro:: Py_BEGIN_ALLOW_THREADS |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1019 | |
| 1020 | This macro expands to ``{ PyThreadState *_save; _save = PyEval_SaveThread();``. |
| 1021 | Note that it contains an opening brace; it must be matched with a following |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1022 | :c:macro:`Py_END_ALLOW_THREADS` macro. See above for further discussion of this |
Victor Stinner | 2914bb3 | 2018-01-29 11:57:45 +0100 | [diff] [blame] | 1023 | macro. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1024 | |
| 1025 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1026 | .. c:macro:: Py_END_ALLOW_THREADS |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1027 | |
| 1028 | This macro expands to ``PyEval_RestoreThread(_save); }``. Note that it contains |
| 1029 | a closing brace; it must be matched with an earlier |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1030 | :c:macro:`Py_BEGIN_ALLOW_THREADS` macro. See above for further discussion of |
Victor Stinner | 2914bb3 | 2018-01-29 11:57:45 +0100 | [diff] [blame] | 1031 | this macro. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1032 | |
| 1033 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1034 | .. c:macro:: Py_BLOCK_THREADS |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1035 | |
| 1036 | This macro expands to ``PyEval_RestoreThread(_save);``: it is equivalent to |
Victor Stinner | 2914bb3 | 2018-01-29 11:57:45 +0100 | [diff] [blame] | 1037 | :c:macro:`Py_END_ALLOW_THREADS` without the closing brace. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1038 | |
| 1039 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1040 | .. c:macro:: Py_UNBLOCK_THREADS |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1041 | |
| 1042 | This macro expands to ``_save = PyEval_SaveThread();``: it is equivalent to |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1043 | :c:macro:`Py_BEGIN_ALLOW_THREADS` without the opening brace and variable |
Victor Stinner | 2914bb3 | 2018-01-29 11:57:45 +0100 | [diff] [blame] | 1044 | declaration. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1045 | |
Antoine Pitrou | bedd2c2 | 2011-01-15 12:54:19 +0000 | [diff] [blame] | 1046 | |
| 1047 | Low-level API |
| 1048 | ------------- |
| 1049 | |
Victor Stinner | 2914bb3 | 2018-01-29 11:57:45 +0100 | [diff] [blame] | 1050 | All of the following functions must be called after :c:func:`Py_Initialize`. |
| 1051 | |
| 1052 | .. versionchanged:: 3.7 |
| 1053 | :c:func:`Py_Initialize()` now initializes the :term:`GIL`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1054 | |
| 1055 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1056 | .. c:function:: PyInterpreterState* PyInterpreterState_New() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1057 | |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 1058 | Create a new interpreter state object. The global interpreter lock need not |
| 1059 | be held, but may be held if it is necessary to serialize calls to this |
| 1060 | function. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1061 | |
Christian Heimes | ed4b321 | 2019-09-12 15:13:02 +0200 | [diff] [blame] | 1062 | .. audit-event:: cpython.PyInterpreterState_New "" c.PyInterpreterState_New |
| 1063 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1064 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1065 | .. c:function:: void PyInterpreterState_Clear(PyInterpreterState *interp) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1066 | |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 1067 | Reset all information in an interpreter state object. The global interpreter |
| 1068 | lock must be held. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1069 | |
Christian Heimes | ed4b321 | 2019-09-12 15:13:02 +0200 | [diff] [blame] | 1070 | .. audit-event:: cpython.PyInterpreterState_Clear "" c.PyInterpreterState_Clear |
| 1071 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1072 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1073 | .. c:function:: void PyInterpreterState_Delete(PyInterpreterState *interp) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1074 | |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 1075 | Destroy an interpreter state object. The global interpreter lock need not be |
| 1076 | held. The interpreter state must have been reset with a previous call to |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1077 | :c:func:`PyInterpreterState_Clear`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1078 | |
| 1079 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1080 | .. c:function:: PyThreadState* PyThreadState_New(PyInterpreterState *interp) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1081 | |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 1082 | Create a new thread state object belonging to the given interpreter object. |
| 1083 | The global interpreter lock need not be held, but may be held if it is |
| 1084 | necessary to serialize calls to this function. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1085 | |
| 1086 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1087 | .. c:function:: void PyThreadState_Clear(PyThreadState *tstate) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1088 | |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 1089 | Reset all information in a thread state object. The global interpreter lock |
| 1090 | must be held. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1091 | |
Victor Stinner | 4d96b46 | 2020-02-01 02:30:25 +0100 | [diff] [blame] | 1092 | .. versionchanged:: 3.9 |
| 1093 | This function now calls the :c:member:`PyThreadState.on_delete` callback. |
| 1094 | Previously, that happened in :c:func:`PyThreadState_Delete`. |
| 1095 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1096 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1097 | .. c:function:: void PyThreadState_Delete(PyThreadState *tstate) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1098 | |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 1099 | Destroy a thread state object. The global interpreter lock need not be held. |
| 1100 | The thread state must have been reset with a previous call to |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1101 | :c:func:`PyThreadState_Clear`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1102 | |
| 1103 | |
Victor Stinner | 8fb02b6 | 2020-03-13 23:38:08 +0100 | [diff] [blame] | 1104 | .. c:function:: void PyThreadState_DeleteCurrent(void) |
Joannah Nanjekye | 8855e47 | 2019-10-04 08:35:42 -0300 | [diff] [blame] | 1105 | |
Victor Stinner | 8fb02b6 | 2020-03-13 23:38:08 +0100 | [diff] [blame] | 1106 | Destroy the current thread state and release the global interpreter lock. |
| 1107 | Like :c:func:`PyThreadState_Delete`, the global interpreter lock need not |
| 1108 | be held. The thread state must have been reset with a previous call |
| 1109 | to :c:func:`PyThreadState_Clear`. |
| 1110 | |
| 1111 | |
Victor Stinner | fd1e1a1 | 2020-03-20 15:51:45 +0100 | [diff] [blame] | 1112 | .. c:function:: PyFrameObject* PyThreadState_GetFrame(PyThreadState *tstate) |
| 1113 | |
Victor Stinner | 4386b90 | 2020-04-29 03:01:43 +0200 | [diff] [blame] | 1114 | Get the current frame of the Python thread state *tstate*. |
Victor Stinner | a42ca74 | 2020-04-28 19:01:31 +0200 | [diff] [blame] | 1115 | |
Victor Stinner | 23c5f93 | 2020-11-09 13:40:47 +0100 | [diff] [blame] | 1116 | Return a :term:`strong reference`. Return ``NULL`` if no frame is currently |
Victor Stinner | 4386b90 | 2020-04-29 03:01:43 +0200 | [diff] [blame] | 1117 | executing. |
Victor Stinner | fd1e1a1 | 2020-03-20 15:51:45 +0100 | [diff] [blame] | 1118 | |
| 1119 | See also :c:func:`PyEval_GetFrame`. |
| 1120 | |
| 1121 | *tstate* must not be ``NULL``. |
| 1122 | |
| 1123 | .. versionadded:: 3.9 |
| 1124 | |
| 1125 | |
Victor Stinner | 5c3cda0 | 2020-03-25 21:23:53 +0100 | [diff] [blame] | 1126 | .. c:function:: uint64_t PyThreadState_GetID(PyThreadState *tstate) |
| 1127 | |
| 1128 | Get the unique thread state identifier of the Python thread state *tstate*. |
| 1129 | |
| 1130 | *tstate* must not be ``NULL``. |
| 1131 | |
| 1132 | .. versionadded:: 3.9 |
| 1133 | |
| 1134 | |
Victor Stinner | 8fb02b6 | 2020-03-13 23:38:08 +0100 | [diff] [blame] | 1135 | .. c:function:: PyInterpreterState* PyThreadState_GetInterpreter(PyThreadState *tstate) |
| 1136 | |
| 1137 | Get the interpreter of the Python thread state *tstate*. |
| 1138 | |
| 1139 | *tstate* must not be ``NULL``. |
| 1140 | |
| 1141 | .. versionadded:: 3.9 |
Joannah Nanjekye | 8855e47 | 2019-10-04 08:35:42 -0300 | [diff] [blame] | 1142 | |
| 1143 | |
Victor Stinner | be79373 | 2020-03-13 18:15:33 +0100 | [diff] [blame] | 1144 | .. c:function:: PyInterpreterState* PyInterpreterState_Get(void) |
| 1145 | |
| 1146 | Get the current interpreter. |
| 1147 | |
| 1148 | Issue a fatal error if there no current Python thread state or no current |
| 1149 | interpreter. It cannot return NULL. |
| 1150 | |
| 1151 | The caller must hold the GIL. |
| 1152 | |
| 1153 | .. versionadded:: 3.9 |
| 1154 | |
| 1155 | |
Victor Stinner | 1a1bd2e | 2020-04-17 19:13:06 +0200 | [diff] [blame] | 1156 | .. c:function:: int64_t PyInterpreterState_GetID(PyInterpreterState *interp) |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 1157 | |
| 1158 | Return the interpreter's unique ID. If there was any error in doing |
Serhiy Storchaka | 5bb0005 | 2018-02-09 13:31:19 +0200 | [diff] [blame] | 1159 | so then ``-1`` is returned and an error is set. |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 1160 | |
Victor Stinner | 71a3522 | 2020-03-26 22:46:14 +0100 | [diff] [blame] | 1161 | The caller must hold the GIL. |
| 1162 | |
Eric Snow | e377416 | 2017-05-22 19:46:40 -0700 | [diff] [blame] | 1163 | .. versionadded:: 3.7 |
| 1164 | |
| 1165 | |
Eric Snow | d2fdd1f | 2019-03-15 17:47:43 -0600 | [diff] [blame] | 1166 | .. c:function:: PyObject* PyInterpreterState_GetDict(PyInterpreterState *interp) |
| 1167 | |
| 1168 | Return a dictionary in which interpreter-specific data may be stored. |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 1169 | If this function returns ``NULL`` then no exception has been raised and |
Eric Snow | d2fdd1f | 2019-03-15 17:47:43 -0600 | [diff] [blame] | 1170 | the caller should assume no interpreter-specific dict is available. |
| 1171 | |
| 1172 | This is not a replacement for :c:func:`PyModule_GetState()`, which |
| 1173 | extensions should use to store interpreter-specific state information. |
| 1174 | |
| 1175 | .. versionadded:: 3.8 |
| 1176 | |
Victor Stinner | 0b72b23 | 2020-03-12 23:18:39 +0100 | [diff] [blame] | 1177 | .. c:type:: PyObject* (*_PyFrameEvalFunction)(PyThreadState *tstate, PyFrameObject *frame, int throwflag) |
| 1178 | |
| 1179 | Type of a frame evaluation function. |
| 1180 | |
| 1181 | The *throwflag* parameter is used by the ``throw()`` method of generators: |
| 1182 | if non-zero, handle the current exception. |
| 1183 | |
| 1184 | .. versionchanged:: 3.9 |
| 1185 | The function now takes a *tstate* parameter. |
| 1186 | |
| 1187 | .. c:function:: _PyFrameEvalFunction _PyInterpreterState_GetEvalFrameFunc(PyInterpreterState *interp) |
| 1188 | |
| 1189 | Get the frame evaluation function. |
| 1190 | |
| 1191 | See the :pep:`523` "Adding a frame evaluation API to CPython". |
| 1192 | |
| 1193 | .. versionadded:: 3.9 |
| 1194 | |
| 1195 | .. c:function:: void _PyInterpreterState_SetEvalFrameFunc(PyInterpreterState *interp, _PyFrameEvalFunction eval_frame); |
| 1196 | |
| 1197 | Set the frame evaluation function. |
| 1198 | |
| 1199 | See the :pep:`523` "Adding a frame evaluation API to CPython". |
| 1200 | |
| 1201 | .. versionadded:: 3.9 |
| 1202 | |
Eric Snow | d2fdd1f | 2019-03-15 17:47:43 -0600 | [diff] [blame] | 1203 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1204 | .. c:function:: PyObject* PyThreadState_GetDict() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1205 | |
| 1206 | Return a dictionary in which extensions can store thread-specific state |
| 1207 | information. Each extension should use a unique key to use to store state in |
| 1208 | the dictionary. It is okay to call this function when no current thread state |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 1209 | is available. If this function returns ``NULL``, no exception has been raised and |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1210 | the caller should assume no current thread state is available. |
| 1211 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1212 | |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 1213 | .. c:function:: int PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1214 | |
| 1215 | Asynchronously raise an exception in a thread. The *id* argument is the thread |
| 1216 | id of the target thread; *exc* is the exception object to be raised. This |
| 1217 | function does not steal any references to *exc*. To prevent naive misuse, you |
| 1218 | must write your own C extension to call this. Must be called with the GIL held. |
| 1219 | Returns the number of thread states modified; this is normally one, but will be |
| 1220 | zero if the thread id isn't found. If *exc* is :const:`NULL`, the pending |
| 1221 | exception (if any) for the thread is cleared. This raises no exceptions. |
| 1222 | |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 1223 | .. versionchanged:: 3.7 |
| 1224 | The type of the *id* parameter changed from :c:type:`long` to |
| 1225 | :c:type:`unsigned long`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1226 | |
Antoine Pitrou | bedd2c2 | 2011-01-15 12:54:19 +0000 | [diff] [blame] | 1227 | .. c:function:: void PyEval_AcquireThread(PyThreadState *tstate) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1228 | |
Antoine Pitrou | bedd2c2 | 2011-01-15 12:54:19 +0000 | [diff] [blame] | 1229 | Acquire the global interpreter lock and set the current thread state to |
Victor Stinner | 17c68b8 | 2020-01-30 12:20:48 +0100 | [diff] [blame] | 1230 | *tstate*, which must not be ``NULL``. The lock must have been created earlier. |
Antoine Pitrou | bedd2c2 | 2011-01-15 12:54:19 +0000 | [diff] [blame] | 1231 | If this thread already has the lock, deadlock ensues. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1232 | |
Joannah Nanjekye | f781d20 | 2019-04-29 04:38:45 -0400 | [diff] [blame] | 1233 | .. note:: |
| 1234 | Calling this function from a thread when the runtime is finalizing |
| 1235 | will terminate the thread, even if the thread was not created by Python. |
| 1236 | You can use :c:func:`_Py_IsFinalizing` or :func:`sys.is_finalizing` to |
| 1237 | check if the interpreter is in process of being finalized before calling |
| 1238 | this function to avoid unwanted termination. |
| 1239 | |
| 1240 | .. versionchanged:: 3.8 |
| 1241 | Updated to be consistent with :c:func:`PyEval_RestoreThread`, |
| 1242 | :c:func:`Py_END_ALLOW_THREADS`, and :c:func:`PyGILState_Ensure`, |
| 1243 | and terminate the current thread if called while the interpreter is finalizing. |
| 1244 | |
Antoine Pitrou | 5ace8e9 | 2011-01-15 13:11:48 +0000 | [diff] [blame] | 1245 | :c:func:`PyEval_RestoreThread` is a higher-level function which is always |
Victor Stinner | 2914bb3 | 2018-01-29 11:57:45 +0100 | [diff] [blame] | 1246 | available (even when threads have not been initialized). |
Antoine Pitrou | 5ace8e9 | 2011-01-15 13:11:48 +0000 | [diff] [blame] | 1247 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1248 | |
Antoine Pitrou | bedd2c2 | 2011-01-15 12:54:19 +0000 | [diff] [blame] | 1249 | .. c:function:: void PyEval_ReleaseThread(PyThreadState *tstate) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1250 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 1251 | Reset the current thread state to ``NULL`` and release the global interpreter |
Antoine Pitrou | bedd2c2 | 2011-01-15 12:54:19 +0000 | [diff] [blame] | 1252 | lock. The lock must have been created earlier and must be held by the current |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 1253 | thread. The *tstate* argument, which must not be ``NULL``, is only used to check |
Antoine Pitrou | bedd2c2 | 2011-01-15 12:54:19 +0000 | [diff] [blame] | 1254 | that it represents the current thread state --- if it isn't, a fatal error is |
| 1255 | reported. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1256 | |
Antoine Pitrou | 5ace8e9 | 2011-01-15 13:11:48 +0000 | [diff] [blame] | 1257 | :c:func:`PyEval_SaveThread` is a higher-level function which is always |
Victor Stinner | 2914bb3 | 2018-01-29 11:57:45 +0100 | [diff] [blame] | 1258 | available (even when threads have not been initialized). |
Antoine Pitrou | 5ace8e9 | 2011-01-15 13:11:48 +0000 | [diff] [blame] | 1259 | |
Antoine Pitrou | bedd2c2 | 2011-01-15 12:54:19 +0000 | [diff] [blame] | 1260 | |
| 1261 | .. c:function:: void PyEval_AcquireLock() |
| 1262 | |
| 1263 | Acquire the global interpreter lock. The lock must have been created earlier. |
| 1264 | If this thread already has the lock, a deadlock ensues. |
| 1265 | |
Antoine Pitrou | 5ace8e9 | 2011-01-15 13:11:48 +0000 | [diff] [blame] | 1266 | .. deprecated:: 3.2 |
Antoine Pitrou | f5cf435 | 2011-01-15 14:31:49 +0000 | [diff] [blame] | 1267 | This function does not update the current thread state. Please use |
Antoine Pitrou | 5ace8e9 | 2011-01-15 13:11:48 +0000 | [diff] [blame] | 1268 | :c:func:`PyEval_RestoreThread` or :c:func:`PyEval_AcquireThread` |
| 1269 | instead. |
| 1270 | |
Joannah Nanjekye | f781d20 | 2019-04-29 04:38:45 -0400 | [diff] [blame] | 1271 | .. note:: |
| 1272 | Calling this function from a thread when the runtime is finalizing |
| 1273 | will terminate the thread, even if the thread was not created by Python. |
| 1274 | You can use :c:func:`_Py_IsFinalizing` or :func:`sys.is_finalizing` to |
| 1275 | check if the interpreter is in process of being finalized before calling |
| 1276 | this function to avoid unwanted termination. |
| 1277 | |
| 1278 | .. versionchanged:: 3.8 |
| 1279 | Updated to be consistent with :c:func:`PyEval_RestoreThread`, |
| 1280 | :c:func:`Py_END_ALLOW_THREADS`, and :c:func:`PyGILState_Ensure`, |
| 1281 | and terminate the current thread if called while the interpreter is finalizing. |
| 1282 | |
Antoine Pitrou | bedd2c2 | 2011-01-15 12:54:19 +0000 | [diff] [blame] | 1283 | |
| 1284 | .. c:function:: void PyEval_ReleaseLock() |
| 1285 | |
| 1286 | Release the global interpreter lock. The lock must have been created earlier. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1287 | |
Antoine Pitrou | 5ace8e9 | 2011-01-15 13:11:48 +0000 | [diff] [blame] | 1288 | .. deprecated:: 3.2 |
Antoine Pitrou | f5cf435 | 2011-01-15 14:31:49 +0000 | [diff] [blame] | 1289 | This function does not update the current thread state. Please use |
Antoine Pitrou | 5ace8e9 | 2011-01-15 13:11:48 +0000 | [diff] [blame] | 1290 | :c:func:`PyEval_SaveThread` or :c:func:`PyEval_ReleaseThread` |
| 1291 | instead. |
| 1292 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1293 | |
Nick Coghlan | 2ab5b09 | 2015-07-03 19:49:15 +1000 | [diff] [blame] | 1294 | .. _sub-interpreter-support: |
| 1295 | |
Antoine Pitrou | 8b50b83 | 2011-01-15 11:57:42 +0000 | [diff] [blame] | 1296 | Sub-interpreter support |
| 1297 | ======================= |
| 1298 | |
| 1299 | While in most uses, you will only embed a single Python interpreter, there |
| 1300 | are cases where you need to create several independent interpreters in the |
Joannah Nanjekye | 854d0a4 | 2019-08-02 12:50:22 -0300 | [diff] [blame] | 1301 | same process and perhaps even in the same thread. Sub-interpreters allow |
| 1302 | you to do that. |
| 1303 | |
| 1304 | The "main" interpreter is the first one created when the runtime initializes. |
| 1305 | It is usually the only Python interpreter in a process. Unlike sub-interpreters, |
| 1306 | the main interpreter has unique process-global responsibilities like signal |
| 1307 | handling. It is also responsible for execution during runtime initialization and |
| 1308 | is usually the active interpreter during runtime finalization. The |
Gurupad Hegde | 6c7bb38 | 2019-12-28 17:16:02 -0500 | [diff] [blame] | 1309 | :c:func:`PyInterpreterState_Main` function returns a pointer to its state. |
Joannah Nanjekye | 854d0a4 | 2019-08-02 12:50:22 -0300 | [diff] [blame] | 1310 | |
| 1311 | You can switch between sub-interpreters using the :c:func:`PyThreadState_Swap` |
| 1312 | function. You can create and destroy them using the following functions: |
Antoine Pitrou | 8b50b83 | 2011-01-15 11:57:42 +0000 | [diff] [blame] | 1313 | |
| 1314 | |
| 1315 | .. c:function:: PyThreadState* Py_NewInterpreter() |
| 1316 | |
| 1317 | .. index:: |
| 1318 | module: builtins |
| 1319 | module: __main__ |
| 1320 | module: sys |
| 1321 | single: stdout (in module sys) |
| 1322 | single: stderr (in module sys) |
| 1323 | single: stdin (in module sys) |
| 1324 | |
| 1325 | Create a new sub-interpreter. This is an (almost) totally separate environment |
| 1326 | for the execution of Python code. In particular, the new interpreter has |
| 1327 | separate, independent versions of all imported modules, including the |
| 1328 | fundamental modules :mod:`builtins`, :mod:`__main__` and :mod:`sys`. The |
| 1329 | table of loaded modules (``sys.modules``) and the module search path |
| 1330 | (``sys.path``) are also separate. The new environment has no ``sys.argv`` |
| 1331 | variable. It has new standard I/O stream file objects ``sys.stdin``, |
| 1332 | ``sys.stdout`` and ``sys.stderr`` (however these refer to the same underlying |
| 1333 | file descriptors). |
| 1334 | |
| 1335 | The return value points to the first thread state created in the new |
| 1336 | sub-interpreter. This thread state is made in the current thread state. |
| 1337 | Note that no actual thread is created; see the discussion of thread states |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 1338 | below. If creation of the new interpreter is unsuccessful, ``NULL`` is |
Antoine Pitrou | 8b50b83 | 2011-01-15 11:57:42 +0000 | [diff] [blame] | 1339 | returned; no exception is set since the exception state is stored in the |
| 1340 | current thread state and there may not be a current thread state. (Like all |
| 1341 | other Python/C API functions, the global interpreter lock must be held before |
| 1342 | calling this function and is still held when it returns; however, unlike most |
| 1343 | other Python/C API functions, there needn't be a current thread state on |
| 1344 | entry.) |
| 1345 | |
| 1346 | .. index:: |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1347 | single: Py_FinalizeEx() |
Antoine Pitrou | 8b50b83 | 2011-01-15 11:57:42 +0000 | [diff] [blame] | 1348 | single: Py_Initialize() |
| 1349 | |
Petr Viktorin | 6c5d661 | 2020-01-09 13:05:18 +0100 | [diff] [blame] | 1350 | Extension modules are shared between (sub-)interpreters as follows: |
| 1351 | |
| 1352 | * For modules using multi-phase initialization, |
| 1353 | e.g. :c:func:`PyModule_FromDefAndSpec`, a separate module object is |
| 1354 | created and initialized for each interpreter. |
| 1355 | Only C-level static and global variables are shared between these |
| 1356 | module objects. |
| 1357 | |
| 1358 | * For modules using single-phase initialization, |
| 1359 | e.g. :c:func:`PyModule_Create`, the first time a particular extension |
| 1360 | is imported, it is initialized normally, and a (shallow) copy of its |
| 1361 | module's dictionary is squirreled away. |
| 1362 | When the same extension is imported by another (sub-)interpreter, a new |
| 1363 | module is initialized and filled with the contents of this copy; the |
| 1364 | extension's ``init`` function is not called. |
| 1365 | Objects in the module's dictionary thus end up shared across |
| 1366 | (sub-)interpreters, which might cause unwanted behavior (see |
| 1367 | `Bugs and caveats`_ below). |
| 1368 | |
| 1369 | Note that this is different from what happens when an extension is |
| 1370 | imported after the interpreter has been completely re-initialized by |
| 1371 | calling :c:func:`Py_FinalizeEx` and :c:func:`Py_Initialize`; in that |
| 1372 | case, the extension's ``initmodule`` function *is* called again. |
| 1373 | As with multi-phase initialization, this means that only C-level static |
| 1374 | and global variables are shared between these modules. |
Antoine Pitrou | 8b50b83 | 2011-01-15 11:57:42 +0000 | [diff] [blame] | 1375 | |
| 1376 | .. index:: single: close() (in module os) |
| 1377 | |
| 1378 | |
| 1379 | .. c:function:: void Py_EndInterpreter(PyThreadState *tstate) |
| 1380 | |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1381 | .. index:: single: Py_FinalizeEx() |
Antoine Pitrou | 8b50b83 | 2011-01-15 11:57:42 +0000 | [diff] [blame] | 1382 | |
| 1383 | Destroy the (sub-)interpreter represented by the given thread state. The given |
| 1384 | thread state must be the current thread state. See the discussion of thread |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 1385 | states below. When the call returns, the current thread state is ``NULL``. All |
Antoine Pitrou | 8b50b83 | 2011-01-15 11:57:42 +0000 | [diff] [blame] | 1386 | thread states associated with this interpreter are destroyed. (The global |
| 1387 | interpreter lock must be held before calling this function and is still held |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 1388 | when it returns.) :c:func:`Py_FinalizeEx` will destroy all sub-interpreters that |
Antoine Pitrou | 8b50b83 | 2011-01-15 11:57:42 +0000 | [diff] [blame] | 1389 | haven't been explicitly destroyed at that point. |
| 1390 | |
| 1391 | |
| 1392 | Bugs and caveats |
| 1393 | ---------------- |
| 1394 | |
| 1395 | Because sub-interpreters (and the main interpreter) are part of the same |
| 1396 | process, the insulation between them isn't perfect --- for example, using |
| 1397 | low-level file operations like :func:`os.close` they can |
| 1398 | (accidentally or maliciously) affect each other's open files. Because of the |
| 1399 | way extensions are shared between (sub-)interpreters, some extensions may not |
Petr Viktorin | 6c5d661 | 2020-01-09 13:05:18 +0100 | [diff] [blame] | 1400 | work properly; this is especially likely when using single-phase initialization |
| 1401 | or (static) global variables. |
| 1402 | It is possible to insert objects created in one sub-interpreter into |
| 1403 | a namespace of another (sub-)interpreter; this should be avoided if possible. |
| 1404 | |
| 1405 | Special care should be taken to avoid sharing user-defined functions, |
| 1406 | methods, instances or classes between sub-interpreters, since import |
| 1407 | operations executed by such objects may affect the wrong (sub-)interpreter's |
| 1408 | dictionary of loaded modules. It is equally important to avoid sharing |
| 1409 | objects from which the above are reachable. |
Antoine Pitrou | 8b50b83 | 2011-01-15 11:57:42 +0000 | [diff] [blame] | 1410 | |
Antoine Pitrou | f1dfe73 | 2011-01-15 12:10:48 +0000 | [diff] [blame] | 1411 | Also note that combining this functionality with :c:func:`PyGILState_\*` APIs |
Ezio Melotti | d92ab08 | 2011-05-05 14:19:48 +0300 | [diff] [blame] | 1412 | is delicate, because these APIs assume a bijection between Python thread states |
Antoine Pitrou | f1dfe73 | 2011-01-15 12:10:48 +0000 | [diff] [blame] | 1413 | and OS-level threads, an assumption broken by the presence of sub-interpreters. |
| 1414 | It is highly recommended that you don't switch sub-interpreters between a pair |
| 1415 | of matching :c:func:`PyGILState_Ensure` and :c:func:`PyGILState_Release` calls. |
| 1416 | Furthermore, extensions (such as :mod:`ctypes`) using these APIs to allow calling |
| 1417 | of Python code from non-Python created threads will probably be broken when using |
| 1418 | sub-interpreters. |
Antoine Pitrou | 8b50b83 | 2011-01-15 11:57:42 +0000 | [diff] [blame] | 1419 | |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1420 | |
| 1421 | Asynchronous Notifications |
| 1422 | ========================== |
| 1423 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 1424 | A mechanism is provided to make asynchronous notifications to the main |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1425 | interpreter thread. These notifications take the form of a function |
Antoine Pitrou | 1a67bee | 2013-09-30 21:35:44 +0200 | [diff] [blame] | 1426 | pointer and a void pointer argument. |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1427 | |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1428 | |
Ezio Melotti | a782cca | 2011-04-28 00:53:14 +0300 | [diff] [blame] | 1429 | .. c:function:: int Py_AddPendingCall(int (*func)(void *), void *arg) |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1430 | |
| 1431 | .. index:: single: Py_AddPendingCall() |
| 1432 | |
Antoine Pitrou | 1a67bee | 2013-09-30 21:35:44 +0200 | [diff] [blame] | 1433 | Schedule a function to be called from the main interpreter thread. On |
Serhiy Storchaka | 1ecf7d2 | 2016-10-27 21:41:19 +0300 | [diff] [blame] | 1434 | success, ``0`` is returned and *func* is queued for being called in the |
| 1435 | main thread. On failure, ``-1`` is returned without setting any exception. |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1436 | |
Antoine Pitrou | 1a67bee | 2013-09-30 21:35:44 +0200 | [diff] [blame] | 1437 | When successfully queued, *func* will be *eventually* called from the |
| 1438 | main interpreter thread with the argument *arg*. It will be called |
| 1439 | asynchronously with respect to normally running Python code, but with |
| 1440 | both these conditions met: |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1441 | |
Antoine Pitrou | 1a67bee | 2013-09-30 21:35:44 +0200 | [diff] [blame] | 1442 | * on a :term:`bytecode` boundary; |
| 1443 | * with the main thread holding the :term:`global interpreter lock` |
| 1444 | (*func* can therefore use the full C API). |
| 1445 | |
Serhiy Storchaka | 1ecf7d2 | 2016-10-27 21:41:19 +0300 | [diff] [blame] | 1446 | *func* must return ``0`` on success, or ``-1`` on failure with an exception |
Antoine Pitrou | 1a67bee | 2013-09-30 21:35:44 +0200 | [diff] [blame] | 1447 | set. *func* won't be interrupted to perform another asynchronous |
| 1448 | notification recursively, but it can still be interrupted to switch |
| 1449 | threads if the global interpreter lock is released. |
| 1450 | |
| 1451 | This function doesn't need a current thread state to run, and it doesn't |
| 1452 | need the global interpreter lock. |
| 1453 | |
Victor Stinner | 50e6e99 | 2020-03-19 02:41:21 +0100 | [diff] [blame] | 1454 | To call this function in a subinterpreter, the caller must hold the GIL. |
| 1455 | Otherwise, the function *func* can be scheduled to be called from the wrong |
| 1456 | interpreter. |
| 1457 | |
Antoine Pitrou | 1a67bee | 2013-09-30 21:35:44 +0200 | [diff] [blame] | 1458 | .. warning:: |
| 1459 | This is a low-level function, only useful for very special cases. |
| 1460 | There is no guarantee that *func* will be called as quick as |
| 1461 | possible. If the main thread is busy executing a system call, |
| 1462 | *func* won't be called before the system call returns. This |
| 1463 | function is generally **not** suitable for calling Python code from |
| 1464 | arbitrary C threads. Instead, use the :ref:`PyGILState API<gilstate>`. |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1465 | |
Victor Stinner | 50e6e99 | 2020-03-19 02:41:21 +0100 | [diff] [blame] | 1466 | .. versionchanged:: 3.9 |
| 1467 | If this function is called in a subinterpreter, the function *func* is |
| 1468 | now scheduled to be called from the subinterpreter, rather than being |
| 1469 | called from the main interpreter. Each subinterpreter now has its own |
| 1470 | list of scheduled calls. |
| 1471 | |
Georg Brandl | 705d9d5 | 2009-05-05 09:29:50 +0000 | [diff] [blame] | 1472 | .. versionadded:: 3.1 |
Benjamin Peterson | a54c909 | 2009-01-13 02:11:23 +0000 | [diff] [blame] | 1473 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1474 | .. _profiling: |
| 1475 | |
| 1476 | Profiling and Tracing |
| 1477 | ===================== |
| 1478 | |
| 1479 | .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org> |
| 1480 | |
| 1481 | |
| 1482 | The Python interpreter provides some low-level support for attaching profiling |
| 1483 | and execution tracing facilities. These are used for profiling, debugging, and |
| 1484 | coverage analysis tools. |
| 1485 | |
Georg Brandl | e6bcc91 | 2008-05-12 18:05:20 +0000 | [diff] [blame] | 1486 | This C interface allows the profiling or tracing code to avoid the overhead of |
| 1487 | calling through Python-level callable objects, making a direct C function call |
| 1488 | instead. The essential attributes of the facility have not changed; the |
| 1489 | interface allows trace functions to be installed per-thread, and the basic |
| 1490 | events reported to the trace function are the same as had been reported to the |
| 1491 | Python-level trace functions in previous versions. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1492 | |
| 1493 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1494 | .. c:type:: int (*Py_tracefunc)(PyObject *obj, PyFrameObject *frame, int what, PyObject *arg) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1495 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1496 | The type of the trace function registered using :c:func:`PyEval_SetProfile` and |
| 1497 | :c:func:`PyEval_SetTrace`. The first parameter is the object passed to the |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1498 | registration function as *obj*, *frame* is the frame object to which the event |
| 1499 | pertains, *what* is one of the constants :const:`PyTrace_CALL`, |
| 1500 | :const:`PyTrace_EXCEPTION`, :const:`PyTrace_LINE`, :const:`PyTrace_RETURN`, |
Xiang Zhang | 255f7a2 | 2018-01-28 17:53:38 +0800 | [diff] [blame] | 1501 | :const:`PyTrace_C_CALL`, :const:`PyTrace_C_EXCEPTION`, :const:`PyTrace_C_RETURN`, |
| 1502 | or :const:`PyTrace_OPCODE`, and *arg* depends on the value of *what*: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1503 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 1504 | +------------------------------+----------------------------------------+ |
| 1505 | | Value of *what* | Meaning of *arg* | |
| 1506 | +==============================+========================================+ |
| 1507 | | :const:`PyTrace_CALL` | Always :c:data:`Py_None`. | |
| 1508 | +------------------------------+----------------------------------------+ |
| 1509 | | :const:`PyTrace_EXCEPTION` | Exception information as returned by | |
| 1510 | | | :func:`sys.exc_info`. | |
| 1511 | +------------------------------+----------------------------------------+ |
| 1512 | | :const:`PyTrace_LINE` | Always :c:data:`Py_None`. | |
| 1513 | +------------------------------+----------------------------------------+ |
| 1514 | | :const:`PyTrace_RETURN` | Value being returned to the caller, | |
| 1515 | | | or ``NULL`` if caused by an exception. | |
| 1516 | +------------------------------+----------------------------------------+ |
| 1517 | | :const:`PyTrace_C_CALL` | Function object being called. | |
| 1518 | +------------------------------+----------------------------------------+ |
| 1519 | | :const:`PyTrace_C_EXCEPTION` | Function object being called. | |
| 1520 | +------------------------------+----------------------------------------+ |
| 1521 | | :const:`PyTrace_C_RETURN` | Function object being called. | |
| 1522 | +------------------------------+----------------------------------------+ |
| 1523 | | :const:`PyTrace_OPCODE` | Always :c:data:`Py_None`. | |
| 1524 | +------------------------------+----------------------------------------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1525 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1526 | .. c:var:: int PyTrace_CALL |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1527 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1528 | The value of the *what* parameter to a :c:type:`Py_tracefunc` function when a new |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1529 | call to a function or method is being reported, or a new entry into a generator. |
| 1530 | Note that the creation of the iterator for a generator function is not reported |
| 1531 | as there is no control transfer to the Python bytecode in the corresponding |
| 1532 | frame. |
| 1533 | |
| 1534 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1535 | .. c:var:: int PyTrace_EXCEPTION |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1536 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1537 | The value of the *what* parameter to a :c:type:`Py_tracefunc` function when an |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1538 | exception has been raised. The callback function is called with this value for |
| 1539 | *what* when after any bytecode is processed after which the exception becomes |
| 1540 | set within the frame being executed. The effect of this is that as exception |
| 1541 | propagation causes the Python stack to unwind, the callback is called upon |
| 1542 | return to each frame as the exception propagates. Only trace functions receives |
| 1543 | these events; they are not needed by the profiler. |
| 1544 | |
| 1545 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1546 | .. c:var:: int PyTrace_LINE |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1547 | |
Xiang Zhang | 255f7a2 | 2018-01-28 17:53:38 +0800 | [diff] [blame] | 1548 | The value passed as the *what* parameter to a :c:type:`Py_tracefunc` function |
| 1549 | (but not a profiling function) when a line-number event is being reported. |
| 1550 | It may be disabled for a frame by setting :attr:`f_trace_lines` to *0* on that frame. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1551 | |
| 1552 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1553 | .. c:var:: int PyTrace_RETURN |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1554 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1555 | The value for the *what* parameter to :c:type:`Py_tracefunc` functions when a |
Xiang Zhang | 79db11c | 2018-01-28 22:54:42 +0800 | [diff] [blame] | 1556 | call is about to return. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1557 | |
| 1558 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1559 | .. c:var:: int PyTrace_C_CALL |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1560 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1561 | The value for the *what* parameter to :c:type:`Py_tracefunc` functions when a C |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1562 | function is about to be called. |
| 1563 | |
| 1564 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1565 | .. c:var:: int PyTrace_C_EXCEPTION |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1566 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1567 | The value for the *what* parameter to :c:type:`Py_tracefunc` functions when a C |
Georg Brandl | 7cb1319 | 2010-08-03 12:06:29 +0000 | [diff] [blame] | 1568 | function has raised an exception. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1569 | |
| 1570 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1571 | .. c:var:: int PyTrace_C_RETURN |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1572 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1573 | The value for the *what* parameter to :c:type:`Py_tracefunc` functions when a C |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1574 | function has returned. |
| 1575 | |
| 1576 | |
Xiang Zhang | 255f7a2 | 2018-01-28 17:53:38 +0800 | [diff] [blame] | 1577 | .. c:var:: int PyTrace_OPCODE |
| 1578 | |
| 1579 | The value for the *what* parameter to :c:type:`Py_tracefunc` functions (but not |
| 1580 | profiling functions) when a new opcode is about to be executed. This event is |
| 1581 | not emitted by default: it must be explicitly requested by setting |
| 1582 | :attr:`f_trace_opcodes` to *1* on the frame. |
| 1583 | |
| 1584 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1585 | .. c:function:: void PyEval_SetProfile(Py_tracefunc func, PyObject *obj) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1586 | |
| 1587 | Set the profiler function to *func*. The *obj* parameter is passed to the |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 1588 | function as its first parameter, and may be any Python object, or ``NULL``. If |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1589 | the profile function needs to maintain state, using a different value for *obj* |
| 1590 | for each thread provides a convenient and thread-safe place to store it. The |
Pablo Galindo | 131fd7f | 2018-01-24 12:57:49 +0000 | [diff] [blame] | 1591 | profile function is called for all monitored events except :const:`PyTrace_LINE` |
Xiang Zhang | 255f7a2 | 2018-01-28 17:53:38 +0800 | [diff] [blame] | 1592 | :const:`PyTrace_OPCODE` and :const:`PyTrace_EXCEPTION`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1593 | |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 1594 | The caller must hold the :term:`GIL`. |
| 1595 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1596 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1597 | .. c:function:: void PyEval_SetTrace(Py_tracefunc func, PyObject *obj) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1598 | |
| 1599 | Set the tracing function to *func*. This is similar to |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1600 | :c:func:`PyEval_SetProfile`, except the tracing function does receive line-number |
Xiang Zhang | 255f7a2 | 2018-01-28 17:53:38 +0800 | [diff] [blame] | 1601 | events and per-opcode events, but does not receive any event related to C function |
| 1602 | objects being called. Any trace function registered using :c:func:`PyEval_SetTrace` |
| 1603 | will not receive :const:`PyTrace_C_CALL`, :const:`PyTrace_C_EXCEPTION` or |
| 1604 | :const:`PyTrace_C_RETURN` as a value for the *what* parameter. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1605 | |
Victor Stinner | 309d7cc | 2020-03-13 16:39:12 +0100 | [diff] [blame] | 1606 | The caller must hold the :term:`GIL`. |
| 1607 | |
| 1608 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1609 | .. _advanced-debugging: |
| 1610 | |
| 1611 | Advanced Debugger Support |
| 1612 | ========================= |
| 1613 | |
| 1614 | .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org> |
| 1615 | |
| 1616 | |
| 1617 | These functions are only intended to be used by advanced debugging tools. |
| 1618 | |
| 1619 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1620 | .. c:function:: PyInterpreterState* PyInterpreterState_Head() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1621 | |
| 1622 | Return the interpreter state object at the head of the list of all such objects. |
| 1623 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1624 | |
Joannah Nanjekye | 8c61739 | 2019-04-01 18:08:43 +0300 | [diff] [blame] | 1625 | .. c:function:: PyInterpreterState* PyInterpreterState_Main() |
| 1626 | |
| 1627 | Return the main interpreter state object. |
| 1628 | |
| 1629 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1630 | .. c:function:: PyInterpreterState* PyInterpreterState_Next(PyInterpreterState *interp) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1631 | |
| 1632 | Return the next interpreter state object after *interp* from the list of all |
| 1633 | such objects. |
| 1634 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1635 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1636 | .. c:function:: PyThreadState * PyInterpreterState_ThreadHead(PyInterpreterState *interp) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1637 | |
Benjamin Peterson | 82f34ad | 2015-01-13 09:17:24 -0500 | [diff] [blame] | 1638 | Return the pointer to the first :c:type:`PyThreadState` object in the list of |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1639 | threads associated with the interpreter *interp*. |
| 1640 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1641 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1642 | .. c:function:: PyThreadState* PyThreadState_Next(PyThreadState *tstate) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1643 | |
| 1644 | Return the next thread state object after *tstate* from the list of all such |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1645 | objects belonging to the same :c:type:`PyInterpreterState` object. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1646 | |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 1647 | |
| 1648 | .. _thread-local-storage: |
| 1649 | |
| 1650 | Thread Local Storage Support |
| 1651 | ============================ |
| 1652 | |
| 1653 | .. sectionauthor:: Masayuki Yamamoto <ma3yuki.8mamo10@gmail.com> |
| 1654 | |
| 1655 | The Python interpreter provides low-level support for thread-local storage |
| 1656 | (TLS) which wraps the underlying native TLS implementation to support the |
| 1657 | Python-level thread local storage API (:class:`threading.local`). The |
| 1658 | CPython C level APIs are similar to those offered by pthreads and Windows: |
Victor Stinner | 474652f | 2020-08-13 22:11:50 +0200 | [diff] [blame] | 1659 | use a thread key and functions to associate a :c:type:`void*` value per |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 1660 | thread. |
| 1661 | |
| 1662 | The GIL does *not* need to be held when calling these functions; they supply |
| 1663 | their own locking. |
| 1664 | |
| 1665 | Note that :file:`Python.h` does not include the declaration of the TLS APIs, |
| 1666 | you need to include :file:`pythread.h` to use thread-local storage. |
| 1667 | |
| 1668 | .. note:: |
| 1669 | None of these API functions handle memory management on behalf of the |
Victor Stinner | 474652f | 2020-08-13 22:11:50 +0200 | [diff] [blame] | 1670 | :c:type:`void*` values. You need to allocate and deallocate them yourself. |
| 1671 | If the :c:type:`void*` values happen to be :c:type:`PyObject*`, these |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 1672 | functions don't do refcount operations on them either. |
| 1673 | |
| 1674 | .. _thread-specific-storage-api: |
| 1675 | |
| 1676 | Thread Specific Storage (TSS) API |
| 1677 | --------------------------------- |
| 1678 | |
| 1679 | TSS API is introduced to supersede the use of the existing TLS API within the |
| 1680 | CPython interpreter. This API uses a new type :c:type:`Py_tss_t` instead of |
| 1681 | :c:type:`int` to represent thread keys. |
| 1682 | |
| 1683 | .. versionadded:: 3.7 |
| 1684 | |
| 1685 | .. seealso:: "A New C-API for Thread-Local Storage in CPython" (:pep:`539`) |
| 1686 | |
| 1687 | |
| 1688 | .. c:type:: Py_tss_t |
| 1689 | |
| 1690 | This data structure represents the state of a thread key, the definition of |
| 1691 | which may depend on the underlying TLS implementation, and it has an |
| 1692 | internal field representing the key's initialization state. There are no |
| 1693 | public members in this structure. |
| 1694 | |
| 1695 | When :ref:`Py_LIMITED_API <stable>` is not defined, static allocation of |
| 1696 | this type by :c:macro:`Py_tss_NEEDS_INIT` is allowed. |
| 1697 | |
| 1698 | |
| 1699 | .. c:macro:: Py_tss_NEEDS_INIT |
| 1700 | |
Masayuki Yamamoto | 831d61d | 2017-10-24 21:58:16 +0900 | [diff] [blame] | 1701 | This macro expands to the initializer for :c:type:`Py_tss_t` variables. |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 1702 | Note that this macro won't be defined with :ref:`Py_LIMITED_API <stable>`. |
| 1703 | |
| 1704 | |
| 1705 | Dynamic Allocation |
| 1706 | ~~~~~~~~~~~~~~~~~~ |
| 1707 | |
| 1708 | Dynamic allocation of the :c:type:`Py_tss_t`, required in extension modules |
| 1709 | built with :ref:`Py_LIMITED_API <stable>`, where static allocation of this type |
| 1710 | is not possible due to its implementation being opaque at build time. |
| 1711 | |
| 1712 | |
| 1713 | .. c:function:: Py_tss_t* PyThread_tss_alloc() |
| 1714 | |
| 1715 | Return a value which is the same state as a value initialized with |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 1716 | :c:macro:`Py_tss_NEEDS_INIT`, or ``NULL`` in the case of dynamic allocation |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 1717 | failure. |
| 1718 | |
| 1719 | |
| 1720 | .. c:function:: void PyThread_tss_free(Py_tss_t *key) |
| 1721 | |
| 1722 | Free the given *key* allocated by :c:func:`PyThread_tss_alloc`, after |
| 1723 | first calling :c:func:`PyThread_tss_delete` to ensure any associated |
| 1724 | thread locals have been unassigned. This is a no-op if the *key* |
| 1725 | argument is `NULL`. |
| 1726 | |
| 1727 | .. note:: |
| 1728 | A freed key becomes a dangling pointer, you should reset the key to |
| 1729 | `NULL`. |
| 1730 | |
| 1731 | |
| 1732 | Methods |
| 1733 | ~~~~~~~ |
| 1734 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 1735 | The parameter *key* of these functions must not be ``NULL``. Moreover, the |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 1736 | behaviors of :c:func:`PyThread_tss_set` and :c:func:`PyThread_tss_get` are |
| 1737 | undefined if the given :c:type:`Py_tss_t` has not been initialized by |
| 1738 | :c:func:`PyThread_tss_create`. |
| 1739 | |
| 1740 | |
| 1741 | .. c:function:: int PyThread_tss_is_created(Py_tss_t *key) |
| 1742 | |
| 1743 | Return a non-zero value if the given :c:type:`Py_tss_t` has been initialized |
| 1744 | by :c:func:`PyThread_tss_create`. |
| 1745 | |
| 1746 | |
| 1747 | .. c:function:: int PyThread_tss_create(Py_tss_t *key) |
| 1748 | |
| 1749 | Return a zero value on successful initialization of a TSS key. The behavior |
| 1750 | is undefined if the value pointed to by the *key* argument is not |
| 1751 | initialized by :c:macro:`Py_tss_NEEDS_INIT`. This function can be called |
| 1752 | repeatedly on the same key -- calling it on an already initialized key is a |
| 1753 | no-op and immediately returns success. |
| 1754 | |
| 1755 | |
| 1756 | .. c:function:: void PyThread_tss_delete(Py_tss_t *key) |
| 1757 | |
| 1758 | Destroy a TSS key to forget the values associated with the key across all |
| 1759 | threads, and change the key's initialization state to uninitialized. A |
| 1760 | destroyed key is able to be initialized again by |
| 1761 | :c:func:`PyThread_tss_create`. This function can be called repeatedly on |
| 1762 | the same key -- calling it on an already destroyed key is a no-op. |
| 1763 | |
| 1764 | |
| 1765 | .. c:function:: int PyThread_tss_set(Py_tss_t *key, void *value) |
| 1766 | |
Victor Stinner | 474652f | 2020-08-13 22:11:50 +0200 | [diff] [blame] | 1767 | Return a zero value to indicate successfully associating a :c:type:`void*` |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 1768 | value with a TSS key in the current thread. Each thread has a distinct |
Victor Stinner | 474652f | 2020-08-13 22:11:50 +0200 | [diff] [blame] | 1769 | mapping of the key to a :c:type:`void*` value. |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 1770 | |
| 1771 | |
| 1772 | .. c:function:: void* PyThread_tss_get(Py_tss_t *key) |
| 1773 | |
Victor Stinner | 474652f | 2020-08-13 22:11:50 +0200 | [diff] [blame] | 1774 | Return the :c:type:`void*` value associated with a TSS key in the current |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 1775 | thread. This returns ``NULL`` if no value is associated with the key in the |
Masayuki Yamamoto | 731e189 | 2017-10-06 19:41:34 +0900 | [diff] [blame] | 1776 | current thread. |
| 1777 | |
| 1778 | |
| 1779 | .. _thread-local-storage-api: |
| 1780 | |
| 1781 | Thread Local Storage (TLS) API |
| 1782 | ------------------------------ |
| 1783 | |
| 1784 | .. deprecated:: 3.7 |
| 1785 | This API is superseded by |
| 1786 | :ref:`Thread Specific Storage (TSS) API <thread-specific-storage-api>`. |
| 1787 | |
| 1788 | .. note:: |
| 1789 | This version of the API does not support platforms where the native TLS key |
| 1790 | is defined in a way that cannot be safely cast to ``int``. On such platforms, |
| 1791 | :c:func:`PyThread_create_key` will return immediately with a failure status, |
| 1792 | and the other TLS functions will all be no-ops on such platforms. |
| 1793 | |
| 1794 | Due to the compatibility problem noted above, this version of the API should not |
| 1795 | be used in new code. |
| 1796 | |
| 1797 | .. c:function:: int PyThread_create_key() |
| 1798 | .. c:function:: void PyThread_delete_key(int key) |
| 1799 | .. c:function:: int PyThread_set_key_value(int key, void *value) |
| 1800 | .. c:function:: void* PyThread_get_key_value(int key) |
| 1801 | .. c:function:: void PyThread_delete_key_value(int key) |
| 1802 | .. c:function:: void PyThread_ReInitTLS() |
| 1803 | |