|
PLearn 0.1
|
#include <PythonObjectWrapper.h>
Static Public Member Functions | |
| static PyObject * | newPyObject (const Object *x) |
Definition at line 536 of file PythonObjectWrapper.h.
| PyObject * PLearn::ConvertToPyObject< Object * >::newPyObject | ( | const Object * | x | ) | [static] |
Definition at line 721 of file PythonObjectWrapper.cc.
References PLearn::addToWrappedObjectsSet(), PLearn::Object::classname(), PLearn::PythonObjectWrapper::initializePython(), PLearn::PythonObjectWrapper::m_pypl_classes, PLearn::PythonObjectWrapper::m_wrapped_objects, PLearn::ConvertToPyObject< T >::newPyObject(), PLERROR, PLearn::PPointable::ref(), and x.
{
// DBG_MODULE_LOG << "ENTER ConvertToPyObject<Object*>::newPyObject "
// << (void*)x << ' ' << (x?x->asString():"") << endl;
// void ptr becomes None
if(!x) return PythonObjectWrapper::newPyObject();
PythonGlobalInterpreterLock gil; // For thread-safety
static PythonEmbedder embedder;
PythonObjectWrapper::initializePython();
//see if this obj. is already wrapped
PythonObjectWrapper::wrapped_objects_t::iterator objit=
PythonObjectWrapper::m_wrapped_objects.find(x);
if(objit != PythonObjectWrapper::m_wrapped_objects.end())
{
PyObject* o= objit->second;
Py_INCREF(o);//new ref
return o;//return ptr to already created pyobj
}
// get ptr of object to wrap
PyObject* plobj= PyCObject_FromVoidPtr(const_cast<Object*>(x), NULL);
// try to find existing python class
string classname= x->classname();
PythonObjectWrapper::pypl_classes_t::iterator clit=
PythonObjectWrapper::m_pypl_classes.find(classname);
if(clit == PythonObjectWrapper::m_pypl_classes.end())
PLERROR("in ConvertToPyObject<Object*>::newPyObject : "
"cannot find python class %s",classname.c_str());
PyObject* the_pyclass= clit->second.pyclass;
//create the python object itself from the_pyclass
PyObject* args= PyTuple_New(0);
PyObject* params= PyDict_New();
PyDict_SetItemString(params, "_cptr", plobj);
Py_DECREF(plobj);
PyObject* the_obj= PyObject_Call(the_pyclass, args, params);
Py_DECREF(args);
Py_DECREF(params);
if(!the_obj)
{
if (PyErr_Occurred()) PyErr_Print();
PLERROR("in PythonObjectWrapper::newPyObject : "
"can't construct a WrappedPLearnObject.");
}
// augment refcount since python now 'points' to this obj.
x->ref();
PythonObjectWrapper::m_wrapped_objects[x]= the_obj;
//checkWrappedObjects(">>>>>>>>>> in newpyobj -> checkWrappedObjects"); // debug only
// perr << "newPyObject: " << (void*)x << " : " << (void*)the_obj << endl;
addToWrappedObjectsSet(the_obj);//Py_INCREF(the_obj);
//printWrappedObjects();
// DBG_MODULE_LOG << "EXIT ConvertToPyObject<Object*>::newPyObject "
// << (void*)x << ' ' << x->asString() << " => "
// << (void*) the_obj << ' ' << PythonObjectWrapper(the_obj) << endl;
return the_obj;
}

1.7.4