6 #include "structmember.h" 7 #if PY_VERSION_HEX >= 0x02050000 14 #define CO_NOFREE 0x0040 30 class TPythonCallback :
public PyCallable {
34 TPythonCallback(
PyObject* callable ):
37 if ( !PyCallable_Check( callable ) ) {
38 PyErr_SetString(PyExc_TypeError,
"parameter must be callable");
42 Py_INCREF( fCallable );
45 virtual ~TPythonCallback() {
46 Py_DECREF( fCallable );
53 if ( PyObject_HasAttrString( fCallable,
"__doc__" )) {
54 return PyObject_GetAttrString( fCallable,
"__doc__" );
56 return GetPrototype();
60 virtual Int_t GetPriority() {
return 100; };
62 virtual Int_t GetMaxArgs() {
return 100; };
77 virtual PyCallable* Clone() {
return new TPythonCallback( *
this ); }
85 newArgs = PyTuple_New( nargs+1 );
87 PyTuple_SET_ITEM( newArgs, 0, (
PyObject*)
self );
88 for (
Py_ssize_t iarg = 0; iarg < nargs; ++iarg ) {
89 PyObject* pyarg = PyTuple_GET_ITEM( args, iarg );
91 PyTuple_SET_ITEM( newArgs, iarg+1, pyarg );
97 return PyObject_Call( fCallable, newArgs, kwds );
104 Bool_t inline IsPseudoFunc( MethodProxy* pymeth )
106 return (
void*)pymeth == (
void*)pymeth->fSelf;
113 static void Clear( PyError_t&
e )
116 Py_XDECREF( e.fType ); Py_XDECREF( e.fValue ); Py_XDECREF( e.fTrace );
117 e.fType = e.fValue = e.fTrace = 0;
129 Int_t nargs = PyTuple_GET_SIZE( args );
130 for (
Int_t i = 0; i < nargs; ++i ) {
132 hash += (hash << 10); hash ^= (hash >> 6);
135 hash += (hash << 3); hash ^= (hash >> 11); hash += (hash << 15);
141 int PriorityCmp( PyCallable* left, PyCallable* right )
143 return left->GetPriority() > right->GetPriority();
147 inline void ResetCallState( ObjectProxy*& selfnew, ObjectProxy* selfold,
Bool_t clear ) {
148 if ( selfnew != selfold ) {
149 Py_XDECREF( selfnew );
164 if (
IsCreator( pymeth->fMethodInfo->fFlags ) ) {
169 pymeth->fSelf->HoldOn();
174 ((ObjectProxy*)result)->HoldOn();
188 ResetCallState( pymeth->fSelf, oldSelf,
kFALSE );
195 PyObject* mp_name( MethodProxy* pymeth,
void* )
202 PyObject* mp_module( MethodProxy* ,
void* )
211 PyObject* mp_doc( MethodProxy* pymeth,
void* )
216 Int_t nMethods = methods.size();
222 PyObject* doc = methods[0]->GetDocString();
230 for (
Int_t i = 1; i < nMethods; ++i ) {
234 Py_DECREF( separator );
242 PyObject* mp_meth_func( MethodProxy* pymeth,
void* )
247 *pymeth->fMethodInfo->fRefCount += 1;
248 newPyMeth->fMethodInfo = pymeth->fMethodInfo;
252 newPyMeth->fSelf = (ObjectProxy*)newPyMeth;
261 PyObject* mp_meth_self( MethodProxy* pymeth,
void* )
263 if ( IsPseudoFunc( pymeth ) ) {
264 PyErr_Format( PyExc_AttributeError,
265 "function %s has no attribute \'im_self\'", pymeth->fMethodInfo->fName.c_str() );
267 }
else if ( pymeth->fSelf != 0 ) {
268 Py_INCREF( (
PyObject*)pymeth->fSelf );
272 Py_INCREF( Py_None );
280 PyObject* mp_meth_class( MethodProxy* pymeth,
void* )
282 if ( ! IsPseudoFunc( pymeth ) ) {
283 PyObject* pyclass = pymeth->fMethodInfo->fMethods[0]->GetScopeProxy();
285 PyErr_Format( PyExc_AttributeError,
286 "function %s has no attribute \'im_class\'", pymeth->fMethodInfo->fName.c_str() );
290 Py_INCREF( Py_None );
297 PyObject* mp_func_closure( MethodProxy* ,
void* )
299 Py_INCREF( Py_None );
306 PyObject* mp_func_code( MethodProxy* pymeth,
void* )
308 #if PY_VERSION_HEX < 0x03000000 313 PyObject* co_varnames = methods.size() == 1 ? methods[0]->GetCoVarNames() :
NULL;
314 if ( !co_varnames ) {
316 co_varnames = PyTuple_New( 1 + 1 );
321 int co_argcount = PyTuple_Size( co_varnames );
324 PyObject* co_code = PyString_FromStringAndSize(
"d\x00\x00S", 4 );
327 PyObject* co_consts = PyTuple_New( 0 );
328 PyObject* co_names = PyTuple_New( 0 );
331 PyObject* co_unused = PyTuple_New( 0 );
334 PyObject* co_filename = PyString_FromString(
"ROOT.py" );
337 PyObject* co_name = PyString_FromString( pymeth->GetName().c_str() );
342 PyObject* co_lnotab = PyString_FromString(
"\x00\x01\x0c\x01" );
360 Py_DECREF( co_lnotab );
361 Py_DECREF( co_name );
362 Py_DECREF( co_unused );
363 Py_DECREF( co_filename );
364 Py_DECREF( co_varnames );
365 Py_DECREF( co_names );
366 Py_DECREF( co_consts );
367 Py_DECREF( co_code );
373 if ( pymeth || !pymeth) Py_INCREF( Py_None );
382 PyObject* mp_func_defaults( MethodProxy* pymeth,
void* )
386 if ( methods.size() != 1 )
387 return PyTuple_New( 0 );
389 int maxarg = methods[0]->GetMaxArgs();
391 PyObject* defaults = PyTuple_New( maxarg );
394 for (
int iarg = 0; iarg < maxarg; ++iarg ) {
395 PyObject* defvalue = methods[0]->GetArgDefault( iarg );
397 PyTuple_SET_ITEM( defaults, itup++, defvalue );
399 _PyTuple_Resize( &defaults, itup );
408 PyObject* mp_func_globals( MethodProxy* ,
void* )
410 PyObject* pyglobal = PyModule_GetDict( PyImport_AddModule( (
char*)
"ROOT" ) );
411 Py_XINCREF( pyglobal );
418 PyObject* mp_getcreates( MethodProxy* pymeth,
void* )
420 return PyInt_FromLong( (
Bool_t)
IsCreator( pymeth->fMethodInfo->fFlags ) );
426 int mp_setcreates( MethodProxy* pymeth,
PyObject* value,
void* )
429 pymeth->fMethodInfo->fFlags &= ~TCallContext::kIsCreator;
433 Long_t iscreator = PyLong_AsLong( value );
434 if ( iscreator == -1 && PyErr_Occurred() ) {
435 PyErr_SetString( PyExc_ValueError,
"a boolean 1 or 0 is required for _creates" );
442 pymeth->fMethodInfo->fFlags &= ~TCallContext::kIsCreator;
450 PyObject* mp_getmempolicy( MethodProxy* pymeth,
void* )
458 return PyInt_FromLong( -1 );
464 int mp_setmempolicy( MethodProxy* pymeth,
PyObject* value,
void* )
466 Long_t mempolicy = PyLong_AsLong( value );
469 pymeth->fMethodInfo->fFlags &= ~TCallContext::kUseStrict;
472 pymeth->fMethodInfo->fFlags &= ~TCallContext::kUseHeuristics;
474 PyErr_SetString( PyExc_ValueError,
475 "expected kMemoryStrict or kMemoryHeuristics as value for _mempolicy" );
486 PyObject* mp_get_manage_smart_ptr( MethodProxy* pymeth,
void* )
488 return PyInt_FromLong(
496 int mp_set_manage_smart_ptr( MethodProxy* pymeth,
PyObject* value,
void* )
498 Long_t policy = PyLong_AsLong( value );
499 if ( policy == -1 && PyErr_Occurred() ) {
500 PyErr_SetString( PyExc_ValueError,
"a boolean 1 or 0 is required for _manage_smart_ptr" );
512 PyObject* mp_getthreaded( MethodProxy* pymeth,
void* )
514 return PyInt_FromLong(
521 int mp_setthreaded( MethodProxy* pymeth,
PyObject* value,
void* )
523 Long_t isthreaded = PyLong_AsLong( value );
524 if ( isthreaded == -1 && PyErr_Occurred() ) {
525 PyErr_SetString( PyExc_ValueError,
"a boolean 1 or 0 is required for _creates" );
532 pymeth->fMethodInfo->fFlags &= ~TCallContext::kReleaseGIL;
539 PyGetSetDef mp_getset[] = {
540 { (
char*)
"__name__", (getter)mp_name,
NULL,
NULL, NULL },
541 { (
char*)
"__module__", (getter)mp_module,
NULL,
NULL, NULL },
542 { (
char*)
"__doc__", (getter)mp_doc,
NULL,
NULL, NULL },
547 { (
char*)
"im_func", (getter)mp_meth_func,
NULL,
NULL, NULL },
548 { (
char*)
"im_self", (getter)mp_meth_self,
NULL,
NULL, NULL },
549 { (
char*)
"im_class", (getter)mp_meth_class,
NULL,
NULL, NULL },
551 { (
char*)
"func_closure", (getter)mp_func_closure,
NULL,
NULL, NULL },
552 { (
char*)
"func_code", (getter)mp_func_code,
NULL,
NULL, NULL },
553 { (
char*)
"func_defaults", (getter)mp_func_defaults,
NULL,
NULL, NULL },
554 { (
char*)
"func_globals", (getter)mp_func_globals,
NULL,
NULL, NULL },
555 { (
char*)
"func_doc", (getter)mp_doc,
NULL,
NULL, NULL },
556 { (
char*)
"func_name", (getter)mp_name,
NULL,
NULL, NULL },
558 { (
char*)
"_creates", (getter)mp_getcreates, (setter)mp_setcreates,
559 (
char*)
"For ownership rules of result: if true, objects are python-owned", NULL },
560 { (
char*)
"_mempolicy", (getter)mp_getmempolicy, (setter)mp_setmempolicy,
561 (
char*)
"For argument ownership rules: like global, either heuristic or strict", NULL },
562 { (
char*)
"_manage_smart_ptr", (getter)mp_get_manage_smart_ptr, (setter)mp_set_manage_smart_ptr,
563 (
char*)
"If a smart pointer is returned, determines management policy.", NULL },
564 { (
char*)
"_threaded", (getter)mp_getthreaded, (setter)mp_setthreaded,
565 (
char*)
"If true, releases GIL on call into C++", NULL },
566 { (
char*)NULL, NULL, NULL, NULL, NULL }
576 if ( IsPseudoFunc( pymeth ) )
577 pymeth->fSelf =
NULL;
579 ObjectProxy* oldSelf = pymeth->fSelf;
582 auto& methods = pymeth->fMethodInfo->fMethods;
583 auto& dispatchMap = pymeth->fMethodInfo->fDispatchMap;
584 auto& mflags = pymeth->fMethodInfo->fFlags;
586 Int_t nMethods = methods.size();
588 TCallContext ctxt = { 0 };
596 if ( nMethods == 1 ) {
597 PyObject* result = methods[0]->Call( pymeth->fSelf, args, kwds, &ctxt );
598 return HandleReturn( pymeth, oldSelf, result );
602 Long_t sighash = HashSignature( args );
605 MethodProxy::DispatchMap_t::iterator
m = dispatchMap.find( sighash );
606 if ( m != dispatchMap.end() ) {
607 Int_t index = m->second;
608 PyObject* result = methods[ index ]->Call( pymeth->fSelf, args, kwds, &ctxt );
609 result = HandleReturn( pymeth, oldSelf, result );
615 ResetCallState( pymeth->fSelf, oldSelf,
kTRUE );
620 std::stable_sort( methods.begin(), methods.end(), PriorityCmp );
624 std::vector< PyError_t > errors;
625 for (
Int_t i = 0; i < nMethods; ++i ) {
626 PyObject* result = methods[i]->Call( pymeth->fSelf, args, kwds, &ctxt );
630 dispatchMap[ sighash ] = i;
631 std::for_each( errors.begin(), errors.end(), PyError_t::Clear );
632 return HandleReturn( pymeth, oldSelf, result );
636 if ( ! PyErr_Occurred() ) {
638 PyObject* sig = methods[i]->GetPrototype();
639 PyErr_Format( PyExc_SystemError,
"%s =>\n %s",
644 PyErr_Fetch( &e.fType, &e.fValue, &e.fTrace );
645 errors.push_back( e );
646 ResetCallState( pymeth->fSelf, oldSelf,
kFALSE );
651 "none of the %d overloaded methods succeeded. Full details:", nMethods );
656 for ( std::vector< PyError_t >::iterator
e = errors.begin();
e != errors.end(); ++
e ) {
657 if (
e->fType != PyExc_NotImplementedError ) {
658 if ( ! exc_type ) exc_type =
e->fType;
659 else if ( exc_type !=
e->fType ) exc_type = PyExc_TypeError;
665 Py_DECREF( separator );
666 std::for_each( errors.begin(), errors.end(), PyError_t::Clear );
669 PyErr_SetObject( exc_type ? exc_type : PyExc_TypeError, value );
677 MethodProxy* mp_descrget( MethodProxy* pymeth, ObjectProxy* pyobj,
PyObject* )
682 *pymeth->fMethodInfo->fRefCount += 1;
683 newPyMeth->fMethodInfo = pymeth->fMethodInfo;
687 newPyMeth->fSelf = pyobj;
698 pymeth->fSelf =
NULL;
699 pymeth->fMethodInfo =
new MethodProxy::MethodInfo_t;
701 PyObject_GC_Track( pymeth );
708 void mp_dealloc( MethodProxy* pymeth )
710 PyObject_GC_UnTrack( pymeth );
712 if ( ! IsPseudoFunc( pymeth ) )
713 Py_CLEAR( pymeth->fSelf );
714 pymeth->fSelf =
NULL;
716 if ( --(*pymeth->fMethodInfo->fRefCount) <= 0 ) {
717 delete pymeth->fMethodInfo;
720 PyObject_GC_Del( pymeth );
728 Long_t mp_hash( MethodProxy* pymeth )
730 return _Py_HashPointer( pymeth->fMethodInfo );
736 int mp_traverse( MethodProxy* pymeth, visitproc visit,
void* args )
738 if ( pymeth->fSelf && ! IsPseudoFunc( pymeth ) )
739 return visit( (
PyObject*)pymeth->fSelf, args );
747 int mp_clear( MethodProxy* pymeth )
749 if ( ! IsPseudoFunc( pymeth ) )
750 Py_CLEAR( pymeth->fSelf );
751 pymeth->fSelf =
NULL;
759 PyObject* mp_richcompare( MethodProxy*
self, MethodProxy* other,
int op )
765 if ( (
Py_TYPE(
self) ==
Py_TYPE(other) && self->fMethodInfo == other->fMethodInfo ) && \
766 ( ( IsPseudoFunc(
self ) && IsPseudoFunc( other ) ) || self->fSelf == other->fSelf ) ) {
767 Py_INCREF( Py_True );
770 Py_INCREF( Py_False );
780 PyErr_Format( PyExc_TypeError,
"disp() argument 1 must be string, not %.50s",
781 sigarg == Py_None ?
"None" :
Py_TYPE(sigarg)->tp_name );
788 for (
Int_t i = 0; i < (
Int_t)methods.size(); ++i ) {
790 PyObject* sig2 = methods[ i ]->GetSignature();
791 if ( PyObject_RichCompareBool( sig1, sig2, Py_EQ ) ) {
796 newmeth->Set( pymeth->fMethodInfo->fName, vec );
798 if ( pymeth->fSelf && ! IsPseudoFunc( pymeth ) ) {
799 Py_INCREF( pymeth->fSelf );
800 newmeth->fSelf = pymeth->fSelf;
818 TPythonCallback* cb =
new TPythonCallback(new_overload);
819 pymeth->AddMethod( cb );
820 Py_INCREF( Py_None );
824 PyMethodDef mp_methods[] = {
825 { (
char*)
"disp", (PyCFunction)mp_disp, METH_O, (
char*)
"select overload for dispatch" },
826 { (
char*)
"__add_overload__", (PyCFunction)mp_add_overload, METH_O, (
char*)
"add a new overload" },
838 (
char*)
"ROOT.MethodProxy",
841 (destructor)mp_dealloc,
851 (ternaryfunc)mp_call,
856 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
857 (
char*)
"PyROOT method proxy (internal)",
858 (traverseproc)mp_traverse,
860 (richcmpfunc)mp_richcompare,
869 (descrgetfunc)mp_descrget,
882 #
if PY_VERSION_HEX >= 0x02030000
885 #
if PY_VERSION_HEX >= 0x02060000
888 #
if PY_VERSION_HEX >= 0x03040000
900 fMethodInfo->fName =
name;
901 fMethodInfo->fMethods.swap( methods );
906 if ( name ==
"__init__" )
911 name.find(
"Clone" ) != std::string::npos )
920 fMethodInfo->fMethods.push_back( pc );
928 fMethodInfo->fMethods.insert( fMethodInfo->fMethods.end(),
938 for ( Methods_t::iterator it = fMethods.begin(); it != fMethods.end(); ++it ) {
#define PyROOT_PyUnicode_FromString
void Set(const std::string &name, std::vector< PyCallable * > &methods)
std::vector< PyCallable *> Methods_t
#define PyVarObject_HEAD_INIT(type, size)
Bool_t IsSorted(UInt_t flags)
#define PyROOT_PyUnicode_FromFormat
#define PyROOT_PyUnicode_Append
MethodProxy::Methods_t fMethods
#define PyROOT_PyUnicode_AsString
Bool_t IsConstructor(UInt_t flags)
Bool_t ObjectProxy_Check(T *object)
R__EXTERN PyObject * gROOTns
PyTypeObject MethodProxy_Type
MethodInfo_t * fMethodInfo
#define PyROOT_PyUnicode_AppendAndDel
void AddMethod(PyCallable *pc)
Fill in the data of a freshly created method proxy.
R__EXTERN PyObject * gLifeLine
#define PyROOT_PyUnicode_Check
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
~MethodInfo_t()
Destructor (this object is reference counted).
size_t SizeOf(TCppType_t klass)
Bool_t IsCreator(UInt_t flags)
PyObject * GetScopeProxy(Cppyy::TCppScope_t)
Retrieve scope proxy from the known ones.
static ECallFlags sMemoryPolicy