blob: f8ab642576e6a527aa02088a9a29c6182217b5c6 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001.. highlightlang:: c
2
3
4.. _initialization:
5
6*****************************************
7Initialization, Finalization, and Threads
8*****************************************
9
10
11.. cfunction:: void Py_Initialize()
12
13 .. index::
14 single: Py_SetProgramName()
15 single: PyEval_InitThreads()
16 single: PyEval_ReleaseLock()
17 single: PyEval_AcquireLock()
18 single: modules (in module sys)
19 single: path (in module sys)
20 module: __builtin__
21 module: __main__
22 module: sys
23 triple: module; search; path
24 single: PySys_SetArgv()
Antoine Pitrou6a265602010-05-21 17:12:38 +000025 single: PySys_SetArgvEx()
Georg Brandl8ec7f652007-08-15 14:28:01 +000026 single: Py_Finalize()
27
28 Initialize the Python interpreter. In an application embedding Python, this
29 should be called before using any other Python/C API functions; with the
30 exception of :cfunc:`Py_SetProgramName`, :cfunc:`PyEval_InitThreads`,
31 :cfunc:`PyEval_ReleaseLock`, and :cfunc:`PyEval_AcquireLock`. This initializes
32 the table of loaded modules (``sys.modules``), and creates the fundamental
33 modules :mod:`__builtin__`, :mod:`__main__` and :mod:`sys`. It also initializes
34 the module search path (``sys.path``). It does not set ``sys.argv``; use
Antoine Pitrou6a265602010-05-21 17:12:38 +000035 :cfunc:`PySys_SetArgvEx` for that. This is a no-op when called for a second time
Georg Brandl8ec7f652007-08-15 14:28:01 +000036 (without calling :cfunc:`Py_Finalize` first). There is no return value; it is a
37 fatal error if the initialization fails.
38
39
40.. cfunction:: void Py_InitializeEx(int initsigs)
41
42 This function works like :cfunc:`Py_Initialize` if *initsigs* is 1. If
43 *initsigs* is 0, it skips initialization registration of signal handlers, which
44 might be useful when Python is embedded.
45
46 .. versionadded:: 2.4
47
48
49.. cfunction:: int Py_IsInitialized()
50
51 Return true (nonzero) when the Python interpreter has been initialized, false
52 (zero) if not. After :cfunc:`Py_Finalize` is called, this returns false until
53 :cfunc:`Py_Initialize` is called again.
54
55
56.. cfunction:: void Py_Finalize()
57
58 Undo all initializations made by :cfunc:`Py_Initialize` and subsequent use of
59 Python/C API functions, and destroy all sub-interpreters (see
60 :cfunc:`Py_NewInterpreter` below) that were created and not yet destroyed since
61 the last call to :cfunc:`Py_Initialize`. Ideally, this frees all memory
62 allocated by the Python interpreter. This is a no-op when called for a second
63 time (without calling :cfunc:`Py_Initialize` again first). There is no return
64 value; errors during finalization are ignored.
65
66 This function is provided for a number of reasons. An embedding application
67 might want to restart Python without having to restart the application itself.
68 An application that has loaded the Python interpreter from a dynamically
69 loadable library (or DLL) might want to free all memory allocated by Python
70 before unloading the DLL. During a hunt for memory leaks in an application a
71 developer might want to free all memory allocated by Python before exiting from
72 the application.
73
74 **Bugs and caveats:** The destruction of modules and objects in modules is done
75 in random order; this may cause destructors (:meth:`__del__` methods) to fail
76 when they depend on other objects (even functions) or modules. Dynamically
77 loaded extension modules loaded by Python are not unloaded. Small amounts of
78 memory allocated by the Python interpreter may not be freed (if you find a leak,
79 please report it). Memory tied up in circular references between objects is not
80 freed. Some memory allocated by extension modules may not be freed. Some
81 extensions may not work properly if their initialization routine is called more
82 than once; this can happen if an application calls :cfunc:`Py_Initialize` and
83 :cfunc:`Py_Finalize` more than once.
84
85
86.. cfunction:: PyThreadState* Py_NewInterpreter()
87
88 .. index::
89 module: __builtin__
90 module: __main__
91 module: sys
92 single: stdout (in module sys)
93 single: stderr (in module sys)
94 single: stdin (in module sys)
95
96 Create a new sub-interpreter. This is an (almost) totally separate environment
97 for the execution of Python code. In particular, the new interpreter has
98 separate, independent versions of all imported modules, including the
99 fundamental modules :mod:`__builtin__`, :mod:`__main__` and :mod:`sys`. The
100 table of loaded modules (``sys.modules``) and the module search path
101 (``sys.path``) are also separate. The new environment has no ``sys.argv``
102 variable. It has new standard I/O stream file objects ``sys.stdin``,
103 ``sys.stdout`` and ``sys.stderr`` (however these refer to the same underlying
104 :ctype:`FILE` structures in the C library).
105
106 The return value points to the first thread state created in the new
107 sub-interpreter. This thread state is made in the current thread state.
108 Note that no actual thread is created; see the discussion of thread states
109 below. If creation of the new interpreter is unsuccessful, *NULL* is
110 returned; no exception is set since the exception state is stored in the
111 current thread state and there may not be a current thread state. (Like all
112 other Python/C API functions, the global interpreter lock must be held before
113 calling this function and is still held when it returns; however, unlike most
114 other Python/C API functions, there needn't be a current thread state on
115 entry.)
116
117 .. index::
118 single: Py_Finalize()
119 single: Py_Initialize()
120
121 Extension modules are shared between (sub-)interpreters as follows: the first
122 time a particular extension is imported, it is initialized normally, and a
123 (shallow) copy of its module's dictionary is squirreled away. When the same
124 extension is imported by another (sub-)interpreter, a new module is initialized
125 and filled with the contents of this copy; the extension's ``init`` function is
126 not called. Note that this is different from what happens when an extension is
127 imported after the interpreter has been completely re-initialized by calling
128 :cfunc:`Py_Finalize` and :cfunc:`Py_Initialize`; in that case, the extension's
129 ``initmodule`` function *is* called again.
130
131 .. index:: single: close() (in module os)
132
133 **Bugs and caveats:** Because sub-interpreters (and the main interpreter) are
134 part of the same process, the insulation between them isn't perfect --- for
135 example, using low-level file operations like :func:`os.close` they can
136 (accidentally or maliciously) affect each other's open files. Because of the
137 way extensions are shared between (sub-)interpreters, some extensions may not
138 work properly; this is especially likely when the extension makes use of
139 (static) global variables, or when the extension manipulates its module's
140 dictionary after its initialization. It is possible to insert objects created
141 in one sub-interpreter into a namespace of another sub-interpreter; this should
142 be done with great care to avoid sharing user-defined functions, methods,
143 instances or classes between sub-interpreters, since import operations executed
144 by such objects may affect the wrong (sub-)interpreter's dictionary of loaded
145 modules. (XXX This is a hard-to-fix bug that will be addressed in a future
146 release.)
147
148 Also note that the use of this functionality is incompatible with extension
149 modules such as PyObjC and ctypes that use the :cfunc:`PyGILState_\*` APIs (and
150 this is inherent in the way the :cfunc:`PyGILState_\*` functions work). Simple
151 things may work, but confusing behavior will always be near.
152
153
154.. cfunction:: void Py_EndInterpreter(PyThreadState *tstate)
155
156 .. index:: single: Py_Finalize()
157
158 Destroy the (sub-)interpreter represented by the given thread state. The given
159 thread state must be the current thread state. See the discussion of thread
160 states below. When the call returns, the current thread state is *NULL*. All
161 thread states associated with this interpreter are destroyed. (The global
162 interpreter lock must be held before calling this function and is still held
163 when it returns.) :cfunc:`Py_Finalize` will destroy all sub-interpreters that
164 haven't been explicitly destroyed at that point.
165
166
167.. cfunction:: void Py_SetProgramName(char *name)
168
169 .. index::
170 single: Py_Initialize()
171 single: main()
172 single: Py_GetPath()
173
174 This function should be called before :cfunc:`Py_Initialize` is called for
175 the first time, if it is called at all. It tells the interpreter the value
176 of the ``argv[0]`` argument to the :cfunc:`main` function of the program.
177 This is used by :cfunc:`Py_GetPath` and some other functions below to find
178 the Python run-time libraries relative to the interpreter executable. The
179 default value is ``'python'``. The argument should point to a
180 zero-terminated character string in static storage whose contents will not
181 change for the duration of the program's execution. No code in the Python
182 interpreter will change the contents of this storage.
183
184
185.. cfunction:: char* Py_GetProgramName()
186
187 .. index:: single: Py_SetProgramName()
188
189 Return the program name set with :cfunc:`Py_SetProgramName`, or the default.
190 The returned string points into static storage; the caller should not modify its
191 value.
192
193
194.. cfunction:: char* Py_GetPrefix()
195
196 Return the *prefix* for installed platform-independent files. This is derived
197 through a number of complicated rules from the program name set with
198 :cfunc:`Py_SetProgramName` and some environment variables; for example, if the
199 program name is ``'/usr/local/bin/python'``, the prefix is ``'/usr/local'``. The
200 returned string points into static storage; the caller should not modify its
201 value. This corresponds to the :makevar:`prefix` variable in the top-level
202 :file:`Makefile` and the :option:`--prefix` argument to the :program:`configure`
203 script at build time. The value is available to Python code as ``sys.prefix``.
204 It is only useful on Unix. See also the next function.
205
206
207.. cfunction:: char* Py_GetExecPrefix()
208
209 Return the *exec-prefix* for installed platform-*dependent* files. This is
210 derived through a number of complicated rules from the program name set with
211 :cfunc:`Py_SetProgramName` and some environment variables; for example, if the
212 program name is ``'/usr/local/bin/python'``, the exec-prefix is
213 ``'/usr/local'``. The returned string points into static storage; the caller
214 should not modify its value. This corresponds to the :makevar:`exec_prefix`
215 variable in the top-level :file:`Makefile` and the :option:`--exec-prefix`
216 argument to the :program:`configure` script at build time. The value is
217 available to Python code as ``sys.exec_prefix``. It is only useful on Unix.
218
219 Background: The exec-prefix differs from the prefix when platform dependent
220 files (such as executables and shared libraries) are installed in a different
221 directory tree. In a typical installation, platform dependent files may be
222 installed in the :file:`/usr/local/plat` subtree while platform independent may
223 be installed in :file:`/usr/local`.
224
225 Generally speaking, a platform is a combination of hardware and software
226 families, e.g. Sparc machines running the Solaris 2.x operating system are
227 considered the same platform, but Intel machines running Solaris 2.x are another
228 platform, and Intel machines running Linux are yet another platform. Different
229 major revisions of the same operating system generally also form different
230 platforms. Non-Unix operating systems are a different story; the installation
231 strategies on those systems are so different that the prefix and exec-prefix are
232 meaningless, and set to the empty string. Note that compiled Python bytecode
233 files are platform independent (but not independent from the Python version by
234 which they were compiled!).
235
236 System administrators will know how to configure the :program:`mount` or
237 :program:`automount` programs to share :file:`/usr/local` between platforms
238 while having :file:`/usr/local/plat` be a different filesystem for each
239 platform.
240
241
242.. cfunction:: char* Py_GetProgramFullPath()
243
244 .. index::
245 single: Py_SetProgramName()
246 single: executable (in module sys)
247
248 Return the full program name of the Python executable; this is computed as a
249 side-effect of deriving the default module search path from the program name
250 (set by :cfunc:`Py_SetProgramName` above). The returned string points into
251 static storage; the caller should not modify its value. The value is available
252 to Python code as ``sys.executable``.
253
254
255.. cfunction:: char* Py_GetPath()
256
257 .. index::
258 triple: module; search; path
259 single: path (in module sys)
260
Georg Brandl54fd8ae2010-01-07 20:54:45 +0000261 Return the default module search path; this is computed from the program name
262 (set by :cfunc:`Py_SetProgramName` above) and some environment variables.
263 The returned string consists of a series of directory names separated by a
264 platform dependent delimiter character. The delimiter character is ``':'``
265 on Unix and Mac OS X, ``';'`` on Windows. The returned string points into
266 static storage; the caller should not modify its value. The list
267 :data:`sys.path` is initialized with this value on interpreter startup; it
268 can be (and usually is) modified later to change the search path for loading
269 modules.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000270
Georg Brandlb19be572007-12-29 10:57:00 +0000271 .. XXX should give the exact rules
Georg Brandl8ec7f652007-08-15 14:28:01 +0000272
273
274.. cfunction:: const char* Py_GetVersion()
275
276 Return the version of this Python interpreter. This is a string that looks
277 something like ::
278
279 "1.5 (#67, Dec 31 1997, 22:34:28) [GCC 2.7.2.2]"
280
281 .. index:: single: version (in module sys)
282
283 The first word (up to the first space character) is the current Python version;
284 the first three characters are the major and minor version separated by a
285 period. The returned string points into static storage; the caller should not
286 modify its value. The value is available to Python code as ``sys.version``.
287
288
Georg Brandl8ec7f652007-08-15 14:28:01 +0000289.. cfunction:: const char* Py_GetPlatform()
290
291 .. index:: single: platform (in module sys)
292
293 Return the platform identifier for the current platform. On Unix, this is
294 formed from the "official" name of the operating system, converted to lower
295 case, followed by the major revision number; e.g., for Solaris 2.x, which is
296 also known as SunOS 5.x, the value is ``'sunos5'``. On Mac OS X, it is
297 ``'darwin'``. On Windows, it is ``'win'``. The returned string points into
298 static storage; the caller should not modify its value. The value is available
299 to Python code as ``sys.platform``.
300
301
302.. cfunction:: const char* Py_GetCopyright()
303
304 Return the official copyright string for the current Python version, for example
305
306 ``'Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam'``
307
308 .. index:: single: copyright (in module sys)
309
310 The returned string points into static storage; the caller should not modify its
311 value. The value is available to Python code as ``sys.copyright``.
312
313
314.. cfunction:: const char* Py_GetCompiler()
315
316 Return an indication of the compiler used to build the current Python version,
317 in square brackets, for example::
318
319 "[GCC 2.7.2.2]"
320
321 .. index:: single: version (in module sys)
322
323 The returned string points into static storage; the caller should not modify its
324 value. The value is available to Python code as part of the variable
325 ``sys.version``.
326
327
328.. cfunction:: const char* Py_GetBuildInfo()
329
330 Return information about the sequence number and build date and time of the
331 current Python interpreter instance, for example ::
332
333 "#67, Aug 1 1997, 22:34:28"
334
335 .. index:: single: version (in module sys)
336
337 The returned string points into static storage; the caller should not modify its
338 value. The value is available to Python code as part of the variable
339 ``sys.version``.
340
341
Antoine Pitrou6a265602010-05-21 17:12:38 +0000342.. cfunction:: void PySys_SetArgvEx(int argc, char **argv, int updatepath)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000343
344 .. index::
345 single: main()
346 single: Py_FatalError()
347 single: argv (in module sys)
348
Georg Brandlacc802b2009-02-05 10:37:07 +0000349 Set :data:`sys.argv` based on *argc* and *argv*. These parameters are
350 similar to those passed to the program's :cfunc:`main` function with the
351 difference that the first entry should refer to the script file to be
352 executed rather than the executable hosting the Python interpreter. If there
353 isn't a script that will be run, the first entry in *argv* can be an empty
354 string. If this function fails to initialize :data:`sys.argv`, a fatal
355 condition is signalled using :cfunc:`Py_FatalError`.
356
Antoine Pitrou6a265602010-05-21 17:12:38 +0000357 If *updatepath* is zero, this is all the function does. If *updatepath*
358 is non-zero, the function also modifies :data:`sys.path` according to the
359 following algorithm:
360
361 - If the name of an existing script is passed in ``argv[0]``, the absolute
362 path of the directory where the script is located is prepended to
363 :data:`sys.path`.
364 - Otherwise (that is, if *argc* is 0 or ``argv[0]`` doesn't point
365 to an existing file name), an empty string is prepended to
366 :data:`sys.path`, which is the same as prepending the current working
367 directory (``"."``).
368
369 .. note::
370 It is recommended that applications embedding the Python interpreter
371 for purposes other than executing a single script pass 0 as *updatepath*,
372 and update :data:`sys.path` themselves if desired.
373 See `CVE-2008-5983 <http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-5983>`_.
374
375 On versions before 2.6.6, you can achieve the same effect by manually
376 popping the first :data:`sys.path` element after having called
377 :cfunc:`PySys_SetArgv`, for example using::
378
379 PyRun_SimpleString("import sys; sys.path.pop(0)\n");
380
381 .. versionadded:: 2.6.6
Georg Brandl8ec7f652007-08-15 14:28:01 +0000382
Georg Brandlb19be572007-12-29 10:57:00 +0000383 .. XXX impl. doesn't seem consistent in allowing 0/NULL for the params;
384 check w/ Guido.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000385
Georg Brandl8ec7f652007-08-15 14:28:01 +0000386
Antoine Pitrou6a265602010-05-21 17:12:38 +0000387.. cfunction:: void PySys_SetArgv(int argc, char **argv)
388
Georg Brandl9933da02010-06-14 15:58:39 +0000389 This function works like :cfunc:`PySys_SetArgvEx` with *updatepath* set to 1.
Antoine Pitrou6a265602010-05-21 17:12:38 +0000390
391
Georg Brandl4400d842009-02-05 11:32:18 +0000392.. cfunction:: void Py_SetPythonHome(char *home)
393
394 Set the default "home" directory, that is, the location of the standard
395 Python libraries. The libraries are searched in
396 :file:`{home}/lib/python{version}` and :file:`{home}/lib/python{version}`.
Benjamin Petersonea7120c2009-09-15 03:36:26 +0000397 The argument should point to a zero-terminated character string in static
398 storage whose contents will not change for the duration of the program's
399 execution. No code in the Python interpreter will change the contents of
400 this storage.
Georg Brandl4400d842009-02-05 11:32:18 +0000401
402
403.. cfunction:: char* Py_GetPythonHome()
404
405 Return the default "home", that is, the value set by a previous call to
406 :cfunc:`Py_SetPythonHome`, or the value of the :envvar:`PYTHONHOME`
407 environment variable if it is set.
408
409
Georg Brandl8ec7f652007-08-15 14:28:01 +0000410.. _threads:
411
412Thread State and the Global Interpreter Lock
413============================================
414
415.. index::
Georg Brandl63e284d2010-10-15 17:52:59 +0000416 single: GIL
Georg Brandl8ec7f652007-08-15 14:28:01 +0000417 single: global interpreter lock
418 single: interpreter lock
419 single: lock, interpreter
420
421The Python interpreter is not fully thread safe. In order to support
Georg Brandl1ede0d62009-04-05 17:17:42 +0000422multi-threaded Python programs, there's a global lock, called the :dfn:`global
423interpreter lock` or :dfn:`GIL`, that must be held by the current thread before
424it can safely access Python objects. Without the lock, even the simplest
425operations could cause problems in a multi-threaded program: for example, when
426two threads simultaneously increment the reference count of the same object, the
427reference count could end up being incremented only once instead of twice.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000428
429.. index:: single: setcheckinterval() (in module sys)
430
431Therefore, the rule exists that only the thread that has acquired the global
432interpreter lock may operate on Python objects or call Python/C API functions.
433In order to support multi-threaded Python programs, the interpreter regularly
434releases and reacquires the lock --- by default, every 100 bytecode instructions
435(this can be changed with :func:`sys.setcheckinterval`). The lock is also
436released and reacquired around potentially blocking I/O operations like reading
437or writing a file, so that other threads can run while the thread that requests
438the I/O is waiting for the I/O operation to complete.
439
440.. index::
441 single: PyThreadState
442 single: PyThreadState
443
444The Python interpreter needs to keep some bookkeeping information separate per
445thread --- for this it uses a data structure called :ctype:`PyThreadState`.
446There's one global variable, however: the pointer to the current
Georg Brandl2622b542009-04-27 17:09:53 +0000447:ctype:`PyThreadState` structure. Before the addition of :dfn:`thread-local
448storage` (:dfn:`TLS`) the current thread state had to be manipulated
449explicitly.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000450
451This is easy enough in most cases. Most code manipulating the global
452interpreter lock has the following simple structure::
453
454 Save the thread state in a local variable.
Georg Brandl1ede0d62009-04-05 17:17:42 +0000455 Release the global interpreter lock.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000456 ...Do some blocking I/O operation...
Georg Brandl1ede0d62009-04-05 17:17:42 +0000457 Reacquire the global interpreter lock.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000458 Restore the thread state from the local variable.
459
460This is so common that a pair of macros exists to simplify it::
461
462 Py_BEGIN_ALLOW_THREADS
463 ...Do some blocking I/O operation...
464 Py_END_ALLOW_THREADS
465
466.. index::
467 single: Py_BEGIN_ALLOW_THREADS
468 single: Py_END_ALLOW_THREADS
469
470The :cmacro:`Py_BEGIN_ALLOW_THREADS` macro opens a new block and declares a
471hidden local variable; the :cmacro:`Py_END_ALLOW_THREADS` macro closes the
472block. Another advantage of using these two macros is that when Python is
473compiled without thread support, they are defined empty, thus saving the thread
Georg Brandl1ede0d62009-04-05 17:17:42 +0000474state and GIL manipulations.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000475
476When thread support is enabled, the block above expands to the following code::
477
478 PyThreadState *_save;
479
480 _save = PyEval_SaveThread();
481 ...Do some blocking I/O operation...
482 PyEval_RestoreThread(_save);
483
484Using even lower level primitives, we can get roughly the same effect as
485follows::
486
487 PyThreadState *_save;
488
489 _save = PyThreadState_Swap(NULL);
490 PyEval_ReleaseLock();
491 ...Do some blocking I/O operation...
492 PyEval_AcquireLock();
493 PyThreadState_Swap(_save);
494
495.. index::
496 single: PyEval_RestoreThread()
497 single: errno
498 single: PyEval_SaveThread()
499 single: PyEval_ReleaseLock()
500 single: PyEval_AcquireLock()
501
502There are some subtle differences; in particular, :cfunc:`PyEval_RestoreThread`
503saves and restores the value of the global variable :cdata:`errno`, since the
504lock manipulation does not guarantee that :cdata:`errno` is left alone. Also,
505when thread support is disabled, :cfunc:`PyEval_SaveThread` and
Georg Brandl1ede0d62009-04-05 17:17:42 +0000506:cfunc:`PyEval_RestoreThread` don't manipulate the GIL; in this case,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000507:cfunc:`PyEval_ReleaseLock` and :cfunc:`PyEval_AcquireLock` are not available.
508This is done so that dynamically loaded extensions compiled with thread support
509enabled can be loaded by an interpreter that was compiled with disabled thread
510support.
511
512The global interpreter lock is used to protect the pointer to the current thread
513state. When releasing the lock and saving the thread state, the current thread
514state pointer must be retrieved before the lock is released (since another
515thread could immediately acquire the lock and store its own thread state in the
516global variable). Conversely, when acquiring the lock and restoring the thread
517state, the lock must be acquired before storing the thread state pointer.
518
Jeroen Ruigrok van der Werven2dcf46e2009-04-25 13:07:40 +0000519It is important to note that when threads are created from C, they don't have
520the global interpreter lock, nor is there a thread state data structure for
521them. Such threads must bootstrap themselves into existence, by first
522creating a thread state data structure, then acquiring the lock, and finally
523storing their thread state pointer, before they can start using the Python/C
524API. When they are done, they should reset the thread state pointer, release
525the lock, and finally free their thread state data structure.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000526
527Beginning with version 2.3, threads can now take advantage of the
528:cfunc:`PyGILState_\*` functions to do all of the above automatically. The
529typical idiom for calling into Python from a C thread is now::
530
531 PyGILState_STATE gstate;
532 gstate = PyGILState_Ensure();
533
534 /* Perform Python actions here. */
535 result = CallSomeFunction();
536 /* evaluate result */
537
538 /* Release the thread. No Python API allowed beyond this point. */
539 PyGILState_Release(gstate);
540
541Note that the :cfunc:`PyGILState_\*` functions assume there is only one global
542interpreter (created automatically by :cfunc:`Py_Initialize`). Python still
543supports the creation of additional interpreters (using
544:cfunc:`Py_NewInterpreter`), but mixing multiple interpreters and the
545:cfunc:`PyGILState_\*` API is unsupported.
546
Thomas Woutersc4dcb382009-09-16 19:55:54 +0000547Another important thing to note about threads is their behaviour in the face
548of the C :cfunc:`fork` call. On most systems with :cfunc:`fork`, after a
549process forks only the thread that issued the fork will exist. That also
550means any locks held by other threads will never be released. Python solves
551this for :func:`os.fork` by acquiring the locks it uses internally before
552the fork, and releasing them afterwards. In addition, it resets any
553:ref:`lock-objects` in the child. When extending or embedding Python, there
554is no way to inform Python of additional (non-Python) locks that need to be
555acquired before or reset after a fork. OS facilities such as
556:cfunc:`posix_atfork` would need to be used to accomplish the same thing.
557Additionally, when extending or embedding Python, calling :cfunc:`fork`
558directly rather than through :func:`os.fork` (and returning to or calling
559into Python) may result in a deadlock by one of Python's internal locks
560being held by a thread that is defunct after the fork.
561:cfunc:`PyOS_AfterFork` tries to reset the necessary locks, but is not
562always able to.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000563
564.. ctype:: PyInterpreterState
565
566 This data structure represents the state shared by a number of cooperating
567 threads. Threads belonging to the same interpreter share their module
568 administration and a few other internal items. There are no public members in
569 this structure.
570
571 Threads belonging to different interpreters initially share nothing, except
572 process state like available memory, open file descriptors and such. The global
573 interpreter lock is also shared by all threads, regardless of to which
574 interpreter they belong.
575
576
577.. ctype:: PyThreadState
578
579 This data structure represents the state of a single thread. The only public
580 data member is :ctype:`PyInterpreterState \*`:attr:`interp`, which points to
581 this thread's interpreter state.
582
583
584.. cfunction:: void PyEval_InitThreads()
585
586 .. index::
587 single: PyEval_ReleaseLock()
588 single: PyEval_ReleaseThread()
589 single: PyEval_SaveThread()
590 single: PyEval_RestoreThread()
591
592 Initialize and acquire the global interpreter lock. It should be called in the
593 main thread before creating a second thread or engaging in any other thread
594 operations such as :cfunc:`PyEval_ReleaseLock` or
595 ``PyEval_ReleaseThread(tstate)``. It is not needed before calling
596 :cfunc:`PyEval_SaveThread` or :cfunc:`PyEval_RestoreThread`.
597
598 .. index:: single: Py_Initialize()
599
600 This is a no-op when called for a second time. It is safe to call this function
601 before calling :cfunc:`Py_Initialize`.
602
603 .. index:: module: thread
604
Georg Brandl1ede0d62009-04-05 17:17:42 +0000605 When only the main thread exists, no GIL operations are needed. This is a
Georg Brandl8ec7f652007-08-15 14:28:01 +0000606 common situation (most Python programs do not use threads), and the lock
Georg Brandl1ede0d62009-04-05 17:17:42 +0000607 operations slow the interpreter down a bit. Therefore, the lock is not
608 created initially. This situation is equivalent to having acquired the lock:
609 when there is only a single thread, all object accesses are safe. Therefore,
610 when this function initializes the global interpreter lock, it also acquires
611 it. Before the Python :mod:`thread` module creates a new thread, knowing
612 that either it has the lock or the lock hasn't been created yet, it calls
613 :cfunc:`PyEval_InitThreads`. When this call returns, it is guaranteed that
614 the lock has been created and that the calling thread has acquired it.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000615
616 It is **not** safe to call this function when it is unknown which thread (if
617 any) currently has the global interpreter lock.
618
619 This function is not available when thread support is disabled at compile time.
620
621
622.. cfunction:: int PyEval_ThreadsInitialized()
623
624 Returns a non-zero value if :cfunc:`PyEval_InitThreads` has been called. This
Georg Brandl1ede0d62009-04-05 17:17:42 +0000625 function can be called without holding the GIL, and therefore can be used to
Georg Brandl8ec7f652007-08-15 14:28:01 +0000626 avoid calls to the locking API when running single-threaded. This function is
627 not available when thread support is disabled at compile time.
628
629 .. versionadded:: 2.4
630
631
632.. cfunction:: void PyEval_AcquireLock()
633
634 Acquire the global interpreter lock. The lock must have been created earlier.
635 If this thread already has the lock, a deadlock ensues. This function is not
636 available when thread support is disabled at compile time.
637
638
639.. cfunction:: void PyEval_ReleaseLock()
640
641 Release the global interpreter lock. The lock must have been created earlier.
642 This function is not available when thread support is disabled at compile time.
643
644
645.. cfunction:: void PyEval_AcquireThread(PyThreadState *tstate)
646
647 Acquire the global interpreter lock and set the current thread state to
648 *tstate*, which should not be *NULL*. The lock must have been created earlier.
649 If this thread already has the lock, deadlock ensues. This function is not
650 available when thread support is disabled at compile time.
651
652
653.. cfunction:: void PyEval_ReleaseThread(PyThreadState *tstate)
654
655 Reset the current thread state to *NULL* and release the global interpreter
656 lock. The lock must have been created earlier and must be held by the current
657 thread. The *tstate* argument, which must not be *NULL*, is only used to check
658 that it represents the current thread state --- if it isn't, a fatal error is
659 reported. This function is not available when thread support is disabled at
660 compile time.
661
662
663.. cfunction:: PyThreadState* PyEval_SaveThread()
664
Georg Brandl1ede0d62009-04-05 17:17:42 +0000665 Release the global interpreter lock (if it has been created and thread
666 support is enabled) and reset the thread state to *NULL*, returning the
667 previous thread state (which is not *NULL*). If the lock has been created,
668 the current thread must have acquired it. (This function is available even
669 when thread support is disabled at compile time.)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000670
671
672.. cfunction:: void PyEval_RestoreThread(PyThreadState *tstate)
673
Georg Brandl1ede0d62009-04-05 17:17:42 +0000674 Acquire the global interpreter lock (if it has been created and thread
675 support is enabled) and set the thread state to *tstate*, which must not be
676 *NULL*. If the lock has been created, the current thread must not have
677 acquired it, otherwise deadlock ensues. (This function is available even
678 when thread support is disabled at compile time.)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000679
Georg Brandl16f1df92007-12-01 22:24:47 +0000680
681.. cfunction:: void PyEval_ReInitThreads()
682
683 This function is called from :cfunc:`PyOS_AfterFork` to ensure that newly
684 created child processes don't hold locks referring to threads which
685 are not running in the child process.
686
687
Georg Brandl8ec7f652007-08-15 14:28:01 +0000688The following macros are normally used without a trailing semicolon; look for
689example usage in the Python source distribution.
690
691
692.. cmacro:: Py_BEGIN_ALLOW_THREADS
693
694 This macro expands to ``{ PyThreadState *_save; _save = PyEval_SaveThread();``.
695 Note that it contains an opening brace; it must be matched with a following
696 :cmacro:`Py_END_ALLOW_THREADS` macro. See above for further discussion of this
697 macro. It is a no-op when thread support is disabled at compile time.
698
699
700.. cmacro:: Py_END_ALLOW_THREADS
701
702 This macro expands to ``PyEval_RestoreThread(_save); }``. Note that it contains
703 a closing brace; it must be matched with an earlier
704 :cmacro:`Py_BEGIN_ALLOW_THREADS` macro. See above for further discussion of
705 this macro. It is a no-op when thread support is disabled at compile time.
706
707
708.. cmacro:: Py_BLOCK_THREADS
709
710 This macro expands to ``PyEval_RestoreThread(_save);``: it is equivalent to
711 :cmacro:`Py_END_ALLOW_THREADS` without the closing brace. It is a no-op when
712 thread support is disabled at compile time.
713
714
715.. cmacro:: Py_UNBLOCK_THREADS
716
717 This macro expands to ``_save = PyEval_SaveThread();``: it is equivalent to
718 :cmacro:`Py_BEGIN_ALLOW_THREADS` without the opening brace and variable
719 declaration. It is a no-op when thread support is disabled at compile time.
720
721All of the following functions are only available when thread support is enabled
Georg Brandl1ede0d62009-04-05 17:17:42 +0000722at compile time, and must be called only when the global interpreter lock has
723been created.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000724
725
726.. cfunction:: PyInterpreterState* PyInterpreterState_New()
727
Georg Brandl1ede0d62009-04-05 17:17:42 +0000728 Create a new interpreter state object. The global interpreter lock need not
729 be held, but may be held if it is necessary to serialize calls to this
730 function.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000731
732
733.. cfunction:: void PyInterpreterState_Clear(PyInterpreterState *interp)
734
Georg Brandl1ede0d62009-04-05 17:17:42 +0000735 Reset all information in an interpreter state object. The global interpreter
736 lock must be held.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000737
738
739.. cfunction:: void PyInterpreterState_Delete(PyInterpreterState *interp)
740
Georg Brandl1ede0d62009-04-05 17:17:42 +0000741 Destroy an interpreter state object. The global interpreter lock need not be
742 held. The interpreter state must have been reset with a previous call to
Georg Brandl8ec7f652007-08-15 14:28:01 +0000743 :cfunc:`PyInterpreterState_Clear`.
744
745
746.. cfunction:: PyThreadState* PyThreadState_New(PyInterpreterState *interp)
747
Georg Brandl1ede0d62009-04-05 17:17:42 +0000748 Create a new thread state object belonging to the given interpreter object.
749 The global interpreter lock need not be held, but may be held if it is
750 necessary to serialize calls to this function.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000751
752
753.. cfunction:: void PyThreadState_Clear(PyThreadState *tstate)
754
Georg Brandl1ede0d62009-04-05 17:17:42 +0000755 Reset all information in a thread state object. The global interpreter lock
756 must be held.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000757
758
759.. cfunction:: void PyThreadState_Delete(PyThreadState *tstate)
760
Georg Brandl1ede0d62009-04-05 17:17:42 +0000761 Destroy a thread state object. The global interpreter lock need not be held.
762 The thread state must have been reset with a previous call to
Georg Brandl8ec7f652007-08-15 14:28:01 +0000763 :cfunc:`PyThreadState_Clear`.
764
765
766.. cfunction:: PyThreadState* PyThreadState_Get()
767
Georg Brandl1ede0d62009-04-05 17:17:42 +0000768 Return the current thread state. The global interpreter lock must be held.
769 When the current thread state is *NULL*, this issues a fatal error (so that
770 the caller needn't check for *NULL*).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000771
772
773.. cfunction:: PyThreadState* PyThreadState_Swap(PyThreadState *tstate)
774
775 Swap the current thread state with the thread state given by the argument
Georg Brandl1ede0d62009-04-05 17:17:42 +0000776 *tstate*, which may be *NULL*. The global interpreter lock must be held.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000777
778
779.. cfunction:: PyObject* PyThreadState_GetDict()
780
781 Return a dictionary in which extensions can store thread-specific state
782 information. Each extension should use a unique key to use to store state in
783 the dictionary. It is okay to call this function when no current thread state
784 is available. If this function returns *NULL*, no exception has been raised and
785 the caller should assume no current thread state is available.
786
787 .. versionchanged:: 2.3
788 Previously this could only be called when a current thread is active, and *NULL*
789 meant that an exception was raised.
790
791
792.. cfunction:: int PyThreadState_SetAsyncExc(long id, PyObject *exc)
793
794 Asynchronously raise an exception in a thread. The *id* argument is the thread
795 id of the target thread; *exc* is the exception object to be raised. This
796 function does not steal any references to *exc*. To prevent naive misuse, you
797 must write your own C extension to call this. Must be called with the GIL held.
798 Returns the number of thread states modified; this is normally one, but will be
799 zero if the thread id isn't found. If *exc* is :const:`NULL`, the pending
800 exception (if any) for the thread is cleared. This raises no exceptions.
801
802 .. versionadded:: 2.3
803
804
805.. cfunction:: PyGILState_STATE PyGILState_Ensure()
806
Georg Brandl1ede0d62009-04-05 17:17:42 +0000807 Ensure that the current thread is ready to call the Python C API regardless
808 of the current state of Python, or of the global interpreter lock. This may
809 be called as many times as desired by a thread as long as each call is
810 matched with a call to :cfunc:`PyGILState_Release`. In general, other
811 thread-related APIs may be used between :cfunc:`PyGILState_Ensure` and
812 :cfunc:`PyGILState_Release` calls as long as the thread state is restored to
813 its previous state before the Release(). For example, normal usage of the
814 :cmacro:`Py_BEGIN_ALLOW_THREADS` and :cmacro:`Py_END_ALLOW_THREADS` macros is
815 acceptable.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000816
817 The return value is an opaque "handle" to the thread state when
Benjamin Peterson9d1e2cd2008-10-10 22:23:41 +0000818 :cfunc:`PyGILState_Ensure` was called, and must be passed to
Georg Brandl8ec7f652007-08-15 14:28:01 +0000819 :cfunc:`PyGILState_Release` to ensure Python is left in the same state. Even
820 though recursive calls are allowed, these handles *cannot* be shared - each
Benjamin Peterson9d1e2cd2008-10-10 22:23:41 +0000821 unique call to :cfunc:`PyGILState_Ensure` must save the handle for its call
822 to :cfunc:`PyGILState_Release`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000823
824 When the function returns, the current thread will hold the GIL. Failure is a
825 fatal error.
826
827 .. versionadded:: 2.3
828
829
830.. cfunction:: void PyGILState_Release(PyGILState_STATE)
831
832 Release any resources previously acquired. After this call, Python's state will
833 be the same as it was prior to the corresponding :cfunc:`PyGILState_Ensure` call
834 (but generally this state will be unknown to the caller, hence the use of the
835 GILState API.)
836
837 Every call to :cfunc:`PyGILState_Ensure` must be matched by a call to
838 :cfunc:`PyGILState_Release` on the same thread.
839
840 .. versionadded:: 2.3
841
842
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +0000843
844Asynchronous Notifications
845==========================
846
Andrew M. Kuchlinga178a692009-04-03 21:45:29 +0000847A mechanism is provided to make asynchronous notifications to the main
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +0000848interpreter thread. These notifications take the form of a function
849pointer and a void argument.
850
851.. index:: single: setcheckinterval() (in module sys)
852
Georg Brandl1ede0d62009-04-05 17:17:42 +0000853Every check interval, when the global interpreter lock is released and
Ezio Melotti062d2b52009-12-19 22:41:49 +0000854reacquired, Python will also call any such provided functions. This can be used
Georg Brandl1ede0d62009-04-05 17:17:42 +0000855for example by asynchronous IO handlers. The notification can be scheduled from
856a worker thread and the actual call than made at the earliest convenience by the
857main thread where it has possession of the global interpreter lock and can
858perform any Python API calls.
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +0000859
Georg Brandldd958e02009-01-13 08:11:07 +0000860.. cfunction:: void Py_AddPendingCall( int (*func)(void *, void *arg) )
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +0000861
862 .. index:: single: Py_AddPendingCall()
863
Georg Brandl1ede0d62009-04-05 17:17:42 +0000864 Post a notification to the Python main thread. If successful, *func* will be
865 called with the argument *arg* at the earliest convenience. *func* will be
866 called having the global interpreter lock held and can thus use the full
867 Python API and can take any action such as setting object attributes to
868 signal IO completion. It must return 0 on success, or -1 signalling an
869 exception. The notification function won't be interrupted to perform another
870 asynchronous notification recursively, but it can still be interrupted to
871 switch threads if the global interpreter lock is released, for example, if it
Ezio Melotti062d2b52009-12-19 22:41:49 +0000872 calls back into Python code.
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +0000873
874 This function returns 0 on success in which case the notification has been
Georg Brandl1ede0d62009-04-05 17:17:42 +0000875 scheduled. Otherwise, for example if the notification buffer is full, it
876 returns -1 without setting any exception.
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +0000877
Georg Brandl1ede0d62009-04-05 17:17:42 +0000878 This function can be called on any thread, be it a Python thread or some
879 other system thread. If it is a Python thread, it doesn't matter if it holds
880 the global interpreter lock or not.
Kristján Valur Jónsson0e2d8c32009-01-09 21:35:16 +0000881
882 .. versionadded:: 2.7
883
884
885
Georg Brandl8ec7f652007-08-15 14:28:01 +0000886.. _profiling:
887
888Profiling and Tracing
889=====================
890
891.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
892
893
894The Python interpreter provides some low-level support for attaching profiling
895and execution tracing facilities. These are used for profiling, debugging, and
896coverage analysis tools.
897
898Starting with Python 2.2, the implementation of this facility was substantially
899revised, and an interface from C was added. This C interface allows the
900profiling or tracing code to avoid the overhead of calling through Python-level
901callable objects, making a direct C function call instead. The essential
902attributes of the facility have not changed; the interface allows trace
903functions to be installed per-thread, and the basic events reported to the trace
904function are the same as had been reported to the Python-level trace functions
905in previous versions.
906
907
908.. ctype:: int (*Py_tracefunc)(PyObject *obj, PyFrameObject *frame, int what, PyObject *arg)
909
910 The type of the trace function registered using :cfunc:`PyEval_SetProfile` and
911 :cfunc:`PyEval_SetTrace`. The first parameter is the object passed to the
912 registration function as *obj*, *frame* is the frame object to which the event
913 pertains, *what* is one of the constants :const:`PyTrace_CALL`,
914 :const:`PyTrace_EXCEPTION`, :const:`PyTrace_LINE`, :const:`PyTrace_RETURN`,
915 :const:`PyTrace_C_CALL`, :const:`PyTrace_C_EXCEPTION`, or
916 :const:`PyTrace_C_RETURN`, and *arg* depends on the value of *what*:
917
918 +------------------------------+--------------------------------------+
919 | Value of *what* | Meaning of *arg* |
920 +==============================+======================================+
921 | :const:`PyTrace_CALL` | Always *NULL*. |
922 +------------------------------+--------------------------------------+
923 | :const:`PyTrace_EXCEPTION` | Exception information as returned by |
924 | | :func:`sys.exc_info`. |
925 +------------------------------+--------------------------------------+
926 | :const:`PyTrace_LINE` | Always *NULL*. |
927 +------------------------------+--------------------------------------+
928 | :const:`PyTrace_RETURN` | Value being returned to the caller. |
929 +------------------------------+--------------------------------------+
930 | :const:`PyTrace_C_CALL` | Name of function being called. |
931 +------------------------------+--------------------------------------+
932 | :const:`PyTrace_C_EXCEPTION` | Always *NULL*. |
933 +------------------------------+--------------------------------------+
934 | :const:`PyTrace_C_RETURN` | Always *NULL*. |
935 +------------------------------+--------------------------------------+
936
937
938.. cvar:: int PyTrace_CALL
939
940 The value of the *what* parameter to a :ctype:`Py_tracefunc` function when a new
941 call to a function or method is being reported, or a new entry into a generator.
942 Note that the creation of the iterator for a generator function is not reported
943 as there is no control transfer to the Python bytecode in the corresponding
944 frame.
945
946
947.. cvar:: int PyTrace_EXCEPTION
948
949 The value of the *what* parameter to a :ctype:`Py_tracefunc` function when an
950 exception has been raised. The callback function is called with this value for
951 *what* when after any bytecode is processed after which the exception becomes
952 set within the frame being executed. The effect of this is that as exception
953 propagation causes the Python stack to unwind, the callback is called upon
954 return to each frame as the exception propagates. Only trace functions receives
955 these events; they are not needed by the profiler.
956
957
958.. cvar:: int PyTrace_LINE
959
960 The value passed as the *what* parameter to a trace function (but not a
961 profiling function) when a line-number event is being reported.
962
963
964.. cvar:: int PyTrace_RETURN
965
966 The value for the *what* parameter to :ctype:`Py_tracefunc` functions when a
967 call is returning without propagating an exception.
968
969
970.. cvar:: int PyTrace_C_CALL
971
972 The value for the *what* parameter to :ctype:`Py_tracefunc` functions when a C
973 function is about to be called.
974
975
976.. cvar:: int PyTrace_C_EXCEPTION
977
978 The value for the *what* parameter to :ctype:`Py_tracefunc` functions when a C
Georg Brandl21946af2010-10-06 09:28:45 +0000979 function has raised an exception.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000980
981
982.. cvar:: int PyTrace_C_RETURN
983
984 The value for the *what* parameter to :ctype:`Py_tracefunc` functions when a C
985 function has returned.
986
987
988.. cfunction:: void PyEval_SetProfile(Py_tracefunc func, PyObject *obj)
989
990 Set the profiler function to *func*. The *obj* parameter is passed to the
991 function as its first parameter, and may be any Python object, or *NULL*. If
992 the profile function needs to maintain state, using a different value for *obj*
993 for each thread provides a convenient and thread-safe place to store it. The
994 profile function is called for all monitored events except the line-number
995 events.
996
997
998.. cfunction:: void PyEval_SetTrace(Py_tracefunc func, PyObject *obj)
999
1000 Set the tracing function to *func*. This is similar to
1001 :cfunc:`PyEval_SetProfile`, except the tracing function does receive line-number
1002 events.
1003
Georg Brandl16f1df92007-12-01 22:24:47 +00001004.. cfunction:: PyObject* PyEval_GetCallStats(PyObject *self)
1005
1006 Return a tuple of function call counts. There are constants defined for the
1007 positions within the tuple:
Georg Brandlc62ef8b2009-01-03 20:55:06 +00001008
Georg Brandl16f1df92007-12-01 22:24:47 +00001009 +-------------------------------+-------+
1010 | Name | Value |
1011 +===============================+=======+
1012 | :const:`PCALL_ALL` | 0 |
1013 +-------------------------------+-------+
1014 | :const:`PCALL_FUNCTION` | 1 |
1015 +-------------------------------+-------+
1016 | :const:`PCALL_FAST_FUNCTION` | 2 |
1017 +-------------------------------+-------+
1018 | :const:`PCALL_FASTER_FUNCTION`| 3 |
1019 +-------------------------------+-------+
1020 | :const:`PCALL_METHOD` | 4 |
1021 +-------------------------------+-------+
1022 | :const:`PCALL_BOUND_METHOD` | 5 |
1023 +-------------------------------+-------+
1024 | :const:`PCALL_CFUNCTION` | 6 |
1025 +-------------------------------+-------+
1026 | :const:`PCALL_TYPE` | 7 |
1027 +-------------------------------+-------+
1028 | :const:`PCALL_GENERATOR` | 8 |
1029 +-------------------------------+-------+
1030 | :const:`PCALL_OTHER` | 9 |
1031 +-------------------------------+-------+
1032 | :const:`PCALL_POP` | 10 |
1033 +-------------------------------+-------+
Georg Brandlc62ef8b2009-01-03 20:55:06 +00001034
Georg Brandl16f1df92007-12-01 22:24:47 +00001035 :const:`PCALL_FAST_FUNCTION` means no argument tuple needs to be created.
1036 :const:`PCALL_FASTER_FUNCTION` means that the fast-path frame setup code is used.
1037
1038 If there is a method call where the call can be optimized by changing
1039 the argument tuple and calling the function directly, it gets recorded
1040 twice.
1041
1042 This function is only present if Python is compiled with :const:`CALL_PROFILE`
1043 defined.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001044
1045.. _advanced-debugging:
1046
1047Advanced Debugger Support
1048=========================
1049
1050.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
1051
1052
1053These functions are only intended to be used by advanced debugging tools.
1054
1055
1056.. cfunction:: PyInterpreterState* PyInterpreterState_Head()
1057
1058 Return the interpreter state object at the head of the list of all such objects.
1059
1060 .. versionadded:: 2.2
1061
1062
1063.. cfunction:: PyInterpreterState* PyInterpreterState_Next(PyInterpreterState *interp)
1064
1065 Return the next interpreter state object after *interp* from the list of all
1066 such objects.
1067
1068 .. versionadded:: 2.2
1069
1070
1071.. cfunction:: PyThreadState * PyInterpreterState_ThreadHead(PyInterpreterState *interp)
1072
1073 Return the a pointer to the first :ctype:`PyThreadState` object in the list of
1074 threads associated with the interpreter *interp*.
1075
1076 .. versionadded:: 2.2
1077
1078
1079.. cfunction:: PyThreadState* PyThreadState_Next(PyThreadState *tstate)
1080
1081 Return the next thread state object after *tstate* from the list of all such
1082 objects belonging to the same :ctype:`PyInterpreterState` object.
1083
1084 .. versionadded:: 2.2
1085