Using exceptions in defined in an extension module inside anotherextension module

Discussion in 'Python' started by Floris Bruynooghe, Dec 24, 2008.

  1. Hello

    If I have an extension module and want to use an exception I can do by
    declaring the exception as "extern PyObject *PyExc_FooError" in the
    object files if I then link those together inside a module where the
    module has them declared the same (but no extern) and then initialises
    them in the PyMODINIT_FUNC using PyErr_NewException.

    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?

    Thanks
    Floris
     
    Floris Bruynooghe, Dec 24, 2008
    #1
    1. Advertising

  2. Christian Heimes wrote:

    > 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. :)


    Well, I hope the wall hurts as much as my head...

    Great tip, at first I wasn't looking forward to importing the module
    in every function where I wanted the exceptions. But then I realised
    they are global variables anyway so I could have them as such again
    and just assign them in the module init function.

    Thanks
    Floris
     
    Floris Bruynooghe, Dec 24, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ahmed Moustafa
    Replies:
    5
    Views:
    30,126
    Chris Smith
    Jul 14, 2004
  2. Anand
    Replies:
    3
    Views:
    921
    Tim Daneliuk
    Nov 8, 2003
  3. Paul Miller
    Replies:
    3
    Views:
    1,065
    Alex Martelli
    Nov 12, 2003
  4. Oodini
    Replies:
    1
    Views:
    1,846
    Keith Thompson
    Sep 27, 2005
  5. Angel Tsankov
    Replies:
    1
    Views:
    899
    Victor Bazarov
    Apr 5, 2006
Loading...

Share This Page