[PATCH STABLE] exewrapper: adapt for legazy HackableMercurial

Adrian Buehlmann adrian at cadifra.com
Tue Jul 31 15:16:38 CDT 2012


# HG changeset patch
# User Adrian Buehlmann <adrian at cadifra.com>
# Date 1343764982 -7200
# Branch stable
# Node ID b4143a951c5569e9d403aea67148718ea3f4b5af
# Parent  a028ce66e2b7d608344e7b5b5c1a55e4a6ac54b5
exewrapper: adapt for legazy HackableMercurial

We now call Py_SetPythonHome() if there is a "hg-python" subdir, which is the
canonical Python installation directory used by HackableMercurial.

Note that we don't call Py_SetPythonHome(), if environment variable PYTHONHOME
was set. This matches the behavior of python.exe and allows to override the
default for HackableMercurial (i.e. ignoring the "hg-python" subdir).

Note that this *still* requires pythonXX.dll next to the hg.exe in the root
directory of HackableMercurial, or globally installed, for example, in
C:/Windows/system32. exewrapper makes no attempt whatsoever to locate
pythonXX.dll. It deliberately depends on standard Windows behavior for locating
that dll.

If there is no "hg-python" subdir found next to hg.exe, we continue to behave
as before, which allows to find a globally installed Python.

diff --git a/mercurial/exewrapper.c b/mercurial/exewrapper.c
--- a/mercurial/exewrapper.c
+++ b/mercurial/exewrapper.c
@@ -20,10 +20,12 @@
 
 
 static char pyscript[MAX_PATH + 10];
+static char pyhome[MAX_PATH + 10];
+static char envpyhome[MAX_PATH + 10];
 
 int main(int argc, char *argv[])
 {
-	char *dot;
+	char *p;
 	int ret;
 	int i;
 	int n;
@@ -38,12 +40,13 @@
 		goto bail;
 	}
 
-	dot = strrchr(pyscript, '.');
-	if (dot == NULL) {
+	p = strrchr(pyscript, '.');
+	if (p == NULL) {
 		err = "malformed module filename";
 		goto bail;
 	}
-	*dot = 0; /* cut trailing ".exe" */
+	*p = 0; /* cut trailing ".exe" */
+	strcpy(pyhome, pyscript);
 
 	hfind = FindFirstFile(pyscript, &fdata);
 	if (hfind != INVALID_HANDLE_VALUE) {
@@ -54,6 +57,29 @@
 		strcat_s(pyscript, sizeof(pyscript), "exe.py");
 	}
 
+	if (GetEnvironmentVariable("PYTHONHOME", envpyhome,
+				   sizeof(envpyhome)) == 0)
+	{
+		/* environment var PYTHONHOME is not set */
+
+		p = strrchr(pyhome, '\\');
+		if (p == NULL) {
+			err = "can't find backslash in module filename";
+			goto bail;
+		}
+		*p = 0; /* cut at directory */
+
+		strcat(pyhome, "\\hg-python"); /* HackableMercurial has its      */
+		                               /* private Python installed there */
+
+		hfind = FindFirstFile(pyhome, &fdata);
+		if (hfind != INVALID_HANDLE_VALUE) {
+			/* path pyhome exists, let's use it */
+			FindClose(hfind);
+			Py_SetPythonHome(pyhome);
+		}
+	}
+
 	/*
 	Only add the pyscript to the args, if it's not already there. It may
 	already be there, if the script spawned a child process of itself, in


More information about the Mercurial-devel mailing list