You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/sphinx/source/exceptions.rst
+28
Original file line number
Diff line number
Diff line change
@@ -87,6 +87,34 @@ The other thing to note is that if there are multiple calls to ``PyErr_SetString
87
87
return NULL;
88
88
}
89
89
90
+
---------------------------------
91
+
Common Exception Patterns
92
+
---------------------------------
93
+
94
+
Here are some common use cases for raising exceptions.
95
+
96
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
97
+
Type Checking
98
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
99
+
100
+
A common requirement is to check the types of the arguments and raise a ``TypeError`` if they are wrong. Here is an example where we require a ``bytes`` object:
101
+
102
+
.. code-block:: c
103
+
:linenos:
104
+
:emphasize-lines: 4-9
105
+
106
+
static PyObject*
107
+
function(PyObject *self, PyObject *arg) {
108
+
/* ... */
109
+
if (! PyBytes_Check(arg)) {
110
+
PyErr_Format(PyExc_TypeError,
111
+
"Argument \"value\" to %s must be a bytes object not a \"%s\"",
Copy file name to clipboardExpand all lines: doc/sphinx/source/parsing_arguments.rst
+62-14
Original file line number
Diff line number
Diff line change
@@ -232,16 +232,34 @@ If you want the function signature to be ``argsKwargs(theString, theOptInt=8)``
232
232
233
233
.. code-block:: c
234
234
235
-
...
235
+
/* ... */
236
236
static char *kwlist[] = {
237
237
"",
238
238
"theOptInt",
239
239
NULL
240
240
};
241
-
...
241
+
/* ... */
242
242
243
243
.. note::
244
244
If you use ``|`` in the parser format string you have to set the default values for those optional arguments yourself in the C code. This is pretty straightforward if they are fundamental C types as ``arg2 = 8`` above. For Python values is a bit more tricky as described next.
245
+
246
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
247
+
Keyword Arguments and C++11
248
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
249
+
250
+
C++11 compilers warn when creating non-const ``char*`` from string literals as we have done with the keyword array above. The solution is to declare these ``const char*`` however ``PyArg_ParseTupleAndKeywords`` expects a ``char **``. The solution is to cast away const in the call:
@@ -461,31 +479,61 @@ For simple default values some macros may help. The first one declares and initi
461
479
} \
462
480
}
463
481
464
-
The second one assigns the argument to the default if it is not initialised. It just takes the name of the argument:
482
+
The second one assigns the argument to the default if it is not initialised and increments the reference count. It just takes the name of the argument:
465
483
466
484
.. code-block:: c
467
485
468
-
#define PY_DEFAULT_ARGUMENT_SET(name) if (! name) name = default_##name
486
+
#define PY_DEFAULT_ARGUMENT_SET(name) if (! name) name = default_##name; \
487
+
Py_INCREF(name)
469
488
470
-
And they can be used thus:
489
+
And they can be used like this when implementing a Python function signature such as::
0 commit comments