From ab7104e34c6ae800938e3039d20ca2993d3db375 Mon Sep 17 00:00:00 2001 From: Marco Sirabella Date: Fri, 3 Jul 2020 11:11:08 -0400 Subject: [PATCH] Add $weechat_sharedir/python for global python package import Related: #1461 Some scripts (eg weechat-matrix) ship directories that need to be imported from the script. Rather than globally installing the python packages to python's `site-packages` the directories can be installed alongside the script in `WEECHAT_SHAREDIR/python`. This change adds that directory to the `$PYTHONPATH` to import successfully. --- src/plugins/python/weechat-python.c | 33 ++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/plugins/python/weechat-python.c b/src/plugins/python/weechat-python.c index 4ef9e553d..2515f6f08 100644 --- a/src/plugins/python/weechat-python.c +++ b/src/plugins/python/weechat-python.c @@ -797,8 +797,8 @@ weechat_python_load (const char *filename, const char *code) #endif /* PY_MAJOR_VERSION >= 3 */ FILE *fp; PyObject *python_path, *path, *module_main, *globals, *rc; - char *weechat_home; - char *str_home; + char *weechat_sharedir, *weechat_home; + char *str_sharedir, *str_home; int len; fp = NULL; @@ -862,8 +862,34 @@ weechat_python_load (const char *filename, const char *code) PyThreadState_Swap (python_current_interpreter); - /* adding $weechat_dir/python in $PYTHONPATH */ + /* adding $weechat_sharedir/python in $PYTHONPATH */ python_path = PySys_GetObject ("path"); + weechat_sharedir = weechat_info_get ("weechat_sharedir", ""); + if (weechat_sharedir) + { + len = strlen (weechat_sharedir) + 1 + strlen (PYTHON_PLUGIN_NAME) + 1; + str_sharedir = malloc (len); + if (str_sharedir) + { + snprintf (str_sharedir, len, "%s/python", weechat_sharedir); +#if PY_MAJOR_VERSION >= 3 + /* python >= 3.x */ + path = PyUnicode_FromString (str_sharedir); +#else + /* python <= 2.x */ + path = PyBytes_FromString (str_sharedir); +#endif /* PY_MAJOR_VERSION >= 3 */ + if (path != NULL) + { + PyList_Insert (python_path, 0, path); + Py_DECREF (path); + } + free (str_sharedir); + } + free (weechat_sharedir); + } + + /* adding $weechat_dir/python in $PYTHONPATH */ weechat_home = weechat_info_get ("weechat_dir", ""); if (weechat_home) { @@ -889,6 +915,7 @@ weechat_python_load (const char *filename, const char *code) free (weechat_home); } + weechat_python_set_output (); python_current_script_filename = filename;