diff -Naur Python-2.7.9-orig/configure.ac Python-2.7.9/configure.ac --- Python-2.7.9-orig/configure.ac 2014-12-11 13:49:48.175400000 +0300 +++ Python-2.7.9/configure.ac 2014-12-11 13:49:48.331400000 +0300 @@ -4895,8 +4895,21 @@ 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 Objects Python Modules" +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 -Naur Python-2.7.9-orig/Makefile.pre.in Python-2.7.9/Makefile.pre.in --- Python-2.7.9-orig/Makefile.pre.in 2014-12-10 18:59:50.000000000 +0300 +++ Python-2.7.9/Makefile.pre.in 2014-12-11 13:49:48.331400000 +0300 @@ -214,7 +214,7 @@ # Modules MODULE_OBJS= \ Modules/config.o \ - Modules/getpath.o \ + @MODULE_GETPATH@ \ Modules/main.o \ Modules/gcmodule.o @@ -591,6 +591,7 @@ -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)"' \ @@ -599,6 +600,13 @@ -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 -Naur Python-2.7.9-orig/PC/getpathp.c Python-2.7.9/PC/getpathp.c --- Python-2.7.9-orig/PC/getpathp.c 2014-12-10 18:59:58.000000000 +0300 +++ Python-2.7.9/PC/getpathp.c 2014-12-11 13:49:48.331400000 +0300 @@ -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 @@ 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 @@ 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 @@ /* 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 @@ } 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 @@ } 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 @@ 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 @@ 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 */