Blob Blame History Raw
diff -rupN Python-2.7.13/configure.ac Python-2.7.13-new/configure.ac
--- Python-2.7.13/configure.ac	2017-01-21 01:46:05.850614559 +0100
+++ Python-2.7.13-new/configure.ac	2017-01-21 01:46:05.853614527 +0100
@@ -4899,8 +4899,21 @@ do
   THREADHEADERS="$THREADHEADERS \$(srcdir)/$h"
 done
 
+dnl getpath module - default sys.path calculations
+AC_SUBST(MODULE_GETPATH)
+MODULE_GETPATH=Modules/getpath.o
+case $host in
+  *-*-mingw*)
+    dnl default sys.path calculations for windows platforms
+    MODULE_GETPATH=PC/getpathp.o
+    ;;
+esac
+
 AC_SUBST(SRCDIRS)
 SRCDIRS="Parser Grammar Objects Python Modules Mac"
+case $host in
+  *-*-mingw*) SRCDIRS="$SRCDIRS PC";;
+esac
 AC_MSG_CHECKING(for build directories)
 for dir in $SRCDIRS; do
     if test ! -d $dir; then
diff -rupN Python-2.7.13/Makefile.pre.in Python-2.7.13-new/Makefile.pre.in
--- Python-2.7.13/Makefile.pre.in	2016-12-17 21:05:06.000000000 +0100
+++ Python-2.7.13-new/Makefile.pre.in	2017-01-21 01:46:05.853614527 +0100
@@ -225,7 +225,7 @@ COVERAGE_REPORT_OPTIONS=--no-branch-cove
 # Modules
 MODULE_OBJS=	\
 		Modules/config.o \
-		Modules/getpath.o \
+		@MODULE_GETPATH@ \
 		Modules/main.o \
 		Modules/gcmodule.o
 
@@ -662,6 +662,7 @@ Modules/getbuildinfo.o: $(PARSER_OBJS) \
 	      -DHGBRANCH="\"`LC_ALL=C $(HGBRANCH)`\"" \
 	      -o $@ $(srcdir)/Modules/getbuildinfo.c
 
+# default sys.path calculations for posix platforms
 Modules/getpath.o: $(srcdir)/Modules/getpath.c Makefile
 	$(CC) -c $(PY_CFLAGS) -DPYTHONPATH='"$(PYTHONPATH)"' \
 		-DPREFIX='"$(prefix)"' \
@@ -670,6 +671,13 @@ Modules/getpath.o: $(srcdir)/Modules/get
 		-DVPATH='"$(VPATH)"' \
 		-o $@ $(srcdir)/Modules/getpath.c
 
+# default sys.path calculations for windows platforms
+PC/getpathp.o: $(srcdir)/PC/getpathp.c Makefile
+	$(CC) -c $(PY_CORE_CFLAGS) \
+		-DVERSION='"$(VERSION)"' \
+		-DSRCDIR='"$(srcdir)"' \
+		-o $@ $(srcdir)/PC/getpathp.c
+
 Modules/python.o: $(srcdir)/Modules/python.c
 	$(MAINCC) -c $(PY_CFLAGS) -o $@ $(srcdir)/Modules/python.c
 
diff -rupN Python-2.7.13/PC/getpathp.c Python-2.7.13-new/PC/getpathp.c
--- Python-2.7.13/PC/getpathp.c	2016-12-17 21:05:07.000000000 +0100
+++ Python-2.7.13-new/PC/getpathp.c	2017-01-21 01:46:05.853614527 +0100
@@ -81,10 +81,26 @@
  * information sources.
  */
 
+#ifndef PYTHONPATH
+#  define PYTHONPATH ".\\DLLs;.\\lib"
+#endif
+
 #ifndef LANDMARK
 #define LANDMARK "lib\\os.py"
 #endif
 
+#ifdef __MINGW32__
+
+static char *lib_python = "lib\\python" VERSION;
+
+#  undef LANDMARK
+#  define LANDMARK "os.py"
+
+#  define USE_POSIX_PREFIX
+
+#endif
+
+
 static char prefix[MAXPATHLEN+1];
 static char progpath[MAXPATHLEN+1];
 static char dllpath[MAXPATHLEN+1];
@@ -168,6 +184,69 @@ join(char *buffer, char *stuff)
     buffer[n+k] = '\0';
 }
 
