[Python-checkins] r83563 - python/branches/py3k/Doc/extending/extending.rst

georg.brandl python-checkins at python.org
Mon Aug 2 22:21:21 CEST 2010


Author: georg.brandl
Date: Mon Aug  2 22:21:21 2010
New Revision: 83563

Log:
#9037: add example how to raise custom exceptions from C code.

Modified:
   python/branches/py3k/Doc/extending/extending.rst

Modified: python/branches/py3k/Doc/extending/extending.rst
==============================================================================
--- python/branches/py3k/Doc/extending/extending.rst	(original)
+++ python/branches/py3k/Doc/extending/extending.rst	Mon Aug  2 22:21:21 2010
@@ -226,9 +226,28 @@
 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:
 


More information about the Python-checkins mailing list