Re: [PATCH] exewrapper: add an ability to always load python from hg-python subdir

2017-03-12 Thread Mads Kiilerich

On 03/12/2017 04:02 PM, Kostia Balytskyi wrote:

# HG changeset patch
# User Kostia Balytskyi 
# Date 1489359573 25200
#  Sun Mar 12 15:59:33 2017 -0700
# Node ID bdd61a3470df08b09bd18bbb40e33a8f7cabe188
# Parent  7548522742b5f4f9f5c0881ae4a2783ecda2f969
exewrapper: add an ability to always load python from hg-python subdir

Currently hg.exe will only try to load python27.dll from hg-python
subdir if PYTHONHOME environment variable is not set and if this
approach fails, proceed to load it from whereever possible. I want
to be able to compile a version of hg.exe which will only use
hg-python and not other options, regardless of its environment. This
patch makes it so running 'python setup.py build_hgexe --usehgpython'
builds such version.


It would be nice if we didn't need this compile flag.

I guess the opposite search order would be perfectly fine: *if* there is 
a hg-python sub folder, then use it. If not, use PYTHONHOME ... or 
search in $PATH.


Do you think that would work?


It also breaks test-check-commit.t becuase it introduces a function
named 'initialize_options', but that is a function from parent class,
so there's not much we can do about it.


Breaks ... how?

And if it breaks it, then it must be fixed or "fixed". That doesn't seem 
to be a part of this.



diff --git a/mercurial/exewrapper.c b/mercurial/exewrapper.c
--- a/mercurial/exewrapper.c
+++ b/mercurial/exewrapper.c
@@ -29,6 +29,14 @@ static char pyhome[MAX_PATH + 10];
  static char envpyhome[MAX_PATH + 10];
  static char pydllfile[MAX_PATH + 10];
  
