Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes for Python3 compatibility. #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions vbench/benchmark.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -17,6 +21,8 @@
import traceback
import inspect

from vbench.compat import exec_

# from pandas.util.testing import set_trace


Expand Down Expand Up @@ -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):
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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, "<magic-timeit>", "exec")

exec code in ns
exec_(code, ns)
timer.inner = ns["inner"]

if ncalls is None:
Expand Down
31 changes: 31 additions & 0 deletions vbench/compat.py
Original file line number Diff line number Diff line change
@@ -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""")
15 changes: 8 additions & 7 deletions vbench/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
__copyright__ = '2012-2013 Wes McKinney, Yaroslav Halchenko'
__license__ = 'MIT'

from __future__ import print_function
import os

import logging
Expand Down Expand Up @@ -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
======================

Expand All @@ -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:
Expand All @@ -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)

5 changes: 4 additions & 1 deletion vbench/runner.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import cPickle as pickle
try:
import cPickle as pickle
except ImportError:
import pickle
import os
import subprocess

Expand Down
1 change: 0 additions & 1 deletion vbench/tests/vbenchtest/vbenchtest
Submodule vbenchtest deleted from ecf481