[PATCH 2 of 3] modulepolicy: create a module for the modulepolicy

timeless timeless at mozdev.org
Wed Mar 9 11:19:10 EST 2016


# HG changeset patch
# User timeless <timeless at mozdev.org>
# Date 1457538421 0
#      Wed Mar 09 15:47:01 2016 +0000
# Node ID 71cb80801ec4e3b6aa519da48b4601c3c7a41615
# Parent  f9363da1d708516fbf365d41c9075018b2ce020c
modulepolicy: create a module for the modulepolicy

Instead of rewriting __init__ to define the modulepolicy,
write out a __modulepolicy__.py file like __version__.py

diff --git a/.hgignore b/.hgignore
--- a/.hgignore
+++ b/.hgignore
@@ -40,6 +40,7 @@
 MANIFEST
 MANIFEST.in
 patches
+mercurial/__modulepolicy__.py
 mercurial/__version__.py
 mercurial/hgpythonlib.h
 mercurial.egg-info
diff --git a/Makefile b/Makefile
--- a/Makefile
+++ b/Makefile
@@ -63,7 +63,7 @@
 		\( -name '*.py[cdo]' -o -name '*.so' \) -exec rm -f '{}' ';'
 	rm -f $(addprefix mercurial/,$(notdir $(wildcard mercurial/pure/[a-z]*.py)))
 	rm -f MANIFEST MANIFEST.in hgext/__index__.py tests/*.err
-	if test -d .hg; then rm -f mercurial/__version__.py; fi
+	if test -d .hg; then rm -f mercurial/__modulepolicy__.py mercurial/__version__.py; fi
 	rm -rf build mercurial/locale
 	$(MAKE) -C doc clean
 
diff --git a/mercurial/__init__.py b/mercurial/__init__.py
--- a/mercurial/__init__.py
+++ b/mercurial/__init__.py
@@ -19,11 +19,14 @@
 #    c - require C extensions
 #    allow - allow pure Python implementation when C loading fails
 #    py - only load pure Python modules
-modulepolicy = '@MODULELOADPOLICY@'
 
 # By default, require the C extensions for performance reasons.
-if modulepolicy == '@' 'MODULELOADPOLICY' '@':
-    modulepolicy = 'c'
+modulepolicy = 'c'
+try:
+    from . import __modulepolicy__
+    modulepolicy = __modulepolicy__.modulepolicy
+except ImportError:
+    pass
 
 # PyPy doesn't load C extensions.
 #
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -334,28 +334,17 @@
         build_py.finalize_options(self)
 
         if self.distribution.pure:
+            modulepolicy = 'py'
             self.distribution.ext_modules = []
         else:
+            modulepolicy = 'c'
             h = os.path.join(get_python_inc(), 'Python.h')
             if not os.path.exists(h):
                 raise SystemExit('Python headers are required to build '
                                  'Mercurial but weren\'t found in %s' % h)
-
-    def copy_file(self, *args, **kwargs):
-        dst, copied = build_py.copy_file(self, *args, **kwargs)
-
-        if copied and dst.endswith('__init__.py'):
-            if self.distribution.pure:
-                modulepolicy = 'py'
-            else:
-                modulepolicy = 'c'
-            content = open(dst, 'rb').read()
-            content = content.replace(b'@MODULELOADPOLICY@',
-                                      modulepolicy.encode(libdir_escape))
-            with open(dst, 'wb') as fh:
-                fh.write(content)
-
-        return dst, copied
+        with open("mercurial/__modulepolicy__.py", "w") as f:
+            f.write('# this file is autogenerated by setup.py\n')
+            f.write('modulepolicy = "%s"\n' % modulepolicy)
 
 class buildhgextindex(Command):
     description = 'generate prebuilt index of hgext (for frozen package)'


More information about the Mercurial-devel mailing list