[Python-checkins] benchmarks: Make perf.py 3.x-compatible

antoine.pitrou python-checkins at python.org
Sun Feb 12 17:11:25 CET 2012


http://hg.python.org/benchmarks/rev/a49e6b66cf50
changeset:   156:a49e6b66cf50
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Sun Feb 12 17:08:26 2012 +0100
summary:
  Make perf.py 3.x-compatible

files:
  perf.py |  24 ++++++++++++++----------
  1 files changed, 14 insertions(+), 10 deletions(-)


diff --git a/perf.py b/perf.py
--- a/perf.py
+++ b/perf.py
@@ -67,7 +67,11 @@
 import tempfile
 import time
 import threading
-import urllib2
+try:
+    from urllib.request import urlopen
+    from urllib.error import URLError
+except ImportError:
+    from urllib2 import urlopen, URLError
 try:
     import multiprocessing
 except ImportError:
@@ -706,7 +710,7 @@
                                           *args, **kwargs)
         base_data = benchmark_function(base_python, options,
                                        *args, **kwargs)
-    except subprocess.CalledProcessError, e:
+    except subprocess.CalledProcessError as e:
         return BenchmarkError(e)
 
     return CompareBenchmarkData(base_data, changed_data, options)
@@ -791,8 +795,8 @@
     """
     tinyurl_api = "http://tinyurl.com/api-create.php?url="
     try:
-        url = urllib2.urlopen(tinyurl_api + url).read()
-    except urllib2.URLError:
+        url = urlopen(tinyurl_api + url).read()
+    except URLError:
         info("failed to call out to tinyurl.com")
     return url
 
@@ -851,7 +855,7 @@
 
 
 def LogCall(command):
-    command = map(str, command)
+    command = list(map(str, command))
     info("Running %s", " ".join(command))
     return command
 
@@ -1050,7 +1054,7 @@
         future = MemoryUsageFuture(subproc.pid)
     stdout, stderr = subproc.communicate()
     if subproc.returncode != 0:
-        raise RuntimeError(u"Benchmark died: " + unicode(stderr, 'ascii'))
+        raise RuntimeError("Benchmark died: " + stderr.decode('latin1'))
     if track_memory:
         mem_usage = future.GetMemoryUsage()
     return stdout, stderr, mem_usage
@@ -1181,7 +1185,7 @@
             result, err = comparer.communicate()
             if comparer.returncode != 0:
                 return BenchmarkError("pybench died: " + err)
-    except subprocess.CalledProcessError, e:
+    except subprocess.CalledProcessError as e:
         return BenchmarkError(e)
 
     if options.verbose:
@@ -1460,7 +1464,7 @@
                                        spitfire_env, extra_args)
         base_data = MeasureSpitfire(base_python, options,
                                     spitfire_env, extra_args)
-    except subprocess.CalledProcessError, e:
+    except subprocess.CalledProcessError as e:
         return str(e)
 
     return CompareBenchmarkData(base_data, changed_data, options)
@@ -1667,7 +1671,7 @@
     command = python + cmd_opts + ["-c", work]
     mem_usage = []
     info("Running `%s` %d times", command, num_loops * 20)
-    for _ in xrange(num_loops):
+    for _ in range(num_loops):
         t0 = time.time()
         _StartupPython(command, mem_usage, track_memory, inherit_env)
         _StartupPython(command, mem_usage, track_memory, inherit_env)
@@ -1957,7 +1961,7 @@
 
 def _FindAllBenchmarks(namespace):
     return dict((name[3:].lower(), func)
-                for (name, func) in sorted(namespace.iteritems())
+                for (name, func) in sorted(namespace.items())
                 if name.startswith("BM_"))
 
 BENCH_FUNCS = _FindAllBenchmarks(globals())

-- 
Repository URL: http://hg.python.org/benchmarks


More information about the Python-checkins mailing list