@@ -45,7 +45,7 @@ extension module :mod:`!custom`:
45
45
allows defining heap-allocated extension types using the
46
46
:c:func: `PyType_FromSpec ` function, which isn't covered in this tutorial.
47
47
48
- .. literalinclude :: ../includes/custom.c
48
+ .. literalinclude :: ../includes/newtypes/ custom.c
49
49
50
50
Now that's quite a bit to take in at once, but hopefully bits will seem familiar
51
51
from the previous chapter. This file defines three things:
@@ -194,36 +194,32 @@ This adds the type to the module dictionary. This allows us to create
194
194
>>> mycustom = custom.Custom()
195
195
196
196
That's it! All that remains is to build it; put the above code in a file called
197
- :file: `custom.c ` and:
197
+ :file: `custom.c `,
198
+
199
+ .. literalinclude :: ../includes/newtypes/pyproject.toml
200
+
201
+ in a file called :file: `pyproject.toml `, and
198
202
199
203
.. code-block :: python
200
204
201
- from distutils.core import setup, Extension
202
- setup(name = " custom" , version = " 1.0" ,
203
- ext_modules = [Extension(" custom" , [" custom.c" ])])
205
+ from setuptools import Extension, setup
206
+ setup(ext_modules = [Extension(" custom" , [" custom.c" ])])
204
207
205
208
in a file called :file: `setup.py `; then typing
206
209
207
210
.. code-block :: shell-session
208
211
209
- $ python setup.py build
212
+ $ python -m pip install .
210
213
211
- at a shell should produce a file :file: `custom.so ` in a subdirectory; move to
212
- that directory and fire up Python --- you should be able to ``import custom `` and
213
- play around with Custom objects.
214
+ in a shell should produce a file :file: `custom.so ` in a subdirectory
215
+ and install it; now fire up Python --- you should be able to ``import custom ``
216
+ and play around with `` Custom `` objects.
214
217
215
218
That wasn't so hard, was it?
216
219
217
220
Of course, the current Custom type is pretty uninteresting. It has no data and
218
221
doesn't do anything. It can't even be subclassed.
219
222
220
- .. note ::
221
- While this documentation showcases the standard :mod: `!distutils ` module
222
- for building C extensions, it is recommended in real-world use cases to
223
- use the newer and better-maintained ``setuptools `` library. Documentation
224
- on how to do this is out of scope for this document and can be found in
225
- the `Python Packaging User's Guide <https://packaging.python.org/tutorials/distributing-packages/ >`_.
226
-
227
223
228
224
Adding data and methods to the Basic example
229
225
============================================
@@ -232,7 +228,7 @@ Let's extend the basic example to add some data and methods. Let's also make
232
228
the type usable as a base class. We'll create a new module, :mod: `!custom2 ` that
233
229
adds these capabilities:
234
230
235
- .. literalinclude :: ../includes/custom2.c
231
+ .. literalinclude :: ../includes/newtypes/ custom2.c
236
232
237
233
238
234
This version of the module has a number of changes.
@@ -514,17 +510,21 @@ We rename :c:func:`!PyInit_custom` to :c:func:`!PyInit_custom2`, update the
514
510
module name in the :c:type: `PyModuleDef ` struct, and update the full class
515
511
name in the :c:type: `PyTypeObject ` struct.
516
512
517
- Finally, we update our :file: `setup.py ` file to build the new module:
513
+ Finally, we update our :file: `setup.py ` file to include the new module,
518
514
519
515
.. code-block :: python
520
516
521
- from distutils.core import setup, Extension
522
- setup(name = " custom" , version = " 1.0" ,
523
- ext_modules = [
524
- Extension(" custom" , [" custom.c" ]),
525
- Extension(" custom2" , [" custom2.c" ]),
526
- ])
517
+ from setuptools import Extension, setup
518
+ setup(ext_modules = [
519
+ Extension(" custom" , [" custom.c" ]),
520
+ Extension(" custom2" , [" custom2.c" ]),
521
+ ])
522
+
523
+ and then we re-install so that we can ``import custom2 ``:
524
+
525
+ .. code-block :: shell-session
527
526
527
+ $ python -m pip install .
528
528
529
529
Providing finer control over data attributes
530
530
============================================
@@ -535,7 +535,7 @@ version of our module, the instance variables :attr:`!first` and :attr:`!last`
535
535
could be set to non-string values or even deleted. We want to make sure that
536
536
these attributes always contain strings.
537
537
538
- .. literalinclude :: ../includes/custom3.c
538
+ .. literalinclude :: ../includes/newtypes/ custom3.c
539
539
540
540
541
541
To provide greater control, over the :attr: `!first ` and :attr: `!last ` attributes,
@@ -682,7 +682,7 @@ To allow a :class:`!Custom` instance participating in a reference cycle to
682
682
be properly detected and collected by the cyclic GC, our :class: `!Custom ` type
683
683
needs to fill two additional slots and to enable a flag that enables these slots:
684
684
685
- .. literalinclude :: ../includes/custom4.c
685
+ .. literalinclude :: ../includes/newtypes/ custom4.c
686
686
687
687
688
688
First, the traversal method lets the cyclic GC know about subobjects that could
@@ -806,7 +806,7 @@ increases an internal counter:
806
806
>>> print(s.increment())
807
807
2
808
808
809
- .. literalinclude :: ../includes/sublist.c
809
+ .. literalinclude :: ../includes/newtypes/ sublist.c
810
810
811
811
812
812
As you can see, the source code closely resembles the :class: `!Custom ` examples in
0 commit comments