34 #if PY_VERSION_HEX >= 0x03030000
35 typedef struct PyDictKeyEntry {
42 typedef struct _dictkeysobject {
47 PyDictKeyEntry dk_entries[1];
50 #define PYROOT_GET_DICT_LOOKUP( mp )\
51 ((dict_lookup_func&)mp->ma_keys->dk_lookup)
55 #define PYROOT_GET_DICT_LOOKUP( mp )\
56 ((dict_lookup_func&)mp->ma_lookup)
68 Py_FatalError(
"deallocating nullptr" );
78 #if PY_VERSION_HEX < 0x03000000
84 #
if PY_VERSION_HEX < 0x03000000
88 #
if PY_VERSION_HEX < 0x03000000
92 #
if PY_VERSION_HEX < 0x03000000
96 #
if PY_VERSION_HEX >= 0x02020000
98 #
if PY_VERSION_HEX < 0x03000000
105 #
if PY_VERSION_HEX >= 0x02050000
110 static PyTypeObject PyNullPtr_t_Type = {
120 (hashfunc)_Py_HashPointer,
121 0, 0, 0, 0, 0, Py_TPFLAGS_DEFAULT, 0, 0, 0, 0, 0, 0, 0,
122 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
123 #if PY_VERSION_HEX >= 0x02030000
126 #if PY_VERSION_HEX >= 0x02060000
129 #if PY_VERSION_HEX >= 0x03040000
142 std::vector<std::pair<Cppyy::TCppType_t, Cppyy::TCppType_t> >
gPinnedTypes;
157 Py_INCREF( Py_None );
166 const char* cname = 0;
long macro_ok = 0;
169 else if ( ! ( args && PyArg_ParseTuple( args, const_cast< char* >(
"s|l" ), &cname, ¯o_ok ) ) )
174 PyErr_Format( PyExc_AttributeError,
"%s", cname );
178 std::string name = cname;
181 if ( name.size() <= 2 || name.substr( 0, 2 ) !=
"__" ) {
207 Py_INCREF( &PyInt_Type );
214 std::ostringstream ismacro;
215 ismacro <<
"#ifdef " << name <<
"\n_pyroot_" << name <<
"=" << name
216 <<
";true;\n#else\nfalse;\n#endif";
217 if (
gROOT->ProcessLine( ismacro.str().c_str() ) ) {
227 PyErr_Format( PyExc_AttributeError,
"%s", name.c_str() );
233 #if PY_VERSION_HEX >= 0x03030000
234 inline PyDictKeyEntry* OrgDictLookup(
240 #define PYROOT_ORGDICT_LOOKUP( mp, key, hash, value_addr )\
241 OrgDictLookup( mp, key, hash, value_addr )
243 PyDictKeyEntry* RootLookDictString(
246 inline PyDictEntry* OrgDictLookup( PyDictObject* mp,
PyObject* key,
Long_t hash )
251 #define PYROOT_ORGDICT_LOOKUP( mp, key, hash, value_addr )\
252 OrgDictLookup( mp, key, hash )
254 PyDictEntry* RootLookDictString( PyDictObject* mp,
PyObject* key,
Long_t hash )
263 if ( PyDict_GetItem( PyEval_GetBuiltins(), key ) != 0 ) {
277 #if PY_VERSION_HEX >= 0x03030000
285 PyObject* val = LookupCppEntity( key, 0 );
302 if ( PyDict_SetItem( (
PyObject*)mp, key, val ) == 0 ) {
328 PyDictObject* dict = 0;
329 if ( ! PyArg_ParseTuple( args, const_cast< char* >(
"O!" ), &PyDict_Type, &dict ) )
339 Py_INCREF( Py_None );
351 PyErr_Format( PyExc_TypeError,
"too few arguments for template instantiation" );
373 if ( PyArg_ParseTuple( args, const_cast< char* >(
"O|O!" ), &pyobj,
388 Py_XDECREF( pyclass );
392 void* addr = (
void*)pyprop->
GetAddress( pyobj );
397 Py_XDECREF( pyprop );
399 PyErr_Format( PyExc_TypeError,
408 PyErr_SetString( PyExc_ValueError,
"invalid argument for AddressOf()" );
417 if ( !PyErr_Occurred() ) {
418 PyObject* str = PyObject_Str( dummy );
422 PyErr_Format( PyExc_ValueError,
"unknown object at %p", (
void*)dummy );
431 void* addr = GetObjectProxyAddress( dummy, args );
434 if ( ! addr && PyTuple_Size( args ) ) {
445 void* addr = GetObjectProxyAddress( dummy, args );
447 return PyLong_FromLong( *(
Long_t*)addr );
448 else if ( PyTuple_Size( args ) ) {
451 if ( addr )
return PyLong_FromLong( (
Long_t)addr );
453 return _addressof_common( dummy );
459 void* addr = GetObjectProxyAddress( dummy, args );
475 pyname = PyObject_Str( pyname );
485 PyErr_SetString( PyExc_TypeError,
486 "BindObject expects a valid class or class name as an argument" );
500 PyErr_Format( PyExc_TypeError,
506 PyObject* pyaddr = PyTuple_GET_ITEM( args, 0 );
508 if ( PyErr_Occurred() ) {
511 addr = PyLong_AsVoidPtr( pyaddr );
512 if ( PyErr_Occurred() ) {
517 if ( ! addr || ! buflen ) {
518 PyErr_SetString( PyExc_TypeError,
519 "BindObject requires a CObject or long integer as first argument" );
525 return BindObject_( addr, PyTuple_GET_ITEM( args, 1 ) );
535 if ( argc != 0 && argc != 1 ) {
536 PyErr_Format( PyExc_TypeError,
537 "MakeNullPointer takes at most 1 argument (" PY_SSIZE_T_FORMAT " given)", argc );
543 Py_INCREF( Py_None );
547 return BindObject_( 0, PyTuple_GET_ITEM( args, 0 ) );
556 if ( ! PyArg_ParseTuple( args, const_cast< char* >(
"O!O!:__expand__" ),
563 PyObject* mod = PyImport_ImportModule( (
char*)
"ROOT" );
565 PyObject* dummy = PyObject_GetAttrString( mod, (
char*)
"kRed" );
573 if ( strcmp( clname,
"TBufferFile" ) == 0 ) {
602 if ( ! PyArg_ParseTuple( args, const_cast< char* >(
"O!" ), &PyInt_Type, &policy ) )
605 Long_t l = PyInt_AS_LONG( policy );
607 Py_INCREF( Py_None );
611 PyErr_Format( PyExc_ValueError,
"Unknown policy %ld", l );
622 if ( ! PyArg_ParseTuple( args, const_cast< char* >(
"O!" ), &PyInt_Type, &policy ) )
625 Long_t l = PyInt_AS_LONG( policy );
627 Py_INCREF( Py_None );
631 PyErr_Format( PyExc_ValueError,
"Unknown policy %ld", l );
641 if ( ! PyArg_ParseTuple( args, const_cast< char* >(
"O!O!" ),
647 Py_INCREF( Py_None );
656 const char* type_name;
657 if ( ! PyArg_ParseTuple( args, const_cast< char* >(
"s" ), &type_name ) )
673 if ( ! PyArg_ParseTuple( args, const_cast< char* >(
"O!O!" ),
688 if ( ! PyArg_ParseTuple( args, const_cast< char* >(
"O!" ),
703 if ( ! PyArg_ParseTuple( args, const_cast< char* >(
"O!O!" ),
717 static PyMethodDef gPyROOTMethods[] = {
719 METH_VARARGS, (
char*)
"PyROOT internal function" },
721 METH_VARARGS, (
char*)
"PyROOT internal function" },
722 { (
char*)
"LookupCppEntity", (PyCFunction)LookupCppEntity,
723 METH_VARARGS, (
char*)
"PyROOT internal function" },
724 { (
char*)
"SetRootLazyLookup", (PyCFunction)SetRootLazyLookup,
725 METH_VARARGS, (
char*)
"PyROOT internal function" },
726 { (
char*)
"MakeRootTemplateClass", (PyCFunction)MakeRootTemplateClass,
727 METH_VARARGS, (
char*)
"PyROOT internal function" },
729 METH_NOARGS, (
char*)
"PyROOT internal function" },
730 { (
char*)
"_ResetRootModule", (PyCFunction)RootModuleResetCallback,
731 METH_NOARGS, (
char*)
"PyROOT internal function" },
732 { (
char*)
"AddressOf", (PyCFunction)AddressOf,
733 METH_VARARGS, (
char*)
"Retrieve address of held object in a buffer" },
734 { (
char*)
"addressof", (PyCFunction)
addressof,
735 METH_VARARGS, (
char*)
"Retrieve address of held object as a value" },
736 { (
char*)
"AsCObject", (PyCFunction)AsCObject,
737 METH_VARARGS, (
char*)
"Retrieve held object in a CObject" },
738 { (
char*)
"BindObject", (PyCFunction)BindObject,
739 METH_VARARGS, (
char*)
"Create an object of given type, from given address" },
740 { (
char*)
"MakeNullPointer", (PyCFunction)MakeNullPointer,
741 METH_VARARGS, (
char*)
"Create a NULL pointer of the given type" },
742 { (
char*)
"_ObjectProxy__expand__", (PyCFunction)ObjectProxyExpand,
743 METH_VARARGS, (
char*)
"Helper method for pickling" },
744 { (
char*)
"SetMemoryPolicy", (PyCFunction)SetMemoryPolicy,
745 METH_VARARGS, (
char*)
"Determines object ownership model" },
746 { (
char*)
"SetSignalPolicy", (PyCFunction)SetSignalPolicy,
747 METH_VARARGS, (
char*)
"Trap signals in safe mode to prevent interpreter abort" },
748 { (
char*)
"SetOwnership", (PyCFunction)SetOwnership,
749 METH_VARARGS, (
char*)
"Modify held C++ object ownership" },
751 METH_VARARGS, (
char*)
"Add a smart pointer to the list of known smart pointer types" },
753 METH_NOARGS, (
char*)
"Install input hook to sent GUI events" },
755 METH_NOARGS, (
char*)
"Remove input hook to sent GUI events" },
756 { (
char*)
"SetTypePinning", (PyCFunction)SetTypePinning,
757 METH_VARARGS, (
char*)
"Install a type pinning" },
758 { (
char*)
"IgnoreTypePinning", (PyCFunction)IgnoreTypePinning,
759 METH_VARARGS, (
char*)
"Don't pin the given type" },
760 { (
char*)
"Cast", (PyCFunction)Cast,
761 METH_VARARGS, (
char*)
"Cast the given object to the given type" },
766 #if PY_VERSION_HEX >= 0x03000000
767 struct module_state {
771 #define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
773 static int rootmodule_traverse(
PyObject* m, visitproc visit,
void* arg )
775 Py_VISIT( GETSTATE( m )->error );
779 static int rootmodule_clear(
PyObject* m )
781 Py_CLEAR( GETSTATE( m )->error );
786 static struct PyModuleDef moduledef = {
787 PyModuleDef_HEAD_INIT,
790 sizeof(
struct module_state),
801 #define PYROOT_INIT_ERROR return NULL
802 extern "C" PyObject* PyInit_libPyROOT()
804 #define PYROOT_INIT_ERROR return
816 #if PY_VERSION_HEX >= 0x03030000
824 #if PY_VERSION_HEX >= 0x03000000
827 gRootModule = Py_InitModule( const_cast< char* >(
"libPyROOT" ), gPyROOTMethods );
836 PyObject* userPythonizations = PyDict_New();
837 PyObject* gblList = PyList_New( 0 );
838 PyDict_SetItemString( userPythonizations,
"__global__", gblList );
839 Py_DECREF( gblList );
840 PyModule_AddObject(
gRootModule,
"UserPythonizations", userPythonizations );
841 PyModule_AddObject(
gRootModule,
"UserExceptions", PyDict_New() );
886 PyModule_AddObject(
gRootModule, (
char*)
"kMemoryHeuristics",
888 PyModule_AddObject(
gRootModule, (
char*)
"kMemoryStrict",
890 PyModule_AddObject(
gRootModule, (
char*)
"kSignalFast",
892 PyModule_AddObject(
gRootModule, (
char*)
"kSignalSafe",
904 #if PY_VERSION_HEX >= 0x03000000
#define PyBytes_FromString
Bool_t InitProxy(PyObject *module, PyTypeObject *pytype, const char *name)
Initialize a proxy class for use by python, and add it to the ROOT module.
#define PyROOT_PyUnicode_FromString
PyDictEntry *(* dict_lookup_func)(PyDictObject *, PyObject *, Long_t)
The concrete implementation of TBuffer for writing/reading to/from a ROOT file or socket...
R__EXTERN PyObject * gDict
PyObject * RemoveGUIEventInputHook()
static void nullptr_dealloc(PyObject *)
PyTypeObject TTupleOfInstances_Type
Representation of C-style array of instances.
PyTypeObject PropertyProxy_Type
R__EXTERN dict_lookup_func gDictLookupOrg
static PyObject * nullptr_repr(PyObject *)
PyObject * BuildTemplateName(PyObject *pyname, PyObject *args, int argoff)
Helper to construct the "< type, type, ... >" part of a templated name (either for a class as in Make...
virtual void * ReadObjectAny(const TClass *cast)
Read object from I/O buffer.
#define PYROOT_INIT_ERROR
static int nullptr_nonzero(PyObject *)
R__EXTERN Bool_t gDictLookupActive
#define PyVarObject_HEAD_INIT(type, size)
#define PY_SSIZE_T_FORMAT
void * GetAddress(ObjectProxy *pyobj)
class attributes, global properties
std::vector< Cppyy::TCppType_t > gIgnorePinnings
R__EXTERN PyObject * gRootModule
static Bool_t SetMemoryPolicy(ECallFlags e)
Set the global memory policy, which affects object ownership when objects are passed as function argu...
PyObject * GetCppGlobal(const std::string &name)
try named global variable/enum (first ROOT, then Cling: sync is too slow)
PyTypeObject TCustomInt_Type
#define PyROOT_PyUnicode_Type
#define PyROOT_PyUnicode_AsString
Bool_t PropertyProxy_Check(T *object)
static PyNumberMethods nullptr_as_number
R__EXTERN PyObject * gNullPtrObject
PyTypeObject PyRootType_Type
PyTypeObject ObjectProxy_Type
void AddSmartPtrType(const std::string &)
Bool_t ObjectProxy_Check(T *object)
R__EXTERN PyObject * gClass
std::vector< std::pair< Cppyy::TCppType_t, Cppyy::TCppType_t > > gPinnedTypes
static Bool_t SetSignalPolicy(ECallFlags e)
Set the global signal policy, which determines whether a jmp address should be saved to return to aft...
#define PYROOT_GET_DICT_LOOKUP(mp)
PyObject * DestroyPyStrings()
Remove all cached python strings.
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)
PyTypeObject MethodProxy_Type
PyObject * PyBuffer_FromMemory(Bool_t *buf, Py_ssize_t size=-1)
PyTypeObject TemplateProxy_Type
Type object to hold TClassRef instance (this is only semantically a presentation of PyRootType instan...
PyObject * CreateScopeProxy(Cppyy::TCppScope_t)
Convenience function with a lookup first through the known existing proxies.
PyObject * InstallGUIEventInputHook()
TCppScope_t GetScope(const std::string &scope_name)
virtual const char * GetName() const
Returns name of object.
R__EXTERN PyObject * gName
PyTypeObject TCustomInstanceMethod_Type
#define PyROOT_PyUnicode_Check
Cppyy::TCppType_t fCppType
static void * PyROOT_PyCapsule_GetPointer(PyObject *capsule, const char *)
Mother of all ROOT objects.
#define PyROOT_PyUnicode_CheckExact
PyObject_HEAD void * fObject
static TPyBufferFactory * Instance()
int GetBuffer(PyObject *pyobject, char tc, int size, void *&buf, Bool_t check=kTRUE)
Retrieve a linear buffer pointer from the given pyobject.
virtual void WriteFastArray(const Bool_t *b, Int_t n)
Write array of n bools into the I/O buffer.
Bool_t PropertyProxy_CheckExact(T *object)
PyTypeObject TCustomFloat_Type
Custom builtins, detectable by type, for pass by ref.
static PyObject * PyROOT_PyCapsule_New(void *cobj, const char *, void(*destr)(void *))
#define PyBytes_AS_STRING
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)
Bool_t IsEnum(const std::string &type_name)
#define PYROOT_ORGDICT_LOOKUP(mp, key, hash, value_addr)