Skip to content

Commit 8b22039

Browse files
committed
suppress warnings and errors when lookup succeeds on a subsequent try
1 parent 4d438e9 commit 8b22039

4 files changed

Lines changed: 41 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
@@ -231,8 +231,8 @@ namespace Cppyy {
231231
CPPYY_IMPORT
232232
bool IsMethodTemplate(TCppScope_t scope, TCppIndex_t imeth);
233233
CPPYY_IMPORT
234-
TCppMethod_t
235-
GetMethodTemplate(TCppScope_t scope, const std::string &name, const std::string &proto, std::ostream &diagnostics);
234+
TCppMethod_t GetMethodTemplate(TCppScope_t scope, const std::string &name, const std::string &proto,
235+
std::ostringstream &diagnostics);
236236

237237
CPPYY_IMPORT
238238
TCppIndex_t GetGlobalOperator(

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

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

221-
Cppyy::TCppMethod_t m2 = Cppyy::GetMethodTemplate(scope, fname, proto, diagnostics);
221+
std::ostringstream diagnostics2;
222+
Cppyy::TCppMethod_t m2 = Cppyy::GetMethodTemplate(scope, fname, proto, diagnostics2);
222223
if (m2 && m2 != cppmeth) {
223224
// replace if the new method with vector was found; otherwise just continue
224225
// with the previously found method with initializer_list.
225226
cppmeth = m2;
226227
resname = Cppyy::GetMethodFullName(cppmeth);
228+
diagnostics = std::move(diagnostics2);
227229
}
228230
}
229231

@@ -241,6 +243,10 @@ PyObject* TemplateProxy::Instantiate(const std::string& fname,
241243
Py_DECREF(pyol);
242244
Py_DECREF(pycachename);
243245
Py_DECREF(dct);
246+
247+
PyErr_Format(PyExc_TypeError, "Failed to instantiate \"%s(%s)\"\n%s", fname.c_str(), proto.c_str(),
248+
diagnostics.str().c_str());
249+
244250
return nullptr;
245251
}
246252

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

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,7 +1837,7 @@ bool Cppyy::IsMethodTemplate(TCppScope_t scope, TCppIndex_t idx)
18371837
static std::map<TDictionary::DeclId_t, CallWrapper*> gMethodTemplates;
18381838

18391839
Cppyy::TCppMethod_t Cppyy::GetMethodTemplate(TCppScope_t scope, const std::string &name, const std::string &proto,
1840-
std::ostream &diagnostics)
1840+
std::ostringstream &diagnostics)
18411841
{
18421842
// There is currently no clean way of extracting a templated method out of ROOT/meta
18431843
// for a variety of reasons, none of them fundamental. The game played below is to
@@ -1854,6 +1854,7 @@ Cppyy::TCppMethod_t Cppyy::GetMethodTemplate(TCppScope_t scope, const std::strin
18541854
// redirect diagnostics, taking the lock to make sure no other calls pollute the results
18551855
R__WRITE_LOCKGUARD(ROOT::gCoreMutex);
18561856

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

18591860
TFunction *func = nullptr;
@@ -1885,15 +1886,25 @@ if (scope == (cppyy_scope_t)GLOBAL_HANDLE) {
18851886

18861887
if (!func && name.back() == '>' && (cl || scope == (cppyy_scope_t)GLOBAL_HANDLE)) {
18871888
// try again, ignoring proto in case full name is complete template
1888-
auto declid = gInterpreter->GetFunction(cl, name.c_str());
1889-
if (declid) {
1890-
auto existing = gMethodTemplates.find(declid);
1891-
if (existing == gMethodTemplates.end()) {
1892-
auto cw = new_CallWrapper(declid, name);
1893-
existing = gMethodTemplates.insert(std::make_pair(declid, cw)).first;
1894-
}
1895-
return (TCppMethod_t)existing->second;
1889+
std::ostringstream diagnostics2;
1890+
TInterpreter::RedirectDiagnostics redirectRAII2(gInterpreter, diagnostics2, /*enableColors*/ true, /*indent*/ 4);
1891+
1892+
auto declid = gInterpreter->GetFunction(cl, name.c_str());
1893+
if (declid) {
1894+
auto existing = gMethodTemplates.find(declid);
1895+
if (existing == gMethodTemplates.end()) {
1896+
auto cw = new_CallWrapper(declid, name);
1897+
existing = gMethodTemplates.insert(std::make_pair(declid, cw)).first;
1898+
}
1899+
// replace diagnostics to suppress spurious errors or warnings from the previous
1900+
// failed lookup
1901+
diagnostics = std::ostringstream();
1902+
diagnostics << diagnosticsold;
1903+
diagnostics << diagnostics2.str();
1904+
1905+
return (TCppMethod_t)existing->second;
18961906
}
1907+
diagnostics << diagnostics2.str();
18971908
}
18981909

18991910
if (func) {
@@ -1909,16 +1920,24 @@ if (scope == (cppyy_scope_t)GLOBAL_HANDLE) {
19091920
if (name.back() == '>') {
19101921
auto pos = name.find('<');
19111922
if (pos != std::string::npos) {
1912-
TCppMethod_t cppmeth = GetMethodTemplate(scope, name.substr(0, pos), proto, diagnostics);
1923+
std::ostringstream diagnostics2;
1924+
TCppMethod_t cppmeth = GetMethodTemplate(scope, name.substr(0, pos), proto, diagnostics2);
19131925
if (cppmeth) {
19141926
// allow if requested template names match up to the result
19151927
const std::string &alt = GetMethodFullName(cppmeth);
19161928
if (name.size() < alt.size() && alt.find('<') == pos) {
19171929
const std::string &partial = name.substr(pos, name.size() - 1 - pos);
1918-
if (strncmp(partial.c_str(), alt.substr(pos, alt.size() - 1 - pos).c_str(), partial.size()) == 0)
1930+
if (strncmp(partial.c_str(), alt.substr(pos, alt.size() - 1 - pos).c_str(), partial.size()) == 0) {
1931+
// replace diagnostics to suppress spurious errors or warnings
1932+
// from the previous failed lookup
1933+
diagnostics = std::ostringstream();
1934+
diagnostics << diagnosticsold;
1935+
diagnostics << diagnostics2.str();
19191936
return cppmeth;
1937+
}
19201938
}
19211939
}
1940+
diagnostics << diagnostics2.str();
19221941
}
19231942
}
19241943

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ namespace Cppyy {
207207
RPY_EXPORTED
208208
bool IsMethodTemplate(TCppScope_t scope, TCppIndex_t imeth);
209209
RPY_EXPORTED
210-
TCppMethod_t
211-
GetMethodTemplate(TCppScope_t scope, const std::string &name, const std::string &proto, std::ostream &diagnostics);
210+
TCppMethod_t GetMethodTemplate(TCppScope_t scope, const std::string &name, const std::string &proto,
211+
std::ostringstream &diagnostics);
212212

213213
RPY_EXPORTED
214214
TCppIndex_t GetGlobalOperator(

0 commit comments

Comments
 (0)