[C++-sig] Trying to clone (again)

Paul F. Kunz Paul_Kunz at slac.stanford.edu
Tue Aug 2 21:50:54 CEST 2005


I'm trying to clone a Python object from C++.   The Python class
inherits from an exposed C++ base class.   To avoid the dangling
pointer problem, I keep a Python reference to the created Python
object like this...

from hippo import FunctionBase
# Define a class in Python that is derived from C++ base class
_clones = []
class Linear ( FunctionBase ) :
    def __init__ ( self ) :
        FunctionBase.__init__( self )
        self.initialize ()

    def clone ( self ) :
        _clones.append ( Linear () )
        return _clones[-1]

Then in the C++ wrapper class, I have

        
FunctionBase *
FunctionWrap::
clone () const  // overrides FunctionBase::clone()
{
#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 ) {
	PyErr_Print ();
	handle_exception ();
	PyGILState_Release ( state );
        return 0;
     }
  } else {

The Python implemented clone() member function is called but
I get the following error message on return ...

TypeError: No registered converter was able to extract a C++ reference to type boost::python::api::object from this Python object of type Linear

Any suggestions?




More information about the Cplusplus-sig mailing list