diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2010-08-13 22:25:01 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2010-08-13 22:25:01 (GMT) |
commit | e9b428f9977f8733e6b0d2c321c093779f95080f (patch) | |
tree | 2583b8f93efed20d9d849cbd1b001e4282e32f00 /Modules | |
parent | 09c449c7de0fea077ceaee5cb04017d6994341e7 (diff) | |
download | cpython-e9b428f9977f8733e6b0d2c321c093779f95080f.zip cpython-e9b428f9977f8733e6b0d2c321c093779f95080f.tar.gz cpython-e9b428f9977f8733e6b0d2c321c093779f95080f.tar.bz2 |
Reimplement addbuilddir() in C inside getpath.c, so as to execute it
at interpreter startup before importing any non-builtin modules.
Should fix #9589.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/getpath.c | 31 | ||||
-rw-r--r-- | Modules/main.c | 12 |
2 files changed, 34 insertions, 9 deletions
diff --git a/Modules/getpath.c b/Modules/getpath.c index 4164a12..fff502e 100644 --- a/Modules/getpath.c +++ b/Modules/getpath.c @@ -394,12 +394,35 @@ search_for_exec_prefix(wchar_t *argv0_path, wchar_t *home) return 1; } - /* Check to see if argv[0] is in the build directory */ + /* Check to see if argv[0] is in the build directory. "pybuilddir.txt" + is written by setup.py and contains the relative path to the location + of shared library modules. */ wcscpy(exec_prefix, argv0_path); - joinpath(exec_prefix, L"Modules/Setup"); + joinpath(exec_prefix, L"pybuilddir.txt"); if (isfile(exec_prefix)) { - reduce(exec_prefix); - return -1; + FILE *f = _Py_wfopen(exec_prefix, L"rb"); + if (f == NULL) + errno = 0; + else { + char buf[MAXPATHLEN+1]; + PyObject *decoded; + wchar_t rel_builddir_path[MAXPATHLEN+1]; + size_t n; + n = fread(buf, 1, MAXPATHLEN, f); + buf[n] = '\0'; + fclose(f); + decoded = PyUnicode_DecodeUTF8(buf, n, "surrogateescape"); + if (decoded != NULL) { + n = PyUnicode_AsWideChar(decoded, rel_builddir_path, MAXPATHLEN); + Py_DECREF(decoded); + if (n >= 0) { + rel_builddir_path[n] = L'\0'; + wcscpy(exec_prefix, argv0_path); + joinpath(exec_prefix, rel_builddir_path); + return -1; + } + } + } } /* Search from argv0_path, until root is found */ diff --git a/Modules/main.c b/Modules/main.c index 7929b05..d605bab 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -101,10 +101,10 @@ PYTHONCASEOK : ignore case in 'import' statements (Windows).\n\ PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.\n\ "; -#ifndef MS_WINDOWS -static FILE* -_wfopen(const wchar_t *path, const wchar_t *mode) +FILE * +_Py_wfopen(const wchar_t *path, const wchar_t *mode) { +#ifndef MS_WINDOWS char cpath[PATH_MAX]; char cmode[10]; size_t r; @@ -119,8 +119,10 @@ _wfopen(const wchar_t *path, const wchar_t *mode) return NULL; } return fopen(cpath, cmode); -} +#else + return _wfopen(path, mode); #endif +} static int @@ -640,7 +642,7 @@ Py_Main(int argc, wchar_t **argv) } if (sts==-1 && filename!=NULL) { - if ((fp = _wfopen(filename, L"r")) == NULL) { + if ((fp = _Py_wfopen(filename, L"r")) == NULL) { char cfilename[PATH_MAX]; size_t r = wcstombs(cfilename, filename, PATH_MAX); if (r == PATH_MAX) |