[Python-Dev] Issue #26204: compiler now emits a SyntaxWarning on constant statement

Victor Stinner victor.stinner at gmail.com
Mon Feb 8 12:44:19 EST 2016


Hi,

I changed the Python compiler to ignore any kind "constant
expressions", whereas it only ignored strings and integers before:
http://bugs.python.org/issue26204

The compiler now also emits a SyntaxWarning on such case. IMHO the
warning can help to detect bugs for developers who just learnt Python.

The warning is *not* emited for strings, since triple quoted strings
are a common syntax for multiline comments.

The warning is *not* emited neither for ellispis (...) since "f():
..." is a legit syntax for abstract function.

Are you ok with the new warning?


New behaviour:

haypo at smithers$ ./python
Python 3.6.0a0 (default:759a975e1230, Feb  8 2016, 18:21:23)
>>> def f():
...  False
...
<stdin>:2: SyntaxWarning: ignore constant statement

>>> import dis; dis.dis(f)
  2           0 LOAD_CONST               0 (None)
              3 RETURN_VALUE


Old behaviour:

haypo at smithers$ python3
Python 3.4.3 (default, Jun 29 2015, 12:16:01)
>>> def f():
...  False
...
>>> import dis; dis.dis(f)
  2           0 LOAD_CONST               1 (False)
              3 POP_TOP
              4 LOAD_CONST               0 (None)
              7 RETURN_VALUE



Before strings and numbers were already ignored. Example:

haypo at smithers$ python3
Python 3.4.3 (default, Jun 29 2015, 12:16:01)

>>> def f():
...  123
...
>>> import dis; dis.dis(f)
  2           0 LOAD_CONST               0 (None)
              3 RETURN_VALUE


Victor


More information about the Python-Dev mailing list