]> arthur.barton.de Git - bup.git/commitdiff
Stash the env PYTHONPATH during startup and restore it in main
authorRob Browning <rlb@defaultvalue.org>
Sat, 13 Feb 2021 19:14:19 +0000 (13:14 -0600)
committerRob Browning <rlb@defaultvalue.org>
Sun, 28 Mar 2021 21:16:52 +0000 (16:16 -0500)
We have to set the PYTHONPATH from bup.c so that Py_Main will be able
to find bup.*, but we don't want that change to affect subprocesses,
so stash the original PYTHONPATH in bup_main.env_pythonpath, and
restore the original environment value once we've made it to the
python side (in bup.main).

Thanks to Johannes Berg for pointing out some issues with a previous
version of the changes.

Signed-off-by: Rob Browning <rlb@defaultvalue.org>
Tested-by: Rob Browning <rlb@defaultvalue.org>
lib/bup/main.py
lib/cmd/bup.c

index e527977a2aa9214f391167b68ffeae163f9f7d05..383611da47717e05f3f30509752a5903c69af8a5 100755 (executable)
@@ -1,10 +1,20 @@
 
 from __future__ import absolute_import, print_function
+
+import bup_main, os, sys
+if bup_main.env_pythonpath:
+    if sys.version_info[0] < 3:
+        os.environ['PYTHONPATH'] = bup_main.env_pythonpath
+    else:
+        os.environb[b'PYTHONPATH'] = bup_main.env_pythonpath
+else:
+    del os.environ['PYTHONPATH']
+
 from importlib import import_module
 from pkgutil import iter_modules
 from subprocess import PIPE
 from threading import Thread
-import errno, getopt, os, re, select, signal, subprocess, sys
+import errno, getopt, re, select, signal, subprocess
 
 from bup import compat, path, helpers
 from bup.compat import (
index 1dcb724d1053d37b4715287f9c8e2d30de393be1..807e26009b028d9bb15f03f140a9be447f37a99e 100644 (file)
@@ -37,6 +37,7 @@ die(int exit_status, const char * const msg, ...)
 
 static int prog_argc = 0;
 static char **prog_argv = NULL;
+static char *orig_env_pythonpath = NULL;
 
 static PyObject*
 get_argv(PyObject *self, PyObject *args)
@@ -62,6 +63,16 @@ static PyMethodDef bup_main_methods[] = {
 
 static int setup_module(PyObject *mod)
 {
+    if (!orig_env_pythonpath) {
+        PyObject_SetAttrString(mod, "env_pythonpath", Py_None);
+    } else {
+        PyObject *py_p = PyBytes_FromString(orig_env_pythonpath);
+        if (!py_p)
+            die(2, "cannot convert PYTHONPATH to bytes: %s\n",
+                orig_env_pythonpath);
+        PyObject_SetAttrString(mod, "env_pythonpath", py_p);
+        Py_DECREF(py_p);
+    }
     return 1;
 }
 
@@ -106,8 +117,12 @@ void PyInit_bup_main(void)
 #endif // PY_MAJOR_VERSION < 3
 
 static void
-setup_bup_main_module(void)
-{
+setup_bup_main_module(void) {
+
+    char *path = getenv("PYTHONPATH");
+    if (path)
+        orig_env_pythonpath = strdup(path);
+
     if (PyImport_AppendInittab("bup_main", PyInit_bup_main) == -1)
         die(2, "unable to register bup_main module\n");
 }