Stéphane Wirtel | cbb6484 | 2019-05-17 11:55:34 +0200 | [diff] [blame] | 1 | .. highlight:: c |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2 | |
| 3 | |
| 4 | .. _api-intro: |
| 5 | |
| 6 | ************ |
| 7 | Introduction |
| 8 | ************ |
| 9 | |
| 10 | The Application Programmer's Interface to Python gives C and C++ programmers |
| 11 | access to the Python interpreter at a variety of levels. The API is equally |
| 12 | usable from C++, but for brevity it is generally referred to as the Python/C |
| 13 | API. There are two fundamentally different reasons for using the Python/C API. |
| 14 | The first reason is to write *extension modules* for specific purposes; these |
| 15 | are C modules that extend the Python interpreter. This is probably the most |
| 16 | common use. The second reason is to use Python as a component in a larger |
| 17 | application; this technique is generally referred to as :dfn:`embedding` Python |
| 18 | in an application. |
| 19 | |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 20 | Writing an extension module is a relatively well-understood process, where a |
| 21 | "cookbook" approach works well. There are several tools that automate the |
| 22 | process to some extent. While people have embedded Python in other |
| 23 | applications since its early existence, the process of embedding Python is |
| 24 | less straightforward than writing an extension. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 25 | |
| 26 | Many API functions are useful independent of whether you're embedding or |
| 27 | extending Python; moreover, most applications that embed Python will need to |
| 28 | provide a custom extension as well, so it's probably a good idea to become |
| 29 | familiar with writing an extension before attempting to embed Python in a real |
| 30 | application. |
| 31 | |
| 32 | |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 33 | Coding standards |
| 34 | ================ |
| 35 | |
| 36 | If you're writing C code for inclusion in CPython, you **must** follow the |
| 37 | guidelines and standards defined in :PEP:`7`. These guidelines apply |
| 38 | regardless of the version of Python you are contributing to. Following these |
| 39 | conventions is not necessary for your own third party extension modules, |
| 40 | unless you eventually expect to contribute them to Python. |
| 41 | |
| 42 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 43 | .. _api-includes: |
| 44 | |
| 45 | Include Files |
| 46 | ============= |
| 47 | |
| 48 | All function, type and macro definitions needed to use the Python/C API are |
| 49 | included in your code by the following line:: |
| 50 | |
Inada Naoki | c88fece | 2019-04-13 10:46:21 +0900 | [diff] [blame] | 51 | #define PY_SSIZE_T_CLEAN |
| 52 | #include <Python.h> |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 53 | |
| 54 | This implies inclusion of the following standard headers: ``<stdio.h>``, |
Georg Brandl | 4f13d61 | 2010-11-23 18:14:57 +0000 | [diff] [blame] | 55 | ``<string.h>``, ``<errno.h>``, ``<limits.h>``, ``<assert.h>`` and ``<stdlib.h>`` |
| 56 | (if available). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 57 | |
Georg Brandl | e720c0a | 2009-04-27 16:20:50 +0000 | [diff] [blame] | 58 | .. note:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 59 | |
| 60 | Since Python may define some pre-processor definitions which affect the standard |
| 61 | headers on some systems, you *must* include :file:`Python.h` before any standard |
| 62 | headers are included. |
| 63 | |
Inada Naoki | c88fece | 2019-04-13 10:46:21 +0900 | [diff] [blame] | 64 | It is recommended to always define ``PY_SSIZE_T_CLEAN`` before including |
| 65 | ``Python.h``. See :ref:`arg-parsing` for a description of this macro. |
| 66 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 67 | All user visible names defined by Python.h (except those defined by the included |
| 68 | standard headers) have one of the prefixes ``Py`` or ``_Py``. Names beginning |
| 69 | with ``_Py`` are for internal use by the Python implementation and should not be |
| 70 | used by extension writers. Structure member names do not have a reserved prefix. |
| 71 | |
Kyle Stanley | b6dafe5 | 2019-09-10 11:09:34 -0400 | [diff] [blame] | 72 | .. note:: |
| 73 | |
| 74 | User code should never define names that begin with ``Py`` or ``_Py``. This |
| 75 | confuses the reader, and jeopardizes the portability of the user code to |
| 76 | future Python versions, which may define additional names beginning with one |
| 77 | of these prefixes. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 78 | |
| 79 | The header files are typically installed with Python. On Unix, these are |
| 80 | located in the directories :file:`{prefix}/include/pythonversion/` and |
| 81 | :file:`{exec_prefix}/include/pythonversion/`, where :envvar:`prefix` and |
| 82 | :envvar:`exec_prefix` are defined by the corresponding parameters to Python's |
Serhiy Storchaka | 885bdc4 | 2016-02-11 13:10:36 +0200 | [diff] [blame] | 83 | :program:`configure` script and *version* is |
| 84 | ``'%d.%d' % sys.version_info[:2]``. On Windows, the headers are installed |
| 85 | in :file:`{prefix}/include`, where :envvar:`prefix` is the installation |
| 86 | directory specified to the installer. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 87 | |
| 88 | To include the headers, place both directories (if different) on your compiler's |
| 89 | search path for includes. Do *not* place the parent directories on the search |
| 90 | path and then use ``#include <pythonX.Y/Python.h>``; this will break on |
| 91 | multi-platform builds since the platform independent headers under |
| 92 | :envvar:`prefix` include the platform specific headers from |
| 93 | :envvar:`exec_prefix`. |
| 94 | |
Kyle Stanley | b6dafe5 | 2019-09-10 11:09:34 -0400 | [diff] [blame] | 95 | C++ users should note that although the API is defined entirely using C, the |
| 96 | header files properly declare the entry points to be ``extern "C"``. As a result, |
| 97 | there is no need to do anything special to use the API from C++. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 98 | |
| 99 | |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 100 | Useful macros |
| 101 | ============= |
| 102 | |
| 103 | Several useful macros are defined in the Python header files. Many are |
| 104 | defined closer to where they are useful (e.g. :c:macro:`Py_RETURN_NONE`). |
| 105 | Others of a more general utility are defined here. This is not necessarily a |
| 106 | complete listing. |
| 107 | |
| 108 | .. c:macro:: Py_UNREACHABLE() |
| 109 | |
Serhiy Storchaka | eebaa9b | 2020-03-09 20:49:52 +0200 | [diff] [blame] | 110 | Use this when you have a code path that cannot be reached by design. |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 111 | For example, in the ``default:`` clause in a ``switch`` statement for which |
| 112 | all possible values are covered in ``case`` statements. Use this in places |
| 113 | where you might be tempted to put an ``assert(0)`` or ``abort()`` call. |
| 114 | |
Serhiy Storchaka | eebaa9b | 2020-03-09 20:49:52 +0200 | [diff] [blame] | 115 | In release mode, the macro helps the compiler to optimize the code, and |
| 116 | avoids a warning about unreachable code. For example, the macro is |
| 117 | implemented with ``__builtin_unreachable()`` on GCC in release mode. |
| 118 | |
| 119 | A use for ``Py_UNREACHABLE()`` is following a call a function that |
| 120 | never returns but that is not declared :c:macro:`_Py_NO_RETURN`. |
| 121 | |
| 122 | If a code path is very unlikely code but can be reached under exceptional |
| 123 | case, this macro must not be used. For example, under low memory condition |
| 124 | or if a system call returns a value out of the expected range. In this |
| 125 | case, it's better to report the error to the caller. If the error cannot |
| 126 | be reported to caller, :c:func:`Py_FatalError` can be used. |
| 127 | |
Petr Viktorin | 8bf288e | 2017-11-08 14:11:16 +0100 | [diff] [blame] | 128 | .. versionadded:: 3.7 |
| 129 | |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 130 | .. c:macro:: Py_ABS(x) |
| 131 | |
| 132 | Return the absolute value of ``x``. |
| 133 | |
Victor Stinner | 54cc0c0 | 2017-11-08 06:06:24 -0800 | [diff] [blame] | 134 | .. versionadded:: 3.3 |
| 135 | |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 136 | .. c:macro:: Py_MIN(x, y) |
| 137 | |
| 138 | Return the minimum value between ``x`` and ``y``. |
| 139 | |
Victor Stinner | 54cc0c0 | 2017-11-08 06:06:24 -0800 | [diff] [blame] | 140 | .. versionadded:: 3.3 |
| 141 | |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 142 | .. c:macro:: Py_MAX(x, y) |
| 143 | |
| 144 | Return the maximum value between ``x`` and ``y``. |
| 145 | |
Victor Stinner | 54cc0c0 | 2017-11-08 06:06:24 -0800 | [diff] [blame] | 146 | .. versionadded:: 3.3 |
| 147 | |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 148 | .. c:macro:: Py_STRINGIFY(x) |
| 149 | |
| 150 | Convert ``x`` to a C string. E.g. ``Py_STRINGIFY(123)`` returns |
| 151 | ``"123"``. |
| 152 | |
Victor Stinner | 54cc0c0 | 2017-11-08 06:06:24 -0800 | [diff] [blame] | 153 | .. versionadded:: 3.4 |
| 154 | |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 155 | .. c:macro:: Py_MEMBER_SIZE(type, member) |
| 156 | |
| 157 | Return the size of a structure (``type``) ``member`` in bytes. |
| 158 | |
Victor Stinner | 54cc0c0 | 2017-11-08 06:06:24 -0800 | [diff] [blame] | 159 | .. versionadded:: 3.6 |
| 160 | |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 161 | .. c:macro:: Py_CHARMASK(c) |
| 162 | |
| 163 | Argument must be a character or an integer in the range [-128, 127] or [0, |
| 164 | 255]. This macro returns ``c`` cast to an ``unsigned char``. |
| 165 | |
Barry Warsaw | a51b90a | 2017-10-06 09:53:48 -0400 | [diff] [blame] | 166 | .. c:macro:: Py_GETENV(s) |
| 167 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 168 | Like ``getenv(s)``, but returns ``NULL`` if :option:`-E` was passed on the |
Barry Warsaw | a51b90a | 2017-10-06 09:53:48 -0400 | [diff] [blame] | 169 | command line (i.e. if ``Py_IgnoreEnvironmentFlag`` is set). |
| 170 | |
Petr Viktorin | 2138163 | 2017-11-08 16:59:20 +0100 | [diff] [blame] | 171 | .. c:macro:: Py_UNUSED(arg) |
| 172 | |
| 173 | Use this for unused arguments in a function definition to silence compiler |
Victor Stinner | b3a9843 | 2019-05-24 15:16:08 +0200 | [diff] [blame] | 174 | warnings. Example: ``int func(int a, int Py_UNUSED(b)) { return a; }``. |
Petr Viktorin | 2138163 | 2017-11-08 16:59:20 +0100 | [diff] [blame] | 175 | |
| 176 | .. versionadded:: 3.4 |
| 177 | |
Zackery Spytz | 3c8724f | 2019-05-28 09:16:33 -0600 | [diff] [blame] | 178 | .. c:macro:: Py_DEPRECATED(version) |
| 179 | |
| 180 | Use this for deprecated declarations. The macro must be placed before the |
| 181 | symbol name. |
| 182 | |
| 183 | Example:: |
| 184 | |
| 185 | Py_DEPRECATED(3.8) PyAPI_FUNC(int) Py_OldFunction(void); |
| 186 | |
| 187 | .. versionchanged:: 3.8 |
| 188 | MSVC support was added. |
| 189 | |
Brad Solomon | b54e46c | 2020-04-26 22:31:44 -0400 | [diff] [blame] | 190 | .. c:macro:: PyDoc_STRVAR(name, str) |
| 191 | |
| 192 | Creates a variable with name ``name`` that can be used in docstrings. |
| 193 | If Python is built without docstrings, the value will be empty. |
| 194 | |
| 195 | Use :c:macro:`PyDoc_STRVAR` for docstrings to support building |
| 196 | Python without docstrings, as specified in :pep:`7`. |
| 197 | |
| 198 | Example:: |
| 199 | |
| 200 | PyDoc_STRVAR(pop_doc, "Remove and return the rightmost element."); |
| 201 | |
| 202 | static PyMethodDef deque_methods[] = { |
| 203 | // ... |
| 204 | {"pop", (PyCFunction)deque_pop, METH_NOARGS, pop_doc}, |
| 205 | // ... |
| 206 | } |
| 207 | |
| 208 | .. c:macro:: PyDoc_STR(str) |
| 209 | |
| 210 | Creates a docstring for the given input string or an empty string |
| 211 | if docstrings are disabled. |
| 212 | |
| 213 | Use :c:macro:`PyDoc_STR` in specifying docstrings to support |
| 214 | building Python without docstrings, as specified in :pep:`7`. |
| 215 | |
| 216 | Example:: |
| 217 | |
| 218 | static PyMethodDef pysqlite_row_methods[] = { |
| 219 | {"keys", (PyCFunction)pysqlite_row_keys, METH_NOARGS, |
| 220 | PyDoc_STR("Returns the keys of the row.")}, |
| 221 | {NULL, NULL} |
| 222 | }; |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 223 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 224 | .. _api-objects: |
| 225 | |
| 226 | Objects, Types and Reference Counts |
| 227 | =================================== |
| 228 | |
| 229 | .. index:: object: type |
| 230 | |
| 231 | Most Python/C API functions have one or more arguments as well as a return value |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 232 | of type :c:type:`PyObject\*`. This type is a pointer to an opaque data type |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 233 | representing an arbitrary Python object. Since all Python object types are |
| 234 | treated the same way by the Python language in most situations (e.g., |
| 235 | assignments, scope rules, and argument passing), it is only fitting that they |
| 236 | should be represented by a single C type. Almost all Python objects live on the |
| 237 | heap: you never declare an automatic or static variable of type |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 238 | :c:type:`PyObject`, only pointer variables of type :c:type:`PyObject\*` can be |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 239 | declared. The sole exception are the type objects; since these must never be |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 240 | deallocated, they are typically static :c:type:`PyTypeObject` objects. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 241 | |
| 242 | All Python objects (even Python integers) have a :dfn:`type` and a |
| 243 | :dfn:`reference count`. An object's type determines what kind of object it is |
| 244 | (e.g., an integer, a list, or a user-defined function; there are many more as |
| 245 | explained in :ref:`types`). For each of the well-known types there is a macro |
| 246 | to check whether an object is of that type; for instance, ``PyList_Check(a)`` is |
| 247 | true if (and only if) the object pointed to by *a* is a Python list. |
| 248 | |
| 249 | |
| 250 | .. _api-refcounts: |
| 251 | |
| 252 | Reference Counts |
| 253 | ---------------- |
| 254 | |
| 255 | The reference count is important because today's computers have a finite (and |
| 256 | often severely limited) memory size; it counts how many different places there |
| 257 | are that have a reference to an object. Such a place could be another object, |
| 258 | or a global (or static) C variable, or a local variable in some C function. |
| 259 | When an object's reference count becomes zero, the object is deallocated. If |
| 260 | it contains references to other objects, their reference count is decremented. |
| 261 | Those other objects may be deallocated in turn, if this decrement makes their |
| 262 | reference count become zero, and so on. (There's an obvious problem with |
| 263 | objects that reference each other here; for now, the solution is "don't do |
| 264 | that.") |
| 265 | |
| 266 | .. index:: |
| 267 | single: Py_INCREF() |
| 268 | single: Py_DECREF() |
| 269 | |
| 270 | Reference counts are always manipulated explicitly. The normal way is to use |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 271 | the macro :c:func:`Py_INCREF` to increment an object's reference count by one, |
| 272 | and :c:func:`Py_DECREF` to decrement it by one. The :c:func:`Py_DECREF` macro |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 273 | is considerably more complex than the incref one, since it must check whether |
| 274 | the reference count becomes zero and then cause the object's deallocator to be |
| 275 | called. The deallocator is a function pointer contained in the object's type |
| 276 | structure. The type-specific deallocator takes care of decrementing the |
| 277 | reference counts for other objects contained in the object if this is a compound |
| 278 | object type, such as a list, as well as performing any additional finalization |
| 279 | that's needed. There's no chance that the reference count can overflow; at |
| 280 | least as many bits are used to hold the reference count as there are distinct |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 281 | memory locations in virtual memory (assuming ``sizeof(Py_ssize_t) >= sizeof(void*)``). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 282 | Thus, the reference count increment is a simple operation. |
| 283 | |
| 284 | It is not necessary to increment an object's reference count for every local |
| 285 | variable that contains a pointer to an object. In theory, the object's |
| 286 | reference count goes up by one when the variable is made to point to it and it |
| 287 | goes down by one when the variable goes out of scope. However, these two |
| 288 | cancel each other out, so at the end the reference count hasn't changed. The |
| 289 | only real reason to use the reference count is to prevent the object from being |
| 290 | deallocated as long as our variable is pointing to it. If we know that there |
| 291 | is at least one other reference to the object that lives at least as long as |
| 292 | our variable, there is no need to increment the reference count temporarily. |
| 293 | An important situation where this arises is in objects that are passed as |
| 294 | arguments to C functions in an extension module that are called from Python; |
| 295 | the call mechanism guarantees to hold a reference to every argument for the |
| 296 | duration of the call. |
| 297 | |
| 298 | However, a common pitfall is to extract an object from a list and hold on to it |
| 299 | for a while without incrementing its reference count. Some other operation might |
| 300 | conceivably remove the object from the list, decrementing its reference count |
Beomsoo Kim | 05c1b38 | 2018-12-17 21:57:03 +0900 | [diff] [blame] | 301 | and possibly deallocating it. The real danger is that innocent-looking |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 302 | operations may invoke arbitrary Python code which could do this; there is a code |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 303 | path which allows control to flow back to the user from a :c:func:`Py_DECREF`, so |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 304 | almost any operation is potentially dangerous. |
| 305 | |
| 306 | A safe approach is to always use the generic operations (functions whose name |
| 307 | begins with ``PyObject_``, ``PyNumber_``, ``PySequence_`` or ``PyMapping_``). |
| 308 | These operations always increment the reference count of the object they return. |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 309 | This leaves the caller with the responsibility to call :c:func:`Py_DECREF` when |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 310 | they are done with the result; this soon becomes second nature. |
| 311 | |
| 312 | |
| 313 | .. _api-refcountdetails: |
| 314 | |
| 315 | Reference Count Details |
| 316 | ^^^^^^^^^^^^^^^^^^^^^^^ |
| 317 | |
| 318 | The reference count behavior of functions in the Python/C API is best explained |
| 319 | in terms of *ownership of references*. Ownership pertains to references, never |
| 320 | to objects (objects are not owned: they are always shared). "Owning a |
| 321 | reference" means being responsible for calling Py_DECREF on it when the |
| 322 | reference is no longer needed. Ownership can also be transferred, meaning that |
| 323 | the code that receives ownership of the reference then becomes responsible for |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 324 | eventually decref'ing it by calling :c:func:`Py_DECREF` or :c:func:`Py_XDECREF` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 325 | when it's no longer needed---or passing on this responsibility (usually to its |
| 326 | caller). When a function passes ownership of a reference on to its caller, the |
| 327 | caller is said to receive a *new* reference. When no ownership is transferred, |
| 328 | the caller is said to *borrow* the reference. Nothing needs to be done for a |
| 329 | borrowed reference. |
| 330 | |
Benjamin Peterson | ad3d5c2 | 2009-02-26 03:38:59 +0000 | [diff] [blame] | 331 | Conversely, when a calling function passes in a reference to an object, there |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 332 | are two possibilities: the function *steals* a reference to the object, or it |
| 333 | does not. *Stealing a reference* means that when you pass a reference to a |
| 334 | function, that function assumes that it now owns that reference, and you are not |
| 335 | responsible for it any longer. |
| 336 | |
| 337 | .. index:: |
| 338 | single: PyList_SetItem() |
| 339 | single: PyTuple_SetItem() |
| 340 | |
| 341 | Few functions steal references; the two notable exceptions are |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 342 | :c:func:`PyList_SetItem` and :c:func:`PyTuple_SetItem`, which steal a reference |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 343 | to the item (but not to the tuple or list into which the item is put!). These |
| 344 | functions were designed to steal a reference because of a common idiom for |
| 345 | populating a tuple or list with newly created objects; for example, the code to |
| 346 | create the tuple ``(1, 2, "three")`` could look like this (forgetting about |
| 347 | error handling for the moment; a better way to code this is shown below):: |
| 348 | |
| 349 | PyObject *t; |
| 350 | |
| 351 | t = PyTuple_New(3); |
Georg Brandl | d019fe2 | 2007-12-08 18:58:51 +0000 | [diff] [blame] | 352 | PyTuple_SetItem(t, 0, PyLong_FromLong(1L)); |
| 353 | PyTuple_SetItem(t, 1, PyLong_FromLong(2L)); |
Gregory P. Smith | 4b52ae8 | 2013-03-22 13:43:30 -0700 | [diff] [blame] | 354 | PyTuple_SetItem(t, 2, PyUnicode_FromString("three")); |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 355 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 356 | Here, :c:func:`PyLong_FromLong` returns a new reference which is immediately |
| 357 | stolen by :c:func:`PyTuple_SetItem`. When you want to keep using an object |
| 358 | although the reference to it will be stolen, use :c:func:`Py_INCREF` to grab |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 359 | another reference before calling the reference-stealing function. |
| 360 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 361 | Incidentally, :c:func:`PyTuple_SetItem` is the *only* way to set tuple items; |
| 362 | :c:func:`PySequence_SetItem` and :c:func:`PyObject_SetItem` refuse to do this |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 363 | since tuples are an immutable data type. You should only use |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 364 | :c:func:`PyTuple_SetItem` for tuples that you are creating yourself. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 365 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 366 | Equivalent code for populating a list can be written using :c:func:`PyList_New` |
| 367 | and :c:func:`PyList_SetItem`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 368 | |
| 369 | However, in practice, you will rarely use these ways of creating and populating |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 370 | a tuple or list. There's a generic function, :c:func:`Py_BuildValue`, that can |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 371 | create most common objects from C values, directed by a :dfn:`format string`. |
| 372 | For example, the above two blocks of code could be replaced by the following |
| 373 | (which also takes care of the error checking):: |
| 374 | |
| 375 | PyObject *tuple, *list; |
| 376 | |
| 377 | tuple = Py_BuildValue("(iis)", 1, 2, "three"); |
| 378 | list = Py_BuildValue("[iis]", 1, 2, "three"); |
| 379 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 380 | It is much more common to use :c:func:`PyObject_SetItem` and friends with items |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 381 | whose references you are only borrowing, like arguments that were passed in to |
| 382 | the function you are writing. In that case, their behaviour regarding reference |
| 383 | counts is much saner, since you don't have to increment a reference count so you |
| 384 | can give a reference away ("have it be stolen"). For example, this function |
| 385 | sets all items of a list (actually, any mutable sequence) to a given item:: |
| 386 | |
| 387 | int |
| 388 | set_all(PyObject *target, PyObject *item) |
| 389 | { |
Antoine Pitrou | 04707c0 | 2012-01-27 14:07:29 +0100 | [diff] [blame] | 390 | Py_ssize_t i, n; |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 391 | |
| 392 | n = PyObject_Length(target); |
| 393 | if (n < 0) |
| 394 | return -1; |
| 395 | for (i = 0; i < n; i++) { |
Antoine Pitrou | 04707c0 | 2012-01-27 14:07:29 +0100 | [diff] [blame] | 396 | PyObject *index = PyLong_FromSsize_t(i); |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 397 | if (!index) |
| 398 | return -1; |
Antoine Pitrou | 04707c0 | 2012-01-27 14:07:29 +0100 | [diff] [blame] | 399 | if (PyObject_SetItem(target, index, item) < 0) { |
| 400 | Py_DECREF(index); |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 401 | return -1; |
Antoine Pitrou | 04707c0 | 2012-01-27 14:07:29 +0100 | [diff] [blame] | 402 | } |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 403 | Py_DECREF(index); |
| 404 | } |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | .. index:: single: set_all() |
| 409 | |
| 410 | The situation is slightly different for function return values. While passing |
| 411 | a reference to most functions does not change your ownership responsibilities |
| 412 | for that reference, many functions that return a reference to an object give |
| 413 | you ownership of the reference. The reason is simple: in many cases, the |
| 414 | returned object is created on the fly, and the reference you get is the only |
| 415 | reference to the object. Therefore, the generic functions that return object |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 416 | references, like :c:func:`PyObject_GetItem` and :c:func:`PySequence_GetItem`, |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 417 | always return a new reference (the caller becomes the owner of the reference). |
| 418 | |
| 419 | It is important to realize that whether you own a reference returned by a |
| 420 | function depends on which function you call only --- *the plumage* (the type of |
| 421 | the object passed as an argument to the function) *doesn't enter into it!* |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 422 | Thus, if you extract an item from a list using :c:func:`PyList_GetItem`, you |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 423 | don't own the reference --- but if you obtain the same item from the same list |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 424 | using :c:func:`PySequence_GetItem` (which happens to take exactly the same |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 425 | arguments), you do own a reference to the returned object. |
| 426 | |
| 427 | .. index:: |
| 428 | single: PyList_GetItem() |
| 429 | single: PySequence_GetItem() |
| 430 | |
| 431 | Here is an example of how you could write a function that computes the sum of |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 432 | the items in a list of integers; once using :c:func:`PyList_GetItem`, and once |
| 433 | using :c:func:`PySequence_GetItem`. :: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 434 | |
| 435 | long |
| 436 | sum_list(PyObject *list) |
| 437 | { |
Antoine Pitrou | 04707c0 | 2012-01-27 14:07:29 +0100 | [diff] [blame] | 438 | Py_ssize_t i, n; |
| 439 | long total = 0, value; |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 440 | PyObject *item; |
| 441 | |
| 442 | n = PyList_Size(list); |
| 443 | if (n < 0) |
| 444 | return -1; /* Not a list */ |
| 445 | for (i = 0; i < n; i++) { |
| 446 | item = PyList_GetItem(list, i); /* Can't fail */ |
Georg Brandl | d019fe2 | 2007-12-08 18:58:51 +0000 | [diff] [blame] | 447 | if (!PyLong_Check(item)) continue; /* Skip non-integers */ |
Antoine Pitrou | 04707c0 | 2012-01-27 14:07:29 +0100 | [diff] [blame] | 448 | value = PyLong_AsLong(item); |
| 449 | if (value == -1 && PyErr_Occurred()) |
| 450 | /* Integer too big to fit in a C long, bail out */ |
| 451 | return -1; |
| 452 | total += value; |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 453 | } |
| 454 | return total; |
| 455 | } |
| 456 | |
| 457 | .. index:: single: sum_list() |
| 458 | |
| 459 | :: |
| 460 | |
| 461 | long |
| 462 | sum_sequence(PyObject *sequence) |
| 463 | { |
Antoine Pitrou | 04707c0 | 2012-01-27 14:07:29 +0100 | [diff] [blame] | 464 | Py_ssize_t i, n; |
| 465 | long total = 0, value; |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 466 | PyObject *item; |
| 467 | n = PySequence_Length(sequence); |
| 468 | if (n < 0) |
| 469 | return -1; /* Has no length */ |
| 470 | for (i = 0; i < n; i++) { |
| 471 | item = PySequence_GetItem(sequence, i); |
| 472 | if (item == NULL) |
| 473 | return -1; /* Not a sequence, or other failure */ |
Antoine Pitrou | 04707c0 | 2012-01-27 14:07:29 +0100 | [diff] [blame] | 474 | if (PyLong_Check(item)) { |
| 475 | value = PyLong_AsLong(item); |
| 476 | Py_DECREF(item); |
| 477 | if (value == -1 && PyErr_Occurred()) |
| 478 | /* Integer too big to fit in a C long, bail out */ |
| 479 | return -1; |
| 480 | total += value; |
| 481 | } |
| 482 | else { |
| 483 | Py_DECREF(item); /* Discard reference ownership */ |
| 484 | } |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 485 | } |
| 486 | return total; |
| 487 | } |
| 488 | |
| 489 | .. index:: single: sum_sequence() |
| 490 | |
| 491 | |
| 492 | .. _api-types: |
| 493 | |
| 494 | Types |
| 495 | ----- |
| 496 | |
| 497 | There are few other data types that play a significant role in the Python/C |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 498 | API; most are simple C types such as :c:type:`int`, :c:type:`long`, |
| 499 | :c:type:`double` and :c:type:`char\*`. A few structure types are used to |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 500 | describe static tables used to list the functions exported by a module or the |
| 501 | data attributes of a new object type, and another is used to describe the value |
| 502 | of a complex number. These will be discussed together with the functions that |
| 503 | use them. |
| 504 | |
| 505 | |
| 506 | .. _api-exceptions: |
| 507 | |
| 508 | Exceptions |
| 509 | ========== |
| 510 | |
| 511 | The Python programmer only needs to deal with exceptions if specific error |
| 512 | handling is required; unhandled exceptions are automatically propagated to the |
| 513 | caller, then to the caller's caller, and so on, until they reach the top-level |
| 514 | interpreter, where they are reported to the user accompanied by a stack |
| 515 | traceback. |
| 516 | |
| 517 | .. index:: single: PyErr_Occurred() |
| 518 | |
Georg Brandl | dd909db | 2010-10-17 06:32:59 +0000 | [diff] [blame] | 519 | For C programmers, however, error checking always has to be explicit. All |
| 520 | functions in the Python/C API can raise exceptions, unless an explicit claim is |
| 521 | made otherwise in a function's documentation. In general, when a function |
| 522 | encounters an error, it sets an exception, discards any object references that |
| 523 | it owns, and returns an error indicator. If not documented otherwise, this |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 524 | indicator is either ``NULL`` or ``-1``, depending on the function's return type. |
Georg Brandl | dd909db | 2010-10-17 06:32:59 +0000 | [diff] [blame] | 525 | A few functions return a Boolean true/false result, with false indicating an |
| 526 | error. Very few functions return no explicit error indicator or have an |
| 527 | ambiguous return value, and require explicit testing for errors with |
| 528 | :c:func:`PyErr_Occurred`. These exceptions are always explicitly documented. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 529 | |
| 530 | .. index:: |
| 531 | single: PyErr_SetString() |
| 532 | single: PyErr_Clear() |
| 533 | |
| 534 | Exception state is maintained in per-thread storage (this is equivalent to |
| 535 | using global storage in an unthreaded application). A thread can be in one of |
| 536 | two states: an exception has occurred, or not. The function |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 537 | :c:func:`PyErr_Occurred` can be used to check for this: it returns a borrowed |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 538 | reference to the exception type object when an exception has occurred, and |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 539 | ``NULL`` otherwise. There are a number of functions to set the exception state: |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 540 | :c:func:`PyErr_SetString` is the most common (though not the most general) |
| 541 | function to set the exception state, and :c:func:`PyErr_Clear` clears the |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 542 | exception state. |
| 543 | |
| 544 | The full exception state consists of three objects (all of which can be |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 545 | ``NULL``): the exception type, the corresponding exception value, and the |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 546 | traceback. These have the same meanings as the Python result of |
| 547 | ``sys.exc_info()``; however, they are not the same: the Python objects represent |
| 548 | the last exception being handled by a Python :keyword:`try` ... |
| 549 | :keyword:`except` statement, while the C level exception state only exists while |
| 550 | an exception is being passed on between C functions until it reaches the Python |
| 551 | bytecode interpreter's main loop, which takes care of transferring it to |
| 552 | ``sys.exc_info()`` and friends. |
| 553 | |
| 554 | .. index:: single: exc_info() (in module sys) |
| 555 | |
| 556 | Note that starting with Python 1.5, the preferred, thread-safe way to access the |
| 557 | exception state from Python code is to call the function :func:`sys.exc_info`, |
| 558 | which returns the per-thread exception state for Python code. Also, the |
| 559 | semantics of both ways to access the exception state have changed so that a |
| 560 | function which catches an exception will save and restore its thread's exception |
| 561 | state so as to preserve the exception state of its caller. This prevents common |
| 562 | bugs in exception handling code caused by an innocent-looking function |
| 563 | overwriting the exception being handled; it also reduces the often unwanted |
| 564 | lifetime extension for objects that are referenced by the stack frames in the |
| 565 | traceback. |
| 566 | |
| 567 | As a general principle, a function that calls another function to perform some |
| 568 | task should check whether the called function raised an exception, and if so, |
| 569 | pass the exception state on to its caller. It should discard any object |
| 570 | references that it owns, and return an error indicator, but it should *not* set |
| 571 | another exception --- that would overwrite the exception that was just raised, |
| 572 | and lose important information about the exact cause of the error. |
| 573 | |
| 574 | .. index:: single: sum_sequence() |
| 575 | |
| 576 | A simple example of detecting exceptions and passing them on is shown in the |
Terry Jan Reedy | 65e69b3 | 2013-03-11 17:23:46 -0400 | [diff] [blame] | 577 | :c:func:`sum_sequence` example above. It so happens that this example doesn't |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 578 | need to clean up any owned references when it detects an error. The following |
| 579 | example function shows some error cleanup. First, to remind you why you like |
| 580 | Python, we show the equivalent Python code:: |
| 581 | |
| 582 | def incr_item(dict, key): |
| 583 | try: |
| 584 | item = dict[key] |
| 585 | except KeyError: |
| 586 | item = 0 |
| 587 | dict[key] = item + 1 |
| 588 | |
| 589 | .. index:: single: incr_item() |
| 590 | |
| 591 | Here is the corresponding C code, in all its glory:: |
| 592 | |
| 593 | int |
| 594 | incr_item(PyObject *dict, PyObject *key) |
| 595 | { |
| 596 | /* Objects all initialized to NULL for Py_XDECREF */ |
| 597 | PyObject *item = NULL, *const_one = NULL, *incremented_item = NULL; |
| 598 | int rv = -1; /* Return value initialized to -1 (failure) */ |
| 599 | |
| 600 | item = PyObject_GetItem(dict, key); |
| 601 | if (item == NULL) { |
| 602 | /* Handle KeyError only: */ |
| 603 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
| 604 | goto error; |
| 605 | |
| 606 | /* Clear the error and use zero: */ |
| 607 | PyErr_Clear(); |
Georg Brandl | d019fe2 | 2007-12-08 18:58:51 +0000 | [diff] [blame] | 608 | item = PyLong_FromLong(0L); |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 609 | if (item == NULL) |
| 610 | goto error; |
| 611 | } |
Georg Brandl | d019fe2 | 2007-12-08 18:58:51 +0000 | [diff] [blame] | 612 | const_one = PyLong_FromLong(1L); |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 613 | if (const_one == NULL) |
| 614 | goto error; |
| 615 | |
| 616 | incremented_item = PyNumber_Add(item, const_one); |
| 617 | if (incremented_item == NULL) |
| 618 | goto error; |
| 619 | |
| 620 | if (PyObject_SetItem(dict, key, incremented_item) < 0) |
| 621 | goto error; |
| 622 | rv = 0; /* Success */ |
| 623 | /* Continue with cleanup code */ |
| 624 | |
| 625 | error: |
| 626 | /* Cleanup code, shared by success and failure path */ |
| 627 | |
| 628 | /* Use Py_XDECREF() to ignore NULL references */ |
| 629 | Py_XDECREF(item); |
| 630 | Py_XDECREF(const_one); |
| 631 | Py_XDECREF(incremented_item); |
| 632 | |
| 633 | return rv; /* -1 for error, 0 for success */ |
| 634 | } |
| 635 | |
| 636 | .. index:: single: incr_item() |
| 637 | |
| 638 | .. index:: |
| 639 | single: PyErr_ExceptionMatches() |
| 640 | single: PyErr_Clear() |
| 641 | single: Py_XDECREF() |
| 642 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 643 | This example represents an endorsed use of the ``goto`` statement in C! |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 644 | It illustrates the use of :c:func:`PyErr_ExceptionMatches` and |
| 645 | :c:func:`PyErr_Clear` to handle specific exceptions, and the use of |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 646 | :c:func:`Py_XDECREF` to dispose of owned references that may be ``NULL`` (note the |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 647 | ``'X'`` in the name; :c:func:`Py_DECREF` would crash when confronted with a |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 648 | ``NULL`` reference). It is important that the variables used to hold owned |
| 649 | references are initialized to ``NULL`` for this to work; likewise, the proposed |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 650 | return value is initialized to ``-1`` (failure) and only set to success after |
| 651 | the final call made is successful. |
| 652 | |
| 653 | |
| 654 | .. _api-embedding: |
| 655 | |
| 656 | Embedding Python |
| 657 | ================ |
| 658 | |
| 659 | The one important task that only embedders (as opposed to extension writers) of |
| 660 | the Python interpreter have to worry about is the initialization, and possibly |
| 661 | the finalization, of the Python interpreter. Most functionality of the |
| 662 | interpreter can only be used after the interpreter has been initialized. |
| 663 | |
| 664 | .. index:: |
| 665 | single: Py_Initialize() |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 666 | module: builtins |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 667 | module: __main__ |
| 668 | module: sys |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 669 | triple: module; search; path |
| 670 | single: path (in module sys) |
| 671 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 672 | The basic initialization function is :c:func:`Py_Initialize`. This initializes |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 673 | the table of loaded modules, and creates the fundamental modules |
Éric Araujo | 8b8f2ec | 2011-03-26 07:22:01 +0100 | [diff] [blame] | 674 | :mod:`builtins`, :mod:`__main__`, and :mod:`sys`. It also |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 675 | initializes the module search path (``sys.path``). |
| 676 | |
Benjamin Peterson | 2ebf8ce | 2010-06-27 21:48:35 +0000 | [diff] [blame] | 677 | .. index:: single: PySys_SetArgvEx() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 678 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 679 | :c:func:`Py_Initialize` does not set the "script argument list" (``sys.argv``). |
Benjamin Peterson | 2ebf8ce | 2010-06-27 21:48:35 +0000 | [diff] [blame] | 680 | If this variable is needed by Python code that will be executed later, it must |
| 681 | be set explicitly with a call to ``PySys_SetArgvEx(argc, argv, updatepath)`` |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 682 | after the call to :c:func:`Py_Initialize`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 683 | |
| 684 | On most systems (in particular, on Unix and Windows, although the details are |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 685 | slightly different), :c:func:`Py_Initialize` calculates the module search path |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 686 | based upon its best guess for the location of the standard Python interpreter |
| 687 | executable, assuming that the Python library is found in a fixed location |
| 688 | relative to the Python interpreter executable. In particular, it looks for a |
| 689 | directory named :file:`lib/python{X.Y}` relative to the parent directory |
| 690 | where the executable named :file:`python` is found on the shell command search |
| 691 | path (the environment variable :envvar:`PATH`). |
| 692 | |
| 693 | For instance, if the Python executable is found in |
| 694 | :file:`/usr/local/bin/python`, it will assume that the libraries are in |
| 695 | :file:`/usr/local/lib/python{X.Y}`. (In fact, this particular path is also |
| 696 | the "fallback" location, used when no executable file named :file:`python` is |
| 697 | found along :envvar:`PATH`.) The user can override this behavior by setting the |
| 698 | environment variable :envvar:`PYTHONHOME`, or insert additional directories in |
| 699 | front of the standard path by setting :envvar:`PYTHONPATH`. |
| 700 | |
| 701 | .. index:: |
| 702 | single: Py_SetProgramName() |
| 703 | single: Py_GetPath() |
| 704 | single: Py_GetPrefix() |
| 705 | single: Py_GetExecPrefix() |
| 706 | single: Py_GetProgramFullPath() |
| 707 | |
| 708 | The embedding application can steer the search by calling |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 709 | ``Py_SetProgramName(file)`` *before* calling :c:func:`Py_Initialize`. Note that |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 710 | :envvar:`PYTHONHOME` still overrides this and :envvar:`PYTHONPATH` is still |
| 711 | inserted in front of the standard path. An application that requires total |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 712 | control has to provide its own implementation of :c:func:`Py_GetPath`, |
| 713 | :c:func:`Py_GetPrefix`, :c:func:`Py_GetExecPrefix`, and |
| 714 | :c:func:`Py_GetProgramFullPath` (all defined in :file:`Modules/getpath.c`). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 715 | |
| 716 | .. index:: single: Py_IsInitialized() |
| 717 | |
| 718 | Sometimes, it is desirable to "uninitialize" Python. For instance, the |
| 719 | application may want to start over (make another call to |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 720 | :c:func:`Py_Initialize`) or the application is simply done with its use of |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 721 | Python and wants to free memory allocated by Python. This can be accomplished |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 722 | by calling :c:func:`Py_FinalizeEx`. The function :c:func:`Py_IsInitialized` returns |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 723 | true if Python is currently in the initialized state. More information about |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 724 | these functions is given in a later chapter. Notice that :c:func:`Py_FinalizeEx` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 725 | does *not* free all memory allocated by the Python interpreter, e.g. memory |
| 726 | allocated by extension modules currently cannot be released. |
| 727 | |
| 728 | |
| 729 | .. _api-debugging: |
| 730 | |
| 731 | Debugging Builds |
| 732 | ================ |
| 733 | |
| 734 | Python can be built with several macros to enable extra checks of the |
| 735 | interpreter and extension modules. These checks tend to add a large amount of |
| 736 | overhead to the runtime so they are not enabled by default. |
| 737 | |
| 738 | A full list of the various types of debugging builds is in the file |
| 739 | :file:`Misc/SpecialBuilds.txt` in the Python source distribution. Builds are |
| 740 | available that support tracing of reference counts, debugging the memory |
| 741 | allocator, or low-level profiling of the main interpreter loop. Only the most |
| 742 | frequently-used builds will be described in the remainder of this section. |
| 743 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 744 | Compiling the interpreter with the :c:macro:`Py_DEBUG` macro defined produces |
| 745 | what is generally meant by "a debug build" of Python. :c:macro:`Py_DEBUG` is |
Éric Araujo | d2f8cec | 2011-06-08 05:29:39 +0200 | [diff] [blame] | 746 | enabled in the Unix build by adding ``--with-pydebug`` to the |
| 747 | :file:`./configure` command. It is also implied by the presence of the |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 748 | not-Python-specific :c:macro:`_DEBUG` macro. When :c:macro:`Py_DEBUG` is enabled |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 749 | in the Unix build, compiler optimization is disabled. |
| 750 | |
| 751 | In addition to the reference count debugging described below, the following |
| 752 | extra checks are performed: |
| 753 | |
| 754 | * Extra checks are added to the object allocator. |
| 755 | |
| 756 | * Extra checks are added to the parser and compiler. |
| 757 | |
| 758 | * Downcasts from wide types to narrow types are checked for loss of information. |
| 759 | |
| 760 | * A number of assertions are added to the dictionary and set implementations. |
| 761 | In addition, the set object acquires a :meth:`test_c_api` method. |
| 762 | |
| 763 | * Sanity checks of the input arguments are added to frame creation. |
| 764 | |
Mark Dickinson | bf5c6a9 | 2009-01-17 10:21:23 +0000 | [diff] [blame] | 765 | * The storage for ints is initialized with a known invalid pattern to catch |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 766 | reference to uninitialized digits. |
| 767 | |
| 768 | * Low-level tracing and extra exception checking are added to the runtime |
| 769 | virtual machine. |
| 770 | |
| 771 | * Extra checks are added to the memory arena implementation. |
| 772 | |
| 773 | * Extra debugging is added to the thread module. |
| 774 | |
| 775 | There may be additional checks not mentioned here. |
| 776 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 777 | Defining :c:macro:`Py_TRACE_REFS` enables reference tracing. When defined, a |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 778 | circular doubly linked list of active objects is maintained by adding two extra |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 779 | fields to every :c:type:`PyObject`. Total allocations are tracked as well. Upon |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 780 | exit, all existing references are printed. (In interactive mode this happens |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 781 | after every statement run by the interpreter.) Implied by :c:macro:`Py_DEBUG`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 782 | |
| 783 | Please refer to :file:`Misc/SpecialBuilds.txt` in the Python source distribution |
| 784 | for more detailed information. |
| 785 | |