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

Bjorn Pettersen BPettersen at NAREX.com
Thu Oct 31 02:07:34 CET 2002


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

-- bjorn




More information about the Cplusplus-sig mailing list