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