summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_timeit.py
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2014-08-23 03:13:50 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2014-08-23 03:13:50 (GMT)
commitef3b9ed0ac99c3825ebf7b716d504d52d080f8c2 (patch)
tree4c0d152397ca91b243e3b00b92a0db3c44f62c09 /Lib/test/test_timeit.py
parent682c04c70c9af6df68cacce47c2f0d18c31dd443 (diff)
downloadcpython-ef3b9ed0ac99c3825ebf7b716d504d52d080f8c2.zip
cpython-ef3b9ed0ac99c3825ebf7b716d504d52d080f8c2.tar.gz
cpython-ef3b9ed0ac99c3825ebf7b716d504d52d080f8c2.tar.bz2
Issue #2527: Add a *globals* argument to timeit functions, in order to override the globals namespace in which the timed code is executed.
Patch by Ben Roberts.
Diffstat (limited to 'Lib/test/test_timeit.py')
-rw-r--r--Lib/test/test_timeit.py16
1 files changed, 14 insertions, 2 deletions
diff --git a/Lib/test/test_timeit.py b/Lib/test/test_timeit.py
index 625fb8d..b3a96bb 100644
--- a/Lib/test/test_timeit.py
+++ b/Lib/test/test_timeit.py
@@ -86,9 +86,10 @@ class TestTimeit(unittest.TestCase):
def fake_callable_stmt(self):
self.fake_timer.inc()
- def timeit(self, stmt, setup, number=None):
+ def timeit(self, stmt, setup, number=None, globals=None):
self.fake_timer = FakeTimer()
- t = timeit.Timer(stmt=stmt, setup=setup, timer=self.fake_timer)
+ t = timeit.Timer(stmt=stmt, setup=setup, timer=self.fake_timer,
+ globals=globals)
kwargs = {}
if number is None:
number = DEFAULT_NUMBER
@@ -127,6 +128,17 @@ class TestTimeit(unittest.TestCase):
timer=FakeTimer())
self.assertEqual(delta_time, 0)
+ def test_timeit_globals_args(self):
+ global _global_timer
+ _global_timer = FakeTimer()
+ t = timeit.Timer(stmt='_global_timer.inc()', timer=_global_timer)
+ self.assertRaises(NameError, t.timeit, number=3)
+ timeit.timeit(stmt='_global_timer.inc()', timer=_global_timer,
+ globals=globals(), number=3)
+ local_timer = FakeTimer()
+ timeit.timeit(stmt='local_timer.inc()', timer=local_timer,
+ globals=locals(), number=3)
+
def repeat(self, stmt, setup, repeat=None, number=None):
self.fake_timer = FakeTimer()
t = timeit.Timer(stmt=stmt, setup=setup, timer=self.fake_timer)