Make sure the Python modules are found from the application's installation by prepending their path to sys.path

This commit is contained in:
Matthias Koefferlein 2023-03-24 19:15:57 +01:00
parent fd0c60761f
commit 9a1c776475
9 changed files with 25 additions and 15 deletions

View File

@ -187,7 +187,7 @@ public:
/**
* @brief Add the given path to the search path ($: in ruby)
*/
virtual void add_path (const std::string &path) = 0;
virtual void add_path (const std::string &path, bool prepend = false) = 0;
/**
* @brief Requires the given module (ruby "require")

View File

@ -321,7 +321,9 @@ PythonInterpreter::PythonInterpreter (bool embedded)
// We can put build-in modules there.
std::string module_path = tl::get_module_path ((void *) &reset_interpreter);
if (! module_path.empty ()) {
add_path (tl::combine_path (tl::absolute_path (module_path), "pymod"));
add_path (tl::combine_path (tl::absolute_path (module_path), "pymod"), true /*prepend*/);
} else {
tl::warn << tl::to_string (tr ("Unable to find built-in Python module library path"));
}
PyObject *pya_module = PyImport_ImportModule (pya_module_name);
@ -368,11 +370,15 @@ PythonInterpreter::make_string (const std::string &s)
}
void
PythonInterpreter::add_path (const std::string &p)
PythonInterpreter::add_path (const std::string &p, bool prepend)
{
PyObject *path = PySys_GetObject ((char *) "path");
if (path != NULL && PyList_Check (path)) {
PyList_Append (path, c2python (p));
if (prepend) {
PyList_Insert (path, 0, c2python (p));
} else {
PyList_Append (path, c2python (p));
}
}
}

View File

@ -108,7 +108,7 @@ public:
/**
* @brief Add the given path to the search path
*/
void add_path (const std::string &path);
void add_path (const std::string &path, bool prepend = false);
/**
* @brief Adds a package location to this interpreter

View File

@ -53,7 +53,7 @@ PythonInterpreter *PythonInterpreter::instance ()
}
void
PythonInterpreter::add_path (const std::string &)
PythonInterpreter::add_path (const std::string &, bool prepend)
{
// .. nothing ..
}

View File

@ -48,7 +48,7 @@ public:
/**
* @brief Add the given path to the search path
*/
void add_path (const std::string &path);
void add_path (const std::string &path, bool prepend);
/**
* @brief Adds a package location to this interpreter

View File

@ -1552,11 +1552,15 @@ struct RubyConstDescriptor
extern "C" void ruby_prog_init();
static void
rba_add_path (const std::string &path)
rba_add_path (const std::string &path, bool prepend)
{
VALUE pv = rb_gv_get ("$:");
if (pv != Qnil && TYPE (pv) == T_ARRAY) {
rb_ary_push (pv, rb_str_new (path.c_str (), long (path.size ())));
if (prepend) {
rb_ary_unshift (pv, rb_str_new (path.c_str (), long (path.size ())));
} else {
rb_ary_push (pv, rb_str_new (path.c_str (), long (path.size ())));
}
}
}
@ -2036,7 +2040,7 @@ RubyInterpreter::initialize (int &main_argc, char **main_argv, int (*main_func)
if (v.is_list ()) {
for (tl::Variant::iterator i = v.begin (); i != v.end (); ++i) {
rba_add_path (i->to_string ());
rba_add_path (i->to_string (), false);
}
}
@ -2133,9 +2137,9 @@ RubyInterpreter::remove_package_location (const std::string & /*package_path*/)
}
void
RubyInterpreter::add_path (const std::string &path)
RubyInterpreter::add_path (const std::string &path, bool prepend)
{
rba_add_path (path);
rba_add_path (path, prepend);
}
void

View File

@ -56,7 +56,7 @@ public:
/**
* @brief Add the given path to the search path ($: in ruby)
*/
void add_path (const std::string &path);
void add_path (const std::string &path, bool prepend = false);
/**
* @brief Adds a package location to this interpreter

View File

@ -63,7 +63,7 @@ RubyInterpreter::remove_package_location (const std::string &)
}
void
RubyInterpreter::add_path (const std::string &)
RubyInterpreter::add_path (const std::string &, bool)
{
// .. nothing ..
}

View File

@ -45,7 +45,7 @@ public:
/**
* @brief Add the given path to the search path ($: in ruby)
*/
void add_path (const std::string &path);
void add_path (const std::string &path, bool prepend);
/**
* @brief Adds a package location to this interpreter