summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-07-15 19:12:33 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-07-15 19:12:33 (GMT)
commit3a2290865972e47d68360af4fed6ad9ef4b4434c (patch)
treea72ae3ae80adcfb5f4997517c53108afe3c0c467 /Lib
parenta7282c0ff7aff5ed00c0355e60fcbd34c45af910 (diff)
parentced770da07f9dbd7cc3afd09c2488c60faefe73c (diff)
downloadcpython-3a2290865972e47d68360af4fed6ad9ef4b4434c.zip
cpython-3a2290865972e47d68360af4fed6ad9ef4b4434c.tar.gz
cpython-3a2290865972e47d68360af4fed6ad9ef4b4434c.tar.bz2
Issue #24631: Fixed regression in the timeit modulu with multyline setup.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_timeit.py10
-rwxr-xr-xLib/timeit.py7
2 files changed, 11 insertions, 6 deletions
diff --git a/Lib/test/test_timeit.py b/Lib/test/test_timeit.py
index 5e9d49e..2db3c1b 100644
--- a/Lib/test/test_timeit.py
+++ b/Lib/test/test_timeit.py
@@ -88,8 +88,8 @@ class TestTimeit(unittest.TestCase):
self.assertRaises(SyntaxError, timeit.Timer, setup='continue')
self.assertRaises(SyntaxError, timeit.Timer, setup='from timeit import *')
- fake_setup = "import timeit; timeit._fake_timer.setup()"
- fake_stmt = "import timeit; timeit._fake_timer.inc()"
+ fake_setup = "import timeit\ntimeit._fake_timer.setup()"
+ fake_stmt = "import timeit\ntimeit._fake_timer.inc()"
def fake_callable_setup(self):
self.fake_timer.setup()
@@ -272,6 +272,12 @@ class TestTimeit(unittest.TestCase):
self.assertEqual(s, "CustomSetup\n" * 3 +
"35 loops, best of 3: 2 sec per loop\n")
+ def test_main_multiple_setups(self):
+ s = self.run_main(seconds_per_increment=2.0,
+ switches=['-n35', '-s', 'a = "CustomSetup"', '-s', 'print(a)'])
+ self.assertEqual(s, "CustomSetup\n" * 3 +
+ "35 loops, best of 3: 2 sec per loop\n")
+
def test_main_fixed_reps(self):
s = self.run_main(seconds_per_increment=60.0, switches=['-r9'])
self.assertEqual(s, "10 loops, best of 9: 60 sec per loop\n")
diff --git a/Lib/timeit.py b/Lib/timeit.py
index d9f9563..2de88f7 100755
--- a/Lib/timeit.py
+++ b/Lib/timeit.py
@@ -109,19 +109,18 @@ class Timer:
if isinstance(setup, str):
# Check that the code can be compiled outside a function
compile(setup, dummy_src_name, "exec")
+ stmtprefix = setup + '\n'
setup = reindent(setup, 4)
elif callable(setup):
local_ns['_setup'] = setup
init += ', _setup=_setup'
+ stmtprefix = ''
setup = '_setup()'
else:
raise ValueError("setup is neither a string nor callable")
if isinstance(stmt, str):
# Check that the code can be compiled outside a function
- if isinstance(setup, str):
- compile(setup + '\n' + stmt, dummy_src_name, "exec")
- else:
- compile(stmt, dummy_src_name, "exec")
+ compile(stmtprefix + stmt, dummy_src_name, "exec")
stmt = reindent(stmt, 8)
elif callable(stmt):
local_ns['_stmt'] = stmt