python: When updating sys.path to include the Samba python path, avoid throwing away...
authorJelmer Vernooij <jelmer@samba.org>
Tue, 29 Dec 2009 15:07:54 +0000 (16:07 +0100)
committerJelmer Vernooij <jelmer@ganieda.vernstok.nl>
Tue, 29 Dec 2009 15:26:20 +0000 (16:26 +0100)
source4/scripting/python/modules.c
source4/scripting/python/modules.h

index 5365c5007d6aa97f50b0595685d03fccf75f3c95..198b3ccf12e8c941655c145792b83a5d28558f71 100644 (file)
@@ -61,10 +61,47 @@ void py_load_samba_modules(void)
        }
 }
 
-void py_update_path(const char *bindir)
+static bool PySys_PathPrepend(PyObject *list, const char *path)
+{
+       PyObject *py_path = PyString_FromString(path);
+       if (py_path == NULL)
+               return false;
+
+       return (PyList_Insert(list, 0, py_path) == 0);
+}
+
+bool py_update_path(const char *bindir)
 {
        char *newpath;
-       asprintf(&newpath, "%s/python:%s/../scripting/python:%s", bindir, bindir, Py_GetPath());
-       PySys_SetPath(newpath);
+       PyObject *mod_sys, *py_path;
+
+       mod_sys = PyImport_ImportModule("sys");
+       if (mod_sys == NULL) {
+               return false;
+       }
+
+       py_path = PyObject_GetAttrString(mod_sys, "path");
+       if (py_path == NULL) {
+               return false;
+       }       
+
+       if (!PyList_Check(py_path)) {
+               return false;
+       }
+
+       asprintf(&newpath, "%s/../scripting/python", bindir);
+       if (!PySys_PathPrepend(py_path, newpath)) {
+               free(newpath);
+               return false;
+       }
        free(newpath);
+
+       asprintf(&newpath, "%s/python", bindir);
+       if (!PySys_PathPrepend(py_path, newpath)) {
+               free(newpath);
+               return false;
+       }
+       free(newpath);
+
+       return true;
 }
index 6b242ee25773beb95bff91665688cdfd6124a7dd..4d1067cdd44e27a80a250d8ea73b8f5d58f4276d 100644 (file)
@@ -21,7 +21,7 @@
 #define __SAMBA_PYTHON_MODULES_H__
 
 void py_load_samba_modules(void);
-void py_update_path(const char *bindir);
+bool py_update_path(const char *bindir);
 
 #define py_iconv_convenience(mem_ctx) smb_iconv_convenience_init(mem_ctx, "ASCII", PyUnicode_GetDefaultEncoding(), true)