From 78fe6a70ee35a65753ab2aba6624fb86c3ae5542 Mon Sep 17 00:00:00 2001 From: Roumen Petrov Date: Thu, 18 Oct 2012 01:38:09 +0300 Subject: [PATCH 3/9] MINGW-implement exec prefix, i.e. use content of pybuilddir.txt --- PC/getpathp.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/PC/getpathp.c b/PC/getpathp.c index daf61c9..0bfd1cf 100644 --- a/PC/getpathp.c +++ b/PC/getpathp.c @@ -470,6 +470,10 @@ find_env_config_value(FILE * env_file, const wchar_t * key, wchar_t * value) return result; } +#ifdef __MINGW32__ +# define USE_EXEC_PREFIX +#endif + static void calculate_path(void) { @@ -478,6 +482,11 @@ calculate_path(void) size_t bufsz; wchar_t *pythonhome = Py_GetPythonHome(); wchar_t *envpath = NULL; +#ifdef USE_EXEC_PREFIX + /* POSIX build system => transfer from getpath.c */ + wchar_t exec_prefix[MAXPATHLEN+1]; + wchar_t *pexec_prefix = NULL; +#endif #ifdef MS_WINDOWS int skiphome, skipdefault; @@ -550,6 +559,42 @@ calculate_path(void) if (envpath && *envpath == '\0') envpath = NULL; +#if defined(USE_EXEC_PREFIX) && defined(Py_ENABLE_SHARED) + /* NOTE from getpath.c */ + /* 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); + join(exec_prefix, L"pybuilddir.txt"); + if (exists(exec_prefix)) { + 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) { + Py_ssize_t k; + k = PyUnicode_AsWideChar(decoded, + rel_builddir_path, MAXPATHLEN); + Py_DECREF(decoded); + if (k >= 0) { + rel_builddir_path[k] = L'\0'; + wcscpy(exec_prefix, argv0_path); + join(exec_prefix, rel_builddir_path); + pexec_prefix = exec_prefix; + } + } + } + } +#endif #ifdef MS_WINDOWS /* Calculate zip archive path */ @@ -606,6 +651,10 @@ calculate_path(void) bufsz = 0; bufsz += wcslen(PYTHONPATH) + 1; bufsz += wcslen(argv0_path) + 1; +#ifdef USE_EXEC_PREFIX + if (pexec_prefix) + bufsz += wcslen(pexec_prefix) + 1; +#endif #ifdef MS_WINDOWS if (userpath) bufsz += wcslen(userpath) + 1; @@ -648,6 +697,13 @@ calculate_path(void) buf = wcschr(buf, L'\0'); *buf++ = DELIM; } +#ifdef USE_EXEC_PREFIX + if (pexec_prefix) { + wcscpy(buf, pexec_prefix); + buf = wcschr(buf, L'\0'); + *buf++ = DELIM; + } +#endif if (userpath) { wcscpy(buf, userpath); buf = wcschr(buf, L'\0'); -- 1.7.12.1