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
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ include(GNUInstallDirs)
# This option won't make a lot of sense since we only ship the shared library in site-packages
# Perhaps this should permanently be OFF and users can build their own CppInterOp if they want to run the tests?
option(CPPJIT_ENABLE_CPPINTEROP_TESTS "enable CppInterOp tests" OFF)
set(CPPINTEROP_GIT_REPOSITORY "https://github.com/compiler-research/CppInterOp.git" CACHE STRING "")
set(CPPINTEROP_GIT_TAG "c8126249dd18363000b89333b34313ca2a83fb82" CACHE STRING "")
set(CPPINTEROP_GIT_REPOSITORY "https://github.com/guitargeek/CppInterOp.git" CACHE STRING "")
set(CPPINTEROP_GIT_TAG "isvisible-name" CACHE STRING "")
set(CPPINTEROP_SOURCE_DIR "" CACHE PATH
"Override default CppInterOp built by ExternalProject_Add, with a path to local CppInterOp source")

Expand Down
117 changes: 87 additions & 30 deletions src/cpyrt/CPPScope.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ using namespace cppjit;

// Standard
#include <algorithm> // for for_each
#include <cctype>
#include <set>
#include <string.h>
#include <string>
Expand Down Expand Up @@ -324,6 +325,74 @@ static PyObject* pt_new(PyTypeObject* subtype, PyObject* args, PyObject* kwds) {
return (PyObject*)result;
}

//----------------------------------------------------------------------------
static bool IsPlainIdentifier(const std::string& name) {
if (name.empty() || std::isdigit((unsigned char)name[0]))
return false;
for (char c : name) {
if (!std::isalnum((unsigned char)c) && c != '_')
return false;
}
return true;
}

// Returns weak references to the proxies of the namespaces that the namespace
// \c pyclass pulls in with "using namespace", refreshing the list as needed.
static const std::vector<PyObject*>& GetUsingScopes(PyObject* pyclass) {
CPPScope* klass = (CPPScope*)pyclass;
const std::vector<interop::TCppScope_t>& uv =
interop::GetUsingNamespaces(klass->fCppType);
if (!klass->fImp.fUsing || uv.size() != klass->fImp.fUsing->size()) {
if (klass->fImp.fUsing) {
for (auto pyref : *klass->fImp.fUsing)
Py_DECREF(pyref);
klass->fImp.fUsing->clear();
} else
klass->fImp.fUsing = new std::vector<PyObject*>;

// reload and reset weak refs
for (auto uid : uv) {
std::string uname = interop::GetScopedFinalName(uid);
PyObject* pyuscope = CreateScopeProxy(uname);
if (pyuscope) {
klass->fImp.fUsing->push_back(PyWeakref_NewRef(pyuscope, nullptr));
// the namespace may not otherwise be held, so tie the lifetimes
PyObject* llname =
PyUnicode_FromString(("__lifeline_" + uname).c_str());
PyType_Type.tp_setattro(pyclass, llname, pyuscope);
Py_DECREF(llname);
Py_DECREF(pyuscope);
} else {
PyErr_Clear();
}
}
}
return *klass->fImp.fUsing;
}

// Checks whether \c pyname is set as a Python attribute on any namespace that
// \c pyclass pulls in with "using namespace", directly or transitively.
static bool HasPythonAttrInUsing(PyObject* pyclass, PyObject* pyname,
std::set<PyObject*>& visited) {
for (auto pyref : GetUsingScopes(pyclass)) {
PyObject* pyuscope = cpyrt_GetWeakRef(pyref);
if (!pyuscope)
continue;
bool found = false;
if (visited.insert(pyuscope).second) {
found = _PyType_Lookup((PyTypeObject*)pyuscope, pyname) ||
_PyType_Lookup(Py_TYPE(pyuscope), pyname) ||
(CPPScope_Check(pyuscope) &&
(((CPPScope*)pyuscope)->fFlags & CPPScope::kIsNamespace) &&
HasPythonAttrInUsing(pyuscope, pyname, visited));
}
Py_DECREF(pyuscope);
if (found)
return true;
}
return false;
}

//----------------------------------------------------------------------------
static PyObject* meta_getattro(PyObject* pyclass, PyObject* pyname) {
// normal type-based lookup
Expand Down Expand Up @@ -411,8 +480,9 @@ static PyObject* meta_getattro(PyObject* pyclass, PyObject* pyname) {
}
}

