Skip to content

Commit

Permalink
Add docs about ext-modules in pyproject.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
abravalheri committed Sep 2, 2024
1 parent bf768e0 commit 11731e2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
53 changes: 41 additions & 12 deletions docs/userguide/ext_modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,41 @@ and all project metadata configuration in the ``pyproject.toml`` file:
version = "0.42"
To instruct setuptools to compile the ``foo.c`` file into the extension module
``mylib.foo``, we need to add a ``setup.py`` file similar to the following:
``mylib.foo``, we need to define an appropriate configuration in either
``pyproject.toml`` [#pyproject.toml]_ or ``setup.py`` file ,
similar to the following:

.. code-block:: python
.. tab:: pyproject.toml

from setuptools import Extension, setup
.. code-block:: toml
setup(
ext_modules=[
Extension(
name="mylib.foo", # as it would be imported
# may include packages/namespaces separated by `.`
sources=["foo.c"], # all sources are compiled into a single binary file
),
[tool.setuptools]
ext-modules = [
{name = "mylib.foo", sources = ["foo.c"]}
]
)
.. tab:: setup.py

.. code-block:: python
from setuptools import Extension, setup
setup(
ext_modules=[
Extension(
name="mylib.foo",
sources=["foo.c"],
),
]
)
The ``name`` value corresponds to how the extension module would be
imported and may include packages/namespaces separated by ``.``.
The ``sources`` value is a list of all source files that are compiled
into a single binary file.
Optionally any other parameter of :class:`setuptools.Extension` can be defined
in the configuration file (but in the case of ``pyproject.toml`` they must be
written using :wiki:`kebab-case` convention).

.. seealso::
You can find more information on the `Python docs about C/C++ extensions`_.
Expand Down Expand Up @@ -168,6 +187,16 @@ Extension API Reference
.. autoclass:: setuptools.Extension


----

.. rubric:: Notes

.. [#pyproject.toml]
Declarative configuration of extension modules via ``pyproject.toml`` was
introduced recently and is still considered experimental.
Therefore it might change in future versions of ``setuptools``.
.. _Python docs about C/C++ extensions: https://docs.python.org/3/extending/extending.html
.. _Cython: https://cython.readthedocs.io/en/stable/index.html
.. _directory options: https://gcc.gnu.org/onlinedocs/gcc/Directory-Options.html
Expand Down
3 changes: 3 additions & 0 deletions docs/userguide/pyproject_config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ file, and can be set via the ``tool.setuptools`` table:
Key Value Type (TOML) Notes
========================= =========================== =========================
``py-modules`` array See tip below.
``ext-modules`` array of **Experimental** - Each item corresponds to a
tables/inline-tables :class:`setuptools.Extension` object and may define
the associated parameters in :wiki:`kebab-case`.
``packages`` array or ``find`` directive See tip below.
``package-dir`` table/inline-table Used when explicitly/manually listing ``packages``.
------------------------- --------------------------- -------------------------
Expand Down
2 changes: 1 addition & 1 deletion setuptools/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class Extension(_Extension):
:keyword bool py_limited_api:
opt-in flag for the usage of :doc:`Python's limited API <python:c-api/stable>`.
:raises setuptools.errors.PlatformError: if 'runtime_library_dirs' is
:raises setuptools.errors.PlatformError: if ``runtime_library_dirs`` is
specified on Windows. (since v63)
"""

Expand Down

0 comments on commit 11731e2

Please sign in to comment.