+/* Compiling with /DUSEHGPYTHON makes Mercurial load Python from hg-python

+subdir regardless of environment in which hg.exe is ran. */
+#ifdef USEHGPYTHON
+static int usehgpython = 1;
+#else
+static int usehgpython = 0;
+#endif
+
  int main(int argc, char *argv[])
  {
char *p;
@@ -71,15 +79,13 @@ int main(int argc, char *argv[])
We first check, that environment variable PYTHONHOME is *not* set.
This just mimicks the behavior of the regular python.exe, which uses
PYTHONHOME to find its installation directory (if it has been set).
-   Note: Users of HackableMercurial are expected to *not* set PYTHONHOME!
+   Note: Users of HackableMercurial are expected to *not* set PYTHONHOME
+   or compile exewrapper.c with /DUSEHGPYTHON.
*/
-   if (GetEnvironmentVariable("PYTHONHOME", envpyhome,
-  sizeof(envpyhome)) == 0)
+   if (usehgpython || (GetEnvironmentVariable("PYTHONHOME", envpyhome,
+  sizeof(envpyhome)) == 0))
{
-   /*
-   Environment var PYTHONHOME is *not* set. Let's see if we are
-   running inside a HackableMercurial.
-   */
+   /* We should try to load Python from hg-python subdir */
  
  		p = strrchr(pyhome, '\\');

if (p == NULL) {
@@ -112,6 +118,14 @@ int main(int argc, char *argv[])
}
Py_SetPythonHome(pyhome);
}
+   else
+   {
+   if (pydll == NULL && usehgpython)
+   {
+   err = "can't find hg-python subdir in Mercurial 
dir";
+   goto bail;
+   }
+   }
}
  
  	if (pydll == NULL) {

diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -405,6 +405,17 @@ class buildhgextindex(Command):
  class buildhgexe(build_ext):
  description = 'compile hg.exe from mercurial/exewrapper.c'
  
+user_options = build_ext.user_options + [

+('usehgpython', None, 'always load python dll from hg-python subdir'),
+]
+
+boolean_options = build_ext.boolean_options + ['usehgpython']
+
+def initialize_options(self):
+self.usehgpython = False
+return build_ext.initialize_options(self)
+
+
  def build_extensions(self):
  if os.name != 'nt':
  return
@@ -442,8 +453,10 @@ class buildhgexe(build_ext):
  with open('mercurial/hgpythonlib.h', 'wb') as f:
  f.write('/* this file is autogenerated by setup.py */\n')
  f.write('#define HGPYTHONLIB "%s"\n' % pythonlib)
+extra_preargs = [] if not self.usehgpython else ["/DUSEHGPYTHON"]
  objects = self.compiler.compile(['mercurial/exewrapper.c'],
- output_dir=self.build_temp)
+ output_dir=self.build_temp,
+ extra_preargs=extra_preargs)
  dir = os.path.dirname(self.get_ext_fullpath('dummy'))
  target = os.path.join(dir, 'hg')
  self.compiler.link_executable(objects, target,
___
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org

[PATCH] exewrapper: add an ability to always load python from hg-python subdir

2017-03-12 Thread Kostia Balytskyi
# HG changeset patch
# User Kostia Balytskyi 
# Date 1489359573 25200
#  Sun Mar 12 15:59:33 2017 -0700
# Node ID bdd61a3470df08b09bd18bbb40e33a8f7cabe188
# Parent  7548522742b5f4f9f5c0881ae4a2783ecda2f969
exewrapper: add an ability to always load python from hg-python subdir

Currently hg.exe will only try to load python27.dll from hg-python
subdir if PYTHONHOME environment variable is not set and if this
approach fails, proceed to load it from whereever possible. I want
to be able to compile a version of hg.exe which will only use
hg-python and not other options, regardless of its environment. This
patch makes it so running 'python setup.py build_hgexe --usehgpython'
builds such version.

It also breaks test-check-commit.t becuase it introduces a function
named 'initialize_options', but that is a function from parent class,
so there's not much we can do about it.

diff --git a/mercurial/exewrapper.c b/mercurial/exewrapper.c
--- a/mercurial/exewrapper.c
+++ b/mercurial/exewrapper.c
@@ -29,6 +29,14 @@ static char pyhome[MAX_PATH + 10];
 static char envpyhome[MAX_PATH + 10];
 static char pydllfile[MAX_PATH + 10];
 
+/* Compiling with /DUSEHGPYTHON makes Mercurial load Python from hg-python
+subdir regardless of environment in which hg.exe is ran. */
+#ifdef USEHGPYTHON
+static int usehgpython = 1;
+#else
+static int usehgpython = 0;
+#endif
+
 int main(int argc, char *argv[])
 {
char *p;
@@ -71,15 +79,13 @@ int main(int argc, char *argv[])
We first check, that environment variable PYTHONHOME is *not* set.
This just mimicks the behavior of the regular python.exe, which uses
PYTHONHOME to find its installation directory (if it has been set).
-   Note: Users of HackableMercurial are expected to *not* set PYTHONHOME!
+   Note: Users of HackableMercurial are expected to *not* set PYTHONHOME
+   or compile exewrapper.c with /DUSEHGPYTHON.
*/
-   if (GetEnvironmentVariable("PYTHONHOME", envpyhome,
-  sizeof(envpyhome)) == 0)
+   if (usehgpython || (GetEnvironmentVariable("PYTHONHOME", envpyhome,
+  sizeof(envpyhome)) == 0))
{
-   /*
-   Environment var PYTHONHOME is *not* set. Let's see if we are
-   running inside a HackableMercurial.
-   */
+   /* We should try to load Python from hg-python subdir */
 
p = strrchr(pyhome, '\\');
if (p == NULL) {
@@ -112,6 +118,14 @@ int main(int argc, char *argv[])
}
Py_SetPythonHome(pyhome);
}
+   else
+   {
+   if (pydll == NULL && usehgpython)
+   {
+   err = "can't find hg-python subdir in Mercurial 
dir";
+   goto bail;
+   }
+   }
}
 
if (pydll == NULL) {
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -405,6 +405,17 @@ class buildhgextindex(Command):
 class buildhgexe(build_ext):
 description = 'compile hg.exe from mercurial/exewrapper.c'
 
+user_options = build_ext.user_options + [
+('usehgpython', None, 'always load python dll from hg-python subdir'),
+]
+
+boolean_options = build_ext.boolean_options + ['usehgpython']
+
+def initialize_options(self):
+self.usehgpython = False
+return build_ext.initialize_options(self)
+
+
 def build_extensions(self):
 if os.name != 'nt':
 return
@@ -442,8 +453,10 @@ class buildhgexe(build_ext):
 with open('mercurial/hgpythonlib.h', 'wb') as f:
 f.write('/* this file is autogenerated by setup.py */\n')
 f.write('#define HGPYTHONLIB "%s"\n' % pythonlib)
+extra_preargs = [] if not self.usehgpython else ["/DUSEHGPYTHON"]
 objects = self.compiler.compile(['mercurial/exewrapper.c'],
- output_dir=self.build_temp)
+ output_dir=self.build_temp,
+ extra_preargs=extra_preargs)
 dir = os.path.dirname(self.get_ext_fullpath('dummy'))
 target = os.path.join(dir, 'hg')
 self.compiler.link_executable(objects, target,
___
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel