Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 : Samba utility functions
4 : Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
5 :
6 : This program is free software; you can redistribute it and/or modify
7 : it under the terms of the GNU General Public License as published by
8 : the Free Software Foundation; either version 3 of the License, or
9 : (at your option) any later version.
10 :
11 : This program is distributed in the hope that it will be useful,
12 : but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : GNU General Public License for more details.
15 :
16 : You should have received a copy of the GNU General Public License
17 : along with this program. If not, see <http://www.gnu.org/licenses/>.
18 : */
19 :
20 : #include <Python.h>
21 : #include "py3compat.h"
22 : #include "includes.h"
23 : #include "python/modules.h"
24 : #include "dynconfig/dynconfig.h"
25 :
26 81 : static bool PySys_PathPrepend(PyObject *list, const char *path)
27 : {
28 : bool ok;
29 81 : PyObject *py_path = PyUnicode_FromString(path);
30 81 : if (py_path == NULL) {
31 0 : return false;
32 : }
33 81 : ok = PyList_Insert(list, 0, py_path) == 0;
34 81 : Py_XDECREF(py_path);
35 81 : return ok;
36 : }
37 :
38 81 : bool py_update_path(void)
39 : {
40 81 : PyObject *mod_sys = NULL;
41 81 : PyObject *py_path = NULL;
42 :
43 81 : mod_sys = PyImport_ImportModule("sys");
44 81 : if (mod_sys == NULL) {
45 0 : return false;
46 : }
47 :
48 81 : py_path = PyObject_GetAttrString(mod_sys, "path");
49 81 : if (py_path == NULL) {
50 0 : goto error;
51 : }
52 :
53 81 : if (!PyList_Check(py_path)) {
54 0 : goto error;
55 : }
56 :
57 81 : if (!PySys_PathPrepend(py_path, dyn_PYTHONDIR)) {
58 0 : goto error;
59 : }
60 :
61 81 : if (strcmp(dyn_PYTHONARCHDIR, dyn_PYTHONDIR) != 0) {
62 0 : if (!PySys_PathPrepend(py_path, dyn_PYTHONARCHDIR)) {
63 0 : goto error;
64 : }
65 : }
66 81 : Py_XDECREF(py_path);
67 81 : Py_XDECREF(mod_sys);
68 81 : return true;
69 0 : error:
70 0 : Py_XDECREF(py_path);
71 0 : Py_XDECREF(mod_sys);
72 0 : return false;
73 : }
|