[issue37500] 3.8.0b2 no longer optimizes away "if 0:" ?

Tim Peters report at bugs.python.org
Fri Jul 5 20:21:07 EDT 2019


Tim Peters <tim at python.org> added the comment:

> we could say that it does not matter if
>
> def f():
>   if 0:
>     yield
>
> should be or not a generator

Slippery slope arguments play better if they're made _before_ a decade has passed after the slope was fully greased.

There's nothing accidental about how `yield` behaves here.  I wrote the original generator PEP, and very deliberately added these to its doctests (in test_generators.py):

"""
>>> def f():
...    if 0:
...        yield
>>> type(f())
<class 'generator'>


>>> def f():
...     if 0:
...         yield 1
>>> type(f())
<class 'generator'>

>>> def f():
...    if "":
...        yield None
>>> type(f())
<class 'generator'>
"""

Any alternate implementation that decided that whether "it's a generator" depended on optimizations would likely fail at least one of those tests.  It was intended to be solely a compile-time decision, based purely on syntactic analysis.

So I've seen no reason to believe - or expect - that the damage here goes - or will ever go - deeper than that some dead code isn't raising compile-time errors in some rare cases (statements allowed only at function level being used in dead code outside function level).

Which should be fixed, if possible.  But, as damage goes - sorry! - it just seems minimal to me.

----------

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


More information about the Python-bugs-list mailing list