mirror of
https://github.com/python/cpython.git
synced 2025-07-23 03:05:38 +00:00
compiler now ignores constant statements
The compile ignores constant statements and emit a SyntaxWarning warning. Don't emit the warning for string statement because triple quoted string is a common syntax for multiline comments. Don't emit the warning on ellipis neither: 'def f(): ...' is a legit syntax for abstract functions. Changes: * test_ast: ignore SyntaxWarning when compiling test statements. Modify test_load_const() to use assignment expressions rather than constant expression. * test_code: add more kinds of constant statements, ignore SyntaxWarning when testing that the compiler removes constant statements. * test_grammar: ignore SyntaxWarning on the statement "1"
This commit is contained in:
parent
51d8c526d5
commit
a2724095cd
5 changed files with 95 additions and 47 deletions
|
@ -66,24 +66,6 @@ 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
|
||||
...
|
||||
|
@ -102,8 +84,10 @@ consts: ('None',)
|
|||
|
||||
"""
|
||||
|
||||
import textwrap
|
||||
import unittest
|
||||
import weakref
|
||||
import warnings
|
||||
from test.support import run_doctest, run_unittest, cpython_only
|
||||
|
||||
|
||||
|
@ -134,6 +118,44 @@ 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):
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue