@@ -20,6 +20,7 @@ This section describes how you write functions that accept Python ``*args`` and
20
20
arguments and how to extract ``PyObject `` or C fundamental types from them.
21
21
22
22
23
+ ====================================
23
24
Specifying the Function Arguments
24
25
====================================
25
26
@@ -31,6 +32,7 @@ Two important features of CPython C functions are:
31
32
32
33
These are described below.
33
34
35
+ -------------------------------
34
36
C Function Declaration
35
37
-------------------------------
36
38
@@ -52,8 +54,18 @@ supress a compiler warning or error thus:
52
54
static PyObject *
53
55
parse_args_kwargs(PyObject *Py_UNUSED(module), PyObject *args, PyObject *kwargs);
54
56
57
+ .. index ::
58
+ single: Parsing Arguments; ml_flags
59
+
60
+ Setting the ``ml_flags `` Field
61
+ ------------------------------
62
+
63
+ The `ml_flags <https://docs.python.org/3.13/c-api/structures.html#c.PyMethodDef.ml_flags >`_ field in
64
+ `PyMethodDef <https://docs.python.org/3.13/c-api/structures.html#c.PyMethodDef >`_ specifies the form of the arguments.
65
+
55
66
.. index ::
56
67
single: Parsing Arguments; No Arguments
68
+ single: Parsing Arguments; METH_NOARGS
57
69
58
70
No Arguments
59
71
^^^^^^^^^^^^^^^^^^
@@ -64,6 +76,7 @@ No Arguments
64
76
65
77
.. index ::
66
78
single: Parsing Arguments; One Argument
79
+ single: Parsing Arguments; METH_O
67
80
68
81
One Argument
69
82
^^^^^^^^^^^^^^^^^^
@@ -74,23 +87,27 @@ One Argument
74
87
75
88
.. index ::
76
89
single: Parsing Arguments; Multiple Arguments
90
+ single: Parsing Arguments; METH_VARARGS
77
91
78
92
Multiple Positional Arguments
79
93
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
80
94
81
95
- The flags will be `METH_VARARGS <https://docs.python.org/3/c-api/structures.html#c.METH_VARARGS >`_
82
96
- The C Function Signature will be ``PyObject *PyCFunction(PyObject *self, PyObject *args); ``
83
- - Second value will be a sequence of arguments.
97
+ - Second value will be a tuple of arguments.
98
+ - `PyArg_ParseTuple() `_ is used to unpack the arguments.
84
99
85
100
.. index ::
86
101
single: Parsing Arguments; Positional and Keyword Arguments
102
+ single: Parsing Arguments; METH_KEYWORDS
87
103
88
104
Positional and Keyword Arguments
89
105
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
90
106
91
107
- The flags will be `METH_NOARGS | METH_KEYWORDS <https://docs.python.org/3/c-api/structures.html#c.METH_KEYWORDS >`_
92
108
- The C Function Signature will be ``PyObject *PyCFunctionWithKeywords(PyObject *self, PyObject *args, PyObject *kwargs); ``
93
109
- Second value will be a sequence of arguments, the third the dictionary of arguments.
110
+ - `PyArg_ParseTupleAndKeywords() `_ is used to unpack the arguments.
94
111
95
112
Documentation:
96
113
@@ -137,7 +154,8 @@ And this would be added to the module, say, by using:
137
154
Parsing the Arguments
138
155
------------------------------
139
156
140
- Once whe have the C function correctly declared then the arguments have to parsed according to their types.
157
+ Once we have the C function correctly declared then the arguments have to parsed according to their types and,
158
+ if required, converted to C types (so-called "unboxing").
141
159
This is done using the `PyArg_ParseTuple() `_ and `PyArg_ParseTupleAndKeywords() `_
142
160
(ignoring “old-style” functions which use `PyArg_Parse <https://docs.python.org/3/c-api/arg.html#c.PyArg_Parse >`_).
143
161
@@ -153,7 +171,7 @@ The reference documentation is excellent: `argument parsing and building values
153
171
154
172
``static PyObject *parse_args(PyObject *module, PyObject *args); ``
155
173
156
- Which expects the Python argument [0] to be a bytes object and the Python argument [1]
174
+ Which expects the Python args [0] to be a bytes object and the Python args [1]
157
175
to be an integer by using:
158
176
159
177
``PyArg_ParseTuple(args, "Si", &arg0, &arg1) ``
@@ -185,6 +203,7 @@ These examples are in ``src/cpy/cParseArgs.c`` and their tests are in ``tests/un
185
203
186
204
.. index ::
187
205
single: Parsing Arguments Example; No Arguments
206
+ single: Parsing Arguments; METH_NOARGS
188
207
189
208
No Arguments
190
209
------------------------------------
@@ -219,6 +238,7 @@ The Python interpreter will raise a ``TypeError`` on any arguments are offered t
219
238
220
239
.. index ::
221
240
single: Parsing Arguments Example; One Argument
241
+ single: Parsing Arguments; METH_O
222
242
223
243
One Argument
224
244
------------------------------------
@@ -304,6 +324,7 @@ Side note: Of course this does not protect you from malicious/badly written code
304
324
305
325
.. index ::
306
326
single: Parsing Arguments Example; Variable Number of Arguments
327
+ single: Parsing Arguments; METH_VARARGS
307
328
308
329
Variable Number of Arguments
309
330
----------------------------------------------------
@@ -316,7 +337,8 @@ In the following code we are expecting a bytes object, an integer and an optiona
316
337
For demonstration purposes, this returns the same three arguments.
317
338
In Python the equivalent function signature would be::
318
339
319
- def parse_args(a: bytes, b: int, c: str = 'default_string') -> typing.Tuple[bytes, int, str]:
340
+ def parse_args(a: bytes, b: int, c: str = 'default_string') \
341
+ -> typing.Tuple[bytes, int, str]:
320
342
321
343
Here is the C code, note the string that describes the argument types passed to ``PyArg_ParseTuple ``, if these types
322
344
are not present a ``ValueError `` will be set.
@@ -361,6 +383,7 @@ Note the wide variety of error messages that are obtained.
361
383
.. index ::
362
384
single: Parsing Arguments Example; Variable Number of Arguments
363
385
single: Parsing Arguments Example; Keyword Arguments
386
+ single: Parsing Arguments; METH_KEYWORDS
364
387
365
388
Variable Number of Arguments and Keyword Arguments
366
389
--------------------------------------------------------------------------
@@ -374,7 +397,8 @@ In the following code we are expecting a sequence and an optional integer count
374
397
It returns the `sequence ` repeated `count ` times.
375
398
In Python the equivalent function declaration would be::
376
399
377
- def parse_args_kwargs(sequence=typing.Sequence[typing.Any], count: = 1) -> typing.Sequence[typing.Any]:
400
+ def parse_args_kwargs(sequence=typing.Sequence[typing.Any], count: = 1) \
401
+ -> typing.Sequence[typing.Any]:
378
402
return sequence * count
379
403
380
404
Here is the C code, note the string ``"O|i" `` that describes the argument types passed to
@@ -393,7 +417,9 @@ Here is the C code, note the string ``"O|i"`` that describes the argument types
393
417
NULL,
394
418
};
395
419
396
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i", kwlist, &py_sequence, &count)) {
420
+ if (!PyArg_ParseTupleAndKeywords(
421
+ args, kwargs, "O|i", kwlist, &py_sequence, &count
422
+ )) {
397
423
goto except;
398
424
}
399
425
@@ -470,6 +496,7 @@ The solution is to cast away const in the call:
470
496
single: Parsing Arguments Example; Default String Arguments
471
497
single: Parsing Arguments Example; Default Bytes Arguments
472
498
single: Default Arguments; C
499
+ single: Py_buffer
473
500
474
501
Default String and Bytes Arguments
475
502
------------------------------------------
@@ -559,8 +586,8 @@ Suppose we want the functional equivalent of the Python function signature
559
586
560
587
.. code-block :: python
561
588
562
- def parse_pos_only_kwd_only (pos1 : str , pos2 : int , / , pos_or_kwd : bytes , * , kwd1 : float ,
563
- kwd2 : int ):
589
+ def parse_pos_only_kwd_only (pos1 : str , pos2 : int , / , pos_or_kwd : bytes , * ,
590
+ kwd1 : float = 256.0 , kwd2 : int = - 421 ):
564
591
return None
565
592
566
593
This is achieved by combining two techniques:
@@ -696,7 +723,7 @@ Being Pythonic with Default Mutable Arguments
696
723
=============================================
697
724
698
725
If the arguments default to some C fundamental type the code above is fine.
699
- However if the arguments default to Python objects then a little more work is needed.
726
+ However if the mutable arguments default to Python objects then a little more work is needed.
700
727
701
728
.. note ::
702
729
@@ -926,7 +953,8 @@ Firstly a macro to declare the static default object:
926
953
927
954
.. warning ::
928
955
929
- When using this macro in a source file then make sure each given "name" argument is unique.
956
+ When using this macro in a source file then make sure each given "name" argument is unique within the
957
+ translation unit.
930
958
931
959
And a macro to set it:
932
960
@@ -937,7 +965,7 @@ And a macro to set it:
937
965
name = default_##name; \
938
966
}
939
967
940
- And a macro to chek the type of the argument:
968
+ And a macro to check the type of the argument:
941
969
942
970
.. code-block :: c
943
971
0 commit comments