Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
CPPClassMethod.cxx
Go to the documentation of this file.
1// Bindings
2#include "cpyrt.h"
3
4using namespace cppjit;
5#include "CPPClassMethod.h"
6#include "CPPInstance.h"
7
8//- public members
9//--------------------------------------------------------------
11 size_t nargsf, PyObject* kwds,
12 CallContext* ctxt) {
13 // preliminary check in case keywords are accidently used (they are ignored
14 // otherwise)
15 if (kwds && ((PyDict_Check(kwds) && PyDict_Size(kwds)) ||
16 (PyTuple_CheckExact(kwds) && PyTuple_GET_SIZE(kwds)))) {
17 PyErr_SetString(PyExc_TypeError, "keyword arguments are not yet supported");
18 return nullptr;
19 }
20
21 // setup as necessary
22 if (!this->Initialize(ctxt))
23 return nullptr;
24
25 // translate the arguments
26 // TODO: The following is not robust and should be revisited e.g. by making
27 // CPPOverloads that have only CPPClassMethods be true Python classmethods?
28 // Note that the original implementation wasn't 100% correct either (e.g.
29 // static size() mapped to len()).
30 //
31 // As-is, if no suitable `self` is given (normal case), but the type of the
32 // first argument matches the enclosing scope of the class method and it isn't
33 // needed for the call, then assume that the method was (incorrectly) bound
34 // and so drop that instance from args.
35 int nargs = (int)cpyrt_PyArgs_GET_SIZE(args, nargsf);
36 if ((!self || (PyObject*)self == Py_None) && nargs) {
37 PyObject* arg0 = cpyrt_PyArgs_GET_ITEM(args, 0);
38 if (CPPInstance_Check(arg0) && fArgsRequired <= nargs - 1 &&
39 interop::IsSubclass(reinterpret_cast<CPPInstance*>(arg0)->ObjectIsA(),
40 GetScope())) {
41 args += 1; // drops first argument
42 nargsf -= 1;
43 }
44 }
45
46 if (!this->ConvertAndSetArgs(args, nargsf, ctxt))
47 return nullptr;
48
49 // execute function
50 return this->Execute(nullptr, 0, ctxt);
51}
52
53//----------------------------------------------------------------------------
55 PyObject* cppname =
56 PyUnicode_FromString((GetReturnTypeName() + " (*)").c_str());
57 PyUnicode_AppendAndDel(&cppname, GetSignature(false /* show_formalargs */));
58 return cppname;
59}
_object PyObject
PyObject * Call(CPPInstance *&self, cpyrt_PyArgs_t args, size_t nargsf, PyObject *kwds, CallContext *ctxt=nullptr) override
PyObject * GetTypeName() override
bool Initialize(CallContext *ctxt=nullptr)
PyObject * Execute(void *self, ptrdiff_t offset, CallContext *ctxt=nullptr)
bool ConvertAndSetArgs(cpyrt_PyArgs_t, size_t nargsf, CallContext *ctxt=nullptr)
interop::TCppScope_t GetScope()
Definition CPPMethod.h:93
PyObject *const * cpyrt_PyArgs_t
Definition cpyrt.h:81
static Py_ssize_t cpyrt_PyArgs_GET_SIZE(cpyrt_PyArgs_t, size_t nargsf)
Definition cpyrt.h:90
static PyObject * cpyrt_PyArgs_GET_ITEM(cpyrt_PyArgs_t args, Py_ssize_t i)
Definition cpyrt.h:82
bool CPPInstance_Check(T *object)
bool IsSubclass(TCppScope_t derived, TCppScope_t base)
Definition API.cxx:34