[issue40670] supplying an empty string to timeit causes an IndentationError

Terry J. Reedy report at bugs.python.org
Sat May 23 02:02:33 EDT 2020


Terry J. Reedy <tjreedy at udel.edu> added the comment:

Let a wcs be a string consisting of only whitespace and comments.
compile(wcs, '', 'exec') treats wcs the same as 'pass'.
Hence, a file with only whitespace and comments is the same as 'pass'.
compile(wcs, '', 'single'), for whatever reason, raises
SyntaxError: unexpected EOF while parsing
To get around this, a wcs input into IDLE's Shell, compiles with 'single', is replaced with 'pass' in codeop._maybe_compile, line 76.  I presume the REPL does the same.

If one thinks of parameter stmt as a top-level statement (or statements), it is reasonable to expect '' to be the same as 'pass'.  If one knows that stmt will be embedded into a compound statement (whether 'while', 'for' or 'def' does not matter here) for repeated execution, then 'pass' is more obviously the minimal statement.

It would have been better if the parameter name were 'suite', as suites can never be only whitespace.  We cannot change this, but I suggest replacing

The constructor takes a statement to be timed, an additional statement used for setup, and a timer function. Both statements default to 'pass'; the timer function is platform-dependent (see the module doc string). stmt and setup may also contain multiple statements separated by ; or newlines, as long as they don’t contain multi-line string literals. 

with the shorter, clearer, and updated

The constructor takes suite of statement to be timed, an additional suite used for setup, and a timer function (default time.perf_counter). Both suites default to 'pass' and may not contain multi-line string literals.

Since 3.3, the default timer is platform-independent, at least from a user viewpoint, and not mentioned in timeit.__doc__.  Suites can always have multiple statments separated by ; and \n.  The only needed qualification is the default and the restriction.

----------
nosy: +terry.reedy

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40670>
_______________________________________


More information about the Python-bugs-list mailing list