]> jfr.im git - irc/weechat/weechat.git/commitdiff
Add $weechat_sharedir/python for global python package import
authorMarco Sirabella <redacted>
Fri, 3 Jul 2020 15:11:08 +0000 (11:11 -0400)
committerSébastien Helleu <redacted>
Tue, 7 Jul 2020 18:26:17 +0000 (20:26 +0200)
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

index 4ef9e553d4f38bf0628aaae99da90c868deedc7b..2515f6f08b55d9a92f6f0e021686e1f4d9a2b958 100644 (file)
@@ -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;