2007-08-15 11:28:01 -03:00
|
|
|
.. highlightlang:: c
|
|
|
|
|
|
|
|
|
|
|
|
.. _initialization:
|
|
|
|
|
|
|
|
*****************************************
|
|
|
|
Initialization, Finalization, and Threads
|
|
|
|
*****************************************
|
|
|
|
|
|
|
|
|
2011-01-15 10:29:23 -04:00
|
|
|
Initializing and finalizing the interpreter
|
|
|
|
===========================================
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: void Py_Initialize()
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
.. index::
|
|
|
|
single: Py_SetProgramName()
|
|
|
|
single: PyEval_InitThreads()
|
|
|
|
single: PyEval_ReleaseLock()
|
|
|
|
single: PyEval_AcquireLock()
|
|
|
|
single: modules (in module sys)
|
|
|
|
single: path (in module sys)
|
|
|
|
module: __builtin__
|
|
|
|
module: __main__
|
|
|
|
module: sys
|
|
|
|
triple: module; search; path
|
|
|
|
single: PySys_SetArgv()
|
2010-05-21 14:12:38 -03:00
|
|
|
single: PySys_SetArgvEx()
|
2007-08-15 11:28:01 -03:00
|
|
|
single: Py_Finalize()
|
|
|
|
|
|
|
|
Initialize the Python interpreter. In an application embedding Python, this
|
|
|
|
should be called before using any other Python/C API functions; with the
|
2012-01-14 11:42:02 -04:00
|
|
|
exception of :c:func:`Py_SetProgramName`, :c:func:`Py_SetPythonHome`, :c:func:`PyEval_InitThreads`,
|
|
|
|
:c:func:`PyEval_ReleaseLock`, and :c:func:`PyEval_AcquireLock`. This initializes
|
2007-08-15 11:28:01 -03:00
|
|
|
the table of loaded modules (``sys.modules``), and creates the fundamental
|
|
|
|
modules :mod:`__builtin__`, :mod:`__main__` and :mod:`sys`. It also initializes
|
|
|
|
the module search path (``sys.path``). It does not set ``sys.argv``; use
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:func:`PySys_SetArgvEx` for that. This is a no-op when called for a second time
|
|
|
|
(without calling :c:func:`Py_Finalize` first). There is no return value; it is a
|
2007-08-15 11:28:01 -03:00
|
|
|
fatal error if the initialization fails.
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: void Py_InitializeEx(int initsigs)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
This function works like :c:func:`Py_Initialize` if *initsigs* is 1. If
|
2007-08-15 11:28:01 -03:00
|
|
|
*initsigs* is 0, it skips initialization registration of signal handlers, which
|
|
|
|
might be useful when Python is embedded.
|
|
|
|
|
|
|
|
.. versionadded:: 2.4
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: int Py_IsInitialized()
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
Return true (nonzero) when the Python interpreter has been initialized, false
|
2012-01-14 11:42:02 -04:00
|
|
|
(zero) if not. After :c:func:`Py_Finalize` is called, this returns false until
|
|
|
|
:c:func:`Py_Initialize` is called again.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: void Py_Finalize()
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
Undo all initializations made by :c:func:`Py_Initialize` and subsequent use of
|
2007-08-15 11:28:01 -03:00
|
|
|
Python/C API functions, and destroy all sub-interpreters (see
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:func:`Py_NewInterpreter` below) that were created and not yet destroyed since
|
|
|
|
the last call to :c:func:`Py_Initialize`. Ideally, this frees all memory
|
2007-08-15 11:28:01 -03:00
|
|
|
allocated by the Python interpreter. This is a no-op when called for a second
|
2012-01-14 11:42:02 -04:00
|
|
|
time (without calling :c:func:`Py_Initialize` again first). There is no return
|
2007-08-15 11:28:01 -03:00
|
|
|
value; errors during finalization are ignored.
|
|
|
|
|
|
|
|
This function is provided for a number of reasons. An embedding application
|
|
|
|
might want to restart Python without having to restart the application itself.
|
|
|
|
An application that has loaded the Python interpreter from a dynamically
|
|
|
|
loadable library (or DLL) might want to free all memory allocated by Python
|
|
|
|
before unloading the DLL. During a hunt for memory leaks in an application a
|
|
|
|
developer might want to free all memory allocated by Python before exiting from
|
|
|
|
the application.
|
|
|
|
|
|
|
|
**Bugs and caveats:** The destruction of modules and objects in modules is done
|
|
|
|
in random order; this may cause destructors (:meth:`__del__` methods) to fail
|
|
|
|
when they depend on other objects (even functions) or modules. Dynamically
|
|
|
|
loaded extension modules loaded by Python are not unloaded. Small amounts of
|
|
|
|
memory allocated by the Python interpreter may not be freed (if you find a leak,
|
|
|
|
please report it). Memory tied up in circular references between objects is not
|
|
|
|
freed. Some memory allocated by extension modules may not be freed. Some
|
|
|
|
extensions may not work properly if their initialization routine is called more
|
2012-01-14 11:42:02 -04:00
|
|
|
than once; this can happen if an application calls :c:func:`Py_Initialize` and
|
|
|
|
:c:func:`Py_Finalize` more than once.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2011-01-15 10:29:23 -04:00
|
|
|
Process-wide parameters
|
|
|
|
=======================
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: void Py_SetProgramName(char *name)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
.. index::
|
|
|
|
single: Py_Initialize()
|
|
|
|
single: main()
|
|
|
|
single: Py_GetPath()
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
This function should be called before :c:func:`Py_Initialize` is called for
|
2007-08-15 11:28:01 -03:00
|
|
|
the first time, if it is called at all. It tells the interpreter the value
|
2012-01-14 11:42:02 -04:00
|
|
|
of the ``argv[0]`` argument to the :c:func:`main` function of the program.
|
|
|
|
This is used by :c:func:`Py_GetPath` and some other functions below to find
|
2007-08-15 11:28:01 -03:00
|
|
|
the Python run-time libraries relative to the interpreter executable. The
|
|
|
|
default value is ``'python'``. The argument should point to a
|
|
|
|
zero-terminated character string in static storage whose contents will not
|
|
|
|
change for the duration of the program's execution. No code in the Python
|
|
|
|
interpreter will change the contents of this storage.
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: char* Py_GetProgramName()
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
.. index:: single: Py_SetProgramName()
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
Return the program name set with :c:func:`Py_SetProgramName`, or the default.
|
2007-08-15 11:28:01 -03:00
|
|
|
The returned string points into static storage; the caller should not modify its
|
|
|
|
value.
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: char* Py_GetPrefix()
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
Return the *prefix* for installed platform-independent files. This is derived
|
|
|
|
through a number of complicated rules from the program name set with
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:func:`Py_SetProgramName` and some environment variables; for example, if the
|
2007-08-15 11:28:01 -03:00
|
|
|
program name is ``'/usr/local/bin/python'``, the prefix is ``'/usr/local'``. The
|
|
|
|
returned string points into static storage; the caller should not modify its
|
|
|
|
value. This corresponds to the :makevar:`prefix` variable in the top-level
|
2011-09-01 14:54:05 -03:00
|
|
|
:file:`Makefile` and the ``--prefix`` argument to the :program:`configure`
|
2007-08-15 11:28:01 -03:00
|
|
|
script at build time. The value is available to Python code as ``sys.prefix``.
|
|
|
|
It is only useful on Unix. See also the next function.
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: char* Py_GetExecPrefix()
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
Return the *exec-prefix* for installed platform-*dependent* files. This is
|
|
|
|
derived through a number of complicated rules from the program name set with
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:func:`Py_SetProgramName` and some environment variables; for example, if the
|
2007-08-15 11:28:01 -03:00
|
|
|
program name is ``'/usr/local/bin/python'``, the exec-prefix is
|
|
|
|
``'/usr/local'``. The returned string points into static storage; the caller
|
|
|
|
should not modify its value. This corresponds to the :makevar:`exec_prefix`
|
2011-09-01 14:54:05 -03:00
|
|
|
variable in the top-level :file:`Makefile` and the ``--exec-prefix``
|
2007-08-15 11:28:01 -03:00
|
|
|
argument to the :program:`configure` script at build time. The value is
|
|
|
|
available to Python code as ``sys.exec_prefix``. It is only useful on Unix.
|
|
|
|
|
|
|
|
Background: The exec-prefix differs from the prefix when platform dependent
|
|
|
|
files (such as executables and shared libraries) are installed in a different
|
|
|
|
directory tree. In a typical installation, platform dependent files may be
|
|
|
|
installed in the :file:`/usr/local/plat` subtree while platform independent may
|
|
|
|
be installed in :file:`/usr/local`.
|
|
|
|
|
|
|
|
Generally speaking, a platform is a combination of hardware and software
|
|
|
|
families, e.g. Sparc machines running the Solaris 2.x operating system are
|
|
|
|
considered the same platform, but Intel machines running Solaris 2.x are another
|
|
|
|
platform, and Intel machines running Linux are yet another platform. Different
|
|
|
|
major revisions of the same operating system generally also form different
|
|
|
|
platforms. Non-Unix operating systems are a different story; the installation
|
|
|
|
strategies on those systems are so different that the prefix and exec-prefix are
|
|
|
|
meaningless, and set to the empty string. Note that compiled Python bytecode
|
|
|
|
files are platform independent (but not independent from the Python version by
|
|
|
|
which they were compiled!).
|
|
|
|
|
|
|
|
System administrators will know how to configure the :program:`mount` or
|
|
|
|
:program:`automount` programs to share :file:`/usr/local` between platforms
|
|
|
|
while having :file:`/usr/local/plat` be a different filesystem for each
|
|
|
|
platform.
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: char* Py_GetProgramFullPath()
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
.. index::
|
|
|
|
single: Py_SetProgramName()
|
|
|
|
single: executable (in module sys)
|
|
|
|
|
|
|
|
Return the full program name of the Python executable; this is computed as a
|
|
|
|
side-effect of deriving the default module search path from the program name
|
2012-01-14 11:42:02 -04:00
|
|
|
(set by :c:func:`Py_SetProgramName` above). The returned string points into
|
2007-08-15 11:28:01 -03:00
|
|
|
static storage; the caller should not modify its value. The value is available
|
|
|
|
to Python code as ``sys.executable``.
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: char* Py_GetPath()
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
.. index::
|
|
|
|
triple: module; search; path
|
|
|
|
single: path (in module sys)
|
|
|
|
|
2010-01-07 16:54:45 -04:00
|
|
|
Return the default module search path; this is computed from the program name
|
2012-01-14 11:42:02 -04:00
|
|
|
(set by :c:func:`Py_SetProgramName` above) and some environment variables.
|
2010-01-07 16:54:45 -04:00
|
|
|
The returned string consists of a series of directory names separated by a
|
|
|
|
platform dependent delimiter character. The delimiter character is ``':'``
|
|
|
|
on Unix and Mac OS X, ``';'`` on Windows. The returned string points into
|
|
|
|
static storage; the caller should not modify its value. The list
|
|
|
|
:data:`sys.path` is initialized with this value on interpreter startup; it
|
|
|
|
can be (and usually is) modified later to change the search path for loading
|
|
|
|
modules.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2007-12-29 06:57:00 -04:00
|
|
|
.. XXX should give the exact rules
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: const char* Py_GetVersion()
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
Return the version of this Python interpreter. This is a string that looks
|
|
|
|
something like ::
|
|
|
|
|
|
|
|
"1.5 (#67, Dec 31 1997, 22:34:28) [GCC 2.7.2.2]"
|
|
|
|
|
|
|
|
.. index:: single: version (in module sys)
|
|
|
|
|
|
|
|
The first word (up to the first space character) is the current Python version;
|
|
|
|
the first three characters are the major and minor version separated by a
|
|
|
|
period. The returned string points into static storage; the caller should not
|
|
|
|
modify its value. The value is available to Python code as ``sys.version``.
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: const char* Py_GetPlatform()
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
.. index:: single: platform (in module sys)
|
|
|
|
|
|
|
|
Return the platform identifier for the current platform. On Unix, this is
|
|
|
|
formed from the "official" name of the operating system, converted to lower
|
|
|
|
case, followed by the major revision number; e.g., for Solaris 2.x, which is
|
|
|
|
also known as SunOS 5.x, the value is ``'sunos5'``. On Mac OS X, it is
|
|
|
|
``'darwin'``. On Windows, it is ``'win'``. The returned string points into
|
|
|
|
static storage; the caller should not modify its value. The value is available
|
|
|
|
to Python code as ``sys.platform``.
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: const char* Py_GetCopyright()
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
Return the official copyright string for the current Python version, for example
|
|
|
|
|
|
|
|
``'Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam'``
|
|
|
|
|
|
|
|
.. index:: single: copyright (in module sys)
|
|
|
|
|
|
|
|
The returned string points into static storage; the caller should not modify its
|
|
|
|
value. The value is available to Python code as ``sys.copyright``.
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: const char* Py_GetCompiler()
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
Return an indication of the compiler used to build the current Python version,
|
|
|
|
in square brackets, for example::
|
|
|
|
|
|
|
|
"[GCC 2.7.2.2]"
|
|
|
|
|
|
|
|
.. index:: single: version (in module sys)
|
|
|
|
|
|
|
|
The returned string points into static storage; the caller should not modify its
|
|
|
|
value. The value is available to Python code as part of the variable
|
|
|
|
``sys.version``.
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: const char* Py_GetBuildInfo()
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
Return information about the sequence number and build date and time of the
|
|
|
|
current Python interpreter instance, for example ::
|
|
|
|
|
|
|
|
"#67, Aug 1 1997, 22:34:28"
|
|
|
|
|
|
|
|
.. index:: single: version (in module sys)
|
|
|
|
|
|
|
|
The returned string points into static storage; the caller should not modify its
|
|
|
|
value. The value is available to Python code as part of the variable
|
|
|
|
``sys.version``.
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: void PySys_SetArgvEx(int argc, char **argv, int updatepath)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
.. index::
|
|
|
|
single: main()
|
|
|
|
single: Py_FatalError()
|
|
|
|
single: argv (in module sys)
|
|
|
|
|
2009-02-05 06:37:07 -04:00
|
|
|
Set :data:`sys.argv` based on *argc* and *argv*. These parameters are
|
2012-01-14 11:42:02 -04:00
|
|
|
similar to those passed to the program's :c:func:`main` function with the
|
2009-02-05 06:37:07 -04:00
|
|
|
difference that the first entry should refer to the script file to be
|
|
|
|
executed rather than the executable hosting the Python interpreter. If there
|
|
|
|
isn't a script that will be run, the first entry in *argv* can be an empty
|
|
|
|
string. If this function fails to initialize :data:`sys.argv`, a fatal
|
2012-01-14 11:42:02 -04:00
|
|
|
condition is signalled using :c:func:`Py_FatalError`.
|
2009-02-05 06:37:07 -04:00
|
|
|
|
2010-05-21 14:12:38 -03:00
|
|
|
If *updatepath* is zero, this is all the function does. If *updatepath*
|
|
|
|
is non-zero, the function also modifies :data:`sys.path` according to the
|
|
|
|
following algorithm:
|
|
|
|
|
|
|
|
- If the name of an existing script is passed in ``argv[0]``, the absolute
|
|
|
|
path of the directory where the script is located is prepended to
|
|
|
|
:data:`sys.path`.
|
|
|
|
- Otherwise (that is, if *argc* is 0 or ``argv[0]`` doesn't point
|
|
|
|
to an existing file name), an empty string is prepended to
|
|
|
|
:data:`sys.path`, which is the same as prepending the current working
|
|
|
|
directory (``"."``).
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
It is recommended that applications embedding the Python interpreter
|
|
|
|
for purposes other than executing a single script pass 0 as *updatepath*,
|
|
|
|
and update :data:`sys.path` themselves if desired.
|
|
|
|
See `CVE-2008-5983 <http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-5983>`_.
|
|
|
|
|
|
|
|
On versions before 2.6.6, you can achieve the same effect by manually
|
|
|
|
popping the first :data:`sys.path` element after having called
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:func:`PySys_SetArgv`, for example using::
|
2010-05-21 14:12:38 -03:00
|
|
|
|
|
|
|
PyRun_SimpleString("import sys; sys.path.pop(0)\n");
|
|
|
|
|
|
|
|
.. versionadded:: 2.6.6
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2007-12-29 06:57:00 -04:00
|
|
|
.. XXX impl. doesn't seem consistent in allowing 0/NULL for the params;
|
|
|
|
check w/ Guido.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: void PySys_SetArgv(int argc, char **argv)
|
2010-05-21 14:12:38 -03:00
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
This function works like :c:func:`PySys_SetArgvEx` with *updatepath* set to 1.
|
2010-05-21 14:12:38 -03:00
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: void Py_SetPythonHome(char *home)
|
2009-02-05 07:32:18 -04:00
|
|
|
|
|
|
|
Set the default "home" directory, that is, the location of the standard
|
Merged revisions 86881,86887,86913-86915,86933,86943,86960,86964,86974,86980,86996,87008 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r86881 | georg.brandl | 2010-11-30 08:43:28 +0100 (Di, 30 Nov 2010) | 1 line
#10584: fix bad links.
........
r86887 | georg.brandl | 2010-11-30 15:57:54 +0100 (Di, 30 Nov 2010) | 1 line
Fix typo.
........
r86913 | georg.brandl | 2010-12-01 16:32:43 +0100 (Mi, 01 Dez 2010) | 1 line
Add missing word, and add a better reference to the actual function.
........
r86914 | georg.brandl | 2010-12-01 16:36:33 +0100 (Mi, 01 Dez 2010) | 1 line
#10594: fix parameter names in PyList API docs.
........
r86915 | georg.brandl | 2010-12-01 16:44:25 +0100 (Mi, 01 Dez 2010) | 1 line
Fix some markup and style in the unittest docs.
........
r86933 | georg.brandl | 2010-12-02 19:02:01 +0100 (Do, 02 Dez 2010) | 1 line
#10597: fix Py_SetPythonHome docs by pointing to where the meaning of PYTHONHOME is already documented.
........
r86943 | georg.brandl | 2010-12-02 23:35:25 +0100 (Do, 02 Dez 2010) | 1 line
Re-add accidentally removed line.
........
r86960 | georg.brandl | 2010-12-03 08:55:44 +0100 (Fr, 03 Dez 2010) | 1 line
#10360: catch TypeError in WeakSet.__contains__, just like WeakKeyDictionary does.
........
r86964 | georg.brandl | 2010-12-03 10:58:38 +0100 (Fr, 03 Dez 2010) | 1 line
#10549: fix interface of docclass() for text documenter.
........
r86974 | georg.brandl | 2010-12-03 16:30:09 +0100 (Fr, 03 Dez 2010) | 1 line
Markup consistency fixes.
........
r86980 | georg.brandl | 2010-12-03 18:19:27 +0100 (Fr, 03 Dez 2010) | 1 line
Fix punctuation.
........
r86996 | georg.brandl | 2010-12-03 20:56:42 +0100 (Fr, 03 Dez 2010) | 1 line
Fix indentation.
........
r87008 | georg.brandl | 2010-12-04 10:04:04 +0100 (Sa, 04 Dez 2010) | 1 line
Fix typo.
........
2011-02-25 06:39:23 -04:00
|
|
|
Python libraries. See :envvar:`PYTHONHOME` for the meaning of the
|
|
|
|
argument string.
|
|
|
|
|
2009-09-15 00:36:26 -03:00
|
|
|
The argument should point to a zero-terminated character string in static
|
|
|
|
storage whose contents will not change for the duration of the program's
|
|
|
|
execution. No code in the Python interpreter will change the contents of
|
|
|
|
this storage.
|
2009-02-05 07:32:18 -04:00
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: char* Py_GetPythonHome()
|
2009-02-05 07:32:18 -04:00
|
|
|
|
|
|
|
Return the default "home", that is, the value set by a previous call to
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:func:`Py_SetPythonHome`, or the value of the :envvar:`PYTHONHOME`
|
2009-02-05 07:32:18 -04:00
|
|
|
environment variable if it is set.
|
|
|
|
|
|
|
|
|
2007-08-15 11:28:01 -03:00
|
|
|
.. _threads:
|
|
|
|
|
|
|
|
Thread State and the Global Interpreter Lock
|
|
|
|
============================================
|
|
|
|
|
|
|
|
.. index::
|
2010-10-15 14:52:59 -03:00
|
|
|
single: GIL
|
2007-08-15 11:28:01 -03:00
|
|
|
single: global interpreter lock
|
|
|
|
single: interpreter lock
|
|
|
|
single: lock, interpreter
|
|
|
|
|
2010-11-26 03:58:55 -04:00
|
|
|
The Python interpreter is not fully thread-safe. In order to support
|
2011-01-15 10:29:23 -04:00
|
|
|
multi-threaded Python programs, there's a global lock, called the :term:`global
|
|
|
|
interpreter lock` or :term:`GIL`, that must be held by the current thread before
|
2009-04-05 14:17:42 -03:00
|
|
|
it can safely access Python objects. Without the lock, even the simplest
|
|
|
|
operations could cause problems in a multi-threaded program: for example, when
|
|
|
|
two threads simultaneously increment the reference count of the same object, the
|
|
|
|
reference count could end up being incremented only once instead of twice.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
.. index:: single: setcheckinterval() (in module sys)
|
|
|
|
|
2011-01-15 10:29:23 -04:00
|
|
|
Therefore, the rule exists that only the thread that has acquired the
|
|
|
|
:term:`GIL` may operate on Python objects or call Python/C API functions.
|
|
|
|
In order to emulate concurrency of execution, the interpreter regularly
|
|
|
|
tries to switch threads (see :func:`sys.setcheckinterval`). The lock is also
|
|
|
|
released around potentially blocking I/O operations like reading or writing
|
|
|
|
a file, so that other Python threads can run in the meantime.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
.. index::
|
|
|
|
single: PyThreadState
|
|
|
|
single: PyThreadState
|
|
|
|
|
2011-01-15 10:29:23 -04:00
|
|
|
The Python interpreter keeps some thread-specific bookkeeping information
|
2012-01-14 11:42:02 -04:00
|
|
|
inside a data structure called :c:type:`PyThreadState`. There's also one
|
|
|
|
global variable pointing to the current :c:type:`PyThreadState`: it can
|
|
|
|
be retrieved using :c:func:`PyThreadState_Get`.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2011-01-15 10:29:23 -04:00
|
|
|
Releasing the GIL from extension code
|
|
|
|
-------------------------------------
|
|
|
|
|
|
|
|
Most extension code manipulating the :term:`GIL` has the following simple
|
|
|
|
structure::
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
Save the thread state in a local variable.
|
2009-04-05 14:17:42 -03:00
|
|
|
Release the global interpreter lock.
|
2011-01-15 10:29:23 -04:00
|
|
|
... Do some blocking I/O operation ...
|
2009-04-05 14:17:42 -03:00
|
|
|
Reacquire the global interpreter lock.
|
2007-08-15 11:28:01 -03:00
|
|
|
Restore the thread state from the local variable.
|
|
|
|
|
|
|
|
This is so common that a pair of macros exists to simplify it::
|
|
|
|
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
2011-01-15 10:29:23 -04:00
|
|
|
... Do some blocking I/O operation ...
|
2007-08-15 11:28:01 -03:00
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
|
|
|
|
.. index::
|
|
|
|
single: Py_BEGIN_ALLOW_THREADS
|
|
|
|
single: Py_END_ALLOW_THREADS
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
The :c:macro:`Py_BEGIN_ALLOW_THREADS` macro opens a new block and declares a
|
|
|
|
hidden local variable; the :c:macro:`Py_END_ALLOW_THREADS` macro closes the
|
2011-01-15 10:29:23 -04:00
|
|
|
block. These two macros are still available when Python is compiled without
|
|
|
|
thread support (they simply have an empty expansion).
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
When thread support is enabled, the block above expands to the following code::
|
|
|
|
|
|
|
|
PyThreadState *_save;
|
|
|
|
|
|
|
|
_save = PyEval_SaveThread();
|
|
|
|
...Do some blocking I/O operation...
|
|
|
|
PyEval_RestoreThread(_save);
|
|
|
|
|
|
|
|
.. index::
|
|
|
|
single: PyEval_RestoreThread()
|
|
|
|
single: PyEval_SaveThread()
|
2011-01-15 10:29:23 -04:00
|
|
|
|
|
|
|
Here is how these functions work: the global interpreter lock is used to protect the pointer to the
|
|
|
|
current thread state. When releasing the lock and saving the thread state,
|
|
|
|
the current thread state pointer must be retrieved before the lock is released
|
|
|
|
(since another thread could immediately acquire the lock and store its own thread
|
|
|
|
state in the global variable). Conversely, when acquiring the lock and restoring
|
|
|
|
the thread state, the lock must be acquired before storing the thread state
|
|
|
|
pointer.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
Calling system I/O functions is the most common use case for releasing
|
|
|
|
the GIL, but it can also be useful before calling long-running computations
|
|
|
|
which don't need access to Python objects, such as compression or
|
|
|
|
cryptographic functions operating over memory buffers. For example, the
|
|
|
|
standard :mod:`zlib` and :mod:`hashlib` modules release the GIL when
|
|
|
|
compressing or hashing data.
|
|
|
|
|
2013-09-30 16:35:44 -03:00
|
|
|
|
|
|
|
.. _gilstate:
|
|
|
|
|
2011-01-15 10:29:23 -04:00
|
|
|
Non-Python created threads
|
|
|
|
--------------------------
|
|
|
|
|
|
|
|
When threads are created using the dedicated Python APIs (such as the
|
|
|
|
:mod:`threading` module), a thread state is automatically associated to them
|
|
|
|
and the code showed above is therefore correct. However, when threads are
|
|
|
|
created from C (for example by a third-party library with its own thread
|
|
|
|
management), they don't hold the GIL, nor is there a thread state structure
|
|
|
|
for them.
|
|
|
|
|
|
|
|
If you need to call Python code from these threads (often this will be part
|
|
|
|
of a callback API provided by the aforementioned third-party library),
|
|
|
|
you must first register these threads with the interpreter by
|
|
|
|
creating a thread state data structure, then acquiring the GIL, and finally
|
|
|
|
storing their thread state pointer, before you can start using the Python/C
|
|
|
|
API. When you are done, you should reset the thread state pointer, release
|
|
|
|
the GIL, and finally free the thread state data structure.
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
The :c:func:`PyGILState_Ensure` and :c:func:`PyGILState_Release` functions do
|
2011-01-15 10:29:23 -04:00
|
|
|
all of the above automatically. The typical idiom for calling into Python
|
|
|
|
from a C thread is::
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
PyGILState_STATE gstate;
|
|
|
|
gstate = PyGILState_Ensure();
|
|
|
|
|
2011-01-15 10:29:23 -04:00
|
|
|
/* Perform Python actions here. */
|
2007-08-15 11:28:01 -03:00
|
|
|
result = CallSomeFunction();
|
2011-01-15 10:29:23 -04:00
|
|
|
/* evaluate result or handle exception */
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
/* Release the thread. No Python API allowed beyond this point. */
|
|
|
|
PyGILState_Release(gstate);
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
Note that the :c:func:`PyGILState_\*` functions assume there is only one global
|
|
|
|
interpreter (created automatically by :c:func:`Py_Initialize`). Python
|
2007-08-15 11:28:01 -03:00
|
|
|
supports the creation of additional interpreters (using
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:func:`Py_NewInterpreter`), but mixing multiple interpreters and the
|
|
|
|
:c:func:`PyGILState_\*` API is unsupported.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
Fix issue #1590864, multiple threads and fork() can cause deadlocks, by
acquiring the import lock around fork() calls. This prevents other threads
from having that lock while the fork happens, and is the recommended way of
dealing with such issues. There are two other locks we care about, the GIL
and the Thread Local Storage lock. The GIL is obviously held when calling
Python functions like os.fork(), and the TLS lock is explicitly reallocated
instead, while also deleting now-orphaned TLS data.
This only fixes calls to os.fork(), not extension modules or embedding
programs calling C's fork() directly. Solving that requires a new set of API
functions, and possibly a rewrite of the Python/thread_*.c mess. Add a
warning explaining the problem to the documentation in the mean time.
This also changes behaviour a little on AIX. Before, AIX (but only AIX) was
getting the import lock reallocated, seemingly to avoid this very same
problem. This is not the right approach, because the import lock is a
re-entrant one, and reallocating would do the wrong thing when forking while
holding the import lock.
Will backport to 2.6, minus the tiny AIX behaviour change.
2009-09-16 16:55:54 -03:00
|
|
|
Another important thing to note about threads is their behaviour in the face
|
2012-01-14 11:42:02 -04:00
|
|
|
of the C :c:func:`fork` call. On most systems with :c:func:`fork`, after a
|
Fix issue #1590864, multiple threads and fork() can cause deadlocks, by
acquiring the import lock around fork() calls. This prevents other threads
from having that lock while the fork happens, and is the recommended way of
dealing with such issues. There are two other locks we care about, the GIL
and the Thread Local Storage lock. The GIL is obviously held when calling
Python functions like os.fork(), and the TLS lock is explicitly reallocated
instead, while also deleting now-orphaned TLS data.
This only fixes calls to os.fork(), not extension modules or embedding
programs calling C's fork() directly. Solving that requires a new set of API
functions, and possibly a rewrite of the Python/thread_*.c mess. Add a
warning explaining the problem to the documentation in the mean time.
This also changes behaviour a little on AIX. Before, AIX (but only AIX) was
getting the import lock reallocated, seemingly to avoid this very same
problem. This is not the right approach, because the import lock is a
re-entrant one, and reallocating would do the wrong thing when forking while
holding the import lock.
Will backport to 2.6, minus the tiny AIX behaviour change.
2009-09-16 16:55:54 -03:00
|
|
|
process forks only the thread that issued the fork will exist. That also
|
|
|
|
means any locks held by other threads will never be released. Python solves
|
|
|
|
this for :func:`os.fork` by acquiring the locks it uses internally before
|
|
|
|
the fork, and releasing them afterwards. In addition, it resets any
|
|
|
|
:ref:`lock-objects` in the child. When extending or embedding Python, there
|
|
|
|
is no way to inform Python of additional (non-Python) locks that need to be
|
|
|
|
acquired before or reset after a fork. OS facilities such as
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:func:`pthread_atfork` would need to be used to accomplish the same thing.
|
|
|
|
Additionally, when extending or embedding Python, calling :c:func:`fork`
|
Fix issue #1590864, multiple threads and fork() can cause deadlocks, by
acquiring the import lock around fork() calls. This prevents other threads
from having that lock while the fork happens, and is the recommended way of
dealing with such issues. There are two other locks we care about, the GIL
and the Thread Local Storage lock. The GIL is obviously held when calling
Python functions like os.fork(), and the TLS lock is explicitly reallocated
instead, while also deleting now-orphaned TLS data.
This only fixes calls to os.fork(), not extension modules or embedding
programs calling C's fork() directly. Solving that requires a new set of API
functions, and possibly a rewrite of the Python/thread_*.c mess. Add a
warning explaining the problem to the documentation in the mean time.
This also changes behaviour a little on AIX. Before, AIX (but only AIX) was
getting the import lock reallocated, seemingly to avoid this very same
problem. This is not the right approach, because the import lock is a
re-entrant one, and reallocating would do the wrong thing when forking while
holding the import lock.
Will backport to 2.6, minus the tiny AIX behaviour change.
2009-09-16 16:55:54 -03:00
|
|
|
directly rather than through :func:`os.fork` (and returning to or calling
|
|
|
|
into Python) may result in a deadlock by one of Python's internal locks
|
|
|
|
being held by a thread that is defunct after the fork.
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:func:`PyOS_AfterFork` tries to reset the necessary locks, but is not
|
Fix issue #1590864, multiple threads and fork() can cause deadlocks, by
acquiring the import lock around fork() calls. This prevents other threads
from having that lock while the fork happens, and is the recommended way of
dealing with such issues. There are two other locks we care about, the GIL
and the Thread Local Storage lock. The GIL is obviously held when calling
Python functions like os.fork(), and the TLS lock is explicitly reallocated
instead, while also deleting now-orphaned TLS data.
This only fixes calls to os.fork(), not extension modules or embedding
programs calling C's fork() directly. Solving that requires a new set of API
functions, and possibly a rewrite of the Python/thread_*.c mess. Add a
warning explaining the problem to the documentation in the mean time.
This also changes behaviour a little on AIX. Before, AIX (but only AIX) was
getting the import lock reallocated, seemingly to avoid this very same
problem. This is not the right approach, because the import lock is a
re-entrant one, and reallocating would do the wrong thing when forking while
holding the import lock.
Will backport to 2.6, minus the tiny AIX behaviour change.
2009-09-16 16:55:54 -03:00
|
|
|
always able to.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2011-01-15 10:29:23 -04:00
|
|
|
|
|
|
|
High-level API
|
|
|
|
--------------
|
|
|
|
|
|
|
|
These are the most commonly used types and functions when writing C extension
|
|
|
|
code, or when embedding the Python interpreter:
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:type:: PyInterpreterState
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
This data structure represents the state shared by a number of cooperating
|
|
|
|
threads. Threads belonging to the same interpreter share their module
|
|
|
|
administration and a few other internal items. There are no public members in
|
|
|
|
this structure.
|
|
|
|
|
|
|
|
Threads belonging to different interpreters initially share nothing, except
|
|
|
|
process state like available memory, open file descriptors and such. The global
|
|
|
|
interpreter lock is also shared by all threads, regardless of to which
|
|
|
|
interpreter they belong.
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:type:: PyThreadState
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
This data structure represents the state of a single thread. The only public
|
2012-01-14 11:42:02 -04:00
|
|
|
data member is :c:type:`PyInterpreterState \*`:attr:`interp`, which points to
|
2007-08-15 11:28:01 -03:00
|
|
|
this thread's interpreter state.
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: void PyEval_InitThreads()
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
.. index::
|
|
|
|
single: PyEval_ReleaseLock()
|
|
|
|
single: PyEval_ReleaseThread()
|
|
|
|
single: PyEval_SaveThread()
|
|
|
|
single: PyEval_RestoreThread()
|
|
|
|
|
|
|
|
Initialize and acquire the global interpreter lock. It should be called in the
|
|
|
|
main thread before creating a second thread or engaging in any other thread
|
2012-01-14 11:42:02 -04:00
|
|
|
operations such as :c:func:`PyEval_ReleaseLock` or
|
2007-08-15 11:28:01 -03:00
|
|
|
``PyEval_ReleaseThread(tstate)``. It is not needed before calling
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:func:`PyEval_SaveThread` or :c:func:`PyEval_RestoreThread`.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
.. index:: single: Py_Initialize()
|
|
|
|
|
|
|
|
This is a no-op when called for a second time. It is safe to call this function
|
2012-01-14 11:42:02 -04:00
|
|
|
before calling :c:func:`Py_Initialize`.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
.. index:: module: thread
|
|
|
|
|
2011-01-15 10:29:23 -04:00
|
|
|
.. note::
|
|
|
|
When only the main thread exists, no GIL operations are needed. This is a
|
|
|
|
common situation (most Python programs do not use threads), and the lock
|
|
|
|
operations slow the interpreter down a bit. Therefore, the lock is not
|
|
|
|
created initially. This situation is equivalent to having acquired the lock:
|
|
|
|
when there is only a single thread, all object accesses are safe. Therefore,
|
|
|
|
when this function initializes the global interpreter lock, it also acquires
|
|
|
|
it. Before the Python :mod:`_thread` module creates a new thread, knowing
|
|
|
|
that either it has the lock or the lock hasn't been created yet, it calls
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:func:`PyEval_InitThreads`. When this call returns, it is guaranteed that
|
2011-01-15 10:29:23 -04:00
|
|
|
the lock has been created and that the calling thread has acquired it.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2011-01-15 10:29:23 -04:00
|
|
|
It is **not** safe to call this function when it is unknown which thread (if
|
|
|
|
any) currently has the global interpreter lock.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2011-01-15 10:29:23 -04:00
|
|
|
This function is not available when thread support is disabled at compile time.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: int PyEval_ThreadsInitialized()
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
Returns a non-zero value if :c:func:`PyEval_InitThreads` has been called. This
|
2009-04-05 14:17:42 -03:00
|
|
|
function can be called without holding the GIL, and therefore can be used to
|
2007-08-15 11:28:01 -03:00
|
|
|
avoid calls to the locking API when running single-threaded. This function is
|
|
|
|
not available when thread support is disabled at compile time.
|
|
|
|
|
|
|
|
.. versionadded:: 2.4
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: PyThreadState* PyEval_SaveThread()
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2009-04-05 14:17:42 -03:00
|
|
|
Release the global interpreter lock (if it has been created and thread
|
|
|
|
support is enabled) and reset the thread state to *NULL*, returning the
|
|
|
|
previous thread state (which is not *NULL*). If the lock has been created,
|
|
|
|
the current thread must have acquired it. (This function is available even
|
|
|
|
when thread support is disabled at compile time.)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: void PyEval_RestoreThread(PyThreadState *tstate)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2009-04-05 14:17:42 -03:00
|
|
|
Acquire the global interpreter lock (if it has been created and thread
|
|
|
|
support is enabled) and set the thread state to *tstate*, which must not be
|
|
|
|
*NULL*. If the lock has been created, the current thread must not have
|
|
|
|
acquired it, otherwise deadlock ensues. (This function is available even
|
|
|
|
when thread support is disabled at compile time.)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2007-12-01 18:24:47 -04:00
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: PyThreadState* PyThreadState_Get()
|
2011-01-15 10:29:23 -04:00
|
|
|
|
|
|
|
Return the current thread state. The global interpreter lock must be held.
|
|
|
|
When the current thread state is *NULL*, this issues a fatal error (so that
|
|
|
|
the caller needn't check for *NULL*).
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: PyThreadState* PyThreadState_Swap(PyThreadState *tstate)
|
2011-01-15 10:29:23 -04:00
|
|
|
|
|
|
|
Swap the current thread state with the thread state given by the argument
|
|
|
|
*tstate*, which may be *NULL*. The global interpreter lock must be held
|
|
|
|
and is not released.
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: void PyEval_ReInitThreads()
|
2007-12-01 18:24:47 -04:00
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
This function is called from :c:func:`PyOS_AfterFork` to ensure that newly
|
2007-12-01 18:24:47 -04:00
|
|
|
created child processes don't hold locks referring to threads which
|
|
|
|
are not running in the child process.
|
|
|
|
|
|
|
|
|
2011-01-15 10:29:23 -04:00
|
|
|
The following functions use thread-local storage, and are not compatible
|
|
|
|
with sub-interpreters:
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: PyGILState_STATE PyGILState_Ensure()
|
2011-01-15 10:29:23 -04:00
|
|
|
|
|
|
|
Ensure that the current thread is ready to call the Python C API regardless
|
|
|
|
of the current state of Python, or of the global interpreter lock. This may
|
|
|
|
be called as many times as desired by a thread as long as each call is
|
2012-01-14 11:42:02 -04:00
|
|
|
matched with a call to :c:func:`PyGILState_Release`. In general, other
|
|
|
|
thread-related APIs may be used between :c:func:`PyGILState_Ensure` and
|
|
|
|
:c:func:`PyGILState_Release` calls as long as the thread state is restored to
|
2011-01-15 10:29:23 -04:00
|
|
|
its previous state before the Release(). For example, normal usage of the
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:macro:`Py_BEGIN_ALLOW_THREADS` and :c:macro:`Py_END_ALLOW_THREADS` macros is
|
2011-01-15 10:29:23 -04:00
|
|
|
acceptable.
|
|
|
|
|
|
|
|
The return value is an opaque "handle" to the thread state when
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:func:`PyGILState_Ensure` was called, and must be passed to
|
|
|
|
:c:func:`PyGILState_Release` to ensure Python is left in the same state. Even
|
2011-01-15 10:29:23 -04:00
|
|
|
though recursive calls are allowed, these handles *cannot* be shared - each
|
2012-01-14 11:42:02 -04:00
|
|
|
unique call to :c:func:`PyGILState_Ensure` must save the handle for its call
|
|
|
|
to :c:func:`PyGILState_Release`.
|
2011-01-15 10:29:23 -04:00
|
|
|
|
|
|
|
When the function returns, the current thread will hold the GIL and be able
|
|
|
|
to call arbitrary Python code. Failure is a fatal error.
|
|
|
|
|
|
|
|
.. versionadded:: 2.3
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: void PyGILState_Release(PyGILState_STATE)
|
2011-01-15 10:29:23 -04:00
|
|
|
|
|
|
|
Release any resources previously acquired. After this call, Python's state will
|
2012-01-14 11:42:02 -04:00
|
|
|
be the same as it was prior to the corresponding :c:func:`PyGILState_Ensure` call
|
2011-01-15 10:29:23 -04:00
|
|
|
(but generally this state will be unknown to the caller, hence the use of the
|
|
|
|
GILState API).
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
Every call to :c:func:`PyGILState_Ensure` must be matched by a call to
|
|
|
|
:c:func:`PyGILState_Release` on the same thread.
|
2011-01-15 10:29:23 -04:00
|
|
|
|
|
|
|
.. versionadded:: 2.3
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: PyThreadState PyGILState_GetThisThreadState()
|
2011-08-07 19:15:57 -03:00
|
|
|
|
|
|
|
Get the current thread state for this thread. May return ``NULL`` if no
|
|
|
|
GILState API has been used on the current thread. Note that the main thread
|
|
|
|
always has such a thread-state, even if no auto-thread-state call has been
|
|
|
|
made on the main thread. This is mainly a helper/diagnostic function.
|
|
|
|
|
|
|
|
.. versionadded:: 2.3
|
|
|
|
|
|
|
|
|
2007-08-15 11:28:01 -03:00
|
|
|
The following macros are normally used without a trailing semicolon; look for
|
|
|
|
example usage in the Python source distribution.
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:macro:: Py_BEGIN_ALLOW_THREADS
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
This macro expands to ``{ PyThreadState *_save; _save = PyEval_SaveThread();``.
|
|
|
|
Note that it contains an opening brace; it must be matched with a following
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:macro:`Py_END_ALLOW_THREADS` macro. See above for further discussion of this
|
2007-08-15 11:28:01 -03:00
|
|
|
macro. It is a no-op when thread support is disabled at compile time.
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:macro:: Py_END_ALLOW_THREADS
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
This macro expands to ``PyEval_RestoreThread(_save); }``. Note that it contains
|
|
|
|
a closing brace; it must be matched with an earlier
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:macro:`Py_BEGIN_ALLOW_THREADS` macro. See above for further discussion of
|
2007-08-15 11:28:01 -03:00
|
|
|
this macro. It is a no-op when thread support is disabled at compile time.
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:macro:: Py_BLOCK_THREADS
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
This macro expands to ``PyEval_RestoreThread(_save);``: it is equivalent to
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:macro:`Py_END_ALLOW_THREADS` without the closing brace. It is a no-op when
|
2007-08-15 11:28:01 -03:00
|
|
|
thread support is disabled at compile time.
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:macro:: Py_UNBLOCK_THREADS
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
This macro expands to ``_save = PyEval_SaveThread();``: it is equivalent to
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:macro:`Py_BEGIN_ALLOW_THREADS` without the opening brace and variable
|
2007-08-15 11:28:01 -03:00
|
|
|
declaration. It is a no-op when thread support is disabled at compile time.
|
|
|
|
|
2011-01-15 10:29:23 -04:00
|
|
|
|
|
|
|
Low-level API
|
|
|
|
-------------
|
|
|
|
|
2007-08-15 11:28:01 -03:00
|
|
|
All of the following functions are only available when thread support is enabled
|
2009-04-05 14:17:42 -03:00
|
|
|
at compile time, and must be called only when the global interpreter lock has
|
|
|
|
been created.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: PyInterpreterState* PyInterpreterState_New()
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2009-04-05 14:17:42 -03:00
|
|
|
Create a new interpreter state object. The global interpreter lock need not
|
|
|
|
be held, but may be held if it is necessary to serialize calls to this
|
|
|
|
function.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: void PyInterpreterState_Clear(PyInterpreterState *interp)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2009-04-05 14:17:42 -03:00
|
|
|
Reset all information in an interpreter state object. The global interpreter
|
|
|
|
lock must be held.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: void PyInterpreterState_Delete(PyInterpreterState *interp)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2009-04-05 14:17:42 -03:00
|
|
|
Destroy an interpreter state object. The global interpreter lock need not be
|
|
|
|
held. The interpreter state must have been reset with a previous call to
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:func:`PyInterpreterState_Clear`.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: PyThreadState* PyThreadState_New(PyInterpreterState *interp)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2009-04-05 14:17:42 -03:00
|
|
|
Create a new thread state object belonging to the given interpreter object.
|
|
|
|
The global interpreter lock need not be held, but may be held if it is
|
|
|
|
necessary to serialize calls to this function.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: void PyThreadState_Clear(PyThreadState *tstate)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2009-04-05 14:17:42 -03:00
|
|
|
Reset all information in a thread state object. The global interpreter lock
|
|
|
|
must be held.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: void PyThreadState_Delete(PyThreadState *tstate)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2009-04-05 14:17:42 -03:00
|
|
|
Destroy a thread state object. The global interpreter lock need not be held.
|
|
|
|
The thread state must have been reset with a previous call to
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:func:`PyThreadState_Clear`.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: PyObject* PyThreadState_GetDict()
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
Return a dictionary in which extensions can store thread-specific state
|
|
|
|
information. Each extension should use a unique key to use to store state in
|
|
|
|
the dictionary. It is okay to call this function when no current thread state
|
|
|
|
is available. If this function returns *NULL*, no exception has been raised and
|
|
|
|
the caller should assume no current thread state is available.
|
|
|
|
|
|
|
|
.. versionchanged:: 2.3
|
|
|
|
Previously this could only be called when a current thread is active, and *NULL*
|
|
|
|
meant that an exception was raised.
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: int PyThreadState_SetAsyncExc(long id, PyObject *exc)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
Asynchronously raise an exception in a thread. The *id* argument is the thread
|
|
|
|
id of the target thread; *exc* is the exception object to be raised. This
|
|
|
|
function does not steal any references to *exc*. To prevent naive misuse, you
|
|
|
|
must write your own C extension to call this. Must be called with the GIL held.
|
|
|
|
Returns the number of thread states modified; this is normally one, but will be
|
|
|
|
zero if the thread id isn't found. If *exc* is :const:`NULL`, the pending
|
|
|
|
exception (if any) for the thread is cleared. This raises no exceptions.
|
|
|
|
|
|
|
|
.. versionadded:: 2.3
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: void PyEval_AcquireThread(PyThreadState *tstate)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2011-01-15 10:29:23 -04:00
|
|
|
Acquire the global interpreter lock and set the current thread state to
|
|
|
|
*tstate*, which should not be *NULL*. The lock must have been created earlier.
|
|
|
|
If this thread already has the lock, deadlock ensues.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:func:`PyEval_RestoreThread` is a higher-level function which is always
|
2011-01-15 10:29:23 -04:00
|
|
|
available (even when thread support isn't enabled or when threads have
|
|
|
|
not been initialized).
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: void PyEval_ReleaseThread(PyThreadState *tstate)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2011-01-15 10:29:23 -04:00
|
|
|
Reset the current thread state to *NULL* and release the global interpreter
|
|
|
|
lock. The lock must have been created earlier and must be held by the current
|
|
|
|
thread. The *tstate* argument, which must not be *NULL*, is only used to check
|
|
|
|
that it represents the current thread state --- if it isn't, a fatal error is
|
|
|
|
reported.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:func:`PyEval_SaveThread` is a higher-level function which is always
|
2011-01-15 10:29:23 -04:00
|
|
|
available (even when thread support isn't enabled or when threads have
|
|
|
|
not been initialized).
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: void PyEval_AcquireLock()
|
2011-01-15 10:29:23 -04:00
|
|
|
|
|
|
|
Acquire the global interpreter lock. The lock must have been created earlier.
|
|
|
|
If this thread already has the lock, a deadlock ensues.
|
|
|
|
|
|
|
|
.. warning::
|
|
|
|
This function does not change the current thread state. Please use
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:func:`PyEval_RestoreThread` or :c:func:`PyEval_AcquireThread`
|
2011-01-15 10:29:23 -04:00
|
|
|
instead.
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: void PyEval_ReleaseLock()
|
2011-01-15 10:29:23 -04:00
|
|
|
|
|
|
|
Release the global interpreter lock. The lock must have been created earlier.
|
|
|
|
|
|
|
|
.. warning::
|
|
|
|
This function does not change the current thread state. Please use
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:func:`PyEval_SaveThread` or :c:func:`PyEval_ReleaseThread`
|
2011-01-15 10:29:23 -04:00
|
|
|
instead.
|
|
|
|
|
|
|
|
|
|
|
|
Sub-interpreter support
|
|
|
|
=======================
|
|
|
|
|
|
|
|
While in most uses, you will only embed a single Python interpreter, there
|
|
|
|
are cases where you need to create several independent interpreters in the
|
|
|
|
same process and perhaps even in the same thread. Sub-interpreters allow
|
|
|
|
you to do that. You can switch between sub-interpreters using the
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:func:`PyThreadState_Swap` function. You can create and destroy them
|
2011-01-15 10:29:23 -04:00
|
|
|
using the following functions:
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: PyThreadState* Py_NewInterpreter()
|
2011-01-15 10:29:23 -04:00
|
|
|
|
|
|
|
.. index::
|
|
|
|
module: builtins
|
|
|
|
module: __main__
|
|
|
|
module: sys
|
|
|
|
single: stdout (in module sys)
|
|
|
|
single: stderr (in module sys)
|
|
|
|
single: stdin (in module sys)
|
|
|
|
|
|
|
|
Create a new sub-interpreter. This is an (almost) totally separate environment
|
|
|
|
for the execution of Python code. In particular, the new interpreter has
|
|
|
|
separate, independent versions of all imported modules, including the
|
|
|
|
fundamental modules :mod:`builtins`, :mod:`__main__` and :mod:`sys`. The
|
|
|
|
table of loaded modules (``sys.modules``) and the module search path
|
|
|
|
(``sys.path``) are also separate. The new environment has no ``sys.argv``
|
|
|
|
variable. It has new standard I/O stream file objects ``sys.stdin``,
|
|
|
|
``sys.stdout`` and ``sys.stderr`` (however these refer to the same underlying
|
|
|
|
file descriptors).
|
|
|
|
|
|
|
|
The return value points to the first thread state created in the new
|
|
|
|
sub-interpreter. This thread state is made in the current thread state.
|
|
|
|
Note that no actual thread is created; see the discussion of thread states
|
|
|
|
below. If creation of the new interpreter is unsuccessful, *NULL* is
|
|
|
|
returned; no exception is set since the exception state is stored in the
|
|
|
|
current thread state and there may not be a current thread state. (Like all
|
|
|
|
other Python/C API functions, the global interpreter lock must be held before
|
|
|
|
calling this function and is still held when it returns; however, unlike most
|
|
|
|
other Python/C API functions, there needn't be a current thread state on
|
|
|
|
entry.)
|
|
|
|
|
|
|
|
.. index::
|
|
|
|
single: Py_Finalize()
|
|
|
|
single: Py_Initialize()
|
|
|
|
|
|
|
|
Extension modules are shared between (sub-)interpreters as follows: the first
|
|
|
|
time a particular extension is imported, it is initialized normally, and a
|
|
|
|
(shallow) copy of its module's dictionary is squirreled away. When the same
|
|
|
|
extension is imported by another (sub-)interpreter, a new module is initialized
|
|
|
|
and filled with the contents of this copy; the extension's ``init`` function is
|
|
|
|
not called. Note that this is different from what happens when an extension is
|
|
|
|
imported after the interpreter has been completely re-initialized by calling
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:func:`Py_Finalize` and :c:func:`Py_Initialize`; in that case, the extension's
|
2011-01-15 10:29:23 -04:00
|
|
|
``initmodule`` function *is* called again.
|
|
|
|
|
|
|
|
.. index:: single: close() (in module os)
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: void Py_EndInterpreter(PyThreadState *tstate)
|
2011-01-15 10:29:23 -04:00
|
|
|
|
|
|
|
.. index:: single: Py_Finalize()
|
|
|
|
|
|
|
|
Destroy the (sub-)interpreter represented by the given thread state. The given
|
|
|
|
thread state must be the current thread state. See the discussion of thread
|
|
|
|
states below. When the call returns, the current thread state is *NULL*. All
|
|
|
|
thread states associated with this interpreter are destroyed. (The global
|
|
|
|
interpreter lock must be held before calling this function and is still held
|
2012-01-14 11:42:02 -04:00
|
|
|
when it returns.) :c:func:`Py_Finalize` will destroy all sub-interpreters that
|
2011-01-15 10:29:23 -04:00
|
|
|
haven't been explicitly destroyed at that point.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2011-01-15 10:29:23 -04:00
|
|
|
Bugs and caveats
|
|
|
|
----------------
|
|
|
|
|
|
|
|
Because sub-interpreters (and the main interpreter) are part of the same
|
|
|
|
process, the insulation between them isn't perfect --- for example, using
|
|
|
|
low-level file operations like :func:`os.close` they can
|
|
|
|
(accidentally or maliciously) affect each other's open files. Because of the
|
|
|
|
way extensions are shared between (sub-)interpreters, some extensions may not
|
|
|
|
work properly; this is especially likely when the extension makes use of
|
|
|
|
(static) global variables, or when the extension manipulates its module's
|
|
|
|
dictionary after its initialization. It is possible to insert objects created
|
|
|
|
in one sub-interpreter into a namespace of another sub-interpreter; this should
|
|
|
|
be done with great care to avoid sharing user-defined functions, methods,
|
|
|
|
instances or classes between sub-interpreters, since import operations executed
|
|
|
|
by such objects may affect the wrong (sub-)interpreter's dictionary of loaded
|
|
|
|
modules.
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
Also note that combining this functionality with :c:func:`PyGILState_\*` APIs
|
2011-05-05 08:19:48 -03:00
|
|
|
is delicate, because these APIs assume a bijection between Python thread states
|
2011-01-15 10:29:23 -04:00
|
|
|
and OS-level threads, an assumption broken by the presence of sub-interpreters.
|
|
|
|
It is highly recommended that you don't switch sub-interpreters between a pair
|
2012-01-14 11:42:02 -04:00
|
|
|
of matching :c:func:`PyGILState_Ensure` and :c:func:`PyGILState_Release` calls.
|
2011-01-15 10:29:23 -04:00
|
|
|
Furthermore, extensions (such as :mod:`ctypes`) using these APIs to allow calling
|
|
|
|
of Python code from non-Python created threads will probably be broken when using
|
|
|
|
sub-interpreters.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2009-01-09 17:35:16 -04:00
|
|
|
|
|
|
|
Asynchronous Notifications
|
|
|
|
==========================
|
|
|
|
|
2009-04-03 18:45:29 -03:00
|
|
|
A mechanism is provided to make asynchronous notifications to the main
|
2009-01-09 17:35:16 -04:00
|
|
|
interpreter thread. These notifications take the form of a function
|
2013-09-30 16:35:44 -03:00
|
|
|
pointer and a void pointer argument.
|
2009-01-09 17:35:16 -04:00
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: int Py_AddPendingCall(int (*func)(void *), void *arg)
|
2009-01-09 17:35:16 -04:00
|
|
|
|
|
|
|
.. index:: single: Py_AddPendingCall()
|
|
|
|
|
2013-09-30 16:35:44 -03:00
|
|
|
Schedule a function to be called from the main interpreter thread. On
|
|
|
|
success, 0 is returned and *func* is queued for being called in the
|
|
|
|
main thread. On failure, -1 is returned without setting any exception.
|
2009-01-09 17:35:16 -04:00
|
|
|
|
2013-09-30 16:35:44 -03:00
|
|
|
When successfully queued, *func* will be *eventually* called from the
|
|
|
|
main interpreter thread with the argument *arg*. It will be called
|
|
|
|
asynchronously with respect to normally running Python code, but with
|
|
|
|
both these conditions met:
|
2009-01-09 17:35:16 -04:00
|
|
|
|
2013-09-30 16:35:44 -03:00
|
|
|
* on a :term:`bytecode` boundary;
|
|
|
|
* with the main thread holding the :term:`global interpreter lock`
|
|
|
|
(*func* can therefore use the full C API).
|
2009-01-09 17:35:16 -04:00
|
|
|
|
2013-09-30 16:35:44 -03:00
|
|
|
*func* must return 0 on success, or -1 on failure with an exception
|
|
|
|
set. *func* won't be interrupted to perform another asynchronous
|
|
|
|
notification recursively, but it can still be interrupted to switch
|
|
|
|
threads if the global interpreter lock is released.
|
2009-01-09 17:35:16 -04:00
|
|
|
|
2013-09-30 16:35:44 -03:00
|
|
|
This function doesn't need a current thread state to run, and it doesn't
|
|
|
|
need the global interpreter lock.
|
|
|
|
|
|
|
|
.. warning::
|
|
|
|
This is a low-level function, only useful for very special cases.
|
|
|
|
There is no guarantee that *func* will be called as quick as
|
|
|
|
possible. If the main thread is busy executing a system call,
|
|
|
|
*func* won't be called before the system call returns. This
|
|
|
|
function is generally **not** suitable for calling Python code from
|
|
|
|
arbitrary C threads. Instead, use the :ref:`PyGILState API<gilstate>`.
|
|
|
|
|
|
|
|
.. versionadded:: 2.7
|
2009-01-09 17:35:16 -04:00
|
|
|
|
|
|
|
|
2007-08-15 11:28:01 -03:00
|
|
|
.. _profiling:
|
|
|
|
|
|
|
|
Profiling and Tracing
|
|
|
|
=====================
|
|
|
|
|
|
|
|
.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
|
|
|
|
|
|
|
|
|
|
|
|
The Python interpreter provides some low-level support for attaching profiling
|
|
|
|
and execution tracing facilities. These are used for profiling, debugging, and
|
|
|
|
coverage analysis tools.
|
|
|
|
|
|
|
|
Starting with Python 2.2, the implementation of this facility was substantially
|
|
|
|
revised, and an interface from C was added. This C interface allows the
|
|
|
|
profiling or tracing code to avoid the overhead of calling through Python-level
|
|
|
|
callable objects, making a direct C function call instead. The essential
|
|
|
|
attributes of the facility have not changed; the interface allows trace
|
|
|
|
functions to be installed per-thread, and the basic events reported to the trace
|
|
|
|
function are the same as had been reported to the Python-level trace functions
|
|
|
|
in previous versions.
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:type:: int (*Py_tracefunc)(PyObject *obj, PyFrameObject *frame, int what, PyObject *arg)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
The type of the trace function registered using :c:func:`PyEval_SetProfile` and
|
|
|
|
:c:func:`PyEval_SetTrace`. The first parameter is the object passed to the
|
2007-08-15 11:28:01 -03:00
|
|
|
registration function as *obj*, *frame* is the frame object to which the event
|
|
|
|
pertains, *what* is one of the constants :const:`PyTrace_CALL`,
|
|
|
|
:const:`PyTrace_EXCEPTION`, :const:`PyTrace_LINE`, :const:`PyTrace_RETURN`,
|
|
|
|
:const:`PyTrace_C_CALL`, :const:`PyTrace_C_EXCEPTION`, or
|
|
|
|
:const:`PyTrace_C_RETURN`, and *arg* depends on the value of *what*:
|
|
|
|
|
|
|
|
+------------------------------+--------------------------------------+
|
|
|
|
| Value of *what* | Meaning of *arg* |
|
|
|
|
+==============================+======================================+
|
|
|
|
| :const:`PyTrace_CALL` | Always *NULL*. |
|
|
|
|
+------------------------------+--------------------------------------+
|
|
|
|
| :const:`PyTrace_EXCEPTION` | Exception information as returned by |
|
|
|
|
| | :func:`sys.exc_info`. |
|
|
|
|
+------------------------------+--------------------------------------+
|
|
|
|
| :const:`PyTrace_LINE` | Always *NULL*. |
|
|
|
|
+------------------------------+--------------------------------------+
|
2010-11-26 03:34:20 -04:00
|
|
|
| :const:`PyTrace_RETURN` | Value being returned to the caller, |
|
|
|
|
| | or *NULL* if caused by an exception. |
|
2007-08-15 11:28:01 -03:00
|
|
|
+------------------------------+--------------------------------------+
|
2010-11-26 03:34:20 -04:00
|
|
|
| :const:`PyTrace_C_CALL` | Function object being called. |
|
2007-08-15 11:28:01 -03:00
|
|
|
+------------------------------+--------------------------------------+
|
2010-11-26 03:34:20 -04:00
|
|
|
| :const:`PyTrace_C_EXCEPTION` | Function object being called. |
|
2007-08-15 11:28:01 -03:00
|
|
|
+------------------------------+--------------------------------------+
|
2010-11-26 03:34:20 -04:00
|
|
|
| :const:`PyTrace_C_RETURN` | Function object being called. |
|
2007-08-15 11:28:01 -03:00
|
|
|
+------------------------------+--------------------------------------+
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:var:: int PyTrace_CALL
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
The value of the *what* parameter to a :c:type:`Py_tracefunc` function when a new
|
2007-08-15 11:28:01 -03:00
|
|
|
call to a function or method is being reported, or a new entry into a generator.
|
|
|
|
Note that the creation of the iterator for a generator function is not reported
|
|
|
|
as there is no control transfer to the Python bytecode in the corresponding
|
|
|
|
frame.
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:var:: int PyTrace_EXCEPTION
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
The value of the *what* parameter to a :c:type:`Py_tracefunc` function when an
|
2007-08-15 11:28:01 -03:00
|
|
|
exception has been raised. The callback function is called with this value for
|
|
|
|
*what* when after any bytecode is processed after which the exception becomes
|
|
|
|
set within the frame being executed. The effect of this is that as exception
|
|
|
|
propagation causes the Python stack to unwind, the callback is called upon
|
|
|
|
return to each frame as the exception propagates. Only trace functions receives
|
|
|
|
these events; they are not needed by the profiler.
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:var:: int PyTrace_LINE
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
The value passed as the *what* parameter to a trace function (but not a
|
|
|
|
profiling function) when a line-number event is being reported.
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:var:: int PyTrace_RETURN
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
The value for the *what* parameter to :c:type:`Py_tracefunc` functions when a
|
2007-08-15 11:28:01 -03:00
|
|
|
call is returning without propagating an exception.
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:var:: int PyTrace_C_CALL
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
The value for the *what* parameter to :c:type:`Py_tracefunc` functions when a C
|
2007-08-15 11:28:01 -03:00
|
|
|
function is about to be called.
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:var:: int PyTrace_C_EXCEPTION
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
The value for the *what* parameter to :c:type:`Py_tracefunc` functions when a C
|
Merged revisions 82798,82805,83659,83977,84015,84018,84141,84264,84326-84327,84480,84482,84484,84530-84531,84553,84619,84915-84916 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r82798 | georg.brandl | 2010-07-11 11:23:11 +0200 (So, 11 Jul 2010) | 1 line
#6774: explain shutdown() behavior varying with platform.
........
r82805 | georg.brandl | 2010-07-11 11:42:10 +0200 (So, 11 Jul 2010) | 1 line
#7935: cross-reference to ast.literal_eval() from eval() docs.
........
r83659 | georg.brandl | 2010-08-03 14:06:29 +0200 (Di, 03 Aug 2010) | 1 line
Terminology fix: exceptions are raised, except in generator.throw().
........
r83977 | georg.brandl | 2010-08-13 17:10:49 +0200 (Fr, 13 Aug 2010) | 1 line
Fix copy-paste error.
........
r84015 | georg.brandl | 2010-08-14 17:44:34 +0200 (Sa, 14 Aug 2010) | 1 line
Add some maintainers.
........
r84018 | georg.brandl | 2010-08-14 17:48:49 +0200 (Sa, 14 Aug 2010) | 1 line
Typo fix.
........
r84141 | georg.brandl | 2010-08-17 16:11:59 +0200 (Di, 17 Aug 2010) | 1 line
Markup nits.
........
r84264 | georg.brandl | 2010-08-22 22:23:38 +0200 (So, 22 Aug 2010) | 1 line
#9649: fix default value description.
........
r84326 | georg.brandl | 2010-08-26 16:30:15 +0200 (Do, 26 Aug 2010) | 1 line
#9689: add links from overview to in-depth class API descriptions.
........
r84327 | georg.brandl | 2010-08-26 16:30:56 +0200 (Do, 26 Aug 2010) | 1 line
#9681: typo.
........
r84480 | georg.brandl | 2010-09-04 00:33:27 +0200 (Sa, 04 Sep 2010) | 1 line
More inclusive title.
........
r84482 | georg.brandl | 2010-09-04 00:40:02 +0200 (Sa, 04 Sep 2010) | 1 line
#9760: clarify what context expression is.
........
r84484 | georg.brandl | 2010-09-04 00:49:27 +0200 (Sa, 04 Sep 2010) | 1 line
Fix missing word.
........
r84530 | georg.brandl | 2010-09-05 19:07:12 +0200 (So, 05 Sep 2010) | 1 line
#9747: fix copy-paste error in getresgid() doc.
........
r84531 | georg.brandl | 2010-09-05 19:09:18 +0200 (So, 05 Sep 2010) | 1 line
#9776: fix some spacing.
........
r84553 | georg.brandl | 2010-09-06 08:49:07 +0200 (Mo, 06 Sep 2010) | 1 line
#9780: both { and } are not valid fill characters.
........
r84619 | georg.brandl | 2010-09-08 12:43:45 +0200 (Mi, 08 Sep 2010) | 1 line
Add Lukasz.
........
r84915 | georg.brandl | 2010-09-20 08:27:02 +0200 (Mo, 20 Sep 2010) | 1 line
Fix typo.
........
r84916 | georg.brandl | 2010-09-20 08:29:01 +0200 (Mo, 20 Sep 2010) | 1 line
Mention % as string formatting.
........
2010-10-06 06:28:45 -03:00
|
|
|
function has raised an exception.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:var:: int PyTrace_C_RETURN
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
The value for the *what* parameter to :c:type:`Py_tracefunc` functions when a C
|
2007-08-15 11:28:01 -03:00
|
|
|
function has returned.
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: void PyEval_SetProfile(Py_tracefunc func, PyObject *obj)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
Set the profiler function to *func*. The *obj* parameter is passed to the
|
|
|
|
function as its first parameter, and may be any Python object, or *NULL*. If
|
|
|
|
the profile function needs to maintain state, using a different value for *obj*
|
|
|
|
for each thread provides a convenient and thread-safe place to store it. The
|
|
|
|
profile function is called for all monitored events except the line-number
|
|
|
|
events.
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: void PyEval_SetTrace(Py_tracefunc func, PyObject *obj)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
Set the tracing function to *func*. This is similar to
|
2012-01-14 11:42:02 -04:00
|
|
|
:c:func:`PyEval_SetProfile`, except the tracing function does receive line-number
|
2007-08-15 11:28:01 -03:00
|
|
|
events.
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: PyObject* PyEval_GetCallStats(PyObject *self)
|
2007-12-01 18:24:47 -04:00
|
|
|
|
|
|
|
Return a tuple of function call counts. There are constants defined for the
|
|
|
|
positions within the tuple:
|
2009-01-03 16:55:06 -04:00
|
|
|
|
2007-12-01 18:24:47 -04:00
|
|
|
+-------------------------------+-------+
|
|
|
|
| Name | Value |
|
|
|
|
+===============================+=======+
|
|
|
|
| :const:`PCALL_ALL` | 0 |
|
|
|
|
+-------------------------------+-------+
|
|
|
|
| :const:`PCALL_FUNCTION` | 1 |
|
|
|
|
+-------------------------------+-------+
|
|
|
|
| :const:`PCALL_FAST_FUNCTION` | 2 |
|
|
|
|
+-------------------------------+-------+
|
|
|
|
| :const:`PCALL_FASTER_FUNCTION`| 3 |
|
|
|
|
+-------------------------------+-------+
|
|
|
|
| :const:`PCALL_METHOD` | 4 |
|
|
|
|
+-------------------------------+-------+
|
|
|
|
| :const:`PCALL_BOUND_METHOD` | 5 |
|
|
|
|
+-------------------------------+-------+
|
|
|
|
| :const:`PCALL_CFUNCTION` | 6 |
|
|
|
|
+-------------------------------+-------+
|
|
|
|
| :const:`PCALL_TYPE` | 7 |
|
|
|
|
+-------------------------------+-------+
|
|
|
|
| :const:`PCALL_GENERATOR` | 8 |
|
|
|
|
+-------------------------------+-------+
|
|
|
|
| :const:`PCALL_OTHER` | 9 |
|
|
|
|
+-------------------------------+-------+
|
|
|
|
| :const:`PCALL_POP` | 10 |
|
|
|
|
+-------------------------------+-------+
|
2009-01-03 16:55:06 -04:00
|
|
|
|
2007-12-01 18:24:47 -04:00
|
|
|
:const:`PCALL_FAST_FUNCTION` means no argument tuple needs to be created.
|
|
|
|
:const:`PCALL_FASTER_FUNCTION` means that the fast-path frame setup code is used.
|
|
|
|
|
|
|
|
If there is a method call where the call can be optimized by changing
|
|
|
|
the argument tuple and calling the function directly, it gets recorded
|
|
|
|
twice.
|
|
|
|
|
|
|
|
This function is only present if Python is compiled with :const:`CALL_PROFILE`
|
|
|
|
defined.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
.. _advanced-debugging:
|
|
|
|
|
|
|
|
Advanced Debugger Support
|
|
|
|
=========================
|
|
|
|
|
|
|
|
.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
|
|
|
|
|
|
|
|
|
|
|
|
These functions are only intended to be used by advanced debugging tools.
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: PyInterpreterState* PyInterpreterState_Head()
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
Return the interpreter state object at the head of the list of all such objects.
|
|
|
|
|
|
|
|
.. versionadded:: 2.2
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: PyInterpreterState* PyInterpreterState_Next(PyInterpreterState *interp)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
Return the next interpreter state object after *interp* from the list of all
|
|
|
|
such objects.
|
|
|
|
|
|
|
|
.. versionadded:: 2.2
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: PyThreadState * PyInterpreterState_ThreadHead(PyInterpreterState *interp)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
Return the a pointer to the first :c:type:`PyThreadState` object in the list of
|
2007-08-15 11:28:01 -03:00
|
|
|
threads associated with the interpreter *interp*.
|
|
|
|
|
|
|
|
.. versionadded:: 2.2
|
|
|
|
|
|
|
|
|
2012-01-14 11:42:02 -04:00
|
|
|
.. c:function:: PyThreadState* PyThreadState_Next(PyThreadState *tstate)
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
Return the next thread state object after *tstate* from the list of all such
|
2012-01-14 11:42:02 -04:00
|
|
|
objects belonging to the same :c:type:`PyInterpreterState` object.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
.. versionadded:: 2.2
|
|
|
|
|