@@ -1338,35 +1338,37 @@ state. Example from the ``setattro`` slot method in
1338
1338
See also :pep: `573 `.
1339
1339
1340
1340
1341
+ .. _clinic-howto-custom-converter :
1342
+
1341
1343
How to write a custom converter
1342
1344
-------------------------------
1343
1345
1344
- As we hinted at in the previous section... you can write your own converters!
1345
- A converter is simply a Python class that inherits from :py:class: `!CConverter `.
1346
- The main purpose of a custom converter is if you have a parameter using
1347
- the ``O& `` format unit—parsing this parameter means calling
1346
+ A converter is a Python class that inherits from :py:class: `!CConverter `.
1347
+ The main purpose of a custom converter, is for parameters parsed with
1348
+ the ``O& `` format unit --- parsing such a parameter means calling
1348
1349
a :c:func: `PyArg_ParseTuple ` "converter function".
1349
1350
1350
- Your converter class should be named ``*something*_converter ``.
1351
- If the name follows this convention, then your converter class
1352
- will be automatically registered with Argument Clinic; its name
1353
- will be the name of your class with the ``_converter `` suffix
1354
- stripped off. (This is accomplished with a metaclass.)
1355
-
1356
- You shouldn't subclass :py:meth: `!CConverter.__init__ `. Instead, you should
1357
- write a :py:meth: `!converter_init ` function. :py:meth: `!converter_init `
1358
- always accepts a *self * parameter; after that, all additional
1359
- parameters *must * be keyword-only. Any arguments passed in to
1360
- the converter in Argument Clinic will be passed along to your
1361
- :py:meth: `!converter_init `.
1351
+ Your converter class should be named :samp: `{ ConverterName } _converter `.
1352
+ By following this convention, your converter class will be automatically
1353
+ registered with Argument Clinic, with its *converter name * being the name of
1354
+ your converter class with the ``_converter `` suffix stripped off.
1362
1355
1363
- There are some additional members of :py:class: `!CConverter ` you may wish
1364
- to specify in your subclass. Here's the current list:
1356
+ Instead of subclassing :py:meth: `!CConverter.__init__ `,
1357
+ write a :py:meth: `!converter_init ` method.
1358
+ Apart for the *self * parameter, all additional :py:meth: `!converter_init `
1359
+ parameters **must ** be keyword-only.
1360
+ Any arguments passed to the converter in Argument Clinic
1361
+ will be passed along to your :py:meth: `!converter_init ` method.
1362
+ See :py:class: `!CConverter ` for a list of members you may wish to specify in
1363
+ your subclass.
1365
1364
1366
1365
.. module :: clinic
1367
1366
1368
1367
.. class :: CConverter
1369
1368
1369
+ The base class for all converters.
1370
+ See :ref: `clinic-howto-custom-converter ` for how to subclass this class.
1371
+
1370
1372
.. attribute :: type
1371
1373
1372
1374
The C type to use for this variable.
@@ -1431,16 +1433,16 @@ Here's the simplest example of a custom converter, from :source:`Modules/zlibmod
1431
1433
[python start generated code]*/
1432
1434
/*[python end generated code: output=da39a3ee5e6b4b0d input=35521e4e733823c7]*/
1433
1435
1434
- This block adds a converter to Argument Clinic named ``ssize_t ``. Parameters
1435
- declared as ``ssize_t `` will be declared as type :c:type: `Py_ssize_t `, and will
1436
- be parsed by the ``'O&' `` format unit, which will call the
1437
- `` ssize_t_converter `` converter function. `` ssize_t `` variables
1438
- automatically support default values.
1436
+ This block adds a converter named ``ssize_t `` to Argument Clinic.
1437
+ Parameters declared as ``ssize_t `` will be declared with type :c:type: `Py_ssize_t `,
1438
+ and will be parsed by the ``'O&' `` format unit,
1439
+ which will call the :c:func: ` ! ssize_t_converter ` converter C function.
1440
+ `` ssize_t `` variables automatically support default values.
1439
1441
1440
1442
More sophisticated custom converters can insert custom C code to
1441
1443
handle initialization and cleanup.
1442
1444
You can see more examples of custom converters in the CPython
1443
- source tree; grep the C files for the string :py:class: ` ! CConverter `.
1445
+ source tree; grep the C files for the string `` CConverter ` `.
1444
1446
1445
1447
1446
1448
How to write a custom return converter
0 commit comments