Commit 51d8c526 authored by Victor Stinner's avatar Victor Stinner

Replace noop constant statement with expression

* Constant statements will be ignored and the compiler will emit a
  SyntaxWarning.
* Replace constant statement (ex: "1") with an expression statement
  (ex: "x=1").
* test_traceback: use context manager on the file.

Issue #26204.
parent f089196b
...@@ -401,7 +401,7 @@ class TestRetrievingSourceCode(GetSourceBase): ...@@ -401,7 +401,7 @@ class TestRetrievingSourceCode(GetSourceBase):
self.assertEqual(normcase(inspect.getsourcefile(mod.spam)), modfile) self.assertEqual(normcase(inspect.getsourcefile(mod.spam)), modfile)
self.assertEqual(normcase(inspect.getsourcefile(git.abuse)), modfile) self.assertEqual(normcase(inspect.getsourcefile(git.abuse)), modfile)
fn = "_non_existing_filename_used_for_sourcefile_test.py" fn = "_non_existing_filename_used_for_sourcefile_test.py"
co = compile("None", fn, "exec") co = compile("x=1", fn, "exec")
self.assertEqual(inspect.getsourcefile(co), None) self.assertEqual(inspect.getsourcefile(co), None)
linecache.cache[co.co_filename] = (1, None, "None", co.co_filename) linecache.cache[co.co_filename] = (1, None, "None", co.co_filename)
try: try:
......
import dis import dis
import re import re
import sys import sys
from io import StringIO import textwrap
import unittest import unittest
from math import copysign
from test.bytecode_helper import BytecodeTestCase from test.bytecode_helper import BytecodeTestCase
...@@ -30,22 +29,25 @@ class TestTranforms(BytecodeTestCase): ...@@ -30,22 +29,25 @@ class TestTranforms(BytecodeTestCase):
def test_global_as_constant(self): def test_global_as_constant(self):
# LOAD_GLOBAL None/True/False --> LOAD_CONST None/True/False # LOAD_GLOBAL None/True/False --> LOAD_CONST None/True/False
def f(x): def f():
None x = None
None x = None
return x return x
def g(x): def g():
True x = True
return x return x
def h(x): def h():
False x = False
return x return x
for func, elem in ((f, None), (g, True), (h, False)): for func, elem in ((f, None), (g, True), (h, False)):
self.assertNotInBytecode(func, 'LOAD_GLOBAL') self.assertNotInBytecode(func, 'LOAD_GLOBAL')
self.assertInBytecode(func, 'LOAD_CONST', elem) self.assertInBytecode(func, 'LOAD_CONST', elem)
def f(): def f():
'Adding a docstring made this test fail in Py2.5.0' 'Adding a docstring made this test fail in Py2.5.0'
return None return None
self.assertNotInBytecode(f, 'LOAD_GLOBAL') self.assertNotInBytecode(f, 'LOAD_GLOBAL')
self.assertInBytecode(f, 'LOAD_CONST', None) self.assertInBytecode(f, 'LOAD_CONST', None)
......
...@@ -230,7 +230,8 @@ class TestSupport(unittest.TestCase): ...@@ -230,7 +230,8 @@ class TestSupport(unittest.TestCase):
def test_check_syntax_error(self): def test_check_syntax_error(self):
support.check_syntax_error(self, "def class") support.check_syntax_error(self, "def class")
self.assertRaises(AssertionError, support.check_syntax_error, self, "1") with self.assertRaises(AssertionError):
support.check_syntax_error(self, "x=1")
def test_CleanImport(self): def test_CleanImport(self):
import importlib import importlib
......
...@@ -338,8 +338,8 @@ class TraceTestCase(unittest.TestCase): ...@@ -338,8 +338,8 @@ class TraceTestCase(unittest.TestCase):
def test_14_onliner_if(self): def test_14_onliner_if(self):
def onliners(): def onliners():
if True: False if True: x=False
else: True else: x=True
return 0 return 0
self.run_and_compare( self.run_and_compare(
onliners, onliners,
......
...@@ -129,12 +129,12 @@ class SyntaxTracebackCases(unittest.TestCase): ...@@ -129,12 +129,12 @@ class SyntaxTracebackCases(unittest.TestCase):
def do_test(firstlines, message, charset, lineno): def do_test(firstlines, message, charset, lineno):
# Raise the message in a subprocess, and catch the output # Raise the message in a subprocess, and catch the output
try: try:
output = open(TESTFN, "w", encoding=charset) with open(TESTFN, "w", encoding=charset) as output:
output.write("""{0}if 1: output.write("""{0}if 1:
import traceback; import traceback;
raise RuntimeError('{1}') raise RuntimeError('{1}')
""".format(firstlines, message)) """.format(firstlines, message))
output.close()
process = subprocess.Popen([sys.executable, TESTFN], process = subprocess.Popen([sys.executable, TESTFN],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT) stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout, stderr = process.communicate() stdout, stderr = process.communicate()
...@@ -176,7 +176,7 @@ class SyntaxTracebackCases(unittest.TestCase): ...@@ -176,7 +176,7 @@ class SyntaxTracebackCases(unittest.TestCase):
do_test(" \t\f\n# coding: {0}\n".format(charset), do_test(" \t\f\n# coding: {0}\n".format(charset),
text, charset, 5) text, charset, 5)
# Issue #18960: coding spec should has no effect # Issue #18960: coding spec should has no effect
do_test("0\n# coding: GBK\n", "h\xe9 ho", 'utf-8', 5) do_test("x=0\n# coding: GBK\n", "h\xe9 ho", 'utf-8', 5)
def test_print_traceback_at_exit(self): def test_print_traceback_at_exit(self):
# Issue #22599: Ensure that it is possible to use the traceback module # Issue #22599: Ensure that it is possible to use the traceback module
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment