[PATCH 5 of 7] setup: support executing with python3 including 2to3

Simon Heimberg simohe at besonet.ch
Tue Nov 8 16:36:11 CST 2011


# HG changeset patch
# User Simon Heimberg <simohe at besonet.ch>
# Date 1313933023 -7200
# Node ID 52ba1200de770ab5bb2c600f03ee9eca4a77ac4c
# Parent  287a7c88281cbe54abb385305ba188afdaa8e1fe
setup: support executing with python3 including 2to3

Merge the code from contrib/setup3.py in setup.
The argument for executing is marked as experimental.

Reason: The file in contrib was outdated (packages, cmdclass, ...)

diff -r 287a7c88281c -r 52ba1200de77 setup.py
--- a/setup.py	Die Nov 01 20:13:53 2011 +0100
+++ b/setup.py	Son Aug 21 15:23:43 2011 +0200
@@ -44,7 +44,8 @@
     pass
 
 if isironpython:
-    print "warning: IronPython detected (no bz2 support)"
+    # print statement is not compatible to python3
+    eval('print "warning: IronPython detected (no bz2 support)"', {})
 else:
     try:
         import bz2
@@ -68,6 +69,18 @@
 from distutils.sysconfig import get_python_inc
 from distutils.version import StrictVersion
 
+convert2to3 = '--c2to3' in sys.argv
+if convert2to3:
+    try:
+        from distutils.command.build_py import build_py_2to3 as build_py
+        from lib2to3.refactor import get_fixers_from_package as getfixers
+    except ImportError:
+        if sys.version_info[0] < 3:
+            raise SystemExit("--c2to3 is only compatible with python3.")
+        raise
+    sys.path.append('contrib')
+
+
 scripts = ['hg']
 if os.name == 'nt':
     scripts.append('contrib/win32/hg.bat')
@@ -190,6 +203,11 @@
     # Insert hgbuildmo first so that files in mercurial/locale/ are found
     # when build_py is run next.
     sub_commands = [('build_mo', None),
+    # We also need build_ext before build_py. Otherwise, when 2to3 is called (in
+    # build_py), it will not find osutil & friends, thinking that those modules are
+    # global and, consequently, making a mess, now that all module imports are
+    # global.
+                    ('build_ext', build.has_ext_modules),
                    ] + build.sub_commands
 
 class hgbuildmo(Command):
@@ -235,6 +253,8 @@
     global_options = Distribution.global_options + \
                      [('pure', None, "use pure (slow) Python "
                         "code instead of C extensions"),
+                      ('c2to3', None, "(experimental!) convert "
+                        "code with 2to3"),
                      ]
 
     def has_ext_modules(self):
@@ -254,6 +274,9 @@
                      ext.name)
 
 class hgbuildpy(build_py):
+    if convert2to3:
+        fixer_names = sorted(set(getfixers("lib2to3.fixes") +
+                                 getfixers("hgfixes")))
 
     def finalize_options(self):
         build_py.finalize_options(self)
@@ -344,7 +367,7 @@
             fp.close()
 
             # skip binary files
-            if '\0' in data:
+            if b('\0') in data:
                 continue
 
             data = data.replace('@LIBDIR@', libdir.encode('string_escape'))


More information about the Mercurial-devel mailing list