Skip to content

Commit

Permalink
add improved error handling to tpp_overload as well
Browse files Browse the repository at this point in the history
  • Loading branch information
bendavid committed Oct 1, 2024
1 parent 6fb1f43 commit 0bf06ad
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions bindings/pyroot/cppyy/CPyCppyy/src/TemplateProxy.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,8 @@ static PyObject* tpp_overload(TemplateProxy* pytmpl, PyObject* args)
Cppyy::TCppMethod_t cppmeth = (Cppyy::TCppMethod_t) 0;
std::string proto;

std::ostringstream diagnostics;

if (PyArg_ParseTuple(args, const_cast<char*>("s|i:__overload__"), &sigarg, &want_const)) {
want_const = PyTuple_GET_SIZE(args) == 1 ? -1 : want_const;

Expand All @@ -825,7 +827,7 @@ static PyObject* tpp_overload(TemplateProxy* pytmpl, PyObject* args)

scope = ((CPPClass*)pytmpl->fTI->fPyClass)->fCppType;
cppmeth = Cppyy::GetMethodTemplate(
scope, pytmpl->fTI->fCppName, proto.substr(1, proto.size()-2));
scope, pytmpl->fTI->fCppName, proto.substr(1, proto.size()-2), diagnostics);
} else if (PyArg_ParseTuple(args, const_cast<char*>("O|i:__overload__"), &sigarg_tuple, &want_const)) {
PyErr_Clear();
want_const = PyTuple_GET_SIZE(args) == 1 ? -1 : want_const;
Expand Down Expand Up @@ -857,17 +859,27 @@ static PyObject* tpp_overload(TemplateProxy* pytmpl, PyObject* args)

scope = ((CPPClass*)pytmpl->fTI->fPyClass)->fCppType;
cppmeth = Cppyy::GetMethodTemplate(
scope, pytmpl->fTI->fCppName, proto.substr(1, proto.size()-2));
scope, pytmpl->fTI->fCppName, proto.substr(1, proto.size()-2), diagnostics);
} else {
PyErr_Format(PyExc_TypeError, "Unexpected arguments to __overload__");
return nullptr;
}

const bool emptydiag = diagnostics.str().find_first_not_of(' ') == diagnostics.str().npos;
if (!emptydiag) {
std::ostringstream warnmsg;
warnmsg << "Compiler warnings during instantiation of \"" << pytmpl->fTI->fCppName << "(" << proto << ")\"\n"
<< diagnostics.str();
PyErr_WarnEx(PyExc_Warning, warnmsg.str().c_str(), 1);
}

// else attempt instantiation
PyObject* pytype = 0, *pyvalue = 0, *pytrace = 0;
PyErr_Fetch(&pytype, &pyvalue, &pytrace);

if (!cppmeth) {
PyErr_Format(PyExc_TypeError, "Failed to instantiate \"%s(%s)\"\n%s", pytmpl->fTI->fCppName.c_str(), proto.c_str(),
diagnostics.str().c_str());
PyErr_Restore(pytype, pyvalue, pytrace);
return nullptr;
}
Expand Down

0 comments on commit 0bf06ad

Please sign in to comment.