Logo ROOT  
Reference Guide
CPPMethod.cxx
Go to the documentation of this file.
1// Bindings
2#include "CPyCppyy.h"
3#include "CPPMethod.h"
4#include "CPPExcInstance.h"
5#include "CPPInstance.h"
6#include "Converters.h"
7#include "Executors.h"
8#include "ProxyWrappers.h"
9#include "PyStrings.h"
10#include "TypeManip.h"
11#include "SignalTryCatch.h"
12#include "Utility.h"
13
15
16// Standard
17#include <assert.h>
18#include <string.h>
19#include <exception>
20#include <sstream>
21#include <string>
22#include <typeinfo>
23#include <memory>
24
25
26//- data and local helpers ---------------------------------------------------
27namespace CPyCppyy {
28 extern PyObject* gThisModule;
29 extern PyObject* gBusException;
31 extern PyObject* gIllException;
33}
34
35
36//- private helpers ----------------------------------------------------------
37inline void CPyCppyy::CPPMethod::Copy_(const CPPMethod& /* other */)
38{
39// fScope and fMethod handled separately
40
41// do not copy caches
42 fExecutor = nullptr;
43 fArgIndices = nullptr;
44 fArgsRequired = -1;
45}
46
47//----------------------------------------------------------------------------
49{
50// destroy executor and argument converters
51 if (fExecutor && fExecutor->HasState()) delete fExecutor;
52
53 for (auto p : fConverters) {
54 if (p && p->HasState()) delete p;
55 }
56
57 delete fArgIndices;
58
59 fExecutor = nullptr;
60 fArgIndices = nullptr;
61 fConverters.clear();
62 fArgsRequired = -1;
63}
64
65//----------------------------------------------------------------------------
67 void* self, ptrdiff_t offset, CallContext* ctxt)
68{
69// call into C++ through fExecutor; abstracted out from Execute() to prevent some
70// code duplication with ProtectedCall()
71 PyObject* result = nullptr;
72
73 try { // C++ try block
74 result = fExecutor->Execute(fMethod, (Cppyy::TCppObject_t)((intptr_t)self+offset), ctxt);
75 } catch (PyException&) {
76 result = nullptr; // error already set
77 } catch (std::exception& e) {
78 // attempt to set the exception to the actual type, to allow catching with the Python C++ type
79 static Cppyy::TCppType_t exc_type = (Cppyy::TCppType_t)Cppyy::GetScope("std::exception");
80
81 PyObject* pyexc_type = nullptr;
82 PyObject* pyexc_obj = nullptr;
83
84 // TODO: factor this code with the same in ProxyWrappers (and cache it there to be able to
85 // look up based on TCppType_t):
86 Cppyy::TCppType_t actual = Cppyy::GetActualClass(exc_type, &e);
87 const std::string& finalname = Cppyy::GetScopedFinalName(actual);
88 const std::string& parentname = TypeManip::extract_namespace(finalname);
89 PyObject* parent = CreateScopeProxy(parentname);
90 if (parent) {
91 pyexc_type = PyObject_GetAttrString(parent,
92 parentname.empty() ? finalname.c_str() : finalname.substr(parentname.size()+2, std::string::npos).c_str());
93 Py_DECREF(parent);
94 }
95
96 if (pyexc_type) {
97 // create a copy of the exception (TODO: factor this code with the same in ProxyWrappers)
98 PyObject* pyclass = CPyCppyy::GetScopeProxy(actual);
99 PyObject* source = BindCppObjectNoCast(&e, actual);
100 PyObject* pyexc_copy = PyObject_CallFunctionObjArgs(pyclass, source, nullptr);
101 Py_DECREF(source);
102 Py_DECREF(pyclass);
103 if (pyexc_copy) {
104 pyexc_obj = CPPExcInstance_Type.tp_new((PyTypeObject*)pyexc_type, nullptr, nullptr);
105 ((CPPExcInstance*)pyexc_obj)->fCppInstance = (PyObject*)pyexc_copy;
106 } else
107 PyErr_Clear();
108 } else
109 PyErr_Clear();
110
111 if (pyexc_type && pyexc_obj) {
112 PyErr_SetObject(pyexc_type, pyexc_obj);
113 Py_DECREF(pyexc_obj);
114 Py_DECREF(pyexc_type);
115 } else {
116 PyErr_Format(PyExc_Exception, "%s (C++ exception)", e.what());
117 Py_XDECREF(pyexc_obj);
118 Py_XDECREF(pyexc_type);
119 }
120
121 result = nullptr;
122 } catch (...) {
123 PyErr_SetString(PyExc_Exception, "unhandled, unknown C++ exception");
124 result = nullptr;
125 }
126
127// TODO: covers the PyException throw case, which does not seem to work on Windows, so
128// instead leaves the error be
129#ifdef _WIN32
130 if (PyErr_Occurred()) {
131 Py_XDECREF(result);
132 result = nullptr;
133 }
134#endif
135
136 return result;
137}
138
139//----------------------------------------------------------------------------
141 void* self, ptrdiff_t offset, CallContext* ctxt)
142{
143// helper code to prevent some code duplication; this code embeds a "try/catch"
144// block that saves the call environment for restoration in case of an otherwise
145// fatal signal
146 PyObject* result = 0;
147
148 TRY { // copy call environment to be able to jump back on signal
149 result = ExecuteFast(self, offset, ctxt);
150 } CATCH(excode) {
151 // Unfortunately, the excodes are not the ones from signal.h, but enums from TSysEvtHandler.h
152 if (excode == 0)
153 PyErr_SetString(gBusException, "bus error in C++; program state was reset");
154 else if (excode == 1)
155 PyErr_SetString(gSegvException, "segfault in C++; program state was reset");
156 else if (excode == 4)
157 PyErr_SetString(gIllException, "illegal instruction in C++; program state was reset");
158 else if (excode == 5)
159 PyErr_SetString(gAbrtException, "abort from C++; program state was reset");
160 else if (excode == 12)
161 PyErr_SetString(PyExc_FloatingPointError, "floating point exception in C++; program state was reset");
162 else
163 PyErr_SetString(PyExc_SystemError, "problem in C++; program state was reset");
164 result = 0;
165 } ENDTRY;
166
167 return result;
168}
169
170//----------------------------------------------------------------------------
172{
173// build buffers for argument dispatching
174 const size_t nArgs = Cppyy::GetMethodNumArgs(fMethod);
175 fConverters.resize(nArgs);
176
177// setup the dispatch cache
178 for (int iarg = 0; iarg < (int)nArgs; ++iarg) {
179 const std::string& fullType = Cppyy::GetMethodArgType(fMethod, iarg);
180 Converter* conv = CreateConverter(fullType);
181 if (!conv) {
182 PyErr_Format(PyExc_TypeError, "argument type %s not handled", fullType.c_str());
183 return false;
184 }
185
186 fConverters[iarg] = conv;
187 }
188
189 return true;
190}
191
192//----------------------------------------------------------------------------
194{
195// install executor conform to the return type
196 executor = CreateExecutor(
197 (bool)fMethod == true ? Cppyy::GetMethodResultType(fMethod) \
198 : Cppyy::GetScopedFinalName(fScope));
199
200 if (!executor)
201 return false;
202
203 return true;
204}
205
206//----------------------------------------------------------------------------
208{
209// built a signature representation (used for doc strings)
210 std::stringstream sig; sig << "(";
211 int count = 0;
212 const size_t nArgs = Cppyy::GetMethodNumArgs(fMethod);
213 for (int iarg = 0; iarg < (int)nArgs; ++iarg) {
214 if (count) sig << (fa ? ", " : ",");
215
216 sig << Cppyy::GetMethodArgType(fMethod, iarg);
217
218 if (fa) {
219 const std::string& parname = Cppyy::GetMethodArgName(fMethod, iarg);
220 if (!parname.empty())
221 sig << " " << parname;
222
223 const std::string& defvalue = Cppyy::GetMethodArgDefault(fMethod, iarg);
224 if (!defvalue.empty())
225 sig << " = " << defvalue;
226 }
227 count++;
228 }
229 sig << ")";
230 return sig.str();
231}
232
233//----------------------------------------------------------------------------
235{
236// helper to report errors in a consistent format (derefs msg)
237 std::string details{};
238
239 PyObject *etype = nullptr, *evalue = nullptr;
240 if (PyErr_Occurred()) {
241 PyObject* etrace = nullptr;
242
243 PyErr_Fetch(&etype, &evalue, &etrace);
244
245 if (evalue) {
246 PyObject* descr = PyObject_Str(evalue);
247 if (descr) {
248 details = CPyCppyy_PyText_AsString(descr);
249 Py_DECREF(descr);
250 }
251 }
252
253 Py_XDECREF(etrace);
254 }
255
256 PyObject* doc = GetDocString();
257 PyObject* errtype = etype;
258 if (!errtype)
259 errtype = PyExc_TypeError;
260 PyObject* pyname = PyObject_GetAttr(errtype, PyStrings::gName);
261 const char* cname = pyname ? CPyCppyy_PyText_AsString(pyname) : "Exception";
262
263 if (!PyType_IsSubtype((PyTypeObject*)errtype, &CPPExcInstance_Type)) {
264 if (details.empty()) {
265 PyErr_Format(errtype, "%s =>\n %s: %s", CPyCppyy_PyText_AsString(doc),
266 cname, msg ? CPyCppyy_PyText_AsString(msg) : "");
267 } else if (msg) {
268 PyErr_Format(errtype, "%s =>\n %s: %s (%s)",
270 details.c_str());
271 } else {
272 PyErr_Format(errtype, "%s =>\n %s: %s",
273 CPyCppyy_PyText_AsString(doc), cname, details.c_str());
274 }
275 } else {
276 Py_XDECREF(((CPPExcInstance*)evalue)->fTopMessage);
277 if (msg) {
278 ((CPPExcInstance*)evalue)->fTopMessage = CPyCppyy_PyText_FromFormat(\
279 "%s =>\n %s: %s | ", CPyCppyy_PyText_AsString(doc), cname, CPyCppyy_PyText_AsString(msg));
280 } else {
281 ((CPPExcInstance*)evalue)->fTopMessage = CPyCppyy_PyText_FromFormat(\
282 "%s =>\n %s: ", CPyCppyy_PyText_AsString(doc), cname);
283 }
284 PyErr_SetObject(errtype, evalue);
285 }
286
287 Py_XDECREF(pyname);
288 Py_XDECREF(evalue);
289 Py_XDECREF(etype);
290 Py_DECREF(doc);
291 Py_XDECREF(msg);
292}
293
294//- constructors and destructor ----------------------------------------------
297 fMethod(method), fScope(scope), fExecutor(nullptr), fArgIndices(nullptr),
298 fArgsRequired(-1)
299{
300 // empty
301}
302
303//----------------------------------------------------------------------------
305 PyCallable(other), fMethod(other.fMethod), fScope(other.fScope)
306{
307 Copy_(other);
308}
309
310//----------------------------------------------------------------------------
312{
313 if (this != &other) {
314 Destroy_();
315 Copy_(other);
316 fScope = other.fScope;
317 fMethod = other.fMethod;
318 }
319
320 return *this;
321}
322
323//----------------------------------------------------------------------------
325{
326 Destroy_();
327}
328
329
330//- public members -----------------------------------------------------------
332{
333// construct python string from the method's prototype
334 return CPyCppyy_PyText_FromFormat("%s%s %s::%s%s",
335 (Cppyy::IsStaticMethod(fMethod) ? "static " : ""),
336 Cppyy::GetMethodResultType(fMethod).c_str(),
337 Cppyy::GetScopedFinalName(fScope).c_str(), Cppyy::GetMethodName(fMethod).c_str(),
338 GetSignatureString(fa).c_str());
339}
340
341//----------------------------------------------------------------------------
343{
344// To help with overload selection, methods are given a priority based on the
345// affinity of Python and C++ types. Priority only matters for methods that have
346// an equal number of arguments and types that are possible substitutes (the
347// normal selection mechanisms would simply distinguish them otherwise).
348
349// The following types are ordered, in favor (variants implicit):
350//
351// bool >> long >> int >> short
352// double >> long double >> float
353// const char* >> char
354//
355// Further, all integer types are preferred over floating point b/c int to float
356// is allowed implicitly, float to int is not.
357//
358// Special cases that are disliked include void* and unknown/incomplete types.
359// Also, moves are preferred over references. std::initializer_list is not a nice
360// conversion candidate either, but needs to be higher priority to mix well with
361// implicit conversions.
362// TODO: extend this to favour classes that are not bases.
363// TODO: profile this method (it's expensive, but should be called too often)
364
365 int priority = 0;
366
367 const size_t nArgs = Cppyy::GetMethodNumArgs(fMethod);
368 for (int iarg = 0; iarg < (int)nArgs; ++iarg) {
369 const std::string aname = Cppyy::GetMethodArgType(fMethod, iarg);
370
371 if (Cppyy::IsBuiltin(aname)) {
372 // integer types
373 if (strstr(aname.c_str(), "bool"))
374 priority += 1; // bool over int (does accept 1 and 0)
375 else if (strstr(aname.c_str(), "long long"))
376 priority += -5; // will very likely fit
377 else if (strstr(aname.c_str(), "long"))
378 priority += -10; // most affine integer type
379 // no need to compare with int; leave at zero
380 else if (strstr(aname.c_str(), "short"))
381 priority += -50; // not really relevant as a type
382
383 // floating point types (note all numbers lower than integer types)
384 else if (strstr(aname.c_str(), "float"))
385 priority += -100; // not really relevant as a type
386 else if (strstr(aname.c_str(), "long double"))
387 priority += -90; // fits double with least loss of precision
388 else if (strstr(aname.c_str(), "double"))
389 priority += -80; // most affine floating point type
390
391 // string/char types
392 else if (strstr(aname.c_str(), "char") && aname[aname.size()-1] != '*')
393 priority += -60; // prefer (const) char* over char
394
395 // oddball
396 else if (strstr(aname.c_str(), "void*"))
397 priority -= 1000; // void*/void** shouldn't be too greedy
398
399 } else {
400 // This is a user-defined type (class, struct, enum, etc.).
401
402 // There's a bit of hysteresis here for templates: once GetScope() is called, their
403 // IsComplete() succeeds, the other way around it does not. Since GetPriority() is
404 // likely called several times in a sort, the GetScope() _must_ come first, or
405 // different GetPriority() calls may return different results (since the 2nd time,
406 // GetScope() will have been called from the first), killing the stable_sort.
407
408 // prefer more derived classes
409 const std::string& clean_name = TypeManip::clean_type(aname, false);
410 Cppyy::TCppScope_t scope = Cppyy::GetScope(clean_name);
411 if (scope)
412 priority += (int)Cppyy::GetNumBases(scope);
413
414 if (Cppyy::IsEnum(clean_name))
415 priority -= 100;
416
417 // a couple of special cases as explained above
418 if (aname.find("initializer_list") != std::string::npos) {
419 priority += 150; // needed for proper implicit conversion rules
420 } else if (aname.rfind("&&", aname.size()-2) != std::string::npos) {
421 priority += 100; // prefer moves over other ref/ptr
422 } else if (!aname.empty() && !Cppyy::IsComplete(aname)) {
423 // class is known, but no dictionary available, 2 more cases: * and &
424 if (aname[aname.size() - 1] == '&')
425 priority += -5000;
426 else
427 priority += -2000; // prefer pointer passing over reference
428 }
429 }
430 }
431
432// prefer methods w/o optional arguments b/c ones with optional arguments are easier to
433// select by providing the optional arguments explicitly
434 priority += ((int)Cppyy::GetMethodReqArgs(fMethod) - (int)nArgs);
435
436// add a small penalty to prefer non-const methods over const ones for get/setitem
437 if (Cppyy::IsConstMethod(fMethod) && Cppyy::GetMethodName(fMethod) == "operator[]")
438 priority += -10;
439
440 return priority;
441}
442
443//----------------------------------------------------------------------------
445{
446// Methods will all void*-like arguments should be sorted after template
447// instanstations, so that they don't greedily take over pointers to object.
448// GetPriority() is too heavy-handed, as it will pull in all the argument
449// types, so use this cheaper check.
450 const size_t nArgs = Cppyy::GetMethodReqArgs(fMethod);
451 if (!nArgs) return false;
452
453 for (int iarg = 0; iarg < (int)nArgs; ++iarg) {
454 const std::string aname = Cppyy::GetMethodArgType(fMethod, iarg);
455 if (aname.find("void*") != 0)
456 return false;
457 }
458 return true;
459}
460
461
462//----------------------------------------------------------------------------
464{
465 return (int)Cppyy::GetMethodNumArgs(fMethod);
466}
467
468//----------------------------------------------------------------------------
470{
471// Build a tuple of the argument types/names.
472 int co_argcount = (int)GetMaxArgs() /* +1 for self */;
473
474// TODO: static methods need no 'self' (but is harmless otherwise)
475
476 PyObject* co_varnames = PyTuple_New(co_argcount+1 /* self */);
477 PyTuple_SET_ITEM(co_varnames, 0, CPyCppyy_PyText_FromString("self"));
478 for (int iarg = 0; iarg < co_argcount; ++iarg) {
479 std::string argrep = Cppyy::GetMethodArgType(fMethod, iarg);
480 const std::string& parname = Cppyy::GetMethodArgName(fMethod, iarg);
481 if (!parname.empty()) {
482 argrep += " ";
483 argrep += parname;
484 }
485
486 PyObject* pyspec = CPyCppyy_PyText_FromString(argrep.c_str());
487 PyTuple_SET_ITEM(co_varnames, iarg+1, pyspec);
488 }
489
490 return co_varnames;
491}
492
494{
495// get the default value (if any) of argument iarg of this method
496 if (iarg >= (int)GetMaxArgs())
497 return nullptr;
498
499 const std::string& defvalue = Cppyy::GetMethodArgDefault(fMethod, iarg);
500 if (!defvalue.empty()) {
501
502 // attempt to evaluate the string representation (will work for all builtin types)
503 PyObject* pyval = (PyObject*)PyRun_String(
504 (char*)defvalue.c_str(), Py_eval_input, gThisModule, gThisModule);
505 if (!pyval && PyErr_Occurred()) {
506 PyErr_Clear();
507 return CPyCppyy_PyText_FromString(defvalue.c_str());
508 }
509
510 return pyval;
511 }
512
513 return nullptr;
514}
515
516//----------------------------------------------------------------------------
518{
519// Get or build the scope of this method.
520 return CreateScopeProxy(fScope);
521}
522
523
524//----------------------------------------------------------------------------
526{
527// Return the C++ pointer of this function
528 return Cppyy::GetFunctionAddress(fMethod, false /* don't check fast path envar */);
529}
530
531
532//----------------------------------------------------------------------------
534{
535// done if cache is already setup
536 if (fArgsRequired != -1)
537 return true;
538
539 if (!InitConverters_())
540 return false;
541
542 if (!InitExecutor_(fExecutor, ctxt))
543 return false;
544
545// minimum number of arguments when calling
546 fArgsRequired = (int)((bool)fMethod == true ? Cppyy::GetMethodReqArgs(fMethod) : 0);
547
548 return true;
549}
550
551//----------------------------------------------------------------------------
553{
554 if (!PyDict_CheckExact(kwds)) {
555 SetPyError_(CPyCppyy_PyText_FromString("received unknown keyword arguments object"));
556 return nullptr;
557 }
558
559 if (PyDict_Size(kwds) == 0 && !self) {
560 Py_INCREF(args);
561 return args;
562 }
563
564 if (!fArgIndices) {
565 fArgIndices = new std::map<std::string, int>{};
566 for (int iarg = 0; iarg < (int)Cppyy::GetMethodNumArgs(fMethod); ++iarg)
567 (*fArgIndices)[Cppyy::GetMethodArgName(fMethod, iarg)] = iarg;
568 }
569
570 Py_ssize_t nKeys = PyDict_Size(kwds);
571 Py_ssize_t nArgs = PyTuple_GET_SIZE(args) + (self ? 1 : 0);
572 if (nKeys+nArgs < fArgsRequired) {
573 SetPyError_(CPyCppyy_PyText_FromFormat(
574 "takes at least %d arguments (%zd given)", fArgsRequired, nKeys+nArgs));
575 return nullptr;
576 }
577
578 PyObject* newArgs = PyTuple_New(nArgs+nKeys);
579
580// set all values to zero to be able to check them later (this also guarantees normal
581// cleanup by the tuple deallocation)
582 for (Py_ssize_t i = 0; i < nArgs+nKeys; ++i)
583 PyTuple_SET_ITEM(newArgs, i, nullptr);
584
585// next, insert the keyword values
586 PyObject *key, *value;
587 Py_ssize_t pos = 0;
588
589 while (PyDict_Next(kwds, &pos, &key, &value)) {
590 const char* ckey = CPyCppyy_PyText_AsStringChecked(key);
591 if (!ckey) {
592 Py_DECREF(newArgs);
593 return nullptr;
594 }
595 auto p = fArgIndices->find(ckey);
596 if (p == fArgIndices->end()) {
597 SetPyError_(CPyCppyy_PyText_FromFormat("%s::%s got an unexpected keyword argument \'%s\'",
598 Cppyy::GetFinalName(fScope).c_str(), Cppyy::GetMethodName(fMethod).c_str(), ckey));
599 Py_DECREF(newArgs);
600 return nullptr;
601 }
602 Py_INCREF(value);
603 PyTuple_SetItem(newArgs, (*fArgIndices)[ckey], value);
604 }
605
606// fill out the rest of the arguments
607 Py_ssize_t start = 0;
608 if (self) {
609 Py_INCREF(self);
610 PyTuple_SET_ITEM(newArgs, 0, self);
611 start = 1;
612 }
613
614 for (Py_ssize_t i = start; i < nArgs; ++i) {
615 if (PyTuple_GET_ITEM(newArgs, i)) {
616 SetPyError_(CPyCppyy_PyText_FromFormat("%s::%s got multiple values for argument %d",
617 Cppyy::GetFinalName(fScope).c_str(), Cppyy::GetMethodName(fMethod).c_str(), (int)i+1));
618 Py_DECREF(newArgs);
619 return nullptr;
620 }
621
622 PyObject* item = PyTuple_GET_ITEM(args, i);
623 Py_INCREF(item);
624 PyTuple_SET_ITEM(newArgs, i, item);
625 }
626
627 return newArgs;
628}
629
630//----------------------------------------------------------------------------
632 CPPInstance*& self, PyObject* args, PyObject* kwds)
633{
634// verify existence of self, return if ok
635 if (self) {
636 if (kwds) return ProcessKeywords(nullptr, args, kwds);
637 Py_INCREF(args);
638 return args;
639 }
640
641// otherwise, check for a suitable 'self' in args and update accordingly
642 if (PyTuple_GET_SIZE(args) != 0) {
643 CPPInstance* pyobj = (CPPInstance*)PyTuple_GET_ITEM(args, 0);
644
645 // demand CPyCppyy object, and an argument that may match down the road
646 if (CPPInstance_Check(pyobj) &&
647 (fScope == Cppyy::gGlobalScope || // free global
648 (pyobj->ObjectIsA() == 0) || // null pointer or ctor call
649 (Cppyy::IsSubtype(pyobj->ObjectIsA(), fScope)))) { // matching types
650
651 // reset self
652 Py_INCREF(pyobj); // corresponding Py_DECREF is in CPPOverload
653 self = pyobj;
654
655 // offset args by 1 (new ref)
656 PyObject* newArgs = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
657
658 // put the keywords, if any, in their places in the arguments array
659 if (kwds) {
660 args = ProcessKeywords(nullptr, newArgs, kwds);
661 Py_DECREF(newArgs);
662 newArgs = args;
663 }
664
665 return newArgs; // may be nullptr if kwds insertion failed
666 }
667 }
668
669// no self, set error and lament
670 SetPyError_(CPyCppyy_PyText_FromFormat(
671 "unbound method %s::%s must be called with a %s instance as first argument",
672 Cppyy::GetFinalName(fScope).c_str(), Cppyy::GetMethodName(fMethod).c_str(),
673 Cppyy::GetFinalName(fScope).c_str()));
674 return nullptr;
675}
676
677//----------------------------------------------------------------------------
679{
680 Py_ssize_t argc = PyTuple_GET_SIZE(args);
681 Py_ssize_t argMax = (Py_ssize_t)fConverters.size();
682
683 if (argMax != argc) {
684 // argc must be between min and max number of arguments
685 if (argc < (Py_ssize_t)fArgsRequired) {
686 SetPyError_(CPyCppyy_PyText_FromFormat(
687 "takes at least %d arguments (%zd given)", fArgsRequired, argc));
688 return false;
689 } else if (argMax < argc) {
690 SetPyError_(CPyCppyy_PyText_FromFormat(
691 "takes at most %zd arguments (%zd given)", argMax, argc));
692 return false;
693 }
694 }
695
696 if (argc == 0)
697 return true;
698
699// pass current scope for which the call is made
700 ctxt->fCurScope = fScope;
701
702// convert the arguments to the method call array
703 bool isOK = true;
704 Parameter* cppArgs = ctxt->GetArgs(argc);
705 for (int i = 0; i < (int)argc; ++i) {
706 if (!fConverters[i]->SetArg(PyTuple_GET_ITEM(args, i), cppArgs[i], ctxt)) {
707 SetPyError_(CPyCppyy_PyText_FromFormat("could not convert argument %d", i+1));
708 isOK = false;
709 break;
710 }
711 }
712
713 return isOK;
714}
715
716//----------------------------------------------------------------------------
717PyObject* CPyCppyy::CPPMethod::Execute(void* self, ptrdiff_t offset, CallContext* ctxt)
718{
719// call the interface method
720 PyObject* result = 0;
721
723 !(ctxt->fFlags & CallContext::kProtected)) {
724 // bypasses try block (i.e. segfaults will abort)
725 result = ExecuteFast(self, offset, ctxt);
726 } else {
727 // at the cost of ~10% performance, don't abort the interpreter on any signal
728 result = ExecuteProtected(self, offset, ctxt);
729 }
730
731// TODO: the following is dreadfully slow and dead-locks on Apache: revisit
732// raising exceptions through callbacks by using magic returns
733// if (result && Utility::PyErr_Occurred_WithGIL()) {
734// // can happen in the case of a CINT error: trigger exception processing
735// Py_DECREF(result);
736// result = 0;
737// } else if (!result && PyErr_Occurred())
738 if (!result && PyErr_Occurred())
739 SetPyError_(0);
740
741 return result;
742}
743
744//----------------------------------------------------------------------------
746 CPPInstance*& self, PyObject* args, PyObject* kwds, CallContext* ctxt)
747{
748// setup as necessary
749 if (fArgsRequired == -1 && !Initialize(ctxt))
750 return nullptr;
751
752// fetch self, verify, and put the arguments in usable order
753 if (!(args = PreProcessArgs(self, args, kwds)))
754 return nullptr;
755
756// translate the arguments
757 if (fArgsRequired || PyTuple_GET_SIZE(args)) {
758 if (!ConvertAndSetArgs(args, ctxt)) {
759 Py_DECREF(args);
760 return nullptr;
761 }
762 }
763
764// get the C++ object that this object proxy is a handle for
765 void* object = self->GetObject();
766
767// validity check that should not fail
768 if (!object) {
769 PyErr_SetString(PyExc_ReferenceError, "attempt to access a null-pointer");
770 Py_DECREF(args);
771 return nullptr;
772 }
773
774// get its class
775 Cppyy::TCppType_t derived = self->ObjectIsA();
776
777// calculate offset (the method expects 'this' to be an object of fScope)
778 ptrdiff_t offset = 0;
779 if (derived && derived != fScope)
780 offset = Cppyy::GetBaseOffset(derived, fScope, object, 1 /* up-cast */);
781
782// actual call; recycle self instead of returning new object for same address objects
783 CPPInstance* pyobj = (CPPInstance*)Execute(object, offset, ctxt);
784 Py_DECREF(args);
785
786 if (CPPInstance_Check(pyobj) &&
787 derived && pyobj->ObjectIsA() == derived &&
788 pyobj->GetObject() == object) {
789 Py_INCREF((PyObject*)self);
790 Py_DECREF(pyobj);
791 return (PyObject*)self;
792 }
793
794 return (PyObject*)pyobj;
795}
796
797//- protected members --------------------------------------------------------
799{
800// construct python string from the method's signature
801 return CPyCppyy_PyText_FromString(GetSignatureString(fa).c_str());
802}
803
804//----------------------------------------------------------------------------
806{
807 return Cppyy::GetMethodResultType(fMethod);
808}
int Py_ssize_t
Definition: CPyCppyy.h:236
#define CPyCppyy_PyText_AsString
Definition: CPyCppyy.h:97
#define CPyCppyy_PyText_FromFormat
Definition: CPyCppyy.h:101
#define CPyCppyy_PyText_AsStringChecked
Definition: CPyCppyy.h:98
#define CPyCppyy_PyText_FromString
Definition: CPyCppyy.h:102
_object PyObject
Definition: PyMethodBase.h:42
#define e(i)
Definition: RSha256.hxx:103
#define CATCH(n)
Definition: TException.h:63
#define ENDTRY
Definition: TException.h:69
#define TRY
Definition: TException.h:56
#define pyname
Definition: TMCParticle.cxx:19
Cppyy::TCppType_t ObjectIsA(bool check_smart=true) const
Definition: CPPInstance.h:106
std::string GetReturnTypeName()
Definition: CPPMethod.cxx:805
virtual ~CPPMethod()
Definition: CPPMethod.cxx:324
bool Initialize(CallContext *ctxt=nullptr)
Definition: CPPMethod.cxx:533
void SetPyError_(PyObject *msg)
Definition: CPPMethod.cxx:234
virtual PyObject * GetSignature(bool show_formalargs=true)
Definition: CPPMethod.cxx:798
Cppyy::TCppScope_t fScope
Definition: CPPMethod.h:76
virtual bool InitExecutor_(Executor *&, CallContext *ctxt=nullptr)
Definition: CPPMethod.cxx:193
virtual PyObject * GetArgDefault(int iarg)
Definition: CPPMethod.cxx:493
virtual PyObject * GetPrototype(bool show_formalargs=true)
Definition: CPPMethod.cxx:331
std::string GetSignatureString(bool show_formalargs=true)
Definition: CPPMethod.cxx:207
PyObject * ExecuteProtected(void *, ptrdiff_t, CallContext *)
Definition: CPPMethod.cxx:140
std::map< std::string, int > * fArgIndices
Definition: CPPMethod.h:81
virtual PyObject * GetCoVarNames()
Definition: CPPMethod.cxx:469
virtual PyObject * GetScopeProxy()
Definition: CPPMethod.cxx:517
PyObject * ExecuteFast(void *, ptrdiff_t, CallContext *)
Definition: CPPMethod.cxx:66
PyObject * Execute(void *self, ptrdiff_t offset, CallContext *ctxt=nullptr)
Definition: CPPMethod.cxx:717
void Copy_(const CPPMethod &)
Definition: CPPMethod.cxx:37
virtual int GetPriority()
Definition: CPPMethod.cxx:342
PyObject * ProcessKeywords(PyObject *self, PyObject *args, PyObject *kwds)
Definition: CPPMethod.cxx:552
Executor * fExecutor
Definition: CPPMethod.h:77
Cppyy::TCppMethod_t fMethod
Definition: CPPMethod.h:75
virtual PyObject * Call(CPPInstance *&self, PyObject *args, PyObject *kwds, CallContext *ctxt=nullptr)
Definition: CPPMethod.cxx:745
virtual bool IsGreedy()
Definition: CPPMethod.cxx:444
bool ConvertAndSetArgs(PyObject *args, CallContext *ctxt=nullptr)
Definition: CPPMethod.cxx:678
CPPMethod & operator=(const CPPMethod &)
Definition: CPPMethod.cxx:311
virtual PyObject * PreProcessArgs(CPPInstance *&self, PyObject *args, PyObject *kwds)
Definition: CPPMethod.cxx:631
virtual int GetMaxArgs()
Definition: CPPMethod.cxx:463
CPPMethod(Cppyy::TCppScope_t scope, Cppyy::TCppMethod_t method)
Definition: CPPMethod.cxx:295
virtual Cppyy::TCppFuncAddr_t GetFunctionAddress()
Definition: CPPMethod.cxx:525
R__EXTERN PyObject * gName
Definition: TPython.cxx:105
std::string clean_type(const std::string &cppname, bool template_strip=true, bool const_strip=true)
Definition: TypeManip.cxx:98
std::string extract_namespace(const std::string &name)
Definition: TypeManip.cxx:159
Set of helper functions that are invoked from the pythonizors, on the Python side.
PyObject * gAbrtException
PyTypeObject CPPExcInstance_Type
PyObject * GetScopeProxy(Cppyy::TCppScope_t)
PyObject * gSegvException
PyObject * BindCppObjectNoCast(Cppyy::TCppObject_t object, Cppyy::TCppType_t klass, const unsigned flags=0)
CPYCPPYY_EXTERN Executor * CreateExecutor(const std::string &name)
Definition: Executors.cxx:748
bool CPPInstance_Check(T *object)
Definition: CPPInstance.h:118
PyObject * CreateScopeProxy(Cppyy::TCppScope_t)
R__EXTERN PyObject * gThisModule
Definition: TPython.cxx:100
PyObject * gIllException
PyObject * gBusException
CPYCPPYY_EXTERN Converter * CreateConverter(const std::string &name, Py_ssize_t *dims=nullptr)
RPY_EXPORTED TCppType_t GetActualClass(TCppType_t klass, TCppObject_t obj)
RPY_EXPORTED ptrdiff_t GetBaseOffset(TCppType_t derived, TCppType_t base, TCppObject_t address, int direction, bool rerror=false)
intptr_t TCppMethod_t
Definition: cpp_cppyy.h:22
RPY_EXPORTED std::string GetMethodArgDefault(TCppMethod_t, TCppIndex_t iarg)
RPY_EXPORTED bool IsBuiltin(const std::string &type_name)
void * TCppObject_t
Definition: cpp_cppyy.h:21
TCppScope_t TCppType_t
Definition: cpp_cppyy.h:19
RPY_EXPORTED bool IsComplete(const std::string &type_name)
RPY_EXPORTED bool IsEnum(const std::string &type_name)
RPY_EXPORTED std::string GetFinalName(TCppType_t type)
RPY_EXPORTED TCppIndex_t GetMethodReqArgs(TCppMethod_t)
RPY_EXPORTED bool IsStaticMethod(TCppMethod_t method)
RPY_EXPORTED std::string GetMethodName(TCppMethod_t)
RPY_EXPORTED TCppIndex_t GetNumBases(TCppType_t type)
RPY_EXPORTED std::string GetMethodArgName(TCppMethod_t, TCppIndex_t iarg)
RPY_EXPORTED bool IsSubtype(TCppType_t derived, TCppType_t base)
RPY_EXPORTED bool IsConstMethod(TCppMethod_t)
size_t TCppScope_t
Definition: cpp_cppyy.h:18
RPY_EXPORTED TCppFuncAddr_t GetFunctionAddress(TCppMethod_t method, bool check_enabled=true)
RPY_EXPORTED TCppIndex_t GetMethodNumArgs(TCppMethod_t)
RPY_EXPORTED std::string GetScopedFinalName(TCppType_t type)
RPY_EXPORTED TCppScope_t GetScope(const std::string &scope_name)
RPY_EXPORTED std::string GetMethodResultType(TCppMethod_t)
RPY_EXPORTED TCppScope_t gGlobalScope
Definition: cpp_cppyy.h:51
void * TCppFuncAddr_t
Definition: cpp_cppyy.h:25
RPY_EXPORTED std::string GetMethodArgType(TCppMethod_t, TCppIndex_t iarg)
void Initialize(Bool_t useTMVAStyle=kTRUE)
Definition: tmvaglob.cxx:176
Cppyy::TCppScope_t fCurScope
Definition: CallContext.h:99
Parameter * GetArgs(size_t sz)
Definition: CallContext.h:81
static ECallFlags sSignalPolicy
Definition: CallContext.h:78