Skip to content

Commit d9e2515

Browse files
bendavidvepadulano
authored andcommitted
suppress warnings and errors when lookup succeeds on a subsequent try
1 parent 629664b commit d9e2515

4 files changed

Lines changed: 40 additions & 16 deletions

File tree

‎bindings/pyroot/cppyy/CPyCppyy/src/Cppyy.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,8 @@ namespace Cppyy {
229229
CPPYY_IMPORT
230230
bool IsMethodTemplate(TCppScope_t scope, TCppIndex_t imeth);
231231
CPPYY_IMPORT
232-
TCppMethod_t
233-
GetMethodTemplate(TCppScope_t scope, const std::string &name, const std::string &proto, std::ostream &diagnostics);
232+
TCppMethod_t GetMethodTemplate(TCppScope_t scope, const std::string &name, const std::string &proto,
233+
std::ostringstream &diagnostics);
234234

235235
CPPYY_IMPORT
236236
TCppIndex_t GetGlobalOperator(

‎bindings/pyroot/cppyy/CPyCppyy/src/TemplateProxy.cxx‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,14 @@ PyObject* TemplateProxy::Instantiate(const std::string& fname,
205205
pos = proto.find("initializer_list", pos + 6);
206206
}
207207

208-
Cppyy::TCppMethod_t m2 = Cppyy::GetMethodTemplate(scope, fname, proto, diagnostics);
208+
std::ostringstream diagnostics2;
209+
Cppyy::TCppMethod_t m2 = Cppyy::GetMethodTemplate(scope, fname, proto, diagnostics2);
209210
if (m2 && m2 != cppmeth) {
210211
// replace if the new method with vector was found; otherwise just continue
211212
// with the previously found method with initializer_list.
212213
cppmeth = m2;
213214
resname = Cppyy::GetMethodFullName(cppmeth);
215+
diagnostics = std::move(diagnostics2);
214216
}
215217
}
216218

@@ -228,6 +230,10 @@ PyObject* TemplateProxy::Instantiate(const std::string& fname,
228230
Py_DECREF(pyol);
229231
Py_DECREF(pycachename);
230232
Py_DECREF(dct);
233+
234+
PyErr_Format(PyExc_TypeError, "Failed to instantiate \"%s(%s)\"\n%s", fname.c_str(), proto.c_str(),
235+
diagnostics.str().c_str());
236+
231237
return nullptr;
232238
}
233239

‎bindings/pyroot/cppyy/cppyy-backend/clingwrapper/src/clingwrapper.cxx‎

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,7 +2109,7 @@ bool template_compare(std::string n1, std::string n2) {
21092109
}
21102110

21112111
Cppyy::TCppMethod_t Cppyy::GetMethodTemplate(TCppScope_t scope, const std::string &name, const std::string &proto,
2112-
std::ostream &diagnostics)
2112+
std::ostringstream &diagnostics)
21132113
{
21142114
// There is currently no clean way of extracting a templated method out of ROOT/meta
21152115
// for a variety of reasons, none of them fundamental. The game played below is to
@@ -2127,6 +2127,7 @@ Cppyy::TCppMethod_t Cppyy::GetMethodTemplate(TCppScope_t scope, const std::strin
21272127
// redirect diagnostics, taking the lock to make sure no other calls pollute the results
21282128
R__WRITE_LOCKGUARD(ROOT::gCoreMutex);
21292129

2130+
const std::string diagnosticsold = diagnostics.str();
21302131
TInterpreter::RedirectDiagnostics redirectRAII(gInterpreter, diagnostics, /*enableColors*/ true, /*indent*/ 4);
21312132

21322133
TFunction *func = nullptr;
@@ -2162,15 +2163,25 @@ Cppyy::TCppMethod_t Cppyy::GetMethodTemplate(TCppScope_t scope, const std::strin
21622163

21632164
if (!func && name.back() == '>' && (cl || scope == (TCppScope_t)GLOBAL_HANDLE)) {
21642165
// try again, ignoring proto in case full name is complete template
2165-
auto declid = gInterpreter->GetFunction(cl, name.c_str());
2166-
if (declid) {
2167-
auto existing = gMethodTemplates.find(declid);
2168-
if (existing == gMethodTemplates.end()) {
2169-
auto cw = new_CallWrapper(declid, name);
2170-
existing = gMethodTemplates.insert(std::make_pair(declid, cw)).first;
2171-
}
2172-
return (TCppMethod_t)existing->second;
2166+
std::ostringstream diagnostics2;
2167+
TInterpreter::RedirectDiagnostics redirectRAII2(gInterpreter, diagnostics2, /*enableColors*/ true, /*indent*/ 4);
2168+
2169+
auto declid = gInterpreter->GetFunction(cl, name.c_str());
2170+
if (declid) {
2171+
auto existing = gMethodTemplates.find(declid);
2172+
if (existing == gMethodTemplates.end()) {
2173+
auto cw = new_CallWrapper(declid, name);
2174+
existing = gMethodTemplates.insert(std::make_pair(declid, cw)).first;
2175+
}
2176+
// replace diagnostics to suppress spurious errors or warnings from the previous
2177+
// failed lookup
2178+
diagnostics = std::ostringstream();
2179+
diagnostics << diagnosticsold;
2180+
diagnostics << diagnostics2.str();
2181+
2182+
return (TCppMethod_t)existing->second;
21732183
}
2184+
diagnostics << diagnostics2.str();
21742185
}
21752186

21762187
if (func) {
@@ -2186,13 +2197,20 @@ Cppyy::TCppMethod_t Cppyy::GetMethodTemplate(TCppScope_t scope, const std::strin
21862197
if (name.back() == '>') {
21872198
auto pos = name.find('<');
21882199
if (pos != std::string::npos) {
2189-
TCppMethod_t cppmeth = GetMethodTemplate(scope, name.substr(0, pos), proto, diagnostics);
2200+
TCppMethod_t cppmeth = GetMethodTemplate(scope, name.substr(0, pos), proto, diagnostics2);
21902201
if (cppmeth) {
21912202
// allow if requested template names match up to the result
21922203
const std::string& alt = GetMethodFullName(cppmeth);
21932204
if (name.size() < alt.size() && alt.find('<') == pos) {
2194-
if (template_compare(name, alt))
2205+
if (template_compare(name, alt)){
2206+
// replace diagnostics to suppress spurious errors or warnings
2207+
// from the previous failed lookup
2208+
diagnostics = std::ostringstream();
2209+
diagnostics << diagnosticsold;
2210+
diagnostics << diagnostics2.str();
21952211
return cppmeth;
2212+
}
2213+
21962214
}
21972215
}
21982216
}

‎bindings/pyroot/cppyy/cppyy-backend/clingwrapper/src/cpp_cppyy.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ namespace Cppyy {
226226
RPY_EXPORTED
227227
bool IsMethodTemplate(TCppScope_t scope, TCppIndex_t imeth);
228228
RPY_EXPORTED
229-
TCppMethod_t
230-
GetMethodTemplate(TCppScope_t scope, const std::string &name, const std::string &proto, std::ostream &diagnostics);
229+
TCppMethod_t GetMethodTemplate(TCppScope_t scope, const std::string &name, const std::string &proto,
230+
std::ostringstream &diagnostics);
231231

232232
RPY_EXPORTED
233233
TCppIndex_t GetGlobalOperator(

0 commit comments

Comments
 (0)