python2: Fix sys.prefix when Windows drive in argv[0]

When using a Python virtualenv, and launching it with a Windows
or Windows mixed path (C:\msys64\tmp\virtualenv\bin\python.exe or
C:/msys64/tmp/virtualenv/bin/python.exe), sys.prefix would always
be returned incorrectly as '/usr'.
This commit is contained in:
Ray Donnelly
2015-07-06 18:29:35 +01:00
parent cd684121c8
commit 14f7216bcd
3 changed files with 40 additions and 135 deletions

View File

@@ -0,0 +1,36 @@
diff -urN Python-2.7.10.orig/Modules/getpath.c Python-2.7.10/Modules/getpath.c
--- Python-2.7.10.orig/Modules/getpath.c 2015-07-06 13:16:43.261177500 +0100
+++ Python-2.7.10/Modules/getpath.c 2015-07-06 13:17:53.505177500 +0100
@@ -229,8 +229,20 @@
static void
copy_absolute(char *path, char *p)
{
+#if defined(__MSYS__)
+ if (p[0] == SEP || (p[0] != '\0' && p[1] == ':'))
+#else
if (p[0] == SEP)
+#endif
strcpy(path, p);
+#if defined(__MSYS__)
+ /* Turn C:* into /c* since sys.path can't have drive seps (:) in
+ since those delimit path list entries. */
+ if (path[0] != '\0' && path[1] == ':') {
+ path[1]=tolower(path[0]);
+ path[0]='/';
+ }
+#endif
else {
if (!getcwd(path, MAXPATHLEN)) {
/* unable to get the current directory */
@@ -482,7 +494,11 @@
}
else
progpath[0] = '\0';
+#if defined(__MSYS__)
+ if (progpath[0] != SEP && progpath[0] != '\0' && progpath[1] != ':')
+#else
if (progpath[0] != SEP && progpath[0] != '\0')
+#endif
absolutize(progpath);
strncpy(argv0_path, progpath, MAXPATHLEN);
argv0_path[MAXPATHLEN] = '\0';

View File

@@ -1,129 +0,0 @@
diff -urN a/Modules/getpath.c b/Modules/getpath.c
--- a/Modules/getpath.c 2013-10-21 11:14:32.295032500 +0100
+++ b/Modules/getpath.c 2013-10-21 12:23:00.165259900 +0100
@@ -10,6 +10,10 @@
#include <mach-o/dyld.h>
#endif
+#if defined(MS_WINDOWS) || defined(__CYGWIN__)
+#include <windows.h>
+#endif
+
/* Search in some common locations for the associated Python libraries.
*
* Two directories must be found, the platform independent directory
@@ -127,7 +131,11 @@
static char prefix[MAXPATHLEN+1];
static char exec_prefix[MAXPATHLEN+1];
-static char progpath[MAXPATHLEN+1];
+static char progpath[MAXPATHLEN+1] = {'\0'};
+#if defined(MS_WINDOWS) || defined(__CYGWIN__)
+static char dllpath[MAXPATHLEN+1] = {'\0'};
+extern HANDLE PyWin_DLLhModule;
+#endif
static char *module_search_path = NULL;
static char lib_python[] = "lib/python" VERSION;
@@ -208,7 +216,11 @@
joinpath(char *buffer, char *stuff)
{
size_t n, k;
+#if defined(MS_WINDOWS) || defined(__CYGWIN__)
+ if (stuff[0] == SEP || (strlen(stuff)>=2 && stuff[0] != 0 && stuff[1] == ':'))
+#else
if (stuff[0] == SEP)
+#endif
n = 0;
else {
n = strlen(buffer);
@@ -222,6 +234,13 @@
k = MAXPATHLEN - n;
strncpy(buffer+n, stuff, k);
buffer[n+k] = '\0';
+#if defined(__CYGWIN__)
+ /* MSYS specific: Turn C:* into /c* */
+ if (strlen(buffer)>2 && buffer[0] != 0 && buffer[1] == ':') {
+ buffer[1]=buffer[0]|32;
+ buffer[0]='/';
+ }
+#endif
}
/* copy_absolute requires that path be allocated at least
@@ -229,8 +248,21 @@
static void
copy_absolute(char *path, char *p)
{
+#if defined(MS_WINDOWS) || defined(__CYGWIN__)
+ if (p[0] == SEP || (strlen(p)>=2 && p[0] != 0 && p[1] == ':'))
+#else
if (p[0] == SEP)
+#endif
+ {
strcpy(path, p);
+#if defined(__CYGWIN__)
+ /* MSYS specific: Turn C:* into /c* */
+ if (strlen(path)>2 && path[0] != 0 && path[1] == ':') {
+ path[1]=path[0]|32;
+ path[0]='/';
+ }
+#endif
+ }
else {
if (!getcwd(path, MAXPATHLEN)) {
/* unable to get the current directory */
@@ -382,6 +414,42 @@
}
+#if defined(MS_WINDOWS) || defined(__CYGWIN__)
+/* Calculates dllpath and progpath, replacing \\ with / */
+int GetWindowsModulePaths()
+{
+ int result = 0;
+ char* seps;
+ result = GetModuleFileNameA(NULL, progpath, MAXPATHLEN);
+ seps = strchr(progpath, '\\');
+ while(seps) {
+ *seps = '/';
+ seps = strchr(seps, '\\');
+ }
+ dllpath[0] = '\0';
+#if defined(Py_ENABLE_SHARED) && !defined(__CYGWIN__)
+ // Hmm, cygwin/MSYS2 replacement for PyWin_DLLhModule is needed.
+ if (PyWin_DLLhModule) {
+ if((GetModuleFileNameA(PyWin_DLLhModule, dllpath, MAXPATHLEN) > 0)) {
+ result = 1;
+ seps = strchr(dllpath, '\\');
+ while(seps) {
+ *seps = '/';
+ seps = strchr(seps, '\\');
+ }
+ }
+ }
+#endif
+
+ /* Just so that 'MSYS specific: Turn C:* into /c*'
+ is done. */
+ absolutize(progpath);
+ absolutize(dllpath);
+
+ return result;
+}
+#endif /* MS_WINDOWS */
+
static void
calculate_path(void)
{
@@ -433,6 +501,10 @@
else if(0 == _NSGetExecutablePath(progpath, &nsexeclength) && progpath[0] == SEP)
;
#endif /* __APPLE__ */
+#if defined(MS_WINDOWS) || defined(__CYGWIN__)
+ else if(GetWindowsModulePaths()) {
+ }
+#endif /* MS_WINDOWS */
else if (path) {
while (1) {
char *delim = strchr(path, DELIM);

View File

@@ -3,7 +3,7 @@
pkgname=python2
pkgver=2.7.10
pkgrel=1
pkgrel=2
_pybasever=2.7
pkgdesc="A high-level scripting language"
arch=('i686' 'x86_64')
@@ -29,7 +29,7 @@ source=(http://www.python.org/ftp/python/${pkgver%rc?}/Python-${pkgver}.tar.xz
0200-msys2.patch
0210-reorder-bininstall-ln-symlink-creation.patch
0250-allow-win-drives-in-os-path-isabs.patch
2.7.5-allow-windows-paths-for-executable.patch)
0260-fix-sys-prefix-when-win-drive-in-argv0.patch)
sha1sums=('ee5a50c5562e7448f037d35fdedc18d95c748b9e'
'ff18e75ffa0351c481ca45065cdcc5cd83857712'
'eb3a15abc19c26754121b9a8f9e3e49055424e09'
@@ -45,7 +45,7 @@ sha1sums=('ee5a50c5562e7448f037d35fdedc18d95c748b9e'
'a1806da6956df1860cc23ff3701cd80cbb126b87'
'e6e13cb541efaaf228355cb1d7d5cd8574757c72'
'80975639f5ad78fd15b8f81bd98fb460c4ee05c4'
'fb78069fadcf5394ba380e26e371ad687d344415')
'6aa9ec9e99b75725a2f4de681ea8dbd0b2cad2e6')
prepare() {
cd "${srcdir}/Python-${pkgver}"
@@ -64,9 +64,7 @@ prepare() {
patch -p1 -i ${srcdir}/0200-msys2.patch
patch -p1 -i ${srcdir}/0210-reorder-bininstall-ln-symlink-creation.patch
patch -p1 -i ${srcdir}/0250-allow-win-drives-in-os-path-isabs.patch
# Incomplete patch from Ray Donnelly
# patch -p1 -i ${srcdir}/2.7.5-allow-windows-paths-for-executable.patch
patch -p1 -i ${srcdir}/0260-fix-sys-prefix-when-win-drive-in-argv0.patch
# Temporary workaround for FS#22322
# See http://bugs.python.org/issue10835 for upstream report