Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
c959b0cd
Commit
c959b0cd
authored
Jan 26, 2015
by
Serhiy Storchaka
Browse files
Options
Browse Files
Download
Plain Diff
Issue #18518: timeit now rejects statements which can't be compiled outside
a function or a loop (e.g. "return" or "break").
parents
23058673
2bef5857
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
21 additions
and
6 deletions
+21
-6
Doc/library/timeit.rst
Doc/library/timeit.rst
+0
-6
Lib/test/test_timeit.py
Lib/test/test_timeit.py
+12
-0
Lib/timeit.py
Lib/timeit.py
+6
-0
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Doc/library/timeit.rst
View file @
c959b0cd
...
...
@@ -69,12 +69,6 @@ The module defines three convenience functions and a public class:
.. versionchanged:: 3.5
The optional *globals* parameter was added.
.. note::
Because :meth:`.timeit` is executing *stmt*, placing a return statement
in *stmt* will prevent :meth:`.timeit` from returning execution time.
It will instead return the data specified by your return statement.
.. function:: repeat(stmt='pass', setup='pass', timer=<default timer>, repeat=3, number=1000000, globals=None)
...
...
Lib/test/test_timeit.py
View file @
c959b0cd
...
...
@@ -73,9 +73,21 @@ class TestTimeit(unittest.TestCase):
def
test_timer_invalid_stmt
(
self
):
self
.
assertRaises
(
ValueError
,
timeit
.
Timer
,
stmt
=
None
)
self
.
assertRaises
(
SyntaxError
,
timeit
.
Timer
,
stmt
=
'return'
)
self
.
assertRaises
(
SyntaxError
,
timeit
.
Timer
,
stmt
=
'yield'
)
self
.
assertRaises
(
SyntaxError
,
timeit
.
Timer
,
stmt
=
'yield from ()'
)
self
.
assertRaises
(
SyntaxError
,
timeit
.
Timer
,
stmt
=
'break'
)
self
.
assertRaises
(
SyntaxError
,
timeit
.
Timer
,
stmt
=
'continue'
)
self
.
assertRaises
(
SyntaxError
,
timeit
.
Timer
,
stmt
=
'from timeit import *'
)
def
test_timer_invalid_setup
(
self
):
self
.
assertRaises
(
ValueError
,
timeit
.
Timer
,
setup
=
None
)
self
.
assertRaises
(
SyntaxError
,
timeit
.
Timer
,
setup
=
'return'
)
self
.
assertRaises
(
SyntaxError
,
timeit
.
Timer
,
setup
=
'yield'
)
self
.
assertRaises
(
SyntaxError
,
timeit
.
Timer
,
setup
=
'yield from ()'
)
self
.
assertRaises
(
SyntaxError
,
timeit
.
Timer
,
setup
=
'break'
)
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()"
...
...
Lib/timeit.py
View file @
c959b0cd
...
...
@@ -115,6 +115,12 @@ class Timer:
local_ns
=
{}
global_ns
=
_globals
()
if
globals
is
None
else
globals
if
isinstance
(
stmt
,
str
):
# Check that the code can be compiled outside a function
if
isinstance
(
setup
,
str
):
compile
(
setup
,
dummy_src_name
,
"exec"
)
compile
(
setup
+
'
\
n
'
+
stmt
,
dummy_src_name
,
"exec"
)
else
:
compile
(
stmt
,
dummy_src_name
,
"exec"
)
stmt
=
reindent
(
stmt
,
8
)
if
isinstance
(
setup
,
str
):
setup
=
reindent
(
setup
,
4
)
...
...
Misc/NEWS
View file @
c959b0cd
...
...
@@ -218,6 +218,9 @@ Core and Builtins
Library
-------
-
Issue
#
18518
:
timeit
now
rejects
statements
which
can
't be compiled outside
a function or a loop (e.g. "return" or "break").
- Issue #23094: Fixed readline with frames in Python implementation of pickle.
- Issue #23268: Fixed bugs in the comparison of ipaddress classes.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment