Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Use setuptools in the tutorial itself
  • Loading branch information
AA-Turner committed Aug 16, 2023
commit 8b281112e7b199b2b5822cfbbf329732899701e4
44 changes: 22 additions & 22 deletions Doc/extending/newtypes_tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -194,36 +194,32 @@ This adds the type to the module dictionary. This allows us to create
>>> mycustom = custom.Custom()

That's it! All that remains is to build it; put the above code in a file called
:file:`custom.c` and:
:file:`custom.c`,

.. literalinclude:: ../includes/newtypes/pyproject.toml

in a file called :file:`pyproject.toml`, and

.. code-block:: python

from distutils.core import setup, Extension
setup(name="custom", version="1.0",
ext_modules=[Extension("custom", ["custom.c"])])
from setuptools import Extension, setup
setup(ext_modules=[Extension("custom", ["custom.c"])])

in a file called :file:`setup.py`; then typing

.. code-block:: shell-session

$ python setup.py build
$ python -m pip install .

at a shell should produce a file :file:`custom.so` in a subdirectory; move to
that directory and fire up Python --- you should be able to ``import custom`` and
play around with Custom objects.
in a shell should produce a file :file:`custom.so` in a subdirectory
and install it; now fire up Python --- you should be able to ``import custom``
and play around with ``Custom`` objects.

That wasn't so hard, was it?

Of course, the current Custom type is pretty uninteresting. It has no data and
doesn't do anything. It can't even be subclassed.

.. note::
While this documentation showcases the standard :mod:`!distutils` module
for building C extensions, it is recommended in real-world use cases to
use the newer and better-maintained ``setuptools`` library. Documentation
on how to do this is out of scope for this document and can be found in
the `Python Packaging User's Guide <https://packaging.python.org/tutorials/distributing-packages/>`_.


Adding data and methods to the Basic example
============================================
Expand Down Expand Up @@ -514,17 +510,21 @@ We rename :c:func:`!PyInit_custom` to :c:func:`!PyInit_custom2`, update the
module name in the :c:type:`PyModuleDef` struct, and update the full class
name in the :c:type:`PyTypeObject` struct.

Finally, we update our :file:`setup.py` file to build the new module:
Finally, we update our :file:`setup.py` file to include the new module,

.. code-block:: python

from distutils.core import setup, Extension
setup(name="custom", version="1.0",
ext_modules=[
Extension("custom", ["custom.c"]),
Extension("custom2", ["custom2.c"]),
])
from setuptools import Extension, setup
setup(ext_modules=[
Extension("custom", ["custom.c"]),
Extension("custom2", ["custom2.c"]),
])

and then we re-install so that we can ``import custom2``:

.. code-block:: shell-session

$ python -m pip install .

Providing finer control over data attributes
============================================
Expand Down
2 changes: 1 addition & 1 deletion Doc/includes/newtypes/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[project]
name = "newtypes_tutorial"
name = "custom"
version = "1"