Browse Source

Add support for virtualenv & site packages

Note : segfault on virtualenv tests...
Yann Weber 4 years ago
parent
commit
81a8bb752e
3 changed files with 34 additions and 1 deletions
  1. 4
    0
      include/conf.h
  2. 1
    0
      include/pyutils.h
  3. 29
    1
      src/pyutils.c

+ 4
- 0
include/conf.h View File

@@ -28,6 +28,7 @@
28 28
 
29 29
 #include <unistd.h>
30 30
 #include <getopt.h>
31
+#include <limits.h>
31 32
 #include <sys/types.h>
32 33
 #include <sys/wait.h>
33 34
 
@@ -123,6 +124,9 @@ struct pyfcgi_context_s {
123 124
 	pid_t **wrk_pids;
124 125
 	/**@brief workers count */
125 126
 	unsigned int n_wrk;
127
+
128
+	wchar_t python_path[PATH_MAX];
129
+	wchar_t venv_path[PATH_MAX];
126 130
 };
127 131
 
128 132
 /**@brief Structure containing configuration

+ 1
- 0
include/pyutils.h View File

@@ -40,6 +40,7 @@ extern PyObject* libpyfcgi_self;
40 40
 void pyinit();
41 41
 
42 42
 /**@brief Add . to the embed pythonpath
43
+ * @deprecated Py_SetPath breaks Py_GetPrefix()...
43 44
  */
44 45
 void update_python_path();
45 46
 

+ 29
- 1
src/pyutils.c View File

@@ -2,8 +2,34 @@
2 2
 
3 3
 void pyinit()
4 4
 {
5
-	update_python_path();
5
+	char *venv_path;
6
+	PyObject *sitemod, *mainfun;
7
+
8
+	if( (venv_path = getenv("VIRTUAL_ENV")) )
9
+	{
10
+		setenv("PYTHONHOME", venv_path, 1);
11
+		swprintf(PyFCGI_conf.context.venv_path, PATH_MAX, L"%s",
12
+			venv_path);
13
+		Py_SetPythonHome(PyFCGI_conf.context.venv_path);
14
+		pyfcgi_log(LOG_INFO, "Virtualenv found : setting PYTHONHOME : '%s'",
15
+			venv_path);
16
+
17
+		swprintf(PyFCGI_conf.context.python_path, PATH_MAX,
18
+			L"%s/bin/python3", venv_path);
19
+		Py_SetProgramName(PyFCGI_conf.context.python_path);
20
+
21
+		pyfcgi_log(LOG_INFO, "Setting programm name : '%ls'",
22
+			PyFCGI_conf.context.python_path);
23
+	}
24
+
6 25
 	Py_Initialize();
26
+	sitemod = PyImport_ImportModule("site");
27
+	mainfun = PyObject_GetAttrString(sitemod, "main");
28
+	PyObject_CallObject(mainfun, NULL);
29
+	
30
+	//Append "." to sys.path
31
+	sitemod = PySys_GetObject("path");
32
+	PyList_Append(sitemod, PyUnicode_FromString("."));
7 33
 }
8 34
 
9 35
 void update_python_path()
@@ -73,6 +99,7 @@ void update_python_path()
73 99
 		goto update_python_path_err_wcwd;
74 100
 	}
75 101
 	ppath = wcsdup(ppath);
102
+dprintf(2, "%ls\n", ppath);
76 103
 	if(!wcwd)
77 104
 	{
78 105
 		err = errno;
@@ -90,6 +117,7 @@ void update_python_path()
90 117
 	}
91 118
 	ppath = wtmp;
92 119
 	wcsncpy(ppath+ppath_sz, wcwd, wcwd_sz);
120
+dprintf(2, "%ls\n", ppath);
93 121
 	Py_SetPath(ppath);
94 122
 
95 123
 	free(ppath);

Loading…
Cancel
Save