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