Skip to content

Commit 11731e2

Browse files
committed
Add docs about ext-modules in pyproject.toml
1 parent bf768e0 commit 11731e2

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed

docs/userguide/ext_modules.rst

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,41 @@ and all project metadata configuration in the ``pyproject.toml`` file:
2727
version = "0.42"
2828
2929
To instruct setuptools to compile the ``foo.c`` file into the extension module
30-
``mylib.foo``, we need to add a ``setup.py`` file similar to the following:
30+
``mylib.foo``, we need to define an appropriate configuration in either
31+
``pyproject.toml`` [#pyproject.toml]_ or ``setup.py`` file ,
32+
similar to the following:
3133

32-
.. code-block:: python
34+
.. tab:: pyproject.toml
3335

34-
from setuptools import Extension, setup
36+
.. code-block:: toml
3537
36-
setup(
37-
ext_modules=[
38-
Extension(
39-
name="mylib.foo", # as it would be imported
40-
# may include packages/namespaces separated by `.`
41-
42-
sources=["foo.c"], # all sources are compiled into a single binary file
43-
),
38+
[tool.setuptools]
39+
ext-modules = [
40+
{name = "mylib.foo", sources = ["foo.c"]}
4441
]
45-
)
42+
43+
.. tab:: setup.py
44+
45+
.. code-block:: python
46+
47+
from setuptools import Extension, setup
48+
49+
setup(
50+
ext_modules=[
51+
Extension(
52+
name="mylib.foo",
53+
sources=["foo.c"],
54+
),
55+
]
56+
)
57+
58+
The ``name`` value corresponds to how the extension module would be
59+
imported and may include packages/namespaces separated by ``.``.
60+
The ``sources`` value is a list of all source files that are compiled
61+
into a single binary file.
62+
Optionally any other parameter of :class:`setuptools.Extension` can be defined
63+
in the configuration file (but in the case of ``pyproject.toml`` they must be
64+
written using :wiki:`kebab-case` convention).
4665

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

170189

190+
----
191+
192+
.. rubric:: Notes
193+
194+
.. [#pyproject.toml]
195+
Declarative configuration of extension modules via ``pyproject.toml`` was
196+
introduced recently and is still considered experimental.
197+
Therefore it might change in future versions of ``setuptools``.
198+
199+
171200
.. _Python docs about C/C++ extensions: https://docs.python.org/3/extending/extending.html
172201
.. _Cython: https://cython.readthedocs.io/en/stable/index.html
173202
.. _directory options: https://gcc.gnu.org/onlinedocs/gcc/Directory-Options.html

docs/userguide/pyproject_config.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ file, and can be set via the ``tool.setuptools`` table:
8888
Key Value Type (TOML) Notes
8989
========================= =========================== =========================
9090
``py-modules`` array See tip below.
91+
``ext-modules`` array of **Experimental** - Each item corresponds to a
92+
tables/inline-tables :class:`setuptools.Extension` object and may define
93+
the associated parameters in :wiki:`kebab-case`.
9194
``packages`` array or ``find`` directive See tip below.
9295
``package-dir`` table/inline-table Used when explicitly/manually listing ``packages``.
9396
------------------------- --------------------------- -------------------------

setuptools/extension.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class Extension(_Extension):
127127
:keyword bool py_limited_api:
128128
opt-in flag for the usage of :doc:`Python's limited API <python:c-api/stable>`.
129129
130-
:raises setuptools.errors.PlatformError: if 'runtime_library_dirs' is
130+
:raises setuptools.errors.PlatformError: if ``runtime_library_dirs`` is
131131
specified on Windows. (since v63)
132132
"""
133133

0 commit comments

Comments
 (0)