Handling User Defined Exceptions returned from C

sarbx at my-deja.com sarbx at my-deja.com
Mon Feb 21 22:51:17 EST 2000


I'm trying to Handle a exception in python returned from C.

The C code is as follows,

static PyObject *SpamError

static PyObject * spam_test(PyObject *self,PyObject *args) {
...
...
...
PyErr_SetString(SpamError, "Testing User Defined Exceptions");
...
..
return PyBuildValue("i", -1);
}

static PyMethodDef SpamMethods[] = {
    {"system",  spam_system, METH_VARARGS},
    {"test",  spam_test, METH_VARARGS},
    {NULL,      NULL}        /* Sentinel */
};

void initspam() {
   PyObject *m, *d;

   m = Py_InitModule("spam", SpamMethods);
   d = PyModule_GetDict(m);
   SpamError = PyErr_NewException("spam.error", NULL, NULL);
   PyDict_SetItemString(d, "error", SpamError);
}

The Python script,

import spam
import sys

try:
  st = spam.test(tel)
except spam.error:
  print sys.exc_info()


But this doesnt seem to work. I thougt it might be a exception name
problem and called the function without the try clause, hoping the
python interperter will raise a unhandled exception, but that didnt
work too.

What am I doing wrong.

Thanks so much.

-sarva


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list