[C++-sig] How do I wrap a C++ object to a PyObject*

David Abrahams dave at boost-consulting.com
Thu Oct 31 05:17:18 CET 2002


"Bjorn Pettersen" <BPettersen at NAREX.com> writes:

> I'm trying to embed Python in my C++ application, and it looks pretty
> easy to convert a PyObject* returned from PyRun_String using the
> 'extract' functionality. How do I go the other way? I.e. I want to set a
> variable in __main__ to an instance of my C++ class (or pass them as
> arguments to PyObject_CallFunctionObjArgs). With SIP there was a
> function called sipMapCppToSelf used like this:
> 
> 	void set(const std::string& name, const LegalStatusCode& value)
> {
> 	    PyObject* key = PyString_FromString(name.c_str());
> 	    PyObject* val = sipMapCppToSelf(&value,
> sipClass_LegalStatusCode);
> 	    PyDict_SetItem(mainmodule, key, val);
> 	    Py_DECREF(key);
> 	    Py_DECREF(val);
> 	}
> 
> I've looked through the documentation but haven't found anything
> obvious...

Let's see. You have a wrapped C++ class somewhere?
If so, you can write in __main__:

  import some_boost_python_module
  x = some_boost_python_module.some_class(some_arg)

But this is trivially explained in the docs, so you must mean
something else...

-- 
                    David Abrahams
dave at boost-consulting.com * http://www.boost-consulting.com





More information about the Cplusplus-sig mailing list