aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Doc/c-api
diff options
context:
space:
mode:
Diffstat (limited to 'Doc/c-api')
-rw-r--r--Doc/c-api/allocation.rst2
-rw-r--r--Doc/c-api/arg.rst1
-rw-r--r--Doc/c-api/exceptions.rst12
-rw-r--r--Doc/c-api/extension-modules.rst247
-rw-r--r--Doc/c-api/index.rst1
-rw-r--r--Doc/c-api/init.rst30
-rw-r--r--Doc/c-api/init_config.rst2
-rw-r--r--Doc/c-api/intro.rst64
-rw-r--r--Doc/c-api/lifecycle.rst6
-rw-r--r--Doc/c-api/module.rst253
-rw-r--r--Doc/c-api/stable.rst1
-rw-r--r--Doc/c-api/sys.rst51
-rw-r--r--Doc/c-api/typeobj.rst20
-rw-r--r--Doc/c-api/unicode.rst41
14 files changed, 541 insertions, 190 deletions
diff --git a/Doc/c-api/allocation.rst b/Doc/c-api/allocation.rst
index f8d01a3f29b..59d913a0462 100644
--- a/Doc/c-api/allocation.rst
+++ b/Doc/c-api/allocation.rst
@@ -153,6 +153,6 @@ Allocating Objects on the Heap
.. seealso::
- :c:func:`PyModule_Create`
+ :ref:`moduleobjects`
To allocate and create extension modules.
diff --git a/Doc/c-api/arg.rst b/Doc/c-api/arg.rst
index 3bbc990b632..49dbc8d71cc 100644
--- a/Doc/c-api/arg.rst
+++ b/Doc/c-api/arg.rst
@@ -685,6 +685,7 @@ Building values
``p`` (:class:`bool`) [int]
Convert a C :c:expr:`int` to a Python :class:`bool` object.
+
.. versionadded:: 3.14
``c`` (:class:`bytes` of length 1) [char]
diff --git a/Doc/c-api/exceptions.rst b/Doc/c-api/exceptions.rst
index c8e1b5c2461..885dbeb7530 100644
--- a/Doc/c-api/exceptions.rst
+++ b/Doc/c-api/exceptions.rst
@@ -982,6 +982,7 @@ the variables:
.. index::
single: PyExc_BaseException (C var)
+ single: PyExc_BaseExceptionGroup (C var)
single: PyExc_Exception (C var)
single: PyExc_ArithmeticError (C var)
single: PyExc_AssertionError (C var)
@@ -1041,6 +1042,8 @@ the variables:
+=========================================+=================================+==========+
| :c:data:`PyExc_BaseException` | :exc:`BaseException` | [1]_ |
+-----------------------------------------+---------------------------------+----------+
+| :c:data:`PyExc_BaseExceptionGroup` | :exc:`BaseExceptionGroup` | [1]_ |
++-----------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_Exception` | :exc:`Exception` | [1]_ |
+-----------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_ArithmeticError` | :exc:`ArithmeticError` | [1]_ |
@@ -1164,6 +1167,9 @@ the variables:
.. versionadded:: 3.6
:c:data:`PyExc_ModuleNotFoundError`.
+.. versionadded:: 3.11
+ :c:data:`PyExc_BaseExceptionGroup`.
+
These are compatibility aliases to :c:data:`PyExc_OSError`:
.. index::
@@ -1207,6 +1213,7 @@ the variables:
single: PyExc_Warning (C var)
single: PyExc_BytesWarning (C var)
single: PyExc_DeprecationWarning (C var)
+ single: PyExc_EncodingWarning (C var)
single: PyExc_FutureWarning (C var)
single: PyExc_ImportWarning (C var)
single: PyExc_PendingDeprecationWarning (C var)
@@ -1225,6 +1232,8 @@ the variables:
+------------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_DeprecationWarning` | :exc:`DeprecationWarning` | |
+------------------------------------------+---------------------------------+----------+
+| :c:data:`PyExc_EncodingWarning` | :exc:`EncodingWarning` | |
++------------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_FutureWarning` | :exc:`FutureWarning` | |
+------------------------------------------+---------------------------------+----------+
| :c:data:`PyExc_ImportWarning` | :exc:`ImportWarning` | |
@@ -1245,6 +1254,9 @@ the variables:
.. versionadded:: 3.2
:c:data:`PyExc_ResourceWarning`.
+.. versionadded:: 3.10
+ :c:data:`PyExc_EncodingWarning`.
+
Notes:
.. [3]
diff --git a/Doc/c-api/extension-modules.rst b/Doc/c-api/extension-modules.rst
new file mode 100644
index 00000000000..4c8212f2f5e
--- /dev/null
+++ b/Doc/c-api/extension-modules.rst
@@ -0,0 +1,247 @@
+.. highlight:: c
+
+.. _extension-modules:
+
+Defining extension modules
+--------------------------
+
+A C extension for CPython is a shared library (for example, a ``.so`` file
+on Linux, ``.pyd`` DLL on Windows), which is loadable into the Python process
+(for example, it is compiled with compatible compiler settings), and which
+exports an :ref:`initialization function <extension-export-hook>`.
+
+To be importable by default (that is, by
+:py:class:`importlib.machinery.ExtensionFileLoader`),
+the shared library must be available on :py:attr:`sys.path`,
+and must be named after the module name plus an extension listed in
+:py:attr:`importlib.machinery.EXTENSION_SUFFIXES`.
+
+.. note::
+
+ Building, packaging and distributing extension modules is best done with
+ third-party tools, and is out of scope of this document.
+ One suitable tool is Setuptools, whose documentation can be found at
+ https://setuptools.pypa.io/en/latest/setuptools.html.
+
+Normally, the initialization function returns a module definition initialized
+using :c:func:`PyModuleDef_Init`.
+This allows splitting the creation process into several phases:
+
+- Before any substantial code is executed, Python can determine which
+ capabilities the module supports, and it can adjust the environment or
+ refuse loading an incompatible extension.
+- By default, Python itself creates the module object -- that is, it does
+ the equivalent of :py:meth:`object.__new__` for classes.
+ It also sets initial attributes like :attr:`~module.__package__` and
+ :attr:`~module.__loader__`.
+- Afterwards, the module object is initialized using extension-specific
+ code -- the equivalent of :py:meth:`~object.__init__` on classes.
+
+This is called *multi-phase initialization* to distinguish it from the legacy
+(but still supported) *single-phase initialization* scheme,
+where the initialization function returns a fully constructed module.
+See the :ref:`single-phase-initialization section below <single-phase-initialization>`
+for details.
+
+.. versionchanged:: 3.5
+
+ Added support for multi-phase initialization (:pep:`489`).
+
+
+Multiple module instances
+.........................
+
+By default, extension modules are not singletons.
+For example, if the :py:attr:`sys.modules` entry is removed and the module
+is re-imported, a new module object is created, and typically populated with
+fresh method and type objects.
+The old module is subject to normal garbage collection.
+This mirrors the behavior of pure-Python modules.
+
+Additional module instances may be created in
+:ref:`sub-interpreters <sub-interpreter-support>`
+or after Python runtime reinitialization
+(:c:func:`Py_Finalize` and :c:func:`Py_Initialize`).
+In these cases, sharing Python objects between module instances would likely
+cause crashes or undefined behavior.
+
+To avoid such issues, each instance of an extension module should
+be *isolated*: changes to one instance should not implicitly affect the others,
+and all state owned by the module, including references to Python objects,
+should be specific to a particular module instance.
+See :ref:`isolating-extensions-howto` for more details and a practical guide.
+
+A simpler way to avoid these issues is
+:ref:`raising an error on repeated initialization <isolating-extensions-optout>`.
+
+All modules are expected to support
+:ref:`sub-interpreters <sub-interpreter-support>`, or otherwise explicitly
+signal a lack of support.
+This is usually achieved by isolation or blocking repeated initialization,
+as above.
+A module may also be limited to the main interpreter using
+the :c:data:`Py_mod_multiple_interpreters` slot.
+
+
+.. _extension-export-hook:
+
+Initialization function
+.......................
+
+The initialization function defined by an extension module has the
+following signature:
+
+.. c:function:: PyObject* PyInit_modulename(void)
+
+Its name should be :samp:`PyInit_{<name>}`, with ``<name>`` replaced by the
+name of the module.
+
+For modules with ASCII-only names, the function must instead be named
+:samp:`PyInit_{<name>}`, with ``<name>`` replaced by the name of the module.
+When using :ref:`multi-phase-initialization`, non-ASCII module names
+are allowed. In this case, the initialization function name is
+:samp:`PyInitU_{<name>}`, with ``<name>`` encoded using Python's
+*punycode* encoding with hyphens replaced by underscores. In Python:
+
+.. code-block:: python
+
+ def initfunc_name(name):
+ try:
+ suffix = b'_' + name.encode('ascii')
+ except UnicodeEncodeError:
+ suffix = b'U_' + name.encode('punycode').replace(b'-', b'_')
+ return b'PyInit' + suffix
+
+It is recommended to define the initialization function using a helper macro:
+
+.. c:macro:: PyMODINIT_FUNC
+
+ Declare an extension module initialization function.
+ This macro:
+
+ * specifies the :c:expr:`PyObject*` return type,
+ * adds any special linkage declarations required by the platform, and
+ * for C++, declares the function as ``extern "C"``.
+
+For example, a module called ``spam`` would be defined like this::
+
+ static struct PyModuleDef spam_module = {
+ .m_base = PyModuleDef_HEAD_INIT,
+ .m_name = "spam",
+ ...
+ };
+
+ PyMODINIT_FUNC
+ PyInit_spam(void)
+ {
+ return PyModuleDef_Init(&spam_module);
+ }
+
+It is possible to export multiple modules from a single shared library by
+defining multiple initialization functions. However, importing them requires
+using symbolic links or a custom importer, because by default only the
+function corresponding to the filename is found.
+See the `Multiple modules in one library <https://peps.python.org/pep-0489/#multiple-modules-in-one-library>`__
+section in :pep:`489` for details.
+
+The initialization function is typically the only non-\ ``static``
+item defined in the module's C source.
+
+
+.. _multi-phase-initialization:
+
+Multi-phase initialization
+..........................
+
+Normally, the :ref:`initialization function <extension-export-hook>`
+(``PyInit_modulename``) returns a :c:type:`PyModuleDef` instance with
+non-``NULL`` :c:member:`~PyModuleDef.m_slots`.
+Before it is returned, the ``PyModuleDef`` instance must be initialized
+using the following function:
+
+
+.. c:function:: PyObject* PyModuleDef_Init(PyModuleDef *def)
+
+ Ensure a module definition is a properly initialized Python object that
+ correctly reports its type and a reference count.
+
+ Return *def* cast to ``PyObject*``, or ``NULL`` if an error occurred.
+
+ Calling this function is required for :ref:`multi-phase-initialization`.
+ It should not be used in other contexts.
+
+ Note that Python assumes that ``PyModuleDef`` structures are statically
+ allocated.
+ This function may return either a new reference or a borrowed one;
+ this reference must not be released.
+
+ .. versionadded:: 3.5
+
+
+.. _single-phase-initialization:
+
+Legacy single-phase initialization
+..................................
+
+.. attention::
+ Single-phase initialization is a legacy mechanism to initialize extension
+ modules, with known drawbacks and design flaws. Extension module authors
+ are encouraged to use multi-phase initialization instead.
+
+In single-phase initialization, the
+:ref:`initialization function <extension-export-hook>` (``PyInit_modulename``)
+should create, populate and return a module object.
+This is typically done using :c:func:`PyModule_Create` and functions like
+:c:func:`PyModule_AddObjectRef`.
+
+Single-phase initialization differs from the :ref:`default <multi-phase-initialization>`
+in the following ways:
+
+* Single-phase modules are, or rather *contain*, “singletons”.
+
+ When the module is first initialized, Python saves the contents of
+ the module's ``__dict__`` (that is, typically, the module's functions and
+ types).
+
+ For subsequent imports, Python does not call the initialization function
+ again.
+ Instead, it creates a new module object with a new ``__dict__``, and copies
+ the saved contents to it.
+ For example, given a single-phase module ``_testsinglephase``
+ [#testsinglephase]_ that defines a function ``sum`` and an exception class
+ ``error``:
+
+ .. code-block:: python
+
+ >>> import sys
+ >>> import _testsinglephase as one
+ >>> del sys.modules['_testsinglephase']
+ >>> import _testsinglephase as two
+ >>> one is two
+ False
+ >>> one.__dict__ is two.__dict__
+ False
+ >>> one.sum is two.sum
+ True
+ >>> one.error is two.error
+ True
+
+ The exact behavior should be considered a CPython implementation detail.
+
+* To work around the fact that ``PyInit_modulename`` does not take a *spec*
+ argument, some state of the import machinery is saved and applied to the
+ first suitable module created during the ``PyInit_modulename`` call.
+ Specifically, when a sub-module is imported, this mechanism prepends the
+ parent package name to the name of the module.
+
+ A single-phase ``PyInit_modulename`` function should create “its” module
+ object as soon as possible, before any other module objects can be created.
+
+* Non-ASCII module names (``PyInitU_modulename``) are not supported.
+
+* Single-phase modules support module lookup functions like
+ :c:func:`PyState_FindModule`.
+
+.. [#testsinglephase] ``_testsinglephase`` is an internal module used \
+ in CPython's self-test suite; your installation may or may not \
+ include it.
diff --git a/Doc/c-api/index.rst b/Doc/c-api/index.rst
index ba56b03c6ac..e9df2a304d9 100644
--- a/Doc/c-api/index.rst
+++ b/Doc/c-api/index.rst
@@ -17,6 +17,7 @@ document the API functions in detail.
veryhigh.rst
refcounting.rst
exceptions.rst
+ extension-modules.rst
utilities.rst
abstract.rst
concrete.rst
diff --git a/Doc/c-api/init.rst b/Doc/c-api/init.rst
index 9c866438b48..3106bf9808f 100644
--- a/Doc/c-api/init.rst
+++ b/Doc/c-api/init.rst
@@ -492,17 +492,8 @@ Initializing and finalizing the interpreter
strings other than those passed in (however, the contents of the strings
pointed to by the argument list are not modified).
- The return value will be ``0`` if the interpreter exits normally (i.e.,
- without an exception), ``1`` if the interpreter exits due to an exception,
- or ``2`` if the argument list does not represent a valid Python command
- line.
-
- Note that if an otherwise unhandled :exc:`SystemExit` is raised, this
- function will not return ``1``, but exit the process, as long as
- ``Py_InspectFlag`` is not set. If ``Py_InspectFlag`` is set, execution will
- drop into the interactive Python prompt, at which point a second otherwise
- unhandled :exc:`SystemExit` will still exit the process, while any other
- means of exiting will set the return value as described above.
+ The return value is ``2`` if the argument list does not represent a valid
+ Python command line, and otherwise the same as :c:func:`Py_RunMain`.
In terms of the CPython runtime configuration APIs documented in the
:ref:`runtime configuration <init-config>` section (and without accounting
@@ -539,23 +530,18 @@ Initializing and finalizing the interpreter
If :c:member:`PyConfig.inspect` is not set (the default), the return value
will be ``0`` if the interpreter exits normally (that is, without raising
- an exception), or ``1`` if the interpreter exits due to an exception. If an
- otherwise unhandled :exc:`SystemExit` is raised, the function will immediately
- exit the process instead of returning ``1``.
+ an exception), the exit status of an unhandled :exc:`SystemExit`, or ``1``
+ for any other unhandled exception.
If :c:member:`PyConfig.inspect` is set (such as when the :option:`-i` option
is used), rather than returning when the interpreter exits, execution will
instead resume in an interactive Python prompt (REPL) using the ``__main__``
module's global namespace. If the interpreter exited with an exception, it
is immediately raised in the REPL session. The function return value is
- then determined by the way the *REPL session* terminates: returning ``0``
- if the session terminates without raising an unhandled exception, exiting
- immediately for an unhandled :exc:`SystemExit`, and returning ``1`` for
- any other unhandled exception.
-
- This function always finalizes the Python interpreter regardless of whether
- it returns a value or immediately exits the process due to an unhandled
- :exc:`SystemExit` exception.
+ then determined by the way the *REPL session* terminates: ``0``, ``1``, or
+ the status of a :exc:`SystemExit`, as specified above.
+
+ This function always finalizes the Python interpreter before it returns.
See :ref:`Python Configuration <init-python-config>` for an example of a
customized Python that always runs in isolated mode using
diff --git a/Doc/c-api/init_config.rst b/Doc/c-api/init_config.rst
index e1931655618..4fd10224262 100644
--- a/Doc/c-api/init_config.rst
+++ b/Doc/c-api/init_config.rst
@@ -2111,7 +2111,7 @@ initialization::
/* Specify sys.path explicitly */
/* If you want to modify the default set of paths, finish
- initialization first and then use PySys_GetObject("path") */
+ initialization first and then use PySys_GetAttrString("path") */
config.module_search_paths_set = 1;
status = PyWideStringList_Append(&config.module_search_paths,
L"/path/to/stdlib");
diff --git a/Doc/c-api/intro.rst b/Doc/c-api/intro.rst
index 0c20ad17194..acce3dc215d 100644
--- a/Doc/c-api/intro.rst
+++ b/Doc/c-api/intro.rst
@@ -111,33 +111,11 @@ Useful macros
=============
Several useful macros are defined in the Python header files. Many are
-defined closer to where they are useful (e.g. :c:macro:`Py_RETURN_NONE`).
+defined closer to where they are useful (for example, :c:macro:`Py_RETURN_NONE`,
+:c:macro:`PyMODINIT_FUNC`).
Others of a more general utility are defined here. This is not necessarily a
complete listing.
-.. c:macro:: PyMODINIT_FUNC
-
- Declare an extension module ``PyInit`` initialization function. The function
- return type is :c:expr:`PyObject*`. The macro declares any special linkage
- declarations required by the platform, and for C++ declares the function as
- ``extern "C"``.
-
- The initialization function must be named :samp:`PyInit_{name}`, where
- *name* is the name of the module, and should be the only non-\ ``static``
- item defined in the module file. Example::
-
- static struct PyModuleDef spam_module = {
- PyModuleDef_HEAD_INIT,
- .m_name = "spam",
- ...
- };
-
- PyMODINIT_FUNC
- PyInit_spam(void)
- {
- return PyModule_Create(&spam_module);
- }
-
.. c:macro:: Py_ABS(x)
@@ -838,3 +816,41 @@ after every statement run by the interpreter.)
Please refer to :file:`Misc/SpecialBuilds.txt` in the Python source distribution
for more detailed information.
+
+
+.. _c-api-tools:
+
+Recommended third party tools
+=============================
+
+The following third party tools offer both simpler and more sophisticated
+approaches to creating C, C++ and Rust extensions for Python:
+
+* `Cython <https://cython.org/>`_
+* `cffi <https://cffi.readthedocs.io>`_
+* `HPy <https://hpyproject.org/>`_
+* `nanobind <https://github.com/wjakob/nanobind>`_ (C++)
+* `Numba <https://numba.pydata.org/>`_
+* `pybind11 <https://pybind11.readthedocs.io/>`_ (C++)
+* `PyO3 <https://pyo3.rs/>`_ (Rust)
+* `SWIG <https://www.swig.org>`_
+
+Using tools such as these can help avoid writing code that is tightly bound to
+a particular version of CPython, avoid reference counting errors, and focus
+more on your own code than on using the CPython API. In general, new versions
+of Python can be supported by updating the tool, and your code will often use
+newer and more efficient APIs automatically. Some tools also support compiling
+for other implementations of Python from a single set of sources.
+
+These projects are not supported by the same people who maintain Python, and
+issues need to be raised with the projects directly. Remember to check that the
+project is still maintained and supported, as the list above may become
+outdated.
+
+.. seealso::
+
+ `Python Packaging User Guide: Binary Extensions <https://packaging.python.org/guides/packaging-binary-extensions/>`_
+ The Python Packaging User Guide not only covers several available
+ tools that simplify the creation of binary extensions, but also
+ discusses the various reasons why creating an extension module may be
+ desirable in the first place.
diff --git a/Doc/c-api/lifecycle.rst b/Doc/c-api/lifecycle.rst
index 0e2ffc096ca..5a170862a26 100644
--- a/Doc/c-api/lifecycle.rst
+++ b/Doc/c-api/lifecycle.rst
@@ -55,16 +55,14 @@ that must be true for *B* to occur after *A*.
.. image:: lifecycle.dot.svg
:align: center
:class: invert-in-dark-mode
- :alt: Diagram showing events in an object's life. Explained in detail
- below.
+ :alt: Diagram showing events in an object's life. Explained in detail below.
.. only:: latex
.. image:: lifecycle.dot.pdf
:align: center
:class: invert-in-dark-mode
- :alt: Diagram showing events in an object's life. Explained in detail
- below.
+ :alt: Diagram showing events in an object's life. Explained in detail below.
.. container::
:name: life-events-graph-description
diff --git a/Doc/c-api/module.rst b/Doc/c-api/module.rst
index f7f4d37d4c7..c8edcecc5b4 100644
--- a/Doc/c-api/module.rst
+++ b/Doc/c-api/module.rst
@@ -127,25 +127,36 @@ Module Objects
unencodable filenames, use :c:func:`PyModule_GetFilenameObject` instead.
-.. _initializing-modules:
+.. _pymoduledef:
-Initializing C modules
-^^^^^^^^^^^^^^^^^^^^^^
+Module definitions
+------------------
-Modules objects are usually created from extension modules (shared libraries
-which export an initialization function), or compiled-in modules
-(where the initialization function is added using :c:func:`PyImport_AppendInittab`).
-See :ref:`building` or :ref:`extending-with-embedding` for details.
+The functions in the previous section work on any module object, including
+modules imported from Python code.
-The initialization function can either pass a module definition instance
-to :c:func:`PyModule_Create`, and return the resulting module object,
-or request "multi-phase initialization" by returning the definition struct itself.
+Modules defined using the C API typically use a *module definition*,
+:c:type:`PyModuleDef` -- a statically allocated, constant “description" of
+how a module should be created.
+
+The definition is usually used to define an extension's “main” module object
+(see :ref:`extension-modules` for details).
+It is also used to
+:ref:`create extension modules dynamically <moduledef-dynamic>`.
+
+Unlike :c:func:`PyModule_New`, the definition allows management of
+*module state* -- a piece of memory that is allocated and cleared together
+with the module object.
+Unlike the module's Python attributes, Python code cannot replace or delete
+data stored in module state.
.. c:type:: PyModuleDef
The module definition struct, which holds all information needed to create
- a module object. There is usually only one statically initialized variable
- of this type for each module.
+ a module object.
+ This structure must be statically allocated (or be otherwise guaranteed
+ to be valid while any modules created from it exist).
+ Usually, there is only one variable of this type for each extension module.
.. c:member:: PyModuleDef_Base m_base
@@ -170,13 +181,15 @@ or request "multi-phase initialization" by returning the definition struct itsel
and freed when the module object is deallocated, after the
:c:member:`~PyModuleDef.m_free` function has been called, if present.
- Setting ``m_size`` to ``-1`` means that the module does not support
- sub-interpreters, because it has global state.
-
Setting it to a non-negative value means that the module can be
re-initialized and specifies the additional amount of memory it requires
- for its state. Non-negative ``m_size`` is required for multi-phase
- initialization.
+ for its state.
+
+ Setting ``m_size`` to ``-1`` means that the module does not support
+ sub-interpreters, because it has global state.
+ Negative ``m_size`` is only allowed when using
+ :ref:`legacy single-phase initialization <single-phase-initialization>`
+ or when :ref:`creating modules dynamically <moduledef-dynamic>`.
See :PEP:`3121` for more details.
@@ -189,7 +202,7 @@ or request "multi-phase initialization" by returning the definition struct itsel
An array of slot definitions for multi-phase initialization, terminated by
a ``{0, NULL}`` entry.
- When using single-phase initialization, *m_slots* must be ``NULL``.
+ When using legacy single-phase initialization, *m_slots* must be ``NULL``.
.. versionchanged:: 3.5
@@ -249,78 +262,9 @@ or request "multi-phase initialization" by returning the definition struct itsel
.. versionchanged:: 3.9
No longer called before the module state is allocated.
-Single-phase initialization
-...........................
-
-The module initialization function may create and return the module object
-directly. This is referred to as "single-phase initialization", and uses one
-of the following two module creation functions:
-
-.. c:function:: PyObject* PyModule_Create(PyModuleDef *def)
-
- Create a new module object, given the definition in *def*. This behaves
- like :c:func:`PyModule_Create2` with *module_api_version* set to
- :c:macro:`PYTHON_API_VERSION`.
-
-.. c:function:: PyObject* PyModule_Create2(PyModuleDef *def, int module_api_version)
-
- Create a new module object, given the definition in *def*, assuming the
- API version *module_api_version*. If that version does not match the version
- of the running interpreter, a :exc:`RuntimeWarning` is emitted.
-
- Return ``NULL`` with an exception set on error.
-
- .. note::
-
- Most uses of this function should be using :c:func:`PyModule_Create`
- instead; only use this if you are sure you need it.
-
-Before it is returned from in the initialization function, the resulting module
-object is typically populated using functions like :c:func:`PyModule_AddObjectRef`.
-
-.. _multi-phase-initialization:
-
-Multi-phase initialization
-..........................
-
-An alternate way to specify extensions is to request "multi-phase initialization".
-Extension modules created this way behave more like Python modules: the
-initialization is split between the *creation phase*, when the module object
-is created, and the *execution phase*, when it is populated.
-The distinction is similar to the :py:meth:`!__new__` and :py:meth:`!__init__` methods
-of classes.
-
-Unlike modules created using single-phase initialization, these modules are not
-singletons: if the *sys.modules* entry is removed and the module is re-imported,
-a new module object is created, and the old module is subject to normal garbage
-collection -- as with Python modules.
-By default, multiple modules created from the same definition should be
-independent: changes to one should not affect the others.
-This means that all state should be specific to the module object (using e.g.
-using :c:func:`PyModule_GetState`), or its contents (such as the module's
-:attr:`~object.__dict__` or individual classes created with :c:func:`PyType_FromSpec`).
-
-All modules created using multi-phase initialization are expected to support
-:ref:`sub-interpreters <sub-interpreter-support>`. Making sure multiple modules
-are independent is typically enough to achieve this.
-
-To request multi-phase initialization, the initialization function
-(PyInit_modulename) returns a :c:type:`PyModuleDef` instance with non-empty
-:c:member:`~PyModuleDef.m_slots`. Before it is returned, the ``PyModuleDef``
-instance must be initialized with the following function:
-
-.. c:function:: PyObject* PyModuleDef_Init(PyModuleDef *def)
-
- Ensures a module definition is a properly initialized Python object that
- correctly reports its type and reference count.
-
- Returns *def* cast to ``PyObject*``, or ``NULL`` if an error occurred.
-
- .. versionadded:: 3.5
-
-The *m_slots* member of the module definition must point to an array of
-``PyModuleDef_Slot`` structures:
+Module slots
+............
.. c:type:: PyModuleDef_Slot
@@ -334,8 +278,6 @@ The *m_slots* member of the module definition must point to an array of
.. versionadded:: 3.5
-The *m_slots* array must be terminated by a slot with id 0.
-
The available slot types are:
.. c:macro:: Py_mod_create
@@ -446,21 +388,48 @@ The available slot types are:
.. versionadded:: 3.13
-See :PEP:`489` for more details on multi-phase initialization.
-Low-level module creation functions
-...................................
+.. _moduledef-dynamic:
-The following functions are called under the hood when using multi-phase
-initialization. They can be used directly, for example when creating module
-objects dynamically. Note that both ``PyModule_FromDefAndSpec`` and
-``PyModule_ExecDef`` must be called to fully initialize a module.
+Creating extension modules dynamically
+--------------------------------------
+
+The following functions may be used to create a module outside of an
+extension's :ref:`initialization function <extension-export-hook>`.
+They are also used in
+:ref:`single-phase initialization <single-phase-initialization>`.
+
+.. c:function:: PyObject* PyModule_Create(PyModuleDef *def)
+
+ Create a new module object, given the definition in *def*.
+ This is a macro that calls :c:func:`PyModule_Create2` with
+ *module_api_version* set to :c:macro:`PYTHON_API_VERSION`, or
+ to :c:macro:`PYTHON_ABI_VERSION` if using the
+ :ref:`limited API <limited-c-api>`.
+
+.. c:function:: PyObject* PyModule_Create2(PyModuleDef *def, int module_api_version)
+
+ Create a new module object, given the definition in *def*, assuming the
+ API version *module_api_version*. If that version does not match the version
+ of the running interpreter, a :exc:`RuntimeWarning` is emitted.
+
+ Return ``NULL`` with an exception set on error.
+
+ This function does not support slots.
+ The :c:member:`~PyModuleDef.m_slots` member of *def* must be ``NULL``.
+
+
+ .. note::
+
+ Most uses of this function should be using :c:func:`PyModule_Create`
+ instead; only use this if you are sure you need it.
.. c:function:: PyObject * PyModule_FromDefAndSpec(PyModuleDef *def, PyObject *spec)
- Create a new module object, given the definition in *def* and the
- ModuleSpec *spec*. This behaves like :c:func:`PyModule_FromDefAndSpec2`
- with *module_api_version* set to :c:macro:`PYTHON_API_VERSION`.
+ This macro calls :c:func:`PyModule_FromDefAndSpec2` with
+ *module_api_version* set to :c:macro:`PYTHON_API_VERSION`, or
+ to :c:macro:`PYTHON_ABI_VERSION` if using the
+ :ref:`limited API <limited-c-api>`.
.. versionadded:: 3.5
@@ -473,6 +442,10 @@ objects dynamically. Note that both ``PyModule_FromDefAndSpec`` and
Return ``NULL`` with an exception set on error.
+ Note that this does not process execution slots (:c:data:`Py_mod_exec`).
+ Both ``PyModule_FromDefAndSpec`` and ``PyModule_ExecDef`` must be called
+ to fully initialize a module.
+
.. note::
Most uses of this function should be using :c:func:`PyModule_FromDefAndSpec`
@@ -486,35 +459,29 @@ objects dynamically. Note that both ``PyModule_FromDefAndSpec`` and
.. versionadded:: 3.5
-.. c:function:: int PyModule_SetDocString(PyObject *module, const char *docstring)
+.. c:macro:: PYTHON_API_VERSION
- Set the docstring for *module* to *docstring*.
- This function is called automatically when creating a module from
- ``PyModuleDef``, using either ``PyModule_Create`` or
- ``PyModule_FromDefAndSpec``.
+ The C API version. Defined for backwards compatibility.
- .. versionadded:: 3.5
+ Currently, this constant is not updated in new Python versions, and is not
+ useful for versioning. This may change in the future.
-.. c:function:: int PyModule_AddFunctions(PyObject *module, PyMethodDef *functions)
+.. c:macro:: PYTHON_ABI_VERSION
- Add the functions from the ``NULL`` terminated *functions* array to *module*.
- Refer to the :c:type:`PyMethodDef` documentation for details on individual
- entries (due to the lack of a shared module namespace, module level
- "functions" implemented in C typically receive the module as their first
- parameter, making them similar to instance methods on Python classes).
- This function is called automatically when creating a module from
- ``PyModuleDef``, using either ``PyModule_Create`` or
- ``PyModule_FromDefAndSpec``.
+ Defined as ``3`` for backwards compatibility.
+
+ Currently, this constant is not updated in new Python versions, and is not
+ useful for versioning. This may change in the future.
- .. versionadded:: 3.5
Support functions
-.................
+-----------------
-The module initialization function (if using single phase initialization) or
-a function called from a module execution slot (if using multi-phase
-initialization), can use the following functions to help initialize the module
-state:
+The following functions are provided to help initialize a module
+state.
+They are intended for a module's execution slots (:c:data:`Py_mod_exec`),
+the initialization function for legacy :ref:`single-phase initialization <single-phase-initialization>`,
+or code that creates modules dynamically.
.. c:function:: int PyModule_AddObjectRef(PyObject *module, const char *name, PyObject *value)
@@ -663,12 +630,39 @@ state:
.. versionadded:: 3.9
+.. c:function:: int PyModule_AddFunctions(PyObject *module, PyMethodDef *functions)
+
+ Add the functions from the ``NULL`` terminated *functions* array to *module*.
+ Refer to the :c:type:`PyMethodDef` documentation for details on individual
+ entries (due to the lack of a shared module namespace, module level
+ "functions" implemented in C typically receive the module as their first
+ parameter, making them similar to instance methods on Python classes).
+
+ This function is called automatically when creating a module from
+ ``PyModuleDef`` (such as when using :ref:`multi-phase-initialization`,
+ ``PyModule_Create``, or ``PyModule_FromDefAndSpec``).
+ Some module authors may prefer defining functions in multiple
+ :c:type:`PyMethodDef` arrays; in that case they should call this function
+ directly.
+
+ .. versionadded:: 3.5
+
+.. c:function:: int PyModule_SetDocString(PyObject *module, const char *docstring)
+
+ Set the docstring for *module* to *docstring*.
+ This function is called automatically when creating a module from
+ ``PyModuleDef`` (such as when using :ref:`multi-phase-initialization`,
+ ``PyModule_Create``, or ``PyModule_FromDefAndSpec``).
+
+ .. versionadded:: 3.5
+
.. c:function:: int PyUnstable_Module_SetGIL(PyObject *module, void *gil)
Indicate that *module* does or does not support running without the global
interpreter lock (GIL), using one of the values from
:c:macro:`Py_mod_gil`. It must be called during *module*'s initialization
- function. If this function is not called during module initialization, the
+ function when using :ref:`single-phase-initialization`.
+ If this function is not called during module initialization, the
import machinery assumes the module does not support running without the
GIL. This function is only available in Python builds configured with
:option:`--disable-gil`.
@@ -677,10 +671,11 @@ state:
.. versionadded:: 3.13
-Module lookup
-^^^^^^^^^^^^^
+Module lookup (single-phase initialization)
+...........................................
-Single-phase initialization creates singleton modules that can be looked up
+The legacy :ref:`single-phase initialization <single-phase-initialization>`
+initialization scheme creates singleton modules that can be looked up
in the context of the current interpreter. This allows the module object to be
retrieved later with only a reference to the module definition.
@@ -701,7 +696,8 @@ since multiple such modules can be created from a single definition.
Only effective on modules created using single-phase initialization.
- Python calls ``PyState_AddModule`` automatically after importing a module,
+ Python calls ``PyState_AddModule`` automatically after importing a module
+ that uses :ref:`single-phase initialization <single-phase-initialization>`,
so it is unnecessary (but harmless) to call it from module initialization
code. An explicit call is needed only if the module's own init code
subsequently calls ``PyState_FindModule``.
@@ -709,6 +705,9 @@ since multiple such modules can be created from a single definition.
mechanisms (either by calling it directly, or by referring to its
implementation for details of the required state updates).
+ If a module was attached previously using the same *def*, it is replaced
+ by the new *module*.
+
The caller must have an :term:`attached thread state`.
Return ``-1`` with an exception set on error, ``0`` on success.
diff --git a/Doc/c-api/stable.rst b/Doc/c-api/stable.rst
index 124e58cf950..9b65e0b8d23 100644
--- a/Doc/c-api/stable.rst
+++ b/Doc/c-api/stable.rst
@@ -51,6 +51,7 @@ It is generally intended for specialized, low-level tools like debuggers.
Projects that use this API are expected to follow
CPython development and spend extra effort adjusting to changes.
+.. _stable-application-binary-interface:
Stable Application Binary Interface
===================================
diff --git a/Doc/c-api/sys.rst b/Doc/c-api/sys.rst
index b3c89800e38..b34936dd55e 100644
--- a/Doc/c-api/sys.rst
+++ b/Doc/c-api/sys.rst
@@ -258,10 +258,57 @@ These are utility functions that make functionality from the :mod:`sys` module
accessible to C code. They all work with the current interpreter thread's
:mod:`sys` module's dict, which is contained in the internal thread state structure.
+.. c:function:: PyObject *PySys_GetAttr(PyObject *name)
+
+ Get the attribute *name* of the :mod:`sys` module.
+ Return a :term:`strong reference`.
+ Raise :exc:`RuntimeError` and return ``NULL`` if it does not exist or
+ if the :mod:`sys` module cannot be found.
+
+ If the non-existing object should not be treated as a failure, you can use
+ :c:func:`PySys_GetOptionalAttr` instead.
+
+ .. versionadded:: next
+
+.. c:function:: PyObject *PySys_GetAttrString(const char *name)
+
+ This is the same as :c:func:`PySys_GetAttr`, but *name* is
+ specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
+ rather than a :c:expr:`PyObject*`.
+
+ If the non-existing object should not be treated as a failure, you can use
+ :c:func:`PySys_GetOptionalAttrString` instead.
+
+ .. versionadded:: next
+
+.. c:function:: int PySys_GetOptionalAttr(PyObject *name, PyObject **result)
+
+ Variant of :c:func:`PySys_GetAttr` which doesn't raise
+ exception if the object does not exist.
+
+ * Set *\*result* to a new :term:`strong reference` to the object and
+ return ``1`` if the object exists.
+ * Set *\*result* to ``NULL`` and return ``0`` without setting an exception
+ if the object does not exist.
+ * Set an exception, set *\*result* to ``NULL``, and return ``-1``,
+ if an error occurred.
+
+ .. versionadded:: next
+
+.. c:function:: int PySys_GetOptionalAttrString(const char *name, PyObject **result)
+
+ This is the same as :c:func:`PySys_GetOptionalAttr`, but *name* is
+ specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
+ rather than a :c:expr:`PyObject*`.
+
+ .. versionadded:: next
+
.. c:function:: PyObject *PySys_GetObject(const char *name)
- Return the object *name* from the :mod:`sys` module or ``NULL`` if it does
- not exist, without setting an exception.
+ Similar to :c:func:`PySys_GetAttrString`, but return a :term:`borrowed
+ reference` and return ``NULL`` *without* setting exception on failure.
+
+ Preserves exception that was set before the call.
.. c:function:: int PySys_SetObject(const char *name, PyObject *v)
diff --git a/Doc/c-api/typeobj.rst b/Doc/c-api/typeobj.rst
index 91046c0e6f1..af2bead3bb5 100644
--- a/Doc/c-api/typeobj.rst
+++ b/Doc/c-api/typeobj.rst
@@ -686,6 +686,26 @@ and :c:data:`PyType_Type` effectively act as defaults.)
instance, and call the type's :c:member:`~PyTypeObject.tp_free` function to
free the object itself.
+ If you may call functions that may set the error indicator, you must use
+ :c:func:`PyErr_GetRaisedException` and :c:func:`PyErr_SetRaisedException`
+ to ensure you don't clobber a preexisting error indicator (the deallocation
+ could have occurred while processing a different error):
+
+ .. code-block:: c
+
+ static void
+ foo_dealloc(foo_object *self)
+ {
+ PyObject *et, *ev, *etb;
+ PyObject *exc = PyErr_GetRaisedException();
+ ...
+ PyErr_SetRaisedException(exc);
+ }
+
+ The dealloc handler itself must not raise an exception; if it hits an error
+ case it should call :c:func:`PyErr_FormatUnraisable` to log (and clear) an
+ unraisable exception.
+
No guarantees are made about when an object is destroyed, except:
* Python will destroy an object immediately or some time after the final
diff --git a/Doc/c-api/unicode.rst b/Doc/c-api/unicode.rst
index cdd90d05b70..84fee05cb4c 100644
--- a/Doc/c-api/unicode.rst
+++ b/Doc/c-api/unicode.rst
@@ -191,6 +191,22 @@ access to internal read-only data of Unicode objects:
.. versionadded:: 3.2
+.. c:function:: Py_hash_t PyUnstable_Unicode_GET_CACHED_HASH(PyObject *str)
+
+ If the hash of *str*, as returned by :c:func:`PyObject_Hash`, has been
+ cached and is immediately available, return it.
+ Otherwise, return ``-1`` *without* setting an exception.
+
+ If *str* is not a string (that is, if ``PyUnicode_Check(obj)``
+ is false), the behavior is undefined.
+
+ This function never fails with an exception.
+
+ Note that there are no guarantees on when an object's hash is cached,
+ and the (non-)existence of a cached hash does not imply that the string has
+ any other properties.
+
+
Unicode Character Properties
""""""""""""""""""""""""""""
@@ -1461,10 +1477,6 @@ the user settings on the machine running the codec.
.. versionadded:: 3.3
-Methods & Slots
-"""""""""""""""
-
-
.. _unicodemethodsandslots:
Methods and Slot Functions
@@ -1726,10 +1738,6 @@ They all return ``NULL`` or ``-1`` if an exception occurs.
from user input, prefer calling :c:func:`PyUnicode_FromString` and
:c:func:`PyUnicode_InternInPlace` directly.
- .. impl-detail::
-
- Strings interned this way are made :term:`immortal`.
-
.. c:function:: unsigned int PyUnicode_CHECK_INTERNED(PyObject *str)
@@ -1806,9 +1814,24 @@ object.
See also :c:func:`PyUnicodeWriter_DecodeUTF8Stateful`.
+.. c:function:: int PyUnicodeWriter_WriteASCII(PyUnicodeWriter *writer, const char *str, Py_ssize_t size)
+
+ Write the ASCII string *str* into *writer*.
+
+ *size* is the string length in bytes. If *size* is equal to ``-1``, call
+ ``strlen(str)`` to get the string length.
+
+ *str* must only contain ASCII characters. The behavior is undefined if
+ *str* contains non-ASCII characters.
+
+ On success, return ``0``.
+ On error, set an exception, leave the writer unchanged, and return ``-1``.
+
+ .. versionadded:: 3.14
+
.. c:function:: int PyUnicodeWriter_WriteWideChar(PyUnicodeWriter *writer, const wchar_t *str, Py_ssize_t size)
- Writer the wide string *str* into *writer*.
+ Write the wide string *str* into *writer*.
*size* is a number of wide characters. If *size* is equal to ``-1``, call
``wcslen(str)`` to get the string length.