interop::TCppScope_t lookup_result = nullptr;
if (!attr) {
interop::TCppScope_t lookup_result = interop::GetNamed(name, scope);
lookup_result = interop::GetNamed(name, scope);
if (interop::IsVariable(lookup_result) ||
interop::IsEnumConstant(lookup_result)) {
attr = (PyObject*)CPPDataMember_New(scope, lookup_result);
Expand Down Expand Up @@ -450,7 +520,7 @@ static PyObject* meta_getattro(PyObject* pyclass, PyObject* pyname) {
// enums types requested as type (rather than the constants)
if (!attr) {
interop::TCppScope_t enumerator =
interop::GetUnderlyingScope(interop::GetNamed(name, scope));
interop::GetUnderlyingScope(lookup_result);
if (interop::IsEnumScope(enumerator)) {
// enum types (incl. named and class enums)
attr = (PyObject*)CPPEnum_New(name, enumerator);
Expand Down Expand Up @@ -481,38 +551,25 @@ static PyObject* meta_getattro(PyObject* pyclass, PyObject* pyname) {
}

if (!attr && (klass->fFlags & CPPScope::kIsNamespace)) {
// refresh using list as necessary
const std::vector<interop::TCppScope_t>& uv =
interop::GetUsingNamespaces(klass->fCppType);
if (!klass->fImp.fUsing || uv.size() != klass->fImp.fUsing->size()) {
if (klass->fImp.fUsing) {
for (auto pyref : *klass->fImp.fUsing)
Py_DECREF(pyref);
klass->fImp.fUsing->clear();
} else
klass->fImp.fUsing = new std::vector<PyObject*>;

// reload and reset weak refs
for (auto uid : uv) {
std::string uname = interop::GetScopedFinalName(uid);
PyObject* pyuscope = CreateScopeProxy(uname);
if (pyuscope) {
klass->fImp.fUsing->push_back(PyWeakref_NewRef(pyuscope, nullptr));
// the namespace may not otherwise be held, so tie the lifetimes
PyObject* llname =
PyUnicode_FromString(("__lifeline_" + uname).c_str());
PyType_Type.tp_setattro(pyclass, llname, pyuscope);
Py_DECREF(llname);
Py_DECREF(pyuscope);
} else {
PyErr_Clear();
}
}
const std::vector<PyObject*>& using_scopes = GetUsingScopes(pyclass);

// Searching the using namespaces repeats the full attribute lookup in each
// of them, which is expensive for names that don't exist. Anything C++ it
// can find is also found by a single unqualified lookup from within this
// namespace, which follows the using-directives. If that finds nothing,
// only attributes set on the Python proxies of the using namespaces remain.
bool search_using = !using_scopes.empty();
if (search_using && IsPlainIdentifier(name) && !interop::IsBuiltin(name) &&
!interop::IsVisibleName(name, klass->fCppType)) {
std::set<PyObject*> visited{pyclass};
search_using = HasPythonAttrInUsing(pyclass, pyname, visited);
}

// 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) {
for (auto pyref : using_scopes) {
if (!search_using)
break;
PyObject* pyuscope = cpyrt_GetWeakRef(pyref);
if (pyuscope) {
attr = PyObject_GetAttr(pyuscope, pyname);
Expand Down
3 changes: 3 additions & 0 deletions src/interop/cppjit_interop.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ RPY_EXPORTED
TCppScope_t GetNamed(const std::string& scope_name,
TCppScope_t parent_scope = TCppScope_t{});
RPY_EXPORTED
bool IsVisibleName(const std::string& name,
TCppScope_t parent_scope = TCppScope_t{});
RPY_EXPORTED
TCppScope_t GetParentScope(TCppScope_t scope);
RPY_EXPORTED
TCppScope_t GetScopeFromType(TCppType_t type);
Expand Down
5 changes: 5 additions & 0 deletions src/interop/interop_wrapper.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,11 @@ interop::TCppScope_t interop::GetNamed(const std::string& name,
return Cpp::GetNamed(name, parent_scope);
}

bool interop::IsVisibleName(const std::string& name, TCppScope_t parent_scope) {
std::lock_guard<std::recursive_mutex> Lock(InterOpMutex);
return Cpp::IsVisibleName(name, parent_scope);
}

interop::TCppScope_t interop::GetParentScope(TCppScope_t scope) {
std::lock_guard<std::recursive_mutex> Lock(InterOpMutex);
return Cpp::GetParentScope(scope);
Expand Down
Loading