38 if ( ! load || ! name )
42 PyObject* modules = PySys_GetObject( const_cast<char*>(
"modules") );
44 PyObject* keys = PyDict_Keys( modules );
45 Bool_t isModule = PySequence_Contains( keys, pyname );
55 std::ostringstream nsCode;
56 nsCode <<
"namespace " << name <<
" {\n";
59 PyObject* mod = PyDict_GetItemString( modules, const_cast<char*>(name) );
60 PyObject* dct = PyModule_GetDict( mod );
61 keys = PyDict_Keys( dct );
63 for (
int i = 0; i < PyList_GET_SIZE( keys ); ++i ) {
64 PyObject* key = PyList_GET_ITEM( keys, i );
71 if ( PyCallable_Check( attr ) && \
76 PyObject* func_code = PyObject_GetAttrString( attr, (
char*)
"func_code" );
78 func_code ? PyObject_GetAttrString( func_code, (
char*)
"co_varnames" ) :
NULL;
79 int nVars = var_names ? PyTuple_GET_SIZE( var_names ) : 0 ;
80 if ( nVars < 0 ) nVars = 0;
81 Py_XDECREF( var_names );
82 Py_XDECREF( func_code );
84 nsCode <<
" TPyReturn " << func_name <<
"(";
85 for (
int ivar = 0; ivar < nVars; ++ivar ) {
86 nsCode <<
"const TPyArg& a" << ivar;
87 if ( ivar != nVars-1 ) nsCode <<
", ";
90 nsCode <<
" std::vector<TPyArg> v; v.reserve(" << nVars <<
");\n";
93 for (
int ivar = 0; ivar < nVars; ++ ivar )
94 nsCode <<
" v.push_back(a" << ivar <<
");\n";
97 nsCode <<
" return TPyReturn(TPyArg::CallMethod((PyObject*)" << (
void*)attr <<
", v)); }\n";
118 std::string clName =
name;
119 std::string::size_type pos = clName.rfind(
'.' );
121 if ( pos == std::string::npos )
124 std::string mdName = clName.substr( 0, pos );
125 clName = clName.substr( pos+1, std::string::npos );
128 Bool_t useNS =
gROOT->GetListOfClasses()->FindObject( mdName.c_str() ) != 0;
136 PyObject* mod = PyImport_AddModule( const_cast< char* >( mdName.c_str() ) );
144 PyDict_GetItemString( PyModule_GetDict( mod ), const_cast< char* >( clName.c_str() ) );
145 Py_XINCREF( pyclass );
154 PyObject* attrs = PyObject_Dir( pyclass );
157 Py_DECREF( pyclass );
162 std::ostringstream proxyCode;
163 if ( useNS ) proxyCode <<
"namespace " << mdName <<
" { ";
164 proxyCode <<
"class " << clName <<
" {\nprivate:\n PyObject* fPyObject;\npublic:\n";
168 for (
int i = 0; i < PyList_GET_SIZE( attrs ); ++i ) {
169 PyObject* label = PyList_GET_ITEM( attrs, i );
174 if ( PyCallable_Check( attr ) ) {
178 PyObject* im_func = PyObject_GetAttrString( attr, (
char*)
"im_func" );
180 im_func ? PyObject_GetAttrString( im_func, (
char*)
"func_code" ) :
NULL;
182 func_code ? PyObject_GetAttrString( func_code, (
char*)
"co_varnames" ) :
NULL;
183 int nVars = var_names ? PyTuple_GET_SIZE( var_names ) - 1 : 0 ;
184 if ( nVars < 0 ) nVars = 0;
185 Py_XDECREF( var_names );
186 Py_XDECREF( func_code );
187 Py_XDECREF( im_func );
189 Bool_t isConstructor = mtName ==
"__init__";
190 Bool_t isDestructor = mtName ==
"__del__";
193 if ( isConstructor ) {
194 hasConstructor =
kTRUE;
195 proxyCode <<
" " << clName <<
"(";
196 }
else if ( isDestructor )
197 proxyCode <<
" ~" << clName <<
"(";
199 proxyCode <<
" TPyReturn " << mtName <<
"(";
200 for (
int ivar = 0; ivar < nVars; ++ivar ) {
201 proxyCode <<
"const TPyArg& a" << ivar;
202 if ( ivar != nVars-1 ) proxyCode <<
", ";
204 proxyCode <<
") {\n";
205 proxyCode <<
" std::vector<TPyArg> v; v.reserve(" << nVars+(isConstructor ? 0 : 1) <<
");\n";
208 if ( ! isConstructor )
209 proxyCode <<
" v.push_back(fPyObject);\n";
212 for (
int ivar = 0; ivar < nVars; ++ ivar )
213 proxyCode <<
" v.push_back(a" << ivar <<
");\n";
216 if ( ! isConstructor )
217 proxyCode <<
" return TPyReturn(TPyArg::CallMethod((PyObject*)" << (
void*)attr <<
", v))";
219 proxyCode <<
" TPyArg::CallConstructor(fPyObject, (PyObject*)" << (
void*)pyclass <<
", v)";
220 proxyCode <<
";\n }\n";
228 if ( ! hasConstructor )
229 proxyCode <<
" " << clName <<
"() {\n TPyArg::CallConstructor(fPyObject, (PyObject*)" << (
void*)pyclass <<
"); }\n";
233 if ( useNS ) proxyCode <<
" }";
237 Py_DECREF( pyclass );
240 if ( !
gInterpreter->LoadText( proxyCode.str().c_str() ) )
244 TClass* klass =
new TClass( useNS ? (mdName+
"::"+clName).c_str() : clName.c_str(), silent );
255 return GetClass( typeinfo.name(), load, silent );
263 return GetClass( typeinfo.name(), load );
#define PyROOT_PyUnicode_FromString
R__EXTERN Bool_t gDictLookupActive
#define PyROOT_PyUnicode_AsString
R__EXTERN PyObject * gBases
virtual TClass * GetClass(const char *name, Bool_t load)
The ROOT global object gROOT contains a list of all defined classes.
static void AddClass(TClass *cl)
static: Add a class to the list and map of classes.