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

Paul F. Kunz Paul_Kunz at slac.stanford.edu
Thu Jul 21 21:01:29 CEST 2005

   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 *
clone () const
  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;

(thanks to Dave for essentially writing this for me).

The problem is that the exception is thrown.   The Python code looks

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++.


More information about the Cplusplus-sig mailing list