diff options
author | Georg Brandl <georg@python.org> | 2010-08-02 20:21:21 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2010-08-02 20:21:21 (GMT) |
commit | 9c491c9b285916d483079fc281461120e9509bd6 (patch) | |
tree | 3a43f74c1df2ab563c0a944ffaeeb20adcf331d8 | |
parent | 1209a8f9bcfecb2a40873685383eba59993fcb38 (diff) | |
download | cpython-9c491c9b285916d483079fc281461120e9509bd6.zip cpython-9c491c9b285916d483079fc281461120e9509bd6.tar.gz cpython-9c491c9b285916d483079fc281461120e9509bd6.tar.bz2 |
#9037: add example how to raise custom exceptions from C code.
-rw-r--r-- | Doc/extending/extending.rst | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/Doc/extending/extending.rst b/Doc/extending/extending.rst index d5f55c7..567fcf8 100644 --- a/Doc/extending/extending.rst +++ b/Doc/extending/extending.rst @@ -226,9 +226,28 @@ needed to ensure that it will not be discarded, causing :cdata:`SpamError` to become a dangling pointer. Should it become a dangling pointer, C code which raises the exception could cause a core dump or other unintended side effects. -We discuss the use of PyMODINIT_FUNC as a function return type later in this +We discuss the use of ``PyMODINIT_FUNC`` as a function return type later in this sample. +The :exc:`spam.error` exception can be raised in your extension module using a +call to :cfunc:`PyErr_SetString` as shown below:: + + static PyObject * + spam_system(PyObject *self, PyObject *args) + { + const char *command; + int sts; + + if (!PyArg_ParseTuple(args, "s", &command)) + return NULL; + sts = system(command); + if (sts < 0) { + PyErr_SetString(SpamError, "System command failed"); + return NULL; + } + return PyLong_FromLong(sts); + } + .. _backtoexample: |