diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2016-02-08 16:57:02 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2016-02-08 16:57:02 (GMT) |
commit | 51d8c526d5634fa1f0e4976fd357c5423a792082 (patch) | |
tree | 5aaa710f4f336f949691c88f70a221169ccc42f8 /Lib/test | |
parent | f089196beb0ad8fb262be42eddd92af3f0fe81c1 (diff) | |
download | cpython-51d8c526d5634fa1f0e4976fd357c5423a792082.zip cpython-51d8c526d5634fa1f0e4976fd357c5423a792082.tar.gz cpython-51d8c526d5634fa1f0e4976fd357c5423a792082.tar.bz2 |
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.
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_inspect.py | 2 | ||||
-rw-r--r-- | Lib/test/test_peepholer.py | 20 | ||||
-rw-r--r-- | Lib/test/test_support.py | 3 | ||||
-rw-r--r-- | Lib/test/test_sys_settrace.py | 4 | ||||
-rw-r--r-- | Lib/test/test_traceback.py | 14 |
5 files changed, 23 insertions, 20 deletions
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 422a3d6..98d596e 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -401,7 +401,7 @@ class TestRetrievingSourceCode(GetSourceBase): self.assertEqual(normcase(inspect.getsourcefile(mod.spam)), modfile) self.assertEqual(normcase(inspect.getsourcefile(git.abuse)), modfile) 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) linecache.cache[co.co_filename] = (1, None, "None", co.co_filename) try: diff --git a/Lib/test/test_peepholer.py b/Lib/test/test_peepholer.py index 41e5091..b033640 100644 --- a/Lib/test/test_peepholer.py +++ b/Lib/test/test_peepholer.py @@ -1,9 +1,8 @@ import dis import re import sys -from io import StringIO +import textwrap import unittest -from math import copysign from test.bytecode_helper import BytecodeTestCase @@ -30,22 +29,25 @@ class TestTranforms(BytecodeTestCase): def test_global_as_constant(self): # LOAD_GLOBAL None/True/False --> LOAD_CONST None/True/False - def f(x): - None - None + def f(): + x = None + x = None return x - def g(x): - True + def g(): + x = True return x - def h(x): - False + def h(): + x = False return x + for func, elem in ((f, None), (g, True), (h, False)): self.assertNotInBytecode(func, 'LOAD_GLOBAL') self.assertInBytecode(func, 'LOAD_CONST', elem) + def f(): 'Adding a docstring made this test fail in Py2.5.0' return None + self.assertNotInBytecode(f, 'LOAD_GLOBAL') self.assertInBytecode(f, 'LOAD_CONST', None) diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py index f86ea91..269d9bf 100644 --- a/Lib/test/test_support.py +++ b/Lib/test/test_support.py @@ -230,7 +230,8 @@ class TestSupport(unittest.TestCase): def test_check_syntax_error(self): 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): import importlib diff --git a/Lib/test/test_sys_settrace.py b/Lib/test/test_sys_settrace.py index ae8f845..bb83623 100644 --- a/Lib/test/test_sys_settrace.py +++ b/Lib/test/test_sys_settrace.py @@ -338,8 +338,8 @@ class TraceTestCase(unittest.TestCase): def test_14_onliner_if(self): def onliners(): - if True: False - else: True + if True: x=False + else: x=True return 0 self.run_and_compare( onliners, diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index b7695d6..2f739ba 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -129,12 +129,12 @@ class SyntaxTracebackCases(unittest.TestCase): def do_test(firstlines, message, charset, lineno): # Raise the message in a subprocess, and catch the output try: - output = open(TESTFN, "w", encoding=charset) - output.write("""{0}if 1: - import traceback; - raise RuntimeError('{1}') - """.format(firstlines, message)) - output.close() + with open(TESTFN, "w", encoding=charset) as output: + output.write("""{0}if 1: + import traceback; + raise RuntimeError('{1}') + """.format(firstlines, message)) + process = subprocess.Popen([sys.executable, TESTFN], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) stdout, stderr = process.communicate() @@ -176,7 +176,7 @@ class SyntaxTracebackCases(unittest.TestCase): do_test(" \t\f\n# coding: {0}\n".format(charset), text, charset, 5) # 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): # Issue #22599: Ensure that it is possible to use the traceback module |