SourceForge has been redesigned. Learn more.
Close

#8 Alteration to the creation of new exceptions

closed-fixed
None
5
2005-08-29
2005-01-12
Joe Love
No

PyCXX has been very friendly to me in all but one way -
creating new exceptions which I can catch in Python.

After trying to come up with a way to work around this
shortcoming I gave up and made a small change to the
PyCXX 5.3.0 code to make it do what I wanted. With
this change, I can now create a new exception of type
Py::ExtensionExceptionType, init() it, and then throw
the exception so that it can be caught in Python. In
addition, it allows for init to define the parent of
the exception (hence the added function).

I realize that the previous code would let you create a
new exception, and throw it at python, however, you
could not catch that exception in python, as it would
complain about the module not containing such an object.

Below is a copy of the attached patch:
diff -r pycxx_5_3_0/CXX/Extensions.hxx
pycxx_5_3_0.mod2/CXX/Extensions.hxx
39a40
> void init( ExtensionModuleBase
&module, const std::string& name,
ExtensionExceptionType &parent );
diff -r pycxx_5_3_0/Src/cxx_extensions.cxx
pycxx_5_3_0.mod2/Src/cxx_extensions.cxx
1262c1262
< void ExtensionExceptionType::init(
ExtensionModuleBase &module, const std::string& name )
---
> void ExtensionExceptionType::init(
ExtensionModuleBase &module, const std::string& name,
ExtensionExceptionType &parent)
1266a1267,1270
> set( PyErr_NewException( const_cast<char *>(
module_name.c_str() ), parent.ptr(), NULL ), true );
> Py_INCREF(this->ptr());
> PyModule_AddObject((module.module()).ptr(),
const_cast<char *>(name.c_str()), this->ptr());
> }
1267a1272,1276
> void ExtensionExceptionType::init(
ExtensionModuleBase &module, const std::string& name )
> {
> std::string module_name( module.fullName() );
> module_name += ".";
> module_name += name;
1268a1278,1279
> Py_INCREF(this->ptr());
> PyModule_AddObject((module.module()).ptr(),
const_cast<char *>(name.c_str()), this->ptr());

Discussion

  • Joe Love

    Joe Love - 2005-01-12

    a better-looking diff (easier to follow, and figure out what changed)

     
  • Joe Love

    Joe Love - 2005-01-12

    Logged In: YES
    user_id=1078925

    (sorry for the followup, I was just informed about using
    diff -u to make it easier to follow).

     
  • Barry Alan Scott

    Logged In: YES
    user_id=28665

    You have always been able to catch the exceptions that you
    create
    in PyCXX. Did you add the exception to your module as an
    attribute
    that you use from python? See the pysvn extension as an example
    that works. http://pysvn.tigris.org, look in pysvn.hpp and
    pysvn.cpp
    for client_error.

    Howeve the patch may add some of the missing additional
    functionality
    for the exceptions mechanism. I'll review and update this issue.

     
  • Barry Alan Scott

    Logged In: YES
    user_id=28665

    I have added the part of the patch that allows you to create
    a exception based on a parent.

    But I think its not the job of this function to add itself
    to your module.
    You should do that seperately to allow for the exception to
    be presented in any way you wish (as an attribute of the
    module in another object of the module, etc).

    For example from pysvn:

    client_error.init( *this, "ClientError" );
    Py::Dict d( moduleDictionary() );
    d["ClientError"] = client_error;

    this will show up in pycxx_5_3_3 after passing regression
    testing

     
  • Barry Alan Scott

    • assigned_to: nobody --> barry-scott
    • status: open --> closed-fixed
     
  • Barry Alan Scott

    Logged In: YES
    user_id=28665

    Shipped in V5.3.4

     

Log in to post a comment.