Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions src/cpyrt/CPPScope.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -505,18 +505,27 @@ static PyObject* meta_getattro(PyObject* pyclass, PyObject* pyname) {
}
}

// try all outstanding using namespaces in turn to find the attribute (will
// cache locally later; TODO: doing so may cause pathological cases)
for (auto pyref : *klass->fImp.fUsing) {
PyObject* pyuscope = cpyrt_GetWeakRef(pyref);
if (pyuscope) {
attr = PyObject_GetAttr(pyuscope, pyname);
if (!attr)
PyErr_Clear();
Py_DECREF(pyuscope);
// Search each namespace at most once per lookup ([namespace.qual]/3), so
// that using-directive cycles terminate.
thread_local std::set<std::pair<PyObject*, std::string>> searched;
thread_local int depth = 0;
if (searched.emplace(pyclass, name).second) {
++depth;
// try all outstanding using namespaces in turn to find the attribute
// (will cache locally later; TODO: doing so may cause pathological cases)
for (auto pyref : *klass->fImp.fUsing) {
PyObject* pyuscope = cpyrt_GetWeakRef(pyref);
if (pyuscope) {
attr = PyObject_GetAttr(pyuscope, pyname);
if (!attr)
PyErr_Clear();
Py_DECREF(pyuscope);
}
if (attr)
break;
}
if (attr)
break;
if (--depth == 0)
searched.clear();
}
}

Expand Down
20 changes: 20 additions & 0 deletions test/test_advancedcpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1098,3 +1098,23 @@ def test31_virtual_base_downcast(self):
assert isinstance(obj3, ns.Visible3)
assert obj3.value == 23
assert obj3.z == 7

def test32_using_directive_cycle(self):
"""Lookup through cyclic using directives searches each namespace once"""

import cppjit

cppjit.cppdef("""\
namespace udir_cycle_a {}
namespace udir_cycle_p { using namespace udir_cycle_a; int x = 1; }
namespace udir_cycle_a { using namespace udir_cycle_p; int y = 2; }""")

a = cppjit.gbl.udir_cycle_a
p = cppjit.gbl.udir_cycle_p
assert a.x == 1
assert p.y == 2

with raises(AttributeError):
a.nothere
with raises(AttributeError):
p.nothere
Loading