Patchwork [10,of,10,V3,py3,support] run-tests: minimum changes required to make run-tests work in python3

login
register
mail settings
Submitter Augie Fackler
Date Sept. 20, 2013, 5:19 p.m.
Message ID <84540f70fcecf6af0f2c.1379697581@arthedain.pit.corp.google.com>
Download mbox | patch
Permalink /patch/2575/
State Deferred
Headers show

Comments

Augie Fackler - Sept. 20, 2013, 5:19 p.m.
# HG changeset patch
# User Augie Fackler <raf@durin42.com>
# Date 1379621130 14400
#      Thu Sep 19 16:05:30 2013 -0400
# Node ID 84540f70fcecf6af0f2c3f25b001d7b773dfcdc3
# Parent  f56a1666fb72e636d1f67fcff48d45abad8b176b
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[0]. 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.

0: subprocess works in bytes, but all the run-tests manipulation of
test data currently happens on unicode string objects in Python 3, as
many of the functions are missing on bytes for now.

Patch

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: