Skip to content

Commit 7916d69

Browse files
committed
[PyROOT] Get names and types of all overloads' signature
1 parent ca72e32 commit 7916d69

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

bindings/pyroot/cppyy/CPyCppyy/src/CPPMethod.cxx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,8 +801,41 @@ PyObject* CPyCppyy::CPPMethod::GetSignature(bool fa)
801801
return CPyCppyy_PyText_FromString(GetSignatureString(fa).c_str());
802802
}
803803

804+
PyObject *CPyCppyy::CPPMethod::GetSignatureNames()
805+
{
806+
// Build a tuple of the argument names for this signature.
807+
int argcount = GetMaxArgs();
808+
809+
PyObject *signature_names = PyTuple_New(argcount);
810+
for (int iarg = 0; iarg < argcount; ++iarg) {
811+
const std::string &argname_cpp = Cppyy::GetMethodArgName(fMethod, iarg);
812+
813+
PyObject *argname_py = CPyCppyy_PyText_FromString(argname_cpp.c_str());
814+
PyTuple_SET_ITEM(signature_names, iarg, argname_py);
815+
}
816+
817+
return signature_names;
818+
}
819+
820+
PyObject *CPyCppyy::CPPMethod::GetSignatureTypes()
821+
{
822+
// Build a tuple of the argument types for this signature.
823+
int argcount = GetMaxArgs();
824+
825+
PyObject *signature_types = PyTuple_New(argcount);
826+
for (int iarg = 0; iarg < argcount; ++iarg) {
827+
const std::string &argtype_cpp = Cppyy::GetMethodArgType(fMethod, iarg);
828+
829+
PyObject *argtype_py = CPyCppyy_PyText_FromString(argtype_cpp.c_str());
830+
PyTuple_SET_ITEM(signature_types, iarg, argtype_py);
831+
}
832+
833+
return signature_types;
834+
}
835+
804836
//----------------------------------------------------------------------------
805837
std::string CPyCppyy::CPPMethod::GetReturnTypeName()
806838
{
807839
return Cppyy::GetMethodResultType(fMethod);
808840
}
841+

bindings/pyroot/cppyy/CPyCppyy/src/CPPMethod.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class CPPMethod : public PyCallable {
2424

2525
public:
2626
virtual PyObject* GetSignature(bool show_formalargs = true);
27+
PyObject* GetSignatureNames() override;
28+
PyObject* GetSignatureTypes() override;
2729
virtual PyObject* GetPrototype(bool show_formalargs = true);
2830
virtual int GetPriority();
2931
virtual bool IsGreedy();
@@ -88,3 +90,4 @@ class CPPMethod : public PyCallable {
8890
} // namespace CPyCppyy
8991

9092
#endif // !CPYCPPYY_CPPMETHOD_H
93+

bindings/pyroot/cppyy/CPyCppyy/src/CPPOverload.cxx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ class TPythonCallback : public PyCallable {
5858
virtual PyObject* GetSignature(bool /*show_formalargs*/ = true) {
5959
return CPyCppyy_PyText_FromString("*args, **kwargs");
6060
}
61+
62+
63+
PyObject* GetSignatureNames() override {
64+
return PyTuple_New(0);
65+
}
66+
67+
PyObject* GetSignatureTypes() override {
68+
return PyTuple_New(0);
69+
}
70+
6171
virtual PyObject* GetPrototype(bool /*show_formalargs*/ = true) {
6272
return CPyCppyy_PyText_FromString("<callback>");
6373
}
@@ -241,6 +251,42 @@ static PyObject* mp_doc(CPPOverload* pymeth, void*)
241251
return doc;
242252
}
243253

254+
static PyObject* mp_func_overloads_types(CPPOverload* pymeth, void*)
255+
{
256+
257+
CPPOverload::Methods_t& methods = pymeth->fMethodInfo->fMethods;
258+
259+
CPPOverload::Methods_t::size_type nMethods = methods.size();
260+
if (nMethods == 0) // from template proxy with no instantiations
261+
return nullptr;
262+
263+
PyObject* overloads_types_dict = PyDict_New();
264+
for (CPPOverload::Methods_t::size_type i = 0; i < nMethods; ++i) {
265+
PyDict_SetItem(overloads_types_dict, methods[i]->GetSignature(), methods[i]->GetSignatureTypes());
266+
}
267+
268+
return overloads_types_dict;
269+
270+
}
271+
272+
static PyObject* mp_func_overloads_names(CPPOverload* pymeth, void*)
273+
{
274+
275+
CPPOverload::Methods_t& methods = pymeth->fMethodInfo->fMethods;
276+
277+
CPPOverload::Methods_t::size_type nMethods = methods.size();
278+
if (nMethods == 0) // from template proxy with no instantiations
279+
return nullptr;
280+
281+
PyObject* overloads_names_dict = PyDict_New();
282+
for (CPPOverload::Methods_t::size_type i = 0; i < nMethods; ++i) {
283+
PyDict_SetItem(overloads_names_dict, methods[i]->GetSignature(), methods[i]->GetSignatureNames());
284+
}
285+
286+
return overloads_names_dict;
287+
288+
}
289+
244290
//----------------------------------------------------------------------------
245291
static PyObject* mp_meth_func(CPPOverload* pymeth, void*)
246292
{
@@ -514,6 +560,8 @@ static PyGetSetDef mp_getset[] = {
514560
{(char*)"func_globals", (getter)mp_func_globals, nullptr, nullptr, nullptr},
515561
{(char*)"func_doc", (getter)mp_doc, nullptr, nullptr, nullptr},
516562
{(char*)"func_name", (getter)mp_name, nullptr, nullptr, nullptr},
563+
{(char*)"func_overloads_types", (getter)mp_func_overloads_types, nullptr, nullptr, nullptr},
564+
{(char*)"func_overloads_names", (getter)mp_func_overloads_names, nullptr, nullptr, nullptr},
517565

518566
{(char*)"__creates__", (getter)mp_getcreates, (setter)mp_setcreates,
519567
(char*)"For ownership rules of result: if true, objects are python-owned", nullptr},
@@ -971,3 +1019,4 @@ CPyCppyy::CPPOverload::MethodInfo_t::~MethodInfo_t()
9711019
}
9721020

9731021
// TODO: something like PyMethod_Fini to clear up the free_list
1022+

bindings/pyroot/cppyy/CPyCppyy/src/PyCallable.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class PyCallable {
1515

1616
public:
1717
virtual PyObject* GetSignature(bool show_formalargs = true) = 0;
18+
virtual PyObject* GetSignatureNames() = 0;
19+
virtual PyObject* GetSignatureTypes() = 0;
1820
virtual PyObject* GetPrototype(bool show_formalargs = true) = 0;
1921
virtual PyObject* GetDocString() { return GetPrototype(); }
2022

@@ -38,3 +40,4 @@ class PyCallable {
3840
} // namespace CPyCppyy
3941

4042
#endif // !CPYCPPYY_PYCALLABLE_H
43+

0 commit comments

Comments
 (0)