From 8c612812d0617f14403f05d05a82d3ff53de4122 Mon Sep 17 00:00:00 2001 From: rockg Date: Thu, 19 Mar 2015 22:01:00 -0400 Subject: [PATCH] Changes for Python3 compatibility. --- vbench/benchmark.py | 22 +++++++++++++-------- vbench/compat.py | 31 ++++++++++++++++++++++++++++++ vbench/reports.py | 15 ++++++++------- vbench/runner.py | 5 ++++- vbench/tests/vbenchtest/vbenchtest | 1 - 5 files changed, 57 insertions(+), 17 deletions(-) create mode 100644 vbench/compat.py delete mode 160000 vbench/tests/vbenchtest/vbenchtest diff --git a/vbench/benchmark.py b/vbench/benchmark.py index 5a4156a..3e54887 100644 --- a/vbench/benchmark.py +++ b/vbench/benchmark.py @@ -1,6 +1,10 @@ # pylint: disable=W0122 -from cStringIO import StringIO +try: + from cStringIO import StringIO +except ImportError: + from io import StringIO + import cProfile try: @@ -17,6 +21,8 @@ import traceback import inspect +from vbench.compat import exec_ + # from pandas.util.testing import set_trace @@ -49,11 +55,11 @@ def __repr__(self): def _setup(self): ns = globals().copy() - exec self.setup in ns + exec_(self.setup, ns) return ns def _cleanup(self, ns): - exec self.cleanup in ns + exec_(self.cleanup, ns) @property def checksum(self): @@ -67,7 +73,7 @@ def profile(self, ncalls): def f(*args, **kw): for i in xrange(ncalls): - exec code in ns + exec_(code, ns) prof.runcall(f) self._cleanup(ns) @@ -109,7 +115,7 @@ def _run(self, ns, ncalls, disable_gc=False): start = time.clock() for _ in xrange(ncalls): - exec code in ns + exec_(code, ns) elapsed = time.clock() - start if disable_gc: @@ -355,13 +361,13 @@ def magic_timeit(ns, stmt, ncalls=None, repeat=3, force_ms=False): # but is there a better way to achieve that the code stmt has access # to the shell namespace? - src = timeit.template % {'stmt': timeit.reindent(stmt, 8), - 'setup': "pass"} + src = timeit.template.format(stmt=timeit.reindent(stmt, 8), + setup="pass") # Track compilation time so it can be reported if too long # Minimum time above which compilation time will be reported code = compile(src, "", "exec") - exec code in ns + exec_(code, ns) timer.inner = ns["inner"] if ncalls is None: diff --git a/vbench/compat.py b/vbench/compat.py new file mode 100644 index 0000000..dafcc12 --- /dev/null +++ b/vbench/compat.py @@ -0,0 +1,31 @@ + +import sys + +PY2 = sys.version_info[0] == 2 +PY3 = sys.version_info[0] == 3 + +if PY3: + import builtins + exec_ = getattr(builtins, "exec") + + def reraise(tp, value, tb=None): + if value is None: + value = tp() + if value.__traceback__ is not tb: + raise value.with_traceback(tb) + raise value + +else: + def exec_(_code_, _globs_=None, _locs_=None): + """Execute code in a namespace.""" + if _globs_ is None: + frame = sys._getframe(1) + _globs_ = frame.f_globals + if _locs_ is None: + _locs_ = frame.f_locals + del frame + elif _locs_ is None: + _locs_ = _globs_ + exec("""exec _code_ in _globs_, _locs_""") + + exec_("""def reraise(tp, value, tb=None): raise tp, value, tb""") \ No newline at end of file diff --git a/vbench/reports.py b/vbench/reports.py index edd9ac6..b3545b8 100644 --- a/vbench/reports.py +++ b/vbench/reports.py @@ -5,6 +5,7 @@ __copyright__ = '2012-2013 Wes McKinney, Yaroslav Halchenko' __license__ = 'MIT' +from __future__ import print_function import os import logging @@ -50,7 +51,7 @@ def generate_rst_files(benchmarks, dbpath, outpath, description=""): f.write(rst_text) with open(os.path.join(outpath, 'index.rst'), 'w') as f: - print >> f, """ + print(""" Performance Benchmarks ====================== @@ -62,7 +63,7 @@ def generate_rst_files(benchmarks, dbpath, outpath, description=""): .. toctree:: :hidden: :maxdepth: 3 -""" % locals() +""" % locals(), file=f) # group benchmarks by module there belonged to benchmarks_by_module = {} for b in benchmarks: @@ -72,14 +73,14 @@ def generate_rst_files(benchmarks, dbpath, outpath, description=""): benchmarks_by_module[module_name].append(b) for modname, mod_bmks in sorted(benchmarks_by_module.items()): - print >> f, ' vb_%s' % modname + print(' vb_%s' % modname, file=f) modpath = os.path.join(outpath, 'vb_%s.rst' % modname) with open(modpath, 'w') as mh: header = '%s\n%s\n\n' % (modname, '=' * len(modname)) - print >> mh, header + print(header, file=mh) for bmk in mod_bmks: - print >> mh, bmk.name - print >> mh, '-' * len(bmk.name) - print >> mh, '.. include:: vbench/%s.rst\n' % bmk.name + print(bmk.name, file=mh) + print('-' * len(bmk.name), file=mh) + print('.. include:: vbench/%s.rst\n' % bmk.name, file=mh) diff --git a/vbench/runner.py b/vbench/runner.py index eb1bb49..70eb960 100644 --- a/vbench/runner.py +++ b/vbench/runner.py @@ -1,4 +1,7 @@ -import cPickle as pickle +try: + import cPickle as pickle +except ImportError: + import pickle import os import subprocess diff --git a/vbench/tests/vbenchtest/vbenchtest b/vbench/tests/vbenchtest/vbenchtest deleted file mode 160000 index ecf481d..0000000 --- a/vbench/tests/vbenchtest/vbenchtest +++ /dev/null @@ -1 +0,0 @@ -Subproject commit ecf481df88f80bea42e33a02491711f51d6d565e