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
108 #
if PY_VERSION_HEX >= 0x03050000
125 (hashfunc)_Py_HashPointer,
126 0, 0, 0, 0, 0, Py_TPFLAGS_DEFAULT, 0, 0, 0, 0, 0, 0, 0,
127 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
128 #if PY_VERSION_HEX >= 0x02030000 131 #if PY_VERSION_HEX >= 0x02060000 134 #if PY_VERSION_HEX >= 0x03040000 147 std::vector<std::pair<Cppyy::TCppType_t, Cppyy::TCppType_t> >
gPinnedTypes;
162 Py_INCREF( Py_None );
171 const char* cname = 0;
long macro_ok = 0;
174 else if ( ! ( args && PyArg_ParseTuple( args, const_cast< char* >(
"s|l" ), &cname, ¯o_ok ) ) )
179 PyErr_Format( PyExc_AttributeError,
"%s", cname );
183 std::string
name = cname;
186 if ( name.size() <= 2 || name.substr( 0, 2 ) !=
"__" ) {
212 Py_INCREF( &PyInt_Type );
219 std::ostringstream ismacro;
220 ismacro <<
"#ifdef " << name <<
"\n_pyroot_" << name <<
"=" << name
221 <<
";true;\n#else\nfalse;\n#endif";
222 if (
gROOT->ProcessLine( ismacro.str().c_str() ) ) {
232 PyErr_Format( PyExc_AttributeError,
"%s", name.c_str() );
238 #if PY_VERSION_HEX >= 0x03030000 239 inline PyDictKeyEntry* OrgDictLookup(
245 #define PYROOT_ORGDICT_LOOKUP( mp, key, hash, value_addr )\ 246 OrgDictLookup( mp, key, hash, value_addr ) 248 PyDictKeyEntry* RootLookDictString(
251 inline PyDictEntry* OrgDictLookup( PyDictObject* mp,
PyObject* key,
Long_t hash )
256 #define PYROOT_ORGDICT_LOOKUP( mp, key, hash, value_addr )\ 257 OrgDictLookup( mp, key, hash ) 259 PyDictEntry* RootLookDictString( PyDictObject* mp,
PyObject* key,
Long_t hash )
268 if ( PyDict_GetItem( PyEval_GetBuiltins(), key ) != 0 ) {
282 #if PY_VERSION_HEX >= 0x03030000 290 PyObject* val = LookupCppEntity( key, 0 );
307 if ( PyDict_SetItem( (
PyObject*)mp, key, val ) == 0 ) {
320 #if PY_VERSION_HEX >= 0x03030000 321 if ( mp->ma_keys->dk_usable <= 0 ) {
326 const int maxinsert = 5;
328 for (
int varmax = 1; varmax <= maxinsert; ++varmax ) {
329 for (
int ivar = 0; ivar < varmax; ++ivar ) {
331 PyDict_SetItem( (
PyObject*)mp, buf[ivar], Py_None);
333 for (
int ivar = 0; ivar < varmax; ++ivar ) {
334 PyDict_DelItem( (
PyObject*)mp, buf[ivar] );
335 Py_DECREF( buf[ivar] );
337 if ( 0 < mp->ma_keys->dk_usable )
363 PyDictObject* dict = 0;
364 if ( ! PyArg_ParseTuple( args, const_cast< char* >(
"O!" ), &PyDict_Type, &dict ) )
375 Py_INCREF( Py_None );
387 PyErr_Format( PyExc_TypeError,
"too few arguments for template instantiation" );
409 if ( PyArg_ParseTuple( args, const_cast< char* >(
"O|O!" ), &pyobj,
424 Py_XDECREF( pyclass );
428 void* addr = (
void*)pyprop->
GetAddress( pyobj );
433 Py_XDECREF( pyprop );
435 PyErr_Format( PyExc_TypeError,
444 PyErr_SetString( PyExc_ValueError,
"invalid argument for AddressOf()" );
453 if ( !PyErr_Occurred() ) {
454 PyObject* str = PyObject_Str( dummy );
458 PyErr_Format( PyExc_ValueError,
"unknown object at %p", (
void*)dummy );
467 void* addr = GetObjectProxyAddress( dummy, args );
469 return BufFac_t::Instance()->PyBuffer_FromMemory( (
Long_t*)addr,
sizeof(
Long_t) );
470 if ( ! addr && PyTuple_Size( args ) ) {
473 return BufFac_t::Instance()->PyBuffer_FromMemory( (
Long_t*)&addr,
sizeof(
Long_t) );
481 void* addr = GetObjectProxyAddress( dummy, args );
483 return PyLong_FromLong( *(
Long_t*)addr );
484 else if ( PyTuple_Size( args ) ) {
487 if ( addr )
return PyLong_FromLong( (
Long_t)addr );
489 return _addressof_common( dummy );
495 void* addr = GetObjectProxyAddress( dummy, args );
512 pyname = PyObject_Str( pyname );
522 PyErr_SetString( PyExc_TypeError,
523 "BindObject expects a valid class or class name as an argument" );
537 PyErr_Format( PyExc_TypeError,
543 PyObject* pyaddr = PyTuple_GET_ITEM( args, 0 );
545 if ( PyErr_Occurred() ) {
548 addr = PyLong_AsVoidPtr( pyaddr );
549 if ( PyErr_Occurred() ) {
554 if ( ! addr || ! buflen ) {
555 PyErr_SetString( PyExc_TypeError,
556 "BindObject requires a CObject or long integer as first argument" );
562 return BindObject_( addr, PyTuple_GET_ITEM( args, 1 ) );
572 if ( argc != 0 && argc != 1 ) {
573 PyErr_Format( PyExc_TypeError,
574 "MakeNullPointer takes at most 1 argument (" PY_SSIZE_T_FORMAT " given)", argc );
580 Py_INCREF( Py_None );
584 return BindObject_( 0, PyTuple_GET_ITEM( args, 0 ) );
593 if ( ! PyArg_ParseTuple( args, const_cast< char* >(
"O!O!:__expand__" ),
600 PyObject* mod = PyImport_ImportModule( (
char*)
"ROOT" );
602 PyObject* dummy = PyObject_GetAttrString( mod, (
char*)
"kRed" );
610 if ( strcmp( clname,
"TBufferFile" ) == 0 ) {
639 if ( ! PyArg_ParseTuple( args, const_cast< char* >(
"O!" ), &PyInt_Type, &policy ) )
642 Long_t l = PyInt_AS_LONG( policy );
644 Py_INCREF( Py_None );
648 PyErr_Format( PyExc_ValueError,
"Unknown policy %ld", l );
659 if ( ! PyArg_ParseTuple( args, const_cast< char* >(
"O!" ), &PyInt_Type, &policy ) )
662 Long_t l = PyInt_AS_LONG( policy );
664 Py_INCREF( Py_None );
668 PyErr_Format( PyExc_ValueError,
"Unknown policy %ld", l );
678 if ( ! PyArg_ParseTuple( args, const_cast< char* >(
"O!O!" ),
682 (
Bool_t)PyLong_AsLong( pykeep ) ? pyobj->
HoldOn() : pyobj->Release();
684 Py_INCREF( Py_None );
693 const char* type_name;
694 if ( ! PyArg_ParseTuple( args, const_cast< char* >(
"s" ), &type_name ) )
710 if ( ! PyArg_ParseTuple( args, const_cast< char* >(
"O!O!" ),
725 if ( ! PyArg_ParseTuple( args, const_cast< char* >(
"O!" ),
740 if ( ! PyArg_ParseTuple( args, const_cast< char* >(
"O!O!" ),
756 METH_VARARGS, (
char*)
"PyROOT internal function" },
758 METH_VARARGS, (
char*)
"PyROOT internal function" },
759 { (
char*)
"LookupCppEntity", (PyCFunction)LookupCppEntity,
760 METH_VARARGS, (
char*)
"PyROOT internal function" },
761 { (
char*)
"SetRootLazyLookup", (PyCFunction)SetRootLazyLookup,
762 METH_VARARGS, (
char*)
"PyROOT internal function" },
763 { (
char*)
"MakeRootTemplateClass", (PyCFunction)MakeRootTemplateClass,
764 METH_VARARGS, (
char*)
"PyROOT internal function" },
766 METH_NOARGS, (
char*)
"PyROOT internal function" },
767 { (
char*)
"_ResetRootModule", (PyCFunction)RootModuleResetCallback,
768 METH_NOARGS, (
char*)
"PyROOT internal function" },
769 { (
char*)
"AddressOf", (PyCFunction)AddressOf,
770 METH_VARARGS, (
char*)
"Retrieve address of held object in a buffer" },
771 { (
char*)
"addressof", (PyCFunction)addressof,
772 METH_VARARGS, (
char*)
"Retrieve address of held object as a value" },
773 { (
char*)
"AsCObject", (PyCFunction)AsCObject,
774 METH_VARARGS, (
char*)
"Retrieve held object in a CObject" },
775 { (
char*)
"BindObject", (PyCFunction)BindObject,
776 METH_VARARGS, (
char*)
"Create an object of given type, from given address" },
777 { (
char*)
"MakeNullPointer", (PyCFunction)MakeNullPointer,
778 METH_VARARGS, (
char*)
"Create a NULL pointer of the given type" },
779 { (
char*)
"_ObjectProxy__expand__", (PyCFunction)ObjectProxyExpand,
780 METH_VARARGS, (
char*)
"Helper method for pickling" },
781 { (
char*)
"SetMemoryPolicy", (PyCFunction)SetMemoryPolicy,
782 METH_VARARGS, (
char*)
"Determines object ownership model" },
783 { (
char*)
"SetSignalPolicy", (PyCFunction)SetSignalPolicy,
784 METH_VARARGS, (
char*)
"Trap signals in safe mode to prevent interpreter abort" },
785 { (
char*)
"SetOwnership", (PyCFunction)SetOwnership,
786 METH_VARARGS, (
char*)
"Modify held C++ object ownership" },
788 METH_VARARGS, (
char*)
"Add a smart pointer to the list of known smart pointer types" },
790 METH_NOARGS, (
char*)
"Install input hook to sent GUI events" },
792 METH_NOARGS, (
char*)
"Remove input hook to sent GUI events" },
793 { (
char*)
"SetTypePinning", (PyCFunction)SetTypePinning,
794 METH_VARARGS, (
char*)
"Install a type pinning" },
795 { (
char*)
"IgnoreTypePinning", (PyCFunction)IgnoreTypePinning,
796 METH_VARARGS, (
char*)
"Don't pin the given type" },
797 { (
char*)
"Cast", (PyCFunction)Cast,
798 METH_VARARGS, (
char*)
"Cast the given object to the given type" },
799 { NULL, NULL, 0, NULL }
803 #if PY_VERSION_HEX >= 0x03000000 804 struct module_state {
808 #define GETSTATE(m) ((struct module_state*)PyModule_GetState(m)) 810 static int rootmodule_traverse(
PyObject*
m, visitproc visit,
void* arg )
812 Py_VISIT( GETSTATE( m )->error );
816 static int rootmodule_clear(
PyObject* m )
818 Py_CLEAR( GETSTATE( m )->error );
823 static struct PyModuleDef moduledef = {
824 PyModuleDef_HEAD_INIT,
827 sizeof(
struct module_state),
838 #define PYROOT_INIT_ERROR return NULL 839 extern "C" PyObject* PyInit_libPyROOT()
841 #define PYROOT_INIT_ERROR return 853 #if PY_VERSION_HEX >= 0x03030000 861 #if PY_VERSION_HEX >= 0x03000000 864 gRootModule = Py_InitModule( const_cast< char* >(
"libPyROOT" ), gPyROOTMethods );
873 PyObject* userPythonizations = PyDict_New();
874 PyObject* gblList = PyList_New( 0 );
875 PyDict_SetItemString( userPythonizations,
"__global__", gblList );
876 Py_DECREF( gblList );
877 PyModule_AddObject(
gRootModule,
"UserPythonizations", userPythonizations );
878 PyModule_AddObject(
gRootModule,
"UserExceptions", PyDict_New() );
929 PyModule_AddObject(
gRootModule, (
char*)
"kMemoryHeuristics",
930 PyInt_FromLong( (
int)TCallContext::kUseHeuristics ) );
931 PyModule_AddObject(
gRootModule, (
char*)
"kMemoryStrict",
932 PyInt_FromLong( (
int)TCallContext::kUseStrict ) );
933 PyModule_AddObject(
gRootModule, (
char*)
"kSignalFast",
934 PyInt_FromLong( (
int)TCallContext::kFast ) );
935 PyModule_AddObject(
gRootModule, (
char*)
"kSignalSafe",
936 PyInt_FromLong( (
int)TCallContext::kSafe ) );
942 TCallContext::SetSignalPolicy(
gROOT->IsBatch() ? TCallContext::kFast : TCallContext::kSafe );
947 #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.
static PyTypeObject PyNullPtr_t_Type
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 PyMethodDef gPyROOTMethods[]
static int nullptr_nonzero(PyObject *)
R__EXTERN Bool_t gDictLookupActive
#define PyVarObject_HEAD_INIT(type, size)
#define PY_SSIZE_T_FORMAT
#define PyROOT_PyUnicode_FromFormat
void * GetAddress(ObjectProxy *pyobj)
class attributes, global properties
std::vector< Cppyy::TCppType_t > gIgnorePinnings
R__EXTERN PyObject * gRootModule
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
#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
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)
R__EXTERN PyObject * gName
PyTypeObject TCustomInstanceMethod_Type
static RooMathCoreReg dummy
#define PyROOT_PyUnicode_Check
Cppyy::TCppType_t fCppType
static void * PyROOT_PyCapsule_GetPointer(PyObject *capsule, const char *)
R__EXTERN PyObject * gCppName
Mother of all ROOT objects.
#define PyROOT_PyUnicode_CheckExact
PyObject_HEAD void * fObject
PyObject _PyROOT_NullPtrStruct
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 *))
virtual const char * GetName() const
Returns name of object.
#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)