+#ifdef USE_POSIX_PREFIX
+
+/* based on getpath.c but with paths relative to executable */
+/* search_for_prefix requires that path be no more than MAXPATHLEN
+   bytes long.
+   return: 1 if found; -1 if found build directory
+*/
+static int
+search_for_posix_prefix(char *argv0_path, char *home, char *_prefix)
+{
+    size_t n;
+
+    /* If PYTHONHOME is set, we believe it unconditionally */
+    if (home) {
+        char *delim;
+        strncpy(prefix, home, MAXPATHLEN);
+        delim = strchr(prefix, DELIM);
+        if (delim)
+            *delim = '\0';
+        join(prefix, lib_python);
+        join(prefix, LANDMARK);
+        return 1;
+    }
+
+    /* Check to see if argv[0] is in the build directory */
+    strcpy(prefix, argv0_path);
+    join(prefix, "Modules\\Setup");
+    if (exists(prefix)) {
+        char *vpath;
+        /* Check source directory if argv0_path is in the build directory. */
+        vpath = _Py_char2wchar(SRCDIR, NULL);
+        if (vpath != NULL) {
+            strcpy(prefix, argv0_path);
+            join(prefix, vpath);
+            PyMem_Free(vpath);
+            join(prefix, "Lib");
+            join(prefix, LANDMARK);
+            if (ismodule(prefix))
+                return -1;
+        }
+    }
+
+    /* Search from argv0_path, until root is found */
+    strcpy(prefix, argv0_path);
+    do {
+        n = strlen(prefix);
+        join(prefix, lib_python);
+        join(prefix, LANDMARK);
+        if (ismodule(prefix))
+            return 1;
+        prefix[n] = '\0';
+        reduce(prefix);
+    } while (prefix[0]);
+
+    /* Configure prefix is unused */
+    (void)_prefix;
+
+    /* Fail */
+    return 0;
+}
+
+#endif /*def USE_POSIX_PREFIX */
+
 /* gotlandmark only called by search_for_prefix, which ensures
    'prefix' is null terminated in bounds.  join() ensures
    'landmark' can not overflow prefix if too long.
@@ -467,6 +546,9 @@ calculate_path(void)
     size_t bufsz;
     char *pythonhome = Py_GetPythonHome();
     char *envpath = Py_GETENV("PYTHONPATH");
+#ifdef USE_POSIX_PREFIX
+    int pfound;
+#endif
 
 #ifdef MS_WINDOWS
     int skiphome, skipdefault;
@@ -480,6 +562,17 @@ calculate_path(void)
     /* progpath guaranteed \0 terminated in MAXPATH+1 bytes. */
     strcpy(argv0_path, progpath);
     reduce(argv0_path);
+
+#ifdef USE_POSIX_PREFIX
+    pfound = search_for_posix_prefix(argv0_path, pythonhome, NULL);
+    if (!pfound) {
+        strncpy(prefix, argv0_path, MAXPATHLEN);
+        reduce(prefix);
+        join(prefix, lib_python);
+    }
+    else
+        reduce(prefix);
+#else
     if (pythonhome == NULL || *pythonhome == '\0') {
         if (search_for_prefix(argv0_path, LANDMARK))
             pythonhome = prefix;
@@ -488,11 +581,11 @@ calculate_path(void)
     }
     else
         strncpy(prefix, pythonhome, MAXPATHLEN);
+#endif
 
     if (envpath && *envpath == '\0')
         envpath = NULL;
 
-
 #ifdef MS_WINDOWS
     /* Calculate zip archive path */
     if (dllpath[0])             /* use name of python DLL */
@@ -546,6 +639,9 @@ calculate_path(void)
     }
     else
         bufsz = 0;
+#ifdef USE_POSIX_PREFIX
+    bufsz += strlen(prefix) + 1;
+#endif
     bufsz += strlen(PYTHONPATH) + 1;
     bufsz += strlen(argv0_path) + 1;
 #ifdef MS_WINDOWS
@@ -590,6 +686,11 @@ calculate_path(void)
         buf = strchr(buf, '\0');
         *buf++ = DELIM;
     }
+#ifdef USE_POSIX_PREFIX
+    strcpy(buf, prefix);
+    buf = strchr(buf, '\0');
+    *buf++ = DELIM;
+#endif
     if (userpath) {
         strcpy(buf, userpath);
         buf = strchr(buf, '\0');
@@ -652,6 +753,12 @@ calculate_path(void)
        on the path, and that our 'prefix' directory is
        the parent of that.
     */
+#ifdef USE_POSIX_PREFIX
+    if (pfound > 0) {
+        reduce(prefix);
+        reduce(prefix);
+    }
+#endif
     if (*prefix=='\0') {
         char lookBuf[MAXPATHLEN+1];
         char *look = buf - 1; /* 'buf' is at the end of the buffer */