# HG changeset patch # User timeless # Date 2016-03-09 15:47:01 # Node ID 17b85d739b6270453914b9bac2354bb5af43c96f # Parent a478816804023a2ad517b86d6091a73fc53f702f setup: create a module for the modulepolicy Instead of rewriting __init__ to define the modulepolicy, write out a __modulepolicy__.py file like __version__.py This should work for both system-wide installation and in-place build. Therefore we can avoid relying on two separate modulepolicy rules, '@MODULELOADPOLICY@' and 'mercurial/modulepolicy'. diff --git a/.hgignore b/.hgignore --- a/.hgignore +++ b/.hgignore @@ -40,6 +40,7 @@ doc/*.[0-9].{x,ht}ml 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,6 +63,7 @@ clean: \( -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 + rm -f mercurial/__modulepolicy__.py if test -d .hg; then rm -f 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 @@ import zipimport # 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 @@ -314,21 +314,16 @@ class hgbuildpy(build_py): 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) + def run(self): + if self.distribution.pure: + modulepolicy = 'py' + else: + modulepolicy = 'c' + with open("mercurial/__modulepolicy__.py", "w") as f: + f.write('# this file is autogenerated by setup.py\n') + f.write('modulepolicy = "%s"\n' % modulepolicy) - 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 + build_py.run(self) class buildhgextindex(Command): description = 'generate prebuilt index of hgext (for frozen package)'