Skip to content

Commit 1be1eb8

Browse files
AA-Turnermiss-islington
authored andcommitted
pythonGH-92584: Remove distutils from the newtypes tutorial includes (pythonGH-108024)
(cherry picked from commit e97b7be) Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
1 parent 1dbf118 commit 1be1eb8

File tree

10 files changed

+42
-43
lines changed

10 files changed

+42
-43
lines changed

Doc/extending/newtypes_tutorial.rst

+27-27
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ extension module :mod:`!custom`:
4545
allows defining heap-allocated extension types using the
4646
:c:func:`PyType_FromSpec` function, which isn't covered in this tutorial.
4747

48-
.. literalinclude:: ../includes/custom.c
48+
.. literalinclude:: ../includes/newtypes/custom.c
4949

5050
Now that's quite a bit to take in at once, but hopefully bits will seem familiar
5151
from the previous chapter. This file defines three things:
@@ -196,36 +196,32 @@ This adds the type to the module dictionary. This allows us to create
196196
>>> mycustom = custom.Custom()
197197
198198
That's it! All that remains is to build it; put the above code in a file called
199-
:file:`custom.c` and:
199+
:file:`custom.c`,
200+
201+
.. literalinclude:: ../includes/newtypes/pyproject.toml
202+
203+
in a file called :file:`pyproject.toml`, and
200204

201205
.. code-block:: python
202206
203-
from distutils.core import setup, Extension
204-
setup(name="custom", version="1.0",
205-
ext_modules=[Extension("custom", ["custom.c"])])
207+
from setuptools import Extension, setup
208+
setup(ext_modules=[Extension("custom", ["custom.c"])])
206209
207210
in a file called :file:`setup.py`; then typing
208211

209212
.. code-block:: shell-session
210213
211-
$ python setup.py build
214+
$ python -m pip install .
212215
213-
at a shell should produce a file :file:`custom.so` in a subdirectory; move to
214-
that directory and fire up Python --- you should be able to ``import custom`` and
215-
play around with Custom objects.
216+
in a shell should produce a file :file:`custom.so` in a subdirectory
217+
and install it; now fire up Python --- you should be able to ``import custom``
218+
and play around with ``Custom`` objects.
216219

217220
That wasn't so hard, was it?
218221

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

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

230226
Adding data and methods to the Basic example
231227
============================================
@@ -234,7 +230,7 @@ Let's extend the basic example to add some data and methods. Let's also make
234230
the type usable as a base class. We'll create a new module, :mod:`!custom2` that
235231
adds these capabilities:
236232

237-
.. literalinclude:: ../includes/custom2.c
233+
.. literalinclude:: ../includes/newtypes/custom2.c
238234

239235

240236
This version of the module has a number of changes.
@@ -516,17 +512,21 @@ We rename :c:func:`!PyInit_custom` to :c:func:`!PyInit_custom2`, update the
516512
module name in the :c:type:`PyModuleDef` struct, and update the full class
517513
name in the :c:type:`PyTypeObject` struct.
518514

519-
Finally, we update our :file:`setup.py` file to build the new module:
515+
Finally, we update our :file:`setup.py` file to include the new module,
520516

521517
.. code-block:: python
522518
523-
from distutils.core import setup, Extension
524-
setup(name="custom", version="1.0",
525-
ext_modules=[
526-
Extension("custom", ["custom.c"]),
527-
Extension("custom2", ["custom2.c"]),
528-
])
519+
from setuptools import Extension, setup
520+
setup(ext_modules=[
521+
Extension("custom", ["custom.c"]),
522+
Extension("custom2", ["custom2.c"]),
523+
])
524+
525+
and then we re-install so that we can ``import custom2``:
526+
527+
.. code-block:: shell-session
529528
529+
$ python -m pip install .
530530
531531
Providing finer control over data attributes
532532
============================================
@@ -537,7 +537,7 @@ version of our module, the instance variables :attr:`!first` and :attr:`!last`
537537
could be set to non-string values or even deleted. We want to make sure that
538538
these attributes always contain strings.
539539

540-
.. literalinclude:: ../includes/custom3.c
540+
.. literalinclude:: ../includes/newtypes/custom3.c
541541

542542

543543
To provide greater control, over the :attr:`!first` and :attr:`!last` attributes,
@@ -684,7 +684,7 @@ To allow a :class:`!Custom` instance participating in a reference cycle to
684684
be properly detected and collected by the cyclic GC, our :class:`!Custom` type
685685
needs to fill two additional slots and to enable a flag that enables these slots:
686686

687-
.. literalinclude:: ../includes/custom4.c
687+
.. literalinclude:: ../includes/newtypes/custom4.c
688688

689689

690690
First, the traversal method lets the cyclic GC know about subobjects that could
@@ -808,7 +808,7 @@ increases an internal counter:
808808
>>> print(s.increment())
809809
2
810810
811-
.. literalinclude:: ../includes/sublist.c
811+
.. literalinclude:: ../includes/newtypes/sublist.c
812812

813813

814814
As you can see, the source code closely resembles the :class:`!Custom` examples in
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Doc/includes/newtypes/pyproject.toml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[build-system]
2+
requires = ["setuptools"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[project]
6+
name = "custom"
7+
version = "1"

Doc/includes/newtypes/setup.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from setuptools import Extension, setup
2+
setup(ext_modules=[
3+
Extension("custom", ["custom.c"]),
4+
Extension("custom2", ["custom2.c"]),
5+
Extension("custom3", ["custom3.c"]),
6+
Extension("custom4", ["custom4.c"]),
7+
Extension("sublist", ["sublist.c"]),
8+
])
File renamed without changes.

Doc/includes/test.py Doc/includes/newtypes/test.py

-7
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,6 @@
187187
>>> gc.enable()
188188
"""
189189

190-
import os
191-
import sys
192-
from distutils.util import get_platform
193-
PLAT_SPEC = "%s-%d.%d" % (get_platform(), *sys.version_info[:2])
194-
src = os.path.join("build", "lib.%s" % PLAT_SPEC)
195-
sys.path.append(src)
196-
197190
if __name__ == "__main__":
198191
import doctest, __main__
199192
doctest.testmod(__main__)

Doc/includes/setup.py

-9
This file was deleted.

0 commit comments

Comments
 (0)