forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TPyClassGenerator.cxx
286 lines (229 loc) · 9.93 KB
/
TPyClassGenerator.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// @(#)root/pyroot:$Id$
// Author: Wim Lavrijsen, May 2004
// Bindings
#include "PyROOT.h"
#include "PyStrings.h"
#include "TPyClassGenerator.h"
#include "TPyReturn.h"
#include "Utility.h"
// ROOT
#include "TClass.h"
#include "TInterpreter.h"
#include "TROOT.h"
// Standard
#include <sstream>
#include <string>
#include <typeinfo>
//- public members -----------------------------------------------------------
TClass* TPyClassGenerator::GetClass( const char* name, Bool_t load )
{
// Just forward.
return GetClass( name, load, kFALSE );
}
//- public members -----------------------------------------------------------
TClass* TPyClassGenerator::GetClass( const char* name, Bool_t load, Bool_t silent )
{
// Class generator to make python classes available to Cling
// called if all other class generators failed, attempt to build from python class
if ( PyROOT::gDictLookupActive == kTRUE )
return 0; // call originated from python
if ( ! load || ! name )
return 0;
PyROOT::PyGILRAII thePyGILRAII;
// first, check whether the name is of a module
PyObject* modules = PySys_GetObject( const_cast<char*>("modules") );
PyObject* pyname = PyROOT_PyUnicode_FromString( name );
PyObject* keys = PyDict_Keys( modules );
Bool_t isModule = PySequence_Contains( keys, pyname );
Py_DECREF( keys );
Py_DECREF( pyname );
if ( isModule ) {
// the normal TClass::GetClass mechanism doesn't allow direct returns, so
// do our own check
TClass* cl = (TClass*)gROOT->GetListOfClasses()->FindObject( name );
if ( cl ) return cl;
std::ostringstream nsCode;
nsCode << "namespace " << name << " {\n";
// add all free functions
PyObject* mod = PyDict_GetItemString( modules, const_cast<char*>(name) );
PyObject* dct = PyModule_GetDict( mod );
keys = PyDict_Keys( dct );
for ( int i = 0; i < PyList_GET_SIZE( keys ); ++i ) {
PyObject* key = PyList_GET_ITEM( keys, i );
Py_INCREF( key );
PyObject* attr = PyDict_GetItem( dct, key );
Py_INCREF( attr );
// TODO: refactor the code below with the class method code
if ( PyCallable_Check( attr ) && \
! (PyClass_Check( attr ) || PyObject_HasAttr( attr, PyROOT::PyStrings::gBases )) ) {
std::string func_name = PyROOT_PyUnicode_AsString( key );
// figure out number of variables required
PyObject* func_code = PyObject_GetAttrString( attr, (char*)"func_code" );
PyObject* var_names =
func_code ? PyObject_GetAttrString( func_code, (char*)"co_varnames" ) : NULL;
int nVars = var_names ? PyTuple_GET_SIZE( var_names ) : 0 /* TODO: probably large number, all default? */;
if ( nVars < 0 ) nVars = 0;
Py_XDECREF( var_names );
Py_XDECREF( func_code );
nsCode << " TPyReturn " << func_name << "(";
for ( int ivar = 0; ivar < nVars; ++ivar ) {
nsCode << "const TPyArg& a" << ivar;
if ( ivar != nVars-1 ) nsCode << ", ";
}
nsCode << ") {\n";
nsCode << " std::vector<TPyArg> v; v.reserve(" << nVars << ");\n";
// add the variables
for ( int ivar = 0; ivar < nVars; ++ ivar )
nsCode << " v.push_back(a" << ivar << ");\n";
// call dispatch (method or class pointer hard-wired)
nsCode << " return TPyReturn(TPyArg::CallMethod((PyObject*)" << (void*)attr << ", v)); }\n";
}
Py_DECREF( attr );
Py_DECREF( key );
}
Py_DECREF( keys );
nsCode << " }";
if ( gInterpreter->LoadText( nsCode.str().c_str() ) ) {
TClass* klass = new TClass( name, silent );
TClass::AddClass( klass );
return klass;
}
return nullptr;
}
// determine module and class name part
std::string clName = name;
std::string::size_type pos = clName.rfind( '.' );
if ( pos == std::string::npos )
return 0; // this isn't a python style class
std::string mdName = clName.substr( 0, pos );
clName = clName.substr( pos+1, std::string::npos );
// create class in namespace, if it exists (no load, silent)
Bool_t useNS = gROOT->GetListOfClasses()->FindObject( mdName.c_str() ) != 0;
if ( ! useNS ) {
// the class itself may exist if we're using the global scope
TClass* cl = (TClass*)gROOT->GetListOfClasses()->FindObject( clName.c_str() );
if ( cl ) return cl;
}
// locate and get class
PyObject* mod = PyImport_AddModule( const_cast< char* >( mdName.c_str() ) );
if ( ! mod ) {
PyErr_Clear();
return 0; // module apparently disappeared
}
Py_INCREF( mod );
PyObject* pyclass =
PyDict_GetItemString( PyModule_GetDict( mod ), const_cast< char* >( clName.c_str() ) );
Py_XINCREF( pyclass );
Py_DECREF( mod );
if ( ! pyclass ) {
PyErr_Clear(); // the class is no longer available?!
return 0;
}
// get a listing of all python-side members
PyObject* attrs = PyObject_Dir( pyclass );
if ( ! attrs ) {
PyErr_Clear();
Py_DECREF( pyclass );
return 0;
}
// pre-amble Cling proxy class
std::ostringstream proxyCode;
if ( useNS ) proxyCode << "namespace " << mdName << " { ";
proxyCode << "class " << clName << " {\nprivate:\n PyObject* fPyObject;\npublic:\n";
// loop over and add member functions
Bool_t hasConstructor = kFALSE, hasDestructor = kFALSE;
for ( int i = 0; i < PyList_GET_SIZE( attrs ); ++i ) {
PyObject* label = PyList_GET_ITEM( attrs, i );
Py_INCREF( label );
PyObject* attr = PyObject_GetAttr( pyclass, label );
// collect only member functions (i.e. callable elements in __dict__)
if ( PyCallable_Check( attr ) ) {
std::string mtName = PyROOT_PyUnicode_AsString( label );
if ( mtName == "__del__" ) {
hasDestructor = kTRUE;
proxyCode << " ~" << clName << "() { TPyArg::CallDestructor(fPyObject); }\n";
continue;
}
Bool_t isConstructor = mtName == "__init__";
if ( !isConstructor && mtName.find("__", 0, 2) == 0 )
continue; // skip all other python special funcs
// figure out number of variables required
#if PY_VERSION_HEX < 0x03000000
PyObject* im_func = PyObject_GetAttrString( attr, (char*)"im_func" );
PyObject* func_code =
im_func ? PyObject_GetAttrString( im_func, (char*)"func_code" ) : NULL;
#else
PyObject* func_code = PyObject_GetAttrString( attr, "__code__" );
#endif
PyObject* var_names =
func_code ? PyObject_GetAttrString( func_code, (char*)"co_varnames" ) : NULL;
if (PyErr_Occurred()) PyErr_Clear(); // happens for slots; default to 0 arguments
int nVars = var_names ? PyTuple_GET_SIZE( var_names ) - 1 /* self */ : 0 /* TODO: probably large number, all default? */;
if ( nVars < 0 ) nVars = 0;
Py_XDECREF( var_names );
Py_XDECREF( func_code );
#if PY_VERSION_HEX < 0x03000000
Py_XDECREF( im_func );
#endif
// method declaration as appropriate
if ( isConstructor ) {
hasConstructor = kTRUE;
proxyCode << " " << clName << "(";
} else // normal method
proxyCode << " TPyReturn " << mtName << "(";
for ( int ivar = 0; ivar < nVars; ++ivar ) {
proxyCode << "const TPyArg& a" << ivar;
if ( ivar != nVars-1 ) proxyCode << ", ";
}
proxyCode << ") {\n";
proxyCode << " std::vector<TPyArg> v; v.reserve(" << nVars+(isConstructor ? 0 : 1) << ");\n";
// add the 'self' argument as appropriate
if ( ! isConstructor )
proxyCode << " v.push_back(fPyObject);\n";
// then add the remaining variables
for ( int ivar = 0; ivar < nVars; ++ ivar )
proxyCode << " v.push_back(a" << ivar << ");\n";
// call dispatch (method or class pointer hard-wired)
if ( ! isConstructor )
proxyCode << " return TPyReturn(TPyArg::CallMethod((PyObject*)" << (void*)attr << ", v))";
else
proxyCode << " TPyArg::CallConstructor(fPyObject, (PyObject*)" << (void*)pyclass << ", v)";
proxyCode << ";\n }\n";
}
// no decref of attr for now (b/c of hard-wired ptr); need cleanup somehow
Py_DECREF( label );
}
// special case if no constructor or destructor
if ( ! hasConstructor )
proxyCode << " " << clName << "() {\n TPyArg::CallConstructor(fPyObject, (PyObject*)" << (void*)pyclass << "); }\n";
if ( ! hasDestructor )
proxyCode << " ~" << clName << "() { TPyArg::CallDestructor(fPyObject); }\n";
// for now, don't allow copying (ref-counting wouldn't work as expected anyway)
proxyCode << " " << clName << "(const " << clName << "&) = delete;\n";
proxyCode << " " << clName << "& operator=(const " << clName << "&) = delete;\n";
// closing and building of Cling proxy class
proxyCode << "};";
if ( useNS ) proxyCode << " }";
Py_DECREF( attrs );
// done with pyclass, decref here, assuming module is kept
Py_DECREF( pyclass );
// body compilation
if ( ! gInterpreter->LoadText( proxyCode.str().c_str() ) )
return nullptr;
// done, let ROOT manage the new class
TClass* klass = new TClass( useNS ? (mdName+"::"+clName).c_str() : clName.c_str(), silent );
TClass::AddClass( klass );
return klass;
}
////////////////////////////////////////////////////////////////////////////////
/// Just forward; based on type name only.
TClass* TPyClassGenerator::GetClass( const std::type_info& typeinfo, Bool_t load, Bool_t silent )
{
return GetClass( typeinfo.name(), load, silent );
}
////////////////////////////////////////////////////////////////////////////////
/// Just forward; based on type name only
TClass* TPyClassGenerator::GetClass( const std::type_info& typeinfo, Bool_t load )
{
return GetClass( typeinfo.name(), load );
}