views:

301

answers:

1

I have a C++ value type wrapped with Boost.Python which has a concept of a NULL value. The relevant parts of the wrapper code appear as follows:

class_<TCurrency> currency( "TCurrency" )
    .def( init<long>() )
    .def( init<const std::string&>() )
    <...>;

Currently, trying to create a NULL instance in Python by passing None to the __init__() method causes the C++ ctor accepting a const string reference to be called with an invalid reference. (&arg == NULL)

Is it possible to trap the case where None is being passed to a constructor and handle it gracefully or at least to throw a meaningful exception before my program crashes?

Using Boost 1.36 and Python 2.6.2.

+2  A: 
Roger Pate
It does seem like there should be an adapter or similar you should be able to apply to, for example, make A(None) use the default ctor. I'm glad you asked this question and I'll be looking forward to a more detailed answer. (From someone else.. ><)
Roger Pate
Interesting. I don't really want to modify the original C++ class to accept a void*, but I've attempted to define an __init__ outside the class. It doesn't actually create an object correctly (this will be the subject of another question) but at least I get a Python tb instead of a crash. Yay! =]
James Emerton