Bug 1449296 - Make gdb/run-tests.py runnable with either Python 2 or 3, r=jorendorff
authorSteve Fink <sfink@mozilla.com>
Tue, 27 Mar 2018 12:35:49 -0700
changeset 773626 80f331e70605d0b9a3f277a5bc69b9521263cc6c
parent 773625 d76c6788d40a2de7616b81ba619343427ef2e30d
child 773627 e32869c3903801dfe6d407a010525dd54b42950c
push id104266
push userbmo:hsivonen@hsivonen.fi
push dateWed, 28 Mar 2018 07:33:03 +0000
reviewersjorendorff
bugs1449296
milestone61.0a1
Bug 1449296 - Make gdb/run-tests.py runnable with either Python 2 or 3, r=jorendorff
js/src/gdb/run-tests.py
js/src/gdb/taskpool.py
--- a/js/src/gdb/run-tests.py
+++ b/js/src/gdb/run-tests.py
@@ -99,17 +99,17 @@ class Summary(object):
         self.update()
 
     def finish(self):
         if not OPTIONS.hide_progress:
             self.bar.finish()
 
         if self.failures:
 
-            print "tests failed:"
+            print("tests failed:")
             for test in self.failures:
                 test.show(sys.stdout)
 
             if OPTIONS.worklist:
                 try:
                     with open(OPTIONS.worklist) as out:
                         for test in self.failures:
                             out.write(test.name + '\n')
@@ -124,17 +124,17 @@ class Summary(object):
                         for test in self.failures:
                             test.show(out)
                 except IOError as err:
                     sys.stderr.write("Error writing worklist file '%s': %s"
                                      % (OPTIONS.write_failures, err))
                     sys.exit(1)
 
         if self.timeouts:
-            print "tests timed out:"
+            print("tests timed out:")
             for test in self.timeouts:
                 test.show(sys.stdout)
 
         if self.failures or self.timeouts:
             sys.exit(2)
 
 class Test(TaskPool.Task):
     def __init__(self, path, summary):
@@ -182,30 +182,30 @@ class Test(TaskPool.Task):
             self.summary.failed(self)
         else:
             self.summary.passed(self)
 
     def onTimeout(self):
         self.summary.timeout(self)
 
     def show_cmd(self, out):
-        print "Command: ", make_shell_cmd(self.cmd())
+        out.write("Command: %s\n" % (make_shell_cmd(self.cmd()),))
 
     def show_output(self, out):
         if self.stdout:
             out.write('Standard output:')
             out.write('\n' + self.stdout + '\n')
         if self.stderr:
             out.write('Standard error:')
             out.write('\n' + self.stderr + '\n')
 
     def show(self, out):
         out.write(self.name + '\n')
         if OPTIONS.write_failure_output:
-            out.write('Command: %s\n' % (make_shell_cmd(self.cmd()),))
+            self.show_cmd(out)
             self.show_output(out)
             out.write('GDB exit code: %r\n' % (self.returncode,))
 
 def find_tests(dir, substring = None):
     ans = []
     for dirpath, _, filenames in os.walk(dir):
         if dirpath == '.':
             continue
--- a/js/src/gdb/taskpool.py
+++ b/js/src/gdb/taskpool.py
@@ -58,24 +58,17 @@ class TaskPool(object):
     class TerminateTask(Exception):
         pass
 
     def __init__(self, tasks, cwd='.', job_limit=4, timeout=150):
         self.pending = iter(tasks)
         self.cwd = cwd
         self.job_limit = job_limit
         self.timeout = timeout
-        self.next_pending = self.get_next_pending()
-
-    # Set self.next_pending to the next task that has not yet been executed.
-    def get_next_pending(self):
-        try:
-            return self.pending.next()
-        except StopIteration:
-            return None
+        self.next_pending = next(self.pending, None)
 
     def run_all(self):
         # The currently running tasks: a set of Task instances.
         running = set()
         with open(os.devnull, 'r') as devnull:
             while True:
                 while len(running) < self.job_limit and self.next_pending:
                     t = self.next_pending
@@ -87,17 +80,17 @@ class TaskPool(object):
                     # the post-'select' code below for details.
                     flags = fcntl.fcntl(p.stdout, fcntl.F_GETFL)
                     fcntl.fcntl(p.stdout, fcntl.F_SETFL, flags | os.O_NONBLOCK)
                     flags = fcntl.fcntl(p.stderr, fcntl.F_GETFL)
                     fcntl.fcntl(p.stderr, fcntl.F_SETFL, flags | os.O_NONBLOCK)
 
                     t.start(p, time.time() + self.timeout)
                     running.add(t)
-                    self.next_pending = self.get_next_pending()
+                    self.next_pending = next(self.pending, None)
 
                 # If we have no tasks running, and the above wasn't able to
                 # start any new ones, then we must be done!
                 if not running:
                     break
 
                 # How many seconds do we have until the earliest deadline?
                 now = time.time()
@@ -113,26 +106,26 @@ class TaskPool(object):
                     # Since we've placed the pipes in non-blocking mode, these
                     # 'read's will simply return as many bytes as are available,
                     # rather than blocking until they have accumulated the full
                     # amount requested (or reached EOF). The 'read's should
                     # never throw, since 'select' has told us there was
                     # something available.
                     if t.pipe.stdout in readable:
                         output = t.pipe.stdout.read(16384)
-                        if output != "":
+                        if len(output):
                             try:
-                                t.onStdout(output)
+                                t.onStdout(output.decode('utf-8'))
                             except TerminateTask:
                                 terminate.add(t)
                     if t.pipe.stderr in readable:
                         output = t.pipe.stderr.read(16384)
-                        if output != "":
+                        if len(output):
                             try:
-                                t.onStderr(output)
+                                t.onStderr(output.decode('utf-8'))
                             except TerminateTask:
                                 terminate.add(t)
                         else:
                             # We assume that, once a task has closed its stderr,
                             # it will soon terminate. If a task closes its
                             # stderr and then hangs, we'll hang too, here.
                             t.pipe.wait()
                             t.onFinished(t.pipe.returncode)
@@ -198,22 +191,22 @@ if __name__ == '__main__':
             def __init__(self, n):
                 super(SortableTask, self).__init__()
                 self.n = n
             def start(self, pipe, deadline):
                 super(SortableTask, self).start(pipe, deadline)
             def cmd(self):
                 return ['sh', '-c', 'echo out; sleep %d; echo err>&2' % (self.n,)]
             def onStdout(self, text):
-                print '%d stdout: %r' % (self.n, text)
+                print('%d stdout: %r' % (self.n, text))
             def onStderr(self, text):
-                print '%d stderr: %r' % (self.n, text)
+                print('%d stderr: %r' % (self.n, text))
             def onFinished(self, returncode):
-                print '%d (rc=%d)' % (self.n, returncode)
+                print('%d (rc=%d)' % (self.n, returncode))
                 sorted.append(self.n)
             def onTimeout(self):
-                print '%d timed out' % (self.n,)
+                print('%d timed out' % (self.n,))
 
         p = TaskPool([SortableTask(_) for _ in ns], job_limit=len(ns), timeout=timeout)
         p.run_all()
         return sorted
 
-    print repr(sleep_sort([1,1,2,3,5,8,13,21,34], 15))
+    print(repr(sleep_sort([1,1,2,3,5,8,13,21,34], 15)))