[C++-sig] python::object that takes ownership of a C++ instance

Francis Moreau francis_moreau at hotmail.com
Fri May 16 21:49:51 CEST 2008


I would like to know if there is the equivalent of manage_new_object 
return_policy but for an argument pass to Python? I have a Python class 
derived from a C++ base class:

class PyEventProcessor(CppEventProcessor):
  def processEvent(in_event):
    # do something

And then in C++ I want to call processEvent and transferring the ownership 
of the event to Python:

void CppEventProcessor_Wrapped::pyProcessEvent(const Event& in_event)
  // This following line is working well, but I want to transfert the 
ownership of the event instead of passing a reference on it.
  return python::call_method<void>(m_pSelf, "processEvent", 

  // I tried something like this but it's not working.
  Event* pNewEvent = in_event.clone();
  python::object obj(&pNewEvent); // Event is a type exposed in Python

I would like to know what is the best way to do something like this, is it 
by using a auto_ptr, or a from_python convertor, or something else? Also, I 
have a few types richer than Event, and exposed to Python too, and I want to 
be able to use them directly in Python, e.g.:

class PyEventProcessor(CppEventProcessor):
  def processEvent(in_event):
    if in_event is ConnectionEvent:
      srcAddr = in_event.getSourceAddr()


More information about the Cplusplus-sig mailing list