[C++-sig] Trying to clone Python object from C++ (again)
David Abrahams
dave at boost-consulting.com
Thu Jul 21 21:27:59 CEST 2005
"Paul F. Kunz" <Paul_Kunz at slac.stanford.edu> writes:
> I'm trying once again to clone a Python object whose class is
> derived from an exposed C++ base class from C++. After I call from
> C++ clone() on the base class pointer, I wind up here...
>
> FunctionBase *
> FunctionWrap::
> clone () const
> {
> #ifndef HAVE_OLD_PYTHON
> PyGILState_STATE state = PyGILState_Ensure ();
> object py_result;
>
> if (override clone = this->get_override("clone"))
> {
> try {
> // The Python class author overrode clone; do
> // whatever she says
> py_result = clone();
> }
> catch ( error_already_set & e ) {
> return 0;
> }
> }
> else
>
> (thanks to Dave for essentially writing this for me).
I wrote that?? I wouldn't want to take responsibility for that
try/catch block. It just discards all the information about what
happened.
> The problem is that the exception is thrown.
Which exception?
> The Python code looks like...
>
> class Linear ( FunctionBase ) :
> def __init__ ( self, other = None ) :
> if other : # copy constructor
> FunctionBase.__init__( self, other )
> else : # default constructor
> FunctionBase.__init__( self )
> print "default called"
> self.initialize ()
>
> def clone ( self ) :
> print "clone called"
> return Linear ()
>
> Where FunctionBase is the exposed C++ class. Both print states are
> executed when I call clone() from C++.
>
> Suggestions?
if (override clone = this->get_override("clone"))
{
// The Python class author overrode clone; do
// whatever she says
py_result = clone();
}
else
??
Or, at the very least,
if (override clone = this->get_override("clone"))
{
// The Python class author overrode clone; do
// whatever she says
try { py_result = clone(); }
catch ( error_already_set & e ) { PyErr_Print(); return 0 }
}
else
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
More information about the Cplusplus-sig
mailing list