summaryrefslogtreecommitdiff
path: root/PC/getpathp.c
diff options
context:
space:
mode:
authorVinay Sajip <vinay_sajip@yahoo.co.uk>2012-05-26 03:45:29 +0100
committerVinay Sajip <vinay_sajip@yahoo.co.uk>2012-05-26 03:45:29 +0100
commitbd885cf8bc59f7621b62029065a7bbfe7ab3b9fc (patch)
treeb4509ec6075d931aee2e92e6affe2a16eb578287 /PC/getpathp.c
parenta124eb0185543ea02b835a6a41f5128c0a2943f5 (diff)
downloadcpython-bd885cf8bc59f7621b62029065a7bbfe7ab3b9fc.tar.gz
Implemented PEP 405 (Python virtual environments).
Diffstat (limited to 'PC/getpathp.c')
-rw-r--r--PC/getpathp.c81
1 files changed, 81 insertions, 0 deletions
diff --git a/PC/getpathp.c b/PC/getpathp.c
index 8921aa01fa..b5bf325f46 100644
--- a/PC/getpathp.c
+++ b/PC/getpathp.c
@@ -423,6 +423,53 @@ get_progpath(void)
progpath[0] = '\0';
}
+static int
+find_env_config_value(FILE * env_file, const wchar_t * key, wchar_t * value)
+{
+ int result = 0; /* meaning not found */
+ char buffer[MAXPATHLEN*2+1]; /* allow extra for key, '=', etc. */
+
+ fseek(env_file, 0, SEEK_SET);
+ while (!feof(env_file)) {
+ char * p = fgets(buffer, MAXPATHLEN*2, env_file);
+ wchar_t tmpbuffer[MAXPATHLEN*2+1];
+ PyObject * decoded;
+ int n;
+
+ if (p == NULL)
+ break;
+ n = strlen(p);
+ if (p[n - 1] != '\n') {
+ /* line has overflowed - bail */
+ break;
+ }
+ if (p[0] == '#') /* Comment - skip */
+ continue;
+ decoded = PyUnicode_DecodeUTF8(buffer, n, "surrogateescape");
+ if (decoded != NULL) {
+ Py_ssize_t k;
+ k = PyUnicode_AsWideChar(decoded,
+ tmpbuffer, MAXPATHLEN * 2);
+ Py_DECREF(decoded);
+ if (k >= 0) {
+ wchar_t * tok = wcstok(tmpbuffer, L" \t\r\n");
+ if ((tok != NULL) && !wcscmp(tok, key)) {
+ tok = wcstok(NULL, L" \t");
+ if ((tok != NULL) && !wcscmp(tok, L"=")) {
+ tok = wcstok(NULL, L"\r\n");
+ if (tok != NULL) {
+ wcsncpy(value, tok, MAXPATHLEN);
+ result = 1;
+ break;
+ }
+ }
+ }
+ }
+ }
+ }
+ return result;
+}
+
static void
calculate_path(void)
{
@@ -457,6 +504,40 @@ calculate_path(void)
/* progpath guaranteed \0 terminated in MAXPATH+1 bytes. */
wcscpy(argv0_path, progpath);
reduce(argv0_path);
+
+ /* Search for an environment configuration file, first in the
+ executable's directory and then in the parent directory.
+ If found, open it for use when searching for prefixes.
+ */
+
+ {
+ wchar_t tmpbuffer[MAXPATHLEN+1];
+ wchar_t *env_cfg = L"pyvenv.cfg";
+ FILE * env_file = NULL;
+
+ wcscpy(tmpbuffer, argv0_path);
+ join(tmpbuffer, env_cfg);
+ env_file = _Py_wfopen(tmpbuffer, L"r");
+ if (env_file == NULL) {
+ errno = 0;
+ reduce(tmpbuffer);
+ reduce(tmpbuffer);
+ join(tmpbuffer, env_cfg);
+ env_file = _Py_wfopen(tmpbuffer, L"r");
+ if (env_file == NULL) {
+ errno = 0;
+ }
+ }
+ if (env_file != NULL) {
+ /* Look for a 'home' variable and set argv0_path to it, if found */
+ if (find_env_config_value(env_file, L"home", tmpbuffer)) {
+ wcscpy(argv0_path, tmpbuffer);
+ }
+ fclose(env_file);
+ env_file = NULL;
+ }
+ }
+
if (pythonhome == NULL || *pythonhome == '\0') {
if (search_for_prefix(argv0_path, LANDMARK))
pythonhome = prefix;