58 typedef std::map< Cppyy::TCppScope_t, PyObject* > PyClassMap_t;
59 PyClassMap_t gPyClasses;
65 Py_XINCREF( pybases );
67 pybases = PyTuple_New( 1 );
72 PyObject* pymetabases = PyTuple_New( PyTuple_GET_SIZE( pybases ) );
73 for (
int i = 0; i < PyTuple_GET_SIZE( pybases ); ++i ) {
76 PyTuple_SET_ITEM( pymetabases, i, btype );
79 PyObject* args = Py_BuildValue( (
char*)
"sO{}", (name+
"_meta").c_str(), pymetabases );
80 Py_DECREF( pymetabases );
90 args = Py_BuildValue( (
char*)
"sO{}",
Cppyy::GetName(name).c_str(), pybases );
91 PyObject* pyclass = ((PyTypeObject*)pymeta)->tp_new( (PyTypeObject*)pymeta, args,
NULL );
100 inline void AddPropertyToClass1(
104 PyObject_SetAttrString( pyclass,
105 const_cast< char* >( property->
GetName().c_str() ), (
PyObject*)property );
110 const_cast< char* >( property->
GetName().c_str() ), (
PyObject*)property );
114 void AddPropertyToClass(
PyObject* pyclass,
119 Py_DECREF( property );
122 void AddPropertyToClass(
PyObject* pyclass,
127 AddPropertyToClass1( pyclass, property,
kTRUE );
128 Py_DECREF( property );
140 inline void AddToGlobalScope(
145 PyModule_AddObject(
gRootModule, const_cast< char* >( label ),
149 std::set< std::string > gSTLTypes, gSTLExceptions;
150 struct InitSTLTypes_t {
154 const std::string nss =
"std::";
156 const char* stlTypes[] = {
"complex",
"exception",
157 "deque",
"list",
"queue",
"stack",
"vector",
158 "map",
"multimap",
"set",
"multiset" };
159 for (
int i = 0; i < int(
sizeof(stlTypes)/
sizeof(stlTypes[0])); ++i ) {
160 gSTLTypes.insert( stlTypes[ i ] );
161 gSTLTypes.insert( nss + stlTypes[ i ] );
164 const char* stlExceptions[] = {
"logic_error",
"domain_error",
165 "invalid_argument",
"length_error",
"out_of_range",
"runtime_error",
166 "range_error",
"overflow_error",
"underflow_error" };
167 for (
int i = 0; i < int(
sizeof(stlExceptions)/
sizeof(stlExceptions[0])); ++i ) {
168 gSTLExceptions.insert( stlExceptions[ i ] );
169 gSTLExceptions.insert( nss + stlExceptions[ i ] );
174 Bool_t LoadDictionaryForSTLType(
const std::string& tname,
void* )
178 std::string sub = tname.substr( 0, tname.find(
"<" ) );
179 if ( gSTLTypes.find( sub ) != gSTLTypes.end() ) {
182 if ( sub.substr( 0, 5 ) ==
"std::" )
183 sub = sub.substr( 5, std::string::npos );
186 gROOT->ProcessLine( (std::string(
"#include <" ) + sub +
">").c_str() );
189 gSTLTypes.erase( gSTLTypes.find( sub ) );
190 gSTLTypes.erase( gSTLTypes.find(
"std::" + sub ) );
194 }
else if ( gSTLExceptions.find( sub ) != gSTLExceptions.end() ) {
200 gROOT->ProcessLine(
"#include <stdexcept>" );
201 gSTLExceptions.clear();
204 std::set< std::string >::iterator excpos = gSTLTypes.find(
"exception" );
205 if ( excpos != gSTLTypes.end() ) {
206 gSTLTypes.erase( excpos );
207 gSTLTypes.erase( gSTLTypes.find(
"std::exception" ) );
225 PyEval_InitThreads();
229 gROOT->GetListOfCleanups()->Add( &m );
247 typedef std::vector< PyCallable* > Callables_t;
248 typedef std::map< std::string, Callables_t > CallableCache_t;
249 CallableCache_t cache;
252 getattrofunc oldgetattro =
Py_TYPE(pyclass)->tp_getattro;
253 Py_TYPE(pyclass)->tp_getattro = PyType_Type.tp_getattro;
274 if ( mtName[0] ==
'~' )
281 if ( mtName ==
"__call__" || mtName ==
"__getitem__" ) {
283 if ( qual_return.find(
"const", 0, 5 ) == std::string::npos ) {
285 if ( ! cpd.empty() && cpd[ cpd.size() - 1 ] ==
'&' ) {
286 setupSetItem =
kTRUE;
295 std::string tmplName =
"";
296 if ( ! (isNamespace || isStatic || isConstructor) && mtName[mtName.size()-1] ==
'>' ) {
297 tmplName = mtName.substr( 0, mtName.find(
'<') );
300 PyObject* attr = PyObject_GetAttrString( pyclass, const_cast< char* >( tmplName.c_str() ) );
305 PyObject_SetAttrString(
306 pyclass, const_cast< char* >( tmplName.c_str() ), (
PyObject*)pytmpl );
323 mtName =
"_" + clName +
"__" + mtName;
331 else if ( isNamespace )
333 else if ( isConstructor ) {
336 hasConstructor =
kTRUE;
341 Callables_t& md = (*(cache.insert(
342 std::make_pair( mtName, Callables_t() ) ).first)).second;
343 md.push_back( pycall );
346 if ( setupSetItem ) {
347 Callables_t& setitem = (*(cache.insert(
348 std::make_pair( std::string(
"__setitem__" ), Callables_t() ) ).first)).second;
353 if ( ! tmplName.empty() ) {
354 PyObject* attr = PyObject_GetAttrString( pyclass, const_cast< char* >( tmplName.c_str() ) );
361 if ( ! isNamespace && ! hasConstructor )
365 for ( CallableCache_t::iterator imd = cache.begin(); imd != cache.end(); ++imd ) {
369 PyObject* attr = PyObject_GetAttrString( pyclass, const_cast< char* >( imd->first.c_str() ) );
372 for ( Callables_t::iterator cit = imd->second.begin(); cit != imd->second.end(); ++cit )
375 if ( ! attr ) PyErr_Clear();
378 PyObject_SetAttrString(
379 pyclass, const_cast< char* >( method->
GetName().c_str() ), (
PyObject*)method );
389 TIter ienum( enums );
414 PyObject* eset = PyObject_GetAttrString( pyclass,
426 AddPropertyToClass( pyclass, scope, idata );
432 AddPropertyToClass( pyclass, scope, idata );
436 Py_TYPE(pyclass)->tp_getattro = oldgetattro;
450 std::vector< std::string > uqb;
451 uqb.reserve( nbases );
453 for (
size_t ibase = 0; ibase < nbases; ++ibase ) {
455 if ( std::find( uqb.begin(), uqb.end(),
name ) == uqb.end() ) {
456 uqb.push_back( name );
463 PyObject* pybases = PyTuple_New( nbases ? nbases : 1 );
470 PyTuple_SET_ITEM( pybases, 0, (
PyObject*)(
void*)&ObjectProxy_Type );
472 for ( std::vector< std::string >::size_type ibase = 0; ibase < nbases; ++ibase ) {
475 Py_DECREF( pybases );
479 PyTuple_SET_ITEM( pybases, ibase, pyclass );
485 PyObject* newpybases = PyTuple_New( nbases + 1 );
486 Py_INCREF( (
PyObject*)(
void*)&ObjectProxy_Type );
487 PyTuple_SET_ITEM( newpybases, 0, (
PyObject*)(
void*)&ObjectProxy_Type );
488 for (
int ibase = 0; ibase < (int)nbases; ++ibase ) {
489 PyObject* pyclass = PyTuple_GET_ITEM( pybases, ibase );
490 Py_INCREF( pyclass );
491 PyTuple_SET_ITEM( newpybases, ibase + 1, pyclass );
493 Py_DECREF( pybases );
494 pybases = newpybases;
506 PyClassMap_t::iterator pci = gPyClasses.find( scope );
507 if ( pci != gPyClasses.end() ) {
508 PyObject* pyclass = PyWeakref_GetObject( pci->second );
510 Py_INCREF( pyclass );
536 if ( PyErr_Occurred() )
547 if ( scope_name.empty() || scope_name ==
"std" ) {
549 PyObject* mods = PyImport_GetModuleDict();
550 PyObject*
gbl = PyDict_GetItemString( mods,
"cppyy.gbl" );
552 if ( scope_name.empty() ) {
556 return PyObject_GetAttrString( gbl,
"std" );
558 PyErr_SetString( PyExc_SystemError,
"could not locate global namespace" );
563 Bool_t force = parent != 0;
566 std::string name = scope_name;
569 std::string scName =
"";
574 PyErr_Format( PyExc_SystemError,
"given scope has no name for %s", name.c_str() );
580 Py_DECREF( pyparent );
581 if ( PyErr_Occurred() )
589 const std::string& lookup = parent ? (scName+
"::"+
name) : name;
595 if ( LoadDictionaryForSTLType( name, (
void*)klass ) ) {
604 PyObject* pytemplate = PyObject_CallFunction(
605 pytcl, const_cast< char* >(
"s" ), const_cast< char* >( lookup.c_str() ) );
609 PyObject_SetAttrString( parent ? parent :
gRootModule, (
char*)name.c_str(), pytemplate );
612 Py_XDECREF( parent );
617 if ( ! parent && scope_name.find(
"ROOT::" ) == std::string::npos ) {
628 PyErr_Format( PyExc_TypeError,
"requested class \'%s\' does not exist", lookup.c_str() );
629 Py_XDECREF( parent );
636 if ( parent ) PyObject_SetAttrString( parent, (
char*)scope_name.c_str(), pyscope );
641 std::string::size_type last = 0;
645 for ( std::string::size_type pos = 0; pos < name.size(); ++pos ) {
646 std::string::value_type
c = name[ pos ];
655 else if ( tpl_open == 0 &&\
656 c ==
':' && pos+1 < name.size() && name[ pos+1 ] ==
':' ) {
658 const std::string& part = name.substr( last, pos-last );
660 PyObject* next = PyObject_GetAttrString(
661 parent ? parent :
gRootModule, const_cast< char* >( part.c_str() ) );
667 Py_XDECREF( parent );
684 std::string unscoped = scope_name.substr( last, std::string::npos );
685 return PyObject_GetAttrString( parent, unscoped.c_str() );
700 PyObject* pyclass = force ? 0 : PyObject_GetAttr( parent, pyactual );
711 if ( pybases != 0 ) {
713 pyclass = CreateNewROOTPythonClass( actual, pybases );
714 Py_DECREF( pybases );
718 if ( pyclass != 0 ) {
721 Py_DECREF( pyclass );
724 PyObject_SetAttr( parent, pyactual, pyclass );
730 if ( pyclass && name != actual )
731 PyObject_SetAttrString( parent, const_cast< char* >( name.c_str() ), pyclass );
733 if ( pyclass && ! bClassFound ) {
735 gPyClasses[ klass ] = PyWeakref_NewRef( pyclass,
NULL );
763 Py_DECREF( pyactual );
767 if ( ! bClassFound ) {
769 Py_XDECREF( pyclass );
775 if ( pyclass && actual !=
"ROOT" ) {
777 std::string pyfullname = lookup;
778 std::string::size_type pos = pyfullname.find(
"::" );
779 while ( pos != std::string::npos ) {
780 pyfullname = pyfullname.replace( pos, 2,
"." );
781 pos = pyfullname.find(
"::", pos );
783 PyObject* modules = PySys_GetObject( const_cast<char*>(
"modules") );
784 if ( modules && PyDict_Check( modules) ) {
785 PyDict_SetItemString( modules,
786 const_cast<char*>((
"ROOT."+pyfullname).c_str()), pyclass );
801 if ( PyErr_Occurred() )
817 const std::vector< Cppyy::TCppMethod_t >& methods =
819 if ( ! methods.empty() ) {
820 std::vector< PyCallable* > overloads;
821 for (
auto method : methods )
834 PyErr_Format( PyExc_LookupError,
"no such global: %s", name.c_str() );
844 PyErr_SetString( PyExc_TypeError,
"attempt to bind ROOT object w/o class" );
856 (
ObjectProxy*)((PyTypeObject*)pyclass)->tp_new( (PyTypeObject*)pyclass, args,
NULL );
858 Py_DECREF( pyclass );
881 PyErr_SetString( PyExc_TypeError,
"attempt to bind ROOT object w/o class" );
910 if ( clActual && klass != clActual ) {
912 clActual, klass, address, -1 ,
true );
913 if ( offset != -1 ) {
914 address = (
void*)((
Long_t)address + offset);
921 Bool_t ignore_pin = std::find(
924 if ( ! ignore_pin ) {
926 if ( klass == std::get<0>(*it) ||
Cppyy::IsSubtype( klass, std::get<0>(*it) ) )
927 klass = std::get<1>(*it);
959 if ( ! gbl || strcmp(gbl->
GetName(),
"") == 0 ) {
960 Py_INCREF( Py_None );
971 PyErr_SetString( PyExc_NotImplementedError,
972 "larger than 1D arrays of objects not supported" );
987 (
unsigned long)gbl->
GetAddress() != (
unsigned long)-1 &&
989 return PyInt_FromLong( (
long)*((
int*)gbl->
GetAddress()) );
virtual const char * GetName() const
Returns name of object.
std::string GetName(const std::string &scope_name)
#define PyROOT_PyUnicode_FromString
static PyObject * RetrieveObject(TObject *object, Cppyy::TCppType_t klass)
lookup <object>, return old proxy if tracked
The TEnum class implements the enum type.
std::string GetScopedFinalName(TCppType_t type)
virtual Int_t GetMaxIndex(Int_t dim) const
Return maximum index for array dimension "dim".
R__EXTERN Int_t gErrorIgnoreLevel
virtual const char * GetFullTypeName() const
Get full type description of global variable, e,g.: "class TDirectory*".
void AddOverload(MethodProxy *mp)
Store overloads of this templated method.
Bool_t IsNamespace(TCppScope_t scope)
virtual PyCallable * Clone()=0
static Bool_t RegisterObject(ObjectProxy *pyobj, TObject *object)
start tracking <object> proxied by <pyobj>
All ROOT classes may have RTTI (run time type identification) support added.
TList * GetListOfEnums(Bool_t load=kTRUE)
Return a list containing the TEnums of a class.
ptrdiff_t GetBaseOffset(TCppType_t derived, TCppType_t base, TCppObject_t address, int direction, bool rerror=false)
Bool_t IsPublicMethod(TCppMethod_t method)
TCppIndex_t GetNumBases(TCppType_t type)
std::vector< TCppMethod_t > GetMethodsFromName(TCppScope_t scope, const std::string &name)
MethodProxy * MethodProxy_New(const std::string &name, std::vector< PyCallable * > &methods)
R__EXTERN PyObject * gTemplate
std::string GetFinalName(TCppType_t type)
virtual void * GetAddress() const
Return address of global.
Bool_t PyRootType_Check(T *object)
ptrdiff_t GetDatamemberOffset(TCppScope_t scope, TCppIndex_t idata)
struct staticInitHelper gbl
TemplateProxy * TemplateProxy_New(const std::string &name, PyObject *pyclass)
Bool_t IsEnumData(TCppScope_t scope, TCppIndex_t idata)
std::string GetDatamemberType(TCppScope_t scope, TCppIndex_t idata)
PyObject * BindCppObjectArray(Cppyy::TCppObject_t address, Cppyy::TCppType_t klass, Int_t size)
TODO: this function exists for symmetry; need to figure out if it's useful.
const TSeqCollection * GetConstants() const
std::string GetBaseName(TCppType_t type, TCppIndex_t ibase)
Sequenceable collection abstract base class.
std::string ResolveName(const std::string &cppitem_name)
std::vector< Cppyy::TCppType_t > gIgnorePinnings
std::string MapOperatorName(const std::string &name, Bool_t bTakesParames)
Map the given C++ operator name on the python equivalent.
R__EXTERN PyObject * gRootModule
PyObject * GetCppGlobal(const std::string &name)
try named global variable/enum (first ROOT, then Cling: sync is too slow)
R__EXTERN PyObject * gModule
TCppIndex_t GetNumDatamembers(TCppScope_t scope)
#define PyROOT_PyUnicode_AsString
PropertyProxy * PropertyProxy_NewConstant(Cppyy::TCppScope_t scope, const std::string &name, void *address)
TCppMethod_t GetMethod(TCppScope_t scope, TCppIndex_t imeth)
Bool_t IsConstructor(TCppMethod_t method)
TCppType_t GetActualClass(TCppType_t klass, TCppObject_t obj)
PyTypeObject PyRootType_Type
PyTypeObject ObjectProxy_Type
Bool_t IsPublicData(TCppScope_t scope, TCppIndex_t idata)
std::string GetMethodName(TCppMethod_t)
R__EXTERN PyObject * gROOTns
R__EXTERN TSystem * gSystem
void * GetAddress() const override
Return address of global.
std::vector< std::pair< Cppyy::TCppType_t, Cppyy::TCppType_t > > gPinnedTypes
PyObject * BindCppGlobal(TGlobal *)
gbl == 0 means global does not exist (rather than gbl is NULL pointer)
PyObject * BindCppObjectNoCast(Cppyy::TCppObject_t object, Cppyy::TCppType_t klass, Bool_t isRef=kFALSE, Bool_t isValue=kFALSE)
only known or knowable objects will be bound (null object is ok)
The TEnumConstant class implements the constants of the enum type.
TCppIndex_t GetDatamemberIndex(TCppScope_t scope, const std::string &name)
The ROOT global object gROOT contains a list of all defined classes.
static int BuildScopeProxyDict(Cppyy::TCppScope_t scope, PyObject *pyclass)
Collect methods and data for the given scope, and add them to the given python proxy object...
Global variables class (global variables are obtained from CINT).
PyObject * TTupleOfInstances_New(Cppyy::TCppObject_t address, Cppyy::TCppType_t klass, Py_ssize_t nelems)
virtual Int_t GetArrayDim() const
Return number of array dimensions.
Long_t GetOffset() const
Get offset from "this".
const char * GetTrueTypeName() const
Get full type description of data member, e,g.: "class TDirectory*".
Bool_t IsSubtype(TCppType_t derived, TCppType_t base)
PyObject * CreateScopeProxy(Cppyy::TCppScope_t)
Convenience function with a lookup first through the known existing proxies.
virtual const char * GetTypeName() const
Get type of global variable, e,g.
TCppScope_t GetScope(const std::string &scope_name)
const std::string & GetName() const
std::string GetMethodResultType(TCppMethod_t)
R__EXTERN PyObject * gName
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
TCppIndex_t GetMethodNumArgs(TCppMethod_t)
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
R__EXTERN PyObject * gCppName
virtual TObject * At(Int_t idx) const =0
Bool_t IsStaticMethod(TCppMethod_t method)
Mother of all ROOT objects.
std::string GetDatamemberName(TCppScope_t scope, TCppIndex_t idata)
Bool_t IsStaticData(TCppScope_t scope, TCppIndex_t idata)
TCppIndex_t GetNumMethods(TCppScope_t scope)
static PyObject * BuildCppClassBases(Cppyy::TCppType_t klass)
Build a tuple of python shadow classes of all the bases of the given 'klass'.
PropertyProxy * PropertyProxy_New(Cppyy::TCppScope_t scope, Cppyy::TCppIndex_t idata)
PyObject * GetScopeProxy(Cppyy::TCppScope_t)
Retrieve scope proxy from the known ones.
std::string ShortType(const char *typeDesc, int mode)
Return the absolute type of typeDesc.
Bool_t MethodProxy_Check(T *object)
TDataMember * GetDataMember(const char *datamember) const
Return pointer to datamember object with name "datamember".
virtual Int_t GetSize() const
PyObject * BindCppObject(Cppyy::TCppObject_t object, Cppyy::TCppType_t klass, Bool_t isRef=kFALSE)
if the object is a null pointer, return a typed one (as needed for overloading)
Template proxy object to return functions and methods.
void Set(void *address, EFlags flags=kNone)
Bool_t TemplateProxy_Check(T *object)
Bool_t Pythonize(PyObject *pyclass, const std::string &name)
const std::string Compound(const std::string &name)
Break down the compound of a fully qualified type name.