38 if ( ! load || !
name )
44 PyObject* modules = PySys_GetObject(
const_cast<char*
>(
"modules") );
46 PyObject* keys = PyDict_Keys( modules );
57 std::ostringstream nsCode;
58 nsCode <<
"namespace " <<
name <<
" {\n";
61 PyObject* mod = PyDict_GetItemString( modules,
const_cast<char*
>(
name) );
62 PyObject* dct = PyModule_GetDict( mod );
63 keys = PyDict_Keys( dct );
65 for (
int i = 0; i < PyList_GET_SIZE( keys ); ++i ) {
66 PyObject* key = PyList_GET_ITEM( keys, i );
69 PyObject* attr = PyDict_GetItem( dct, key );
73 if ( PyCallable_Check( attr ) && \
78 PyObject* func_code = PyObject_GetAttrString( attr, (
char*)
"func_code" );
80 func_code ? PyObject_GetAttrString( func_code, (
char*)
"co_varnames" ) : NULL;
81 int nVars = var_names ? PyTuple_GET_SIZE( var_names ) : 0 ;
82 if ( nVars < 0 ) nVars = 0;
83 Py_XDECREF( var_names );
84 Py_XDECREF( func_code );
86 nsCode <<
" TPyReturn " << func_name <<
"(";
87 for (
int ivar = 0; ivar < nVars; ++ivar ) {
88 nsCode <<
"const TPyArg& a" << ivar;
89 if ( ivar != nVars-1 ) nsCode <<
", ";
92 nsCode <<
" std::vector<TPyArg> v; v.reserve(" << nVars <<
");\n";
95 for (
int ivar = 0; ivar < nVars; ++ ivar )
96 nsCode <<
" v.push_back(a" << ivar <<
");\n";
99 nsCode <<
" return TPyReturn(TPyArg::CallMethod((PyObject*)" << (
void*)attr <<
", v)); }\n";
120 std::string clName =
name;
121 std::string::size_type pos = clName.rfind(
'.' );
123 if ( pos == std::string::npos )
126 std::string mdName = clName.substr( 0, pos );
127 clName = clName.substr( pos+1, std::string::npos );
130 Bool_t useNS =
gROOT->GetListOfClasses()->FindObject( mdName.c_str() ) != 0;
138 PyObject* mod = PyImport_AddModule(
const_cast< char*
>( mdName.c_str() ) );
146 PyDict_GetItemString( PyModule_GetDict( mod ),
const_cast< char*
>( clName.c_str() ) );
147 Py_XINCREF( pyclass );
156 PyObject* attrs = PyObject_Dir( pyclass );
159 Py_DECREF( pyclass );
164 std::ostringstream proxyCode;
165 if ( useNS ) proxyCode <<
"namespace " << mdName <<
" { ";
166 proxyCode <<
"class " << clName <<
" {\nprivate:\n PyObject* fPyObject;\npublic:\n";
170 for (
int i = 0; i < PyList_GET_SIZE( attrs ); ++i ) {
171 PyObject* label = PyList_GET_ITEM( attrs, i );
173 PyObject* attr = PyObject_GetAttr( pyclass, label );
176 if ( PyCallable_Check( attr ) ) {
179 if ( mtName ==
"__del__" ) {
180 hasDestructor =
kTRUE;
181 proxyCode <<
" ~" << clName <<
"() { TPyArg::CallDestructor(fPyObject); }\n";
185 Bool_t isConstructor = mtName ==
"__init__";
186 if ( !isConstructor && mtName.find(
"__", 0, 2) == 0 )
190#if PY_VERSION_HEX < 0x03000000
191 PyObject* im_func = PyObject_GetAttrString( attr, (
char*)
"im_func" );
193 im_func ? PyObject_GetAttrString( im_func, (
char*)
"func_code" ) : NULL;
195 PyObject* func_code = PyObject_GetAttrString( attr,
"__code__" );
198 func_code ? PyObject_GetAttrString( func_code, (
char*)
"co_varnames" ) : NULL;
199 if (PyErr_Occurred()) PyErr_Clear();
201 int nVars = var_names ? PyTuple_GET_SIZE( var_names ) - 1 : 0 ;
202 if ( nVars < 0 ) nVars = 0;
203 Py_XDECREF( var_names );
204 Py_XDECREF( func_code );
205#if PY_VERSION_HEX < 0x03000000
206 Py_XDECREF( im_func );
210 if ( isConstructor ) {
211 hasConstructor =
kTRUE;
212 proxyCode <<
" " << clName <<
"(";
214 proxyCode <<
" TPyReturn " << mtName <<
"(";
215 for (
int ivar = 0; ivar < nVars; ++ivar ) {
216 proxyCode <<
"const TPyArg& a" << ivar;
217 if ( ivar != nVars-1 ) proxyCode <<
", ";
219 proxyCode <<
") {\n";
220 proxyCode <<
" std::vector<TPyArg> v; v.reserve(" << nVars+(isConstructor ? 0 : 1) <<
");\n";
223 if ( ! isConstructor )
224 proxyCode <<
" v.push_back(fPyObject);\n";
227 for (
int ivar = 0; ivar < nVars; ++ ivar )
228 proxyCode <<
" v.push_back(a" << ivar <<
");\n";
231 if ( ! isConstructor )
232 proxyCode <<
" return TPyReturn(TPyArg::CallMethod((PyObject*)" << (
void*)attr <<
", v))";
234 proxyCode <<
" TPyArg::CallConstructor(fPyObject, (PyObject*)" << (
void*)pyclass <<
", v)";
235 proxyCode <<
";\n }\n";
243 if ( ! hasConstructor )
244 proxyCode <<
" " << clName <<
"() {\n TPyArg::CallConstructor(fPyObject, (PyObject*)" << (
void*)pyclass <<
"); }\n";
246 if ( ! hasDestructor )
247 proxyCode <<
" ~" << clName <<
"() { TPyArg::CallDestructor(fPyObject); }\n";
250 proxyCode <<
" " << clName <<
"(const " << clName <<
"&) = delete;\n";
251 proxyCode <<
" " << clName <<
"& operator=(const " << clName <<
"&) = delete;\n";
255 if ( useNS ) proxyCode <<
" }";
259 Py_DECREF( pyclass );
262 if ( !
gInterpreter->LoadText( proxyCode.str().c_str() ) )
266 TClass* klass =
new TClass( useNS ? (mdName+
"::"+clName).c_str() : clName.c_str(), silent );
277 return GetClass( typeinfo.name(), load, silent );
285 return GetClass( typeinfo.name(), load );
#define PyROOT_PyUnicode_AsString
#define PyROOT_PyUnicode_FromString
TClass instances represent classes, structs and namespaces in the ROOT type system.
static void AddClass(TClass *cl)
static: Add a class to the list and map of classes.
virtual TClass * GetClass(const char *name, Bool_t load)
R__EXTERN PyObject * gBases
R__EXTERN Bool_t gDictLookupActive