summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorValentin Bartenev <vbart@nginx.com>2018-04-24 20:49:36 +0300
committerValentin Bartenev <vbart@nginx.com>2018-04-24 20:49:36 +0300
commit749f7c03408d0109598f65fabb8ae0bea04a2c33 (patch)
treeae1756f2a2199b6b3cea5540f717272a5dadf99a
parent24a292a362307578d296dd0811df0440cb7ad4a5 (diff)
downloadunit-749f7c03408d0109598f65fabb8ae0bea04a2c33.tar.gz
unit-749f7c03408d0109598f65fabb8ae0bea04a2c33.tar.bz2
Support for PEP 405 virtual environments.
This closes #96 issue on GitHub.
Diffstat (limited to '')
-rw-r--r--src/nxt_python_wsgi.c58
1 files changed, 50 insertions, 8 deletions
diff --git a/src/nxt_python_wsgi.c b/src/nxt_python_wsgi.c
index 1c1cf03d..ab9663a8 100644
--- a/src/nxt_python_wsgi.c
+++ b/src/nxt_python_wsgi.c
@@ -202,8 +202,17 @@ static nxt_int_t
nxt_python_init(nxt_task_t *task, nxt_common_app_conf_t *conf)
{
char *nxt_py_module;
+ size_t len;
PyObject *obj, *pypath, *module;
nxt_python_app_conf_t *c;
+#if PY_MAJOR_VERSION == 3
+ char *path;
+ size_t size;
+ nxt_int_t pep405;
+
+ static const char pyvenv[] = "pyvenv.cfg";
+ static const char bin_python[] = "/bin/python";
+#endif
c = &conf->u.python;
@@ -213,23 +222,56 @@ nxt_python_init(nxt_task_t *task, nxt_common_app_conf_t *conf)
}
if (c->home != NULL) {
- size_t len;
-
len = nxt_strlen(c->home);
- nxt_py_home = nxt_malloc(sizeof(*nxt_py_home) * (len + 1));
+#if PY_MAJOR_VERSION == 3
+
+ path = nxt_malloc(len + sizeof(pyvenv));
+ if (nxt_slow_path(path == NULL)) {
+ nxt_alert(task, "Failed to allocate memory");
+ return NXT_ERROR;
+ }
+
+ nxt_memcpy(path, c->home, len);
+ nxt_memcpy(path + len, pyvenv, sizeof(pyvenv));
+
+ pep405 = (access(path, R_OK) == 0);
+
+ nxt_free(path);
+
+ if (pep405) {
+ size = (len + sizeof(bin_python)) * sizeof(wchar_t);
+
+ } else {
+ size = (len + 1) * sizeof(wchar_t);
+ }
+
+ nxt_py_home = nxt_malloc(size);
if (nxt_slow_path(nxt_py_home == NULL)) {
- nxt_alert(task, "Failed to allocate buffer for home path");
+ nxt_alert(task, "Failed to allocate memory");
return NXT_ERROR;
}
-#if PY_MAJOR_VERSION == 3
- mbstowcs(nxt_py_home, c->home, len + 1);
+ if (pep405) {
+ mbstowcs(nxt_py_home, c->home, len);
+ mbstowcs(nxt_py_home + len, bin_python, sizeof(bin_python));
+ Py_SetProgramName(nxt_py_home);
+
+ } else {
+ mbstowcs(nxt_py_home, c->home, len + 1);
+ Py_SetPythonHome(nxt_py_home);
+ }
+
#else
- nxt_memcpy(nxt_py_home, c->home, len + 1);
-#endif
+ nxt_py_home = nxt_malloc(len + 1);
+ if (nxt_slow_path(nxt_py_home == NULL)) {
+ nxt_alert(task, "Failed to allocate memory");
+ return NXT_ERROR;
+ }
+ nxt_memcpy(nxt_py_home, c->home, len + 1);
Py_SetPythonHome(nxt_py_home);
+#endif
}
Py_InitializeEx(0);