Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 15a3095

Browse files
committed
compiler: don't emit SyntaxWarning on const stmt
Issue #26204: the compiler doesn't emit SyntaxWarning warnings anymore when constant statements are ignored.
1 parent 896632e commit 15a3095

5 files changed

Lines changed: 27 additions & 75 deletions

File tree

Lib/test/test_ast.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import os
44
import sys
55
import unittest
6-
import warnings
76
import weakref
87

98
from test import support
@@ -240,10 +239,8 @@ def test_snippets(self):
240239
ast_tree = compile(i, "?", kind, ast.PyCF_ONLY_AST)
241240
self.assertEqual(to_tuple(ast_tree), o)
242241
self._assertTrueorder(ast_tree, (0, 0))
243-
with warnings.catch_warnings():
244-
warnings.filterwarnings('ignore', category=SyntaxWarning)
245-
with self.subTest(action="compiling", input=i, kind=kind):
246-
compile(ast_tree, "?", kind)
242+
with self.subTest(action="compiling", input=i, kind=kind):
243+
compile(ast_tree, "?", kind)
247244

248245
def test_slice(self):
249246
slc = ast.parse("x[::]").body[0].value.slice

Lib/test/test_code.py

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,24 @@
6666
flags: 67
6767
consts: ('None',)
6868
69+
>>> def optimize_away():
70+
... 'doc string'
71+
... 'not a docstring'
72+
... 53
73+
... 0x53
74+
75+
>>> dump(optimize_away.__code__)
76+
name: optimize_away
77+
argcount: 0
78+
kwonlyargcount: 0
79+
names: ()
80+
varnames: ()
81+
cellvars: ()
82+
freevars: ()
83+
nlocals: 0
84+
flags: 67
85+
consts: ("'doc string'", 'None')
86+
6987
>>> def keywordonly_args(a,b,*,k1):
7088
... return a,b,k1
7189
...
@@ -84,10 +102,8 @@
84102
85103
"""
86104

87-
import textwrap
88105
import unittest
89106
import weakref
90-
import warnings
91107
from test.support import run_doctest, run_unittest, cpython_only
92108

93109

@@ -118,44 +134,6 @@ def test_newempty(self):
118134
self.assertEqual(co.co_name, "funcname")
119135
self.assertEqual(co.co_firstlineno, 15)
120136

121-
def dump(self, co):
122-
dump = {}
123-
for attr in ["name", "argcount", "kwonlyargcount", "names", "varnames",
124-
"cellvars", "freevars", "nlocals", "flags"]:
125-
dump[attr] = getattr(co, "co_" + attr)
126-
dump['consts'] = tuple(consts(co.co_consts))
127-
return dump
128-
129-
def test_optimize_away(self):
130-
ns = {}
131-
with warnings.catch_warnings():
132-
warnings.filterwarnings('ignore', category=SyntaxWarning)
133-
exec(textwrap.dedent('''
134-
def optimize_away():
135-
'doc string'
136-
'not a docstring'
137-
53
138-
0x53
139-
b'bytes'
140-
1.0
141-
True
142-
False
143-
None
144-
...
145-
'''), ns)
146-
147-
self.assertEqual(self.dump(ns['optimize_away'].__code__),
148-
{'name': 'optimize_away',
149-
'argcount': 0,
150-
'kwonlyargcount': 0,
151-
'names': (),
152-
'varnames': (),
153-
'cellvars': (),
154-
'freevars': (),
155-
'nlocals': 0,
156-
'flags': 67,
157-
'consts': ("'doc string'", 'None')})
158-
159137

160138
class CodeWeakRefTest(unittest.TestCase):
161139

Lib/test/test_grammar.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import sys
88
# testing import *
99
from sys import *
10-
from test import support
1110

1211

1312
class TokenTests(unittest.TestCase):
@@ -425,11 +424,8 @@ def foo():
425424
# Tested below
426425

427426
def test_expr_stmt(self):
428-
msg = 'ignore constant statement'
429-
with support.check_warnings((msg, SyntaxWarning)):
430-
exec("1")
431-
432427
# (exprlist '=')* exprlist
428+
1
433429
1, 2, 3
434430
x = 1
435431
x = 1, 2, 3

Misc/NEWS

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ Release date: tba
1010
Core and Builtins
1111
-----------------
1212

13-
- Issue #26204: The compiler now ignores constant statements (ex: "def f(): 1")
14-
and emit a SyntaxWarning warning. The warning is not emitted for string and
15-
ellipsis (...) statements.
13+
- Issue #26204: The compiler now ignores all constant statements: bytes, str,
14+
int, float, complex, name constants (None, False, True), Ellipsis
15+
and ast.Constant; not only str and int. For example, ``1.0`` is now ignored
16+
in ``def f(): 1.0``.
1617

1718
- Issue #4806: Avoid masking the original TypeError exception when using star
1819
(*) unpacking in function calls. Based on patch by Hagen Fürstenau and

Python/compile.c

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2619,33 +2619,13 @@ compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
26192619
switch (value->kind)
26202620
{
26212621
case Str_kind:
2622+
case Num_kind:
26222623
case Ellipsis_kind:
2623-
/* Issue #26204: ignore string statement, but don't emit a
2624-
* SyntaxWarning. Triple quoted strings is a common syntax for
2625-
* multiline comments.
2626-
*
2627-
* Don't emit warning on "def f(): ..." neither. It's a legit syntax
2628-
* for abstract function. */
2629-
return 1;
2630-
26312624
case Bytes_kind:
2632-
case Num_kind:
26332625
case NameConstant_kind:
26342626
case Constant_kind:
2635-
{
2636-
PyObject *msg = PyUnicode_FromString("ignore constant statement");
2637-
if (msg == NULL)
2638-
return 0;
2639-
if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning,
2640-
msg,
2641-
c->c_filename, c->u->u_lineno,
2642-
NULL, NULL) == -1) {
2643-
Py_DECREF(msg);
2644-
return 0;
2645-
}
2646-
Py_DECREF(msg);
2627+
/* ignore constant statement */
26472628
return 1;
2648-
}
26492629

26502630
default:
26512631
break;

0 commit comments

Comments
 (0)