Handling exception thrown by Boost.Python c-extension in Python code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MarkE

    #1

    Handling exception thrown by Boost.Python c-extension in Python code

    I'm just getting started on Boost Python and may have missed this
    obvious looking problem somewhere.

    Given a c-extension "testext" written using Boost Python containing a
    base class "Base", a derived class "Derived", and a function
    "doSomethin g" which expects a "Derived" parameter, if I pass it a
    "Base" parameter an exception is thrown. This is a
    Boost.Python.Ar gumentError. My question is how do I catch this error ?

    I tried the following bit of investigation:
    #Start code
    import testext
    b = testext.Base()
    try:
    testext.doSomet hing(b)
    except Exception, e:
    pass
    help(e.__class_ _)
    #End code

    which produces
    #Start output
    Help on class ArgumentError:

    class ArgumentError(e xceptions.TypeE rror)
    | Method resolution order:
    | ArgumentError
    | exceptions.Type Error
    | exceptions.Stan dardError
    | exceptions.Exce ption
    |
    | Methods inherited from exceptions.Exce ption:
    |
    | __getitem__(... )
    |
    | __init__(...)
    |
    | __str__(...)
    #End output

    "print e" produces "<Boost.Python. ArgumentError instance>"

    So I could handle this by writing an except clause for TypeError.

    Boost.Python doesn't exist as a module i.e. it's not in sys.modules,
    and I don't know how to import it - should there be a Boost.Python
    module somewhere on my PythonPath that I've forgotten to setup ?
    Is there a standard way of catching these errors by their actual
    type ?
    Is there an easy way to export the exception classes from my c-
    extension (testext) so that I can use that ? Thus "except
    testext.Argumen tError" would catch the "Boost.Python.A rgumentError" ?

    Thanks for any help,
    Mark

Working...