mirror of https://github.com/python/cpython.git
Issue #24631: Fixed regression in the timeit modulu with multyline setup.
This commit is contained in:
parent
76d508b5d4
commit
ced770da07
|
@ -88,8 +88,8 @@ def test_timer_invalid_setup(self):
|
||||||
self.assertRaises(SyntaxError, timeit.Timer, setup='continue')
|
self.assertRaises(SyntaxError, timeit.Timer, setup='continue')
|
||||||
self.assertRaises(SyntaxError, timeit.Timer, setup='from timeit import *')
|
self.assertRaises(SyntaxError, timeit.Timer, setup='from timeit import *')
|
||||||
|
|
||||||
fake_setup = "import timeit; timeit._fake_timer.setup()"
|
fake_setup = "import timeit\ntimeit._fake_timer.setup()"
|
||||||
fake_stmt = "import timeit; timeit._fake_timer.inc()"
|
fake_stmt = "import timeit\ntimeit._fake_timer.inc()"
|
||||||
|
|
||||||
def fake_callable_setup(self):
|
def fake_callable_setup(self):
|
||||||
self.fake_timer.setup()
|
self.fake_timer.setup()
|
||||||
|
@ -272,6 +272,12 @@ def test_main_setup(self):
|
||||||
self.assertEqual(s, "CustomSetup\n" * 3 +
|
self.assertEqual(s, "CustomSetup\n" * 3 +
|
||||||
"35 loops, best of 3: 2 sec per loop\n")
|
"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):
|
def test_main_fixed_reps(self):
|
||||||
s = self.run_main(seconds_per_increment=60.0, switches=['-r9'])
|
s = self.run_main(seconds_per_increment=60.0, switches=['-r9'])
|
||||||
self.assertEqual(s, "10 loops, best of 9: 60 sec per loop\n")
|
self.assertEqual(s, "10 loops, best of 9: 60 sec per loop\n")
|
||||||
|
|
|
@ -109,19 +109,18 @@ def __init__(self, stmt="pass", setup="pass", timer=default_timer,
|
||||||
if isinstance(setup, str):
|
if isinstance(setup, str):
|
||||||
# Check that the code can be compiled outside a function
|
# Check that the code can be compiled outside a function
|
||||||
compile(setup, dummy_src_name, "exec")
|
compile(setup, dummy_src_name, "exec")
|
||||||
|
stmtprefix = setup + '\n'
|
||||||
setup = reindent(setup, 4)
|
setup = reindent(setup, 4)
|
||||||
elif callable(setup):
|
elif callable(setup):
|
||||||
local_ns['_setup'] = setup
|
local_ns['_setup'] = setup
|
||||||
init += ', _setup=_setup'
|
init += ', _setup=_setup'
|
||||||
|
stmtprefix = ''
|
||||||
setup = '_setup()'
|
setup = '_setup()'
|
||||||
else:
|
else:
|
||||||
raise ValueError("setup is neither a string nor callable")
|
raise ValueError("setup is neither a string nor callable")
|
||||||
if isinstance(stmt, str):
|
if isinstance(stmt, str):
|
||||||
# Check that the code can be compiled outside a function
|
# Check that the code can be compiled outside a function
|
||||||
if isinstance(setup, str):
|
compile(stmtprefix + stmt, dummy_src_name, "exec")
|
||||||
compile(setup + '\n' + stmt, dummy_src_name, "exec")
|
|
||||||
else:
|
|
||||||
compile(stmt, dummy_src_name, "exec")
|
|
||||||
stmt = reindent(stmt, 8)
|
stmt = reindent(stmt, 8)
|
||||||
elif callable(stmt):
|
elif callable(stmt):
|
||||||
local_ns['_stmt'] = stmt
|
local_ns['_stmt'] = stmt
|
||||||
|
|
|
@ -17,6 +17,8 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #24631: Fixed regression in the timeit modulu with multyline setup.
|
||||||
|
|
||||||
- Issue #18622: unittest.mock.mock_open().reset_mock would recurse infinitely.
|
- Issue #18622: unittest.mock.mock_open().reset_mock would recurse infinitely.
|
||||||
Patch from Nicola Palumbo and Laurent De Buyst.
|
Patch from Nicola Palumbo and Laurent De Buyst.
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue