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();
217 PyObject* doc = methods[0]->GetDocString();
225 for (
Int_t i = 1; i < nMethods; ++i ) {
229 Py_DECREF( separator );
237 PyObject* mp_meth_func( MethodProxy* pymeth,
void* )
242 *pymeth->fMethodInfo->fRefCount += 1;
243 newPyMeth->fMethodInfo = pymeth->fMethodInfo;
247 newPyMeth->fSelf = (ObjectProxy*)newPyMeth;
256 PyObject* mp_meth_self( MethodProxy* pymeth,
void* )
258 if ( IsPseudoFunc( pymeth ) ) {
259 PyErr_Format( PyExc_AttributeError,
260 "function %s has no attribute \'im_self\'", pymeth->fMethodInfo->fName.c_str() );
262 }
else if ( pymeth->fSelf != 0 ) {
263 Py_INCREF( (
PyObject*)pymeth->fSelf );
267 Py_INCREF( Py_None );
275 PyObject* mp_meth_class( MethodProxy* pymeth,
void* )
277 if ( ! IsPseudoFunc( pymeth ) ) {
278 PyObject* pyclass = pymeth->fMethodInfo->fMethods[0]->GetScopeProxy();
280 PyErr_Format( PyExc_AttributeError,
281 "function %s has no attribute \'im_class\'", pymeth->fMethodInfo->fName.c_str() );
285 Py_INCREF( Py_None );
292 PyObject* mp_func_closure( MethodProxy* ,
void* )
294 Py_INCREF( Py_None );
301 PyObject* mp_func_code( MethodProxy* pymeth,
void* )
303 #if PY_VERSION_HEX < 0x03000000 308 PyObject* co_varnames = methods.size() == 1 ? methods[0]->GetCoVarNames() :
NULL;
309 if ( !co_varnames ) {
311 co_varnames = PyTuple_New( 1 + 1 );
316 int co_argcount = PyTuple_Size( co_varnames );
319 PyObject* co_code = PyString_FromStringAndSize(
"d\x00\x00S", 4 );
322 PyObject* co_consts = PyTuple_New( 0 );
323 PyObject* co_names = PyTuple_New( 0 );
326 PyObject* co_unused = PyTuple_New( 0 );
329 PyObject* co_filename = PyString_FromString(
"ROOT.py" );
332 PyObject* co_name = PyString_FromString( pymeth->GetName().c_str() );
337 PyObject* co_lnotab = PyString_FromString(
"\x00\x01\x0c\x01" );
355 Py_DECREF( co_lnotab );
356 Py_DECREF( co_name );
357 Py_DECREF( co_unused );
358 Py_DECREF( co_filename );
359 Py_DECREF( co_varnames );
360 Py_DECREF( co_names );
361 Py_DECREF( co_consts );
362 Py_DECREF( co_code );
368 if ( pymeth || !pymeth) Py_INCREF( Py_None );
377 PyObject* mp_func_defaults( MethodProxy* pymeth,
void* )
381 if ( methods.size() != 1 )
382 return PyTuple_New( 0 );
384 int maxarg = methods[0]->GetMaxArgs();
386 PyObject* defaults = PyTuple_New( maxarg );
389 for (
int iarg = 0; iarg < maxarg; ++iarg ) {
390 PyObject* defvalue = methods[0]->GetArgDefault( iarg );
392 PyTuple_SET_ITEM( defaults, itup++, defvalue );
394 _PyTuple_Resize( &defaults, itup );
403 PyObject* mp_func_globals( MethodProxy* ,
void* )
405 PyObject* pyglobal = PyModule_GetDict( PyImport_AddModule( (
char*)
"ROOT" ) );
406 Py_XINCREF( pyglobal );
413 PyObject* mp_getcreates( MethodProxy* pymeth,
void* )
415 return PyInt_FromLong( (
Bool_t)
IsCreator( pymeth->fMethodInfo->fFlags ) );
421 int mp_setcreates( MethodProxy* pymeth,
PyObject* value,
void* )
424 pymeth->fMethodInfo->fFlags &= ~TCallContext::kIsCreator;
428 Long_t iscreator = PyLong_AsLong( value );
429 if ( iscreator == -1 && PyErr_Occurred() ) {
430 PyErr_SetString( PyExc_ValueError,
"a boolean 1 or 0 is required for _creates" );
437 pymeth->fMethodInfo->fFlags &= ~TCallContext::kIsCreator;
445 PyObject* mp_getmempolicy( MethodProxy* pymeth,
void* )
453 return PyInt_FromLong( -1 );
459 int mp_setmempolicy( MethodProxy* pymeth,
PyObject* value,
void* )
461 Long_t mempolicy = PyLong_AsLong( value );
464 pymeth->fMethodInfo->fFlags &= ~TCallContext::kUseStrict;
467 pymeth->fMethodInfo->fFlags &= ~TCallContext::kUseHeuristics;
469 PyErr_SetString( PyExc_ValueError,
470 "expected kMemoryStrict or kMemoryHeuristics as value for _mempolicy" );
481 PyObject* mp_get_manage_smart_ptr( MethodProxy* pymeth,
void* )
483 return PyInt_FromLong(
491 int mp_set_manage_smart_ptr( MethodProxy* pymeth,
PyObject* value,
void* )
493 Long_t policy = PyLong_AsLong( value );
494 if ( policy == -1 && PyErr_Occurred() ) {
495 PyErr_SetString( PyExc_ValueError,
"a boolean 1 or 0 is required for _manage_smart_ptr" );
507 PyObject* mp_getthreaded( MethodProxy* pymeth,
void* )
509 return PyInt_FromLong(
516 int mp_setthreaded( MethodProxy* pymeth,
PyObject* value,
void* )
518 Long_t isthreaded = PyLong_AsLong( value );
519 if ( isthreaded == -1 && PyErr_Occurred() ) {
520 PyErr_SetString( PyExc_ValueError,
"a boolean 1 or 0 is required for _creates" );
527 pymeth->fMethodInfo->fFlags &= ~TCallContext::kReleaseGIL;
534 PyGetSetDef mp_getset[] = {
535 { (
char*)
"__name__", (getter)mp_name,
NULL,
NULL, NULL },
536 { (
char*)
"__module__", (getter)mp_module,
NULL,
NULL, NULL },
537 { (
char*)
"__doc__", (getter)mp_doc,
NULL,
NULL, NULL },
542 { (
char*)
"im_func", (getter)mp_meth_func,
NULL,
NULL, NULL },
543 { (
char*)
"im_self", (getter)mp_meth_self,
NULL,
NULL, NULL },
544 { (
char*)
"im_class", (getter)mp_meth_class,
NULL,
NULL, NULL },
546 { (
char*)
"func_closure", (getter)mp_func_closure,
NULL,
NULL, NULL },
547 { (
char*)
"func_code", (getter)mp_func_code,
NULL,
NULL, NULL },
548 { (
char*)
"func_defaults", (getter)mp_func_defaults,
NULL,
NULL, NULL },
549 { (
char*)
"func_globals", (getter)mp_func_globals,
NULL,
NULL, NULL },
550 { (
char*)
"func_doc", (getter)mp_doc,
NULL,
NULL, NULL },
551 { (
char*)
"func_name", (getter)mp_name,
NULL,
NULL, NULL },
553 { (
char*)
"_creates", (getter)mp_getcreates, (setter)mp_setcreates,
554 (
char*)
"For ownership rules of result: if true, objects are python-owned", NULL },
555 { (
char*)
"_mempolicy", (getter)mp_getmempolicy, (setter)mp_setmempolicy,
556 (
char*)
"For argument ownership rules: like global, either heuristic or strict", NULL },
557 { (
char*)
"_manage_smart_ptr", (getter)mp_get_manage_smart_ptr, (setter)mp_set_manage_smart_ptr,
558 (
char*)
"If a smart pointer is returned, determines management policy.", NULL },
559 { (
char*)
"_threaded", (getter)mp_getthreaded, (setter)mp_setthreaded,
560 (
char*)
"If true, releases GIL on call into C++", NULL },
561 { (
char*)NULL, NULL, NULL, NULL, NULL }
571 if ( IsPseudoFunc( pymeth ) )
572 pymeth->fSelf =
NULL;
574 ObjectProxy* oldSelf = pymeth->fSelf;
577 auto& methods = pymeth->fMethodInfo->fMethods;
578 auto& dispatchMap = pymeth->fMethodInfo->fDispatchMap;
579 auto& mflags = pymeth->fMethodInfo->fFlags;
581 Int_t nMethods = methods.size();
583 TCallContext ctxt = { 0 };
591 if ( nMethods == 1 ) {
592 PyObject* result = methods[0]->Call( pymeth->fSelf, args, kwds, &ctxt );
593 return HandleReturn( pymeth, oldSelf, result );
597 Long_t sighash = HashSignature( args );
600 MethodProxy::DispatchMap_t::iterator
m = dispatchMap.find( sighash );
601 if ( m != dispatchMap.end() ) {
602 Int_t index = m->second;
603 PyObject* result = methods[ index ]->Call( pymeth->fSelf, args, kwds, &ctxt );
604 result = HandleReturn( pymeth, oldSelf, result );
610 ResetCallState( pymeth->fSelf, oldSelf,
kTRUE );
615 std::stable_sort( methods.begin(), methods.end(), PriorityCmp );
619 std::vector< PyError_t > errors;
620 for (
Int_t i = 0; i < nMethods; ++i ) {
621 PyObject* result = methods[i]->Call( pymeth->fSelf, args, kwds, &ctxt );
625 dispatchMap[ sighash ] = i;
626 std::for_each( errors.begin(), errors.end(), PyError_t::Clear );
627 return HandleReturn( pymeth, oldSelf, result );
631 if ( ! PyErr_Occurred() ) {
633 PyObject* sig = methods[i]->GetPrototype();
634 PyErr_Format( PyExc_SystemError,
"%s =>\n %s",
639 PyErr_Fetch( &e.fType, &e.fValue, &e.fTrace );
640 errors.push_back( e );
641 ResetCallState( pymeth->fSelf, oldSelf,
kFALSE );
646 "none of the %d overloaded methods succeeded. Full details:", nMethods );
651 for ( std::vector< PyError_t >::iterator
e = errors.begin();
e != errors.end(); ++
e ) {
652 if (
e->fType != PyExc_NotImplementedError ) {
653 if ( ! exc_type ) exc_type =
e->fType;
654 else if ( exc_type !=
e->fType ) exc_type = PyExc_TypeError;
660 Py_DECREF( separator );
661 std::for_each( errors.begin(), errors.end(), PyError_t::Clear );
664 PyErr_SetObject( exc_type ? exc_type : PyExc_TypeError, value );
672 MethodProxy* mp_descrget( MethodProxy* pymeth, ObjectProxy* pyobj,
PyObject* )
677 *pymeth->fMethodInfo->fRefCount += 1;
678 newPyMeth->fMethodInfo = pymeth->fMethodInfo;
682 newPyMeth->fSelf = pyobj;
693 pymeth->fSelf =
NULL;
694 pymeth->fMethodInfo =
new MethodProxy::MethodInfo_t;
696 PyObject_GC_Track( pymeth );
703 void mp_dealloc( MethodProxy* pymeth )
705 PyObject_GC_UnTrack( pymeth );
707 if ( ! IsPseudoFunc( pymeth ) )
708 Py_CLEAR( pymeth->fSelf );
709 pymeth->fSelf =
NULL;
711 if ( --(*pymeth->fMethodInfo->fRefCount) <= 0 ) {
712 delete pymeth->fMethodInfo;
715 PyObject_GC_Del( pymeth );
723 Long_t mp_hash( MethodProxy* pymeth )
725 return _Py_HashPointer( pymeth->fMethodInfo );
731 int mp_traverse( MethodProxy* pymeth, visitproc visit,
void* args )
733 if ( pymeth->fSelf && ! IsPseudoFunc( pymeth ) )
734 return visit( (
PyObject*)pymeth->fSelf, args );
742 int mp_clear( MethodProxy* pymeth )
744 if ( ! IsPseudoFunc( pymeth ) )
745 Py_CLEAR( pymeth->fSelf );
746 pymeth->fSelf =
NULL;
754 PyObject* mp_richcompare( MethodProxy*
self, MethodProxy* other,
int op )
760 if ( (
Py_TYPE(
self) ==
Py_TYPE(other) && self->fMethodInfo == other->fMethodInfo ) && \
761 ( ( IsPseudoFunc(
self ) && IsPseudoFunc( other ) ) || self->fSelf == other->fSelf ) ) {
762 Py_INCREF( Py_True );
765 Py_INCREF( Py_False );
775 PyErr_Format( PyExc_TypeError,
"disp() argument 1 must be string, not %.50s",
776 sigarg == Py_None ?
"None" :
Py_TYPE(sigarg)->tp_name );
783 for (
Int_t i = 0; i < (
Int_t)methods.size(); ++i ) {
785 PyObject* sig2 = methods[ i ]->GetSignature();
786 if ( PyObject_RichCompareBool( sig1, sig2, Py_EQ ) ) {
791 newmeth->Set( pymeth->fMethodInfo->fName, vec );
793 if ( pymeth->fSelf && ! IsPseudoFunc( pymeth ) ) {
794 Py_INCREF( pymeth->fSelf );
795 newmeth->fSelf = pymeth->fSelf;
813 TPythonCallback* cb =
new TPythonCallback(new_overload);
814 pymeth->AddMethod( cb );
815 Py_INCREF( Py_None );
819 PyMethodDef mp_methods[] = {
820 { (
char*)
"disp", (PyCFunction)mp_disp, METH_O, (
char*)
"select overload for dispatch" },
821 { (
char*)
"__add_overload__", (PyCFunction)mp_add_overload, METH_O, (
char*)
"add a new overload" },
833 (
char*)
"ROOT.MethodProxy",
836 (destructor)mp_dealloc,
846 (ternaryfunc)mp_call,
851 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
852 (
char*)
"PyROOT method proxy (internal)",
853 (traverseproc)mp_traverse,
855 (richcmpfunc)mp_richcompare,
864 (descrgetfunc)mp_descrget,
877 #
if PY_VERSION_HEX >= 0x02030000
880 #
if PY_VERSION_HEX >= 0x02060000
883 #
if PY_VERSION_HEX >= 0x03040000
895 fMethodInfo->fName =
name;
896 fMethodInfo->fMethods.swap( methods );
901 if ( name ==
"__init__" )
906 name.find(
"Clone" ) != std::string::npos )
915 fMethodInfo->fMethods.push_back( pc );
923 fMethodInfo->fMethods.insert( fMethodInfo->fMethods.end(),
933 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