Skip to content
Merged
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
10 changes: 8 additions & 2 deletions phlex/app/load_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ using namespace std::string_literals;
namespace phlex::experimental {

namespace {
constexpr std::string_view pymodule_name{"pymodule"};

// If factory function goes out of scope, then the library is unloaded...and that's
// bad.
std::vector<std::function<detail::module_creator_t>> create_module;
Expand All @@ -40,7 +42,11 @@ namespace phlex::experimental {
std::filesystem::path shared_library_path{subdir};
shared_library_path /= "lib" + spec + ".so";
if (exists(shared_library_path)) {
return dll::import_alias<creator_t>(shared_library_path, symbol_name);
// Load pymodule with rtld_global to make Python symbols available to extension modules
// (e.g., NumPy). Load all other plugins with rtld_local (default) to avoid symbol collisions.
auto const load_mode =
(spec == pymodule_name) ? dll::load_mode::rtld_global : dll::load_mode::default_mode;
return dll::import_alias<creator_t>(shared_library_path, symbol_name, load_mode);
}
}
throw std::runtime_error("Could not locate library with specification '"s + spec +
Expand All @@ -65,7 +71,7 @@ namespace phlex::experimental {
}
throw std::runtime_error(msg);
}
raw_config["cpp"] = "pymodule";
raw_config["cpp"] = pymodule_name;
}

return raw_config;
Expand Down
1 change: 0 additions & 1 deletion plugins/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ add_library(
src/errorwrap.cpp
)
target_link_libraries(pymodule PRIVATE phlex::module Python::Python Python::NumPy)

target_compile_definitions(pymodule PRIVATE NPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION)

install(TARGETS pymodule LIBRARY DESTINATION lib)
15 changes: 0 additions & 15 deletions plugins/python/src/pymodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,21 +105,6 @@ static bool initialize()
return true;
}

// TODO: the Python library is already loaded (b/c it's linked with
// this module), but its symbols need to be exposed globally to Python
// extension modules such as ctypes, yet this module is loaded with
// private visibility only. The workaround here locates the library and
// reloads (the handle is leaked b/c there's no knowing when it needs
// to be offloaded).
void* addr = dlsym(RTLD_DEFAULT, "Py_IsInitialized");
if (addr) {
Dl_info info;
if (dladdr(addr, &info) == 0 || info.dli_fname == 0 || info.dli_fname[0] == 0) {
throw std::runtime_error("unable to determine linked libpython");
}
dlopen(info.dli_fname, RTLD_GLOBAL | RTLD_NOW);
}

PyConfig config;
PyConfig_InitPythonConfig(&config);
PyConfig_SetString(&config, &config.program_name, L"phlex");
Expand Down
Loading