[PATCH 5 of 5 RFC py3 support] run-tests: minimum changes required to make run-tests work in python3

Augie Fackler raf at durin42.com
Thu Sep 19 15:08:14 CDT 2013


# HG changeset patch
# User Augie Fackler <raf at durin42.com>
# Date 1379621130 14400
#      Thu Sep 19 16:05:30 2013 -0400
# Node ID ff2da594a06def2e61f9e5e6226dcc27419a76fb
# Parent  cc547e41d77a130261625702e358307dde42dc39
run-tests: minimum changes required to make run-tests work in python3

This is just doing the dumbest thing possible and assumes test
input/output is all utf-8. This works for now, at least for the simple
tests I'm interested in trying to run, so I'm calling it progress
until I have to come back and figure out something smarter.

With these changes in place, all tests continue to pass on python2,
and you can run tests on python3 by running 2to3 over run-tests.py and
killdaemons.py. At that point, demandimport breaks on python3, causing
all tests to fail, but at least it's some progress.

diff --git a/tests/run-tests.py b/tests/run-tests.py
--- a/tests/run-tests.py
+++ b/tests/run-tests.py
@@ -60,11 +60,24 @@
 
 processlock = threading.Lock()
 
-# subprocess._cleanup can race with any Popen.wait or Popen.poll on py24
-# http://bugs.python.org/issue1731717 for details. We shouldn't be producing
-# zombies but it's pretty harmless even if we do.
-if sys.version_info[1] < 5:
-    subprocess._cleanup = lambda: None
+if sys.version_info[0] >= 3:
+    def makebytes(d):
+        if isinstance(d, str):
+            return d.encode('utf-8')
+        return d
+    def makestr(d):
+        if isinstance(d, bytes):
+            return d.decode('utf-8')
+        return d
+else:
+    # subprocess._cleanup can race with any Popen.wait or Popen.poll on py24
+    # http://bugs.python.org/issue1731717 for details. We shouldn't be producing
+    # zombies but it's pretty harmless even if we do.
+    if sys.version_info[1] < 5:
+        subprocess._cleanup = lambda: None
+    def makebytes(d):
+        return d
+    makestr = makebytes
 
 closefds = os.name == 'posix'
 def Popen4(cmd, wd, timeout, env=None):
@@ -764,7 +777,7 @@
     fd, name = tempfile.mkstemp(suffix='hg-tst')
     try:
         for l in script:
-            os.write(fd, l)
+            os.write(fd, makebytes(l))
         os.close(fd)
 
         cmd = '%s "%s"' % (options.shell, name)
@@ -860,6 +873,7 @@
     if abort:
         raise KeyboardInterrupt()
 
+    output = makestr(output)
     for s, r in replacements:
         output = re.sub(s, r, output)
     return ret, output.splitlines(True)
@@ -994,7 +1008,7 @@
         # Save errors to a file for diagnosis
         f = open(err, "wb")
         for line in out:
-            f.write(line)
+            f.write(makebytes(line))
         f.close()
 
     if skipped:


More information about the Mercurial-devel mailing list