Using exceptions in defined in an extension module inside another extension module

Christian Heimes lists at cheimes.de
Wed Dec 24 17:09:17 CET 2008


Floris Bruynooghe schrieb:
> What I can't work out however is how to then be able to raise this
> exception in another extension module.  Just defining it as "extern"
> doesn't work, even if I make sure the first module -that creates the
> exception- gets loaded first.  Because the symbol is defined in the
> first extension module the dynamic linker can't find it as it only
> seems to look in the main python executable for symbols used in
> dlloaded sofiles.
> 
> Does anyone have an idea of how you can do this?

The answer is so obvious that you are going to bang your head against
the next wall. You have to do exactly the same as you'd do with a pure
Python module: import it. :)

static PyObject *yourexc = NULL;

PyObject *yourmod = PyImport_ImportModule("yourmod");
if (yourmod == NULL)
    return NULL;
*yourexc = PyObject_GetAttrString(yourmod, "YourException");
if (yourexc == NULL)
    return NULL;

Christian




More information about the Python-list mailing list