mirror of
https://github.com/python/cpython.git
synced 2025-11-02 03:01:58 +00:00
compiler: don't emit SyntaxWarning on const stmt
Issue #26204: the compiler doesn't emit SyntaxWarning warnings anymore when constant statements are ignored.
This commit is contained in:
parent
896632ea6b
commit
15a3095d64
5 changed files with 28 additions and 76 deletions
|
|
@ -3,7 +3,6 @@ import dis
|
|||
import os
|
||||
import sys
|
||||
import unittest
|
||||
import warnings
|
||||
import weakref
|
||||
|
||||
from test import support
|
||||
|
|
@ -240,10 +239,8 @@ class AST_Tests(unittest.TestCase):
|
|||
ast_tree = compile(i, "?", kind, ast.PyCF_ONLY_AST)
|
||||
self.assertEqual(to_tuple(ast_tree), o)
|
||||
self._assertTrueorder(ast_tree, (0, 0))
|
||||
with warnings.catch_warnings():
|
||||
warnings.filterwarnings('ignore', category=SyntaxWarning)
|
||||
with self.subTest(action="compiling", input=i, kind=kind):
|
||||
compile(ast_tree, "?", kind)
|
||||
with self.subTest(action="compiling", input=i, kind=kind):
|
||||
compile(ast_tree, "?", kind)
|
||||
|
||||
def test_slice(self):
|
||||
slc = ast.parse("x[::]").body[0].value.slice
|
||||
|
|
|
|||
|
|
@ -66,6 +66,24 @@ nlocals: 1
|
|||
flags: 67
|
||||
consts: ('None',)
|
||||
|
||||
>>> def optimize_away():
|
||||
... 'doc string'
|
||||
... 'not a docstring'
|
||||
... 53
|
||||
... 0x53
|
||||
|
||||
>>> dump(optimize_away.__code__)
|
||||
name: optimize_away
|
||||
argcount: 0
|
||||
kwonlyargcount: 0
|
||||
names: ()
|
||||
varnames: ()
|
||||
cellvars: ()
|
||||
freevars: ()
|
||||
nlocals: 0
|
||||
flags: 67
|
||||
consts: ("'doc string'", 'None')
|
||||
|
||||
>>> def keywordonly_args(a,b,*,k1):
|
||||
... return a,b,k1
|
||||
...
|
||||
|
|
@ -84,10 +102,8 @@ consts: ('None',)
|
|||
|
||||
"""
|
||||
|
||||
import textwrap
|
||||
import unittest
|
||||
import weakref
|
||||
import warnings
|
||||
from test.support import run_doctest, run_unittest, cpython_only
|
||||
|
||||
|
||||
|
|
@ -118,44 +134,6 @@ class CodeTest(unittest.TestCase):
|
|||
self.assertEqual(co.co_name, "funcname")
|
||||
self.assertEqual(co.co_firstlineno, 15)
|
||||
|
||||
def dump(self, co):
|
||||
dump = {}
|
||||
for attr in ["name", "argcount", "kwonlyargcount", "names", "varnames",
|
||||
"cellvars", "freevars", "nlocals", "flags"]:
|
||||
dump[attr] = getattr(co, "co_" + attr)
|
||||
dump['consts'] = tuple(consts(co.co_consts))
|
||||
return dump
|
||||
|
||||
def test_optimize_away(self):
|
||||
ns = {}
|
||||
with warnings.catch_warnings():
|
||||
warnings.filterwarnings('ignore', category=SyntaxWarning)
|
||||
exec(textwrap.dedent('''
|
||||
def optimize_away():
|
||||
'doc string'
|
||||
'not a docstring'
|
||||
53
|
||||
0x53
|
||||
b'bytes'
|
||||
1.0
|
||||
True
|
||||
False
|
||||
None
|
||||
...
|
||||
'''), ns)
|
||||
|
||||
self.assertEqual(self.dump(ns['optimize_away'].__code__),
|
||||
{'name': 'optimize_away',
|
||||
'argcount': 0,
|
||||
'kwonlyargcount': 0,
|
||||
'names': (),
|
||||
'varnames': (),
|
||||
'cellvars': (),
|
||||
'freevars': (),
|
||||
'nlocals': 0,
|
||||
'flags': 67,
|
||||
'consts': ("'doc string'", 'None')})
|
||||
|
||||
|
||||
class CodeWeakRefTest(unittest.TestCase):
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ import unittest
|
|||
import sys
|
||||
# testing import *
|
||||
from sys import *
|
||||
from test import support
|
||||
|
||||
|
||||
class TokenTests(unittest.TestCase):
|
||||
|
|
@ -425,11 +424,8 @@ class GrammarTests(unittest.TestCase):
|
|||
# Tested below
|
||||
|
||||
def test_expr_stmt(self):
|
||||
msg = 'ignore constant statement'
|
||||
with support.check_warnings((msg, SyntaxWarning)):
|
||||
exec("1")
|
||||
|
||||
# (exprlist '=')* exprlist
|
||||
1
|
||||
1, 2, 3
|
||||
x = 1
|
||||
x = 1, 2, 3
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue