]> granicus.if.org Git - python/commitdiff
compiler: don't emit SyntaxWarning on const stmt
authorVictor Stinner <victor.stinner@gmail.com>
Mon, 8 Feb 2016 21:45:06 +0000 (22:45 +0100)
committerVictor Stinner <victor.stinner@gmail.com>
Mon, 8 Feb 2016 21:45:06 +0000 (22:45 +0100)
Issue #26204: the compiler doesn't emit SyntaxWarning warnings anymore when
constant statements are ignored.

Lib/test/test_ast.py
Lib/test/test_code.py
Lib/test/test_grammar.py
Misc/NEWS
Python/compile.c

index dbcd9f74ffa5ead6657555f934e8b3bb0880eeb0..a025c20006c6265188888b481bacb27eac2e1ced 100644 (file)
@@ -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
index 2cf088ce3ca93bcd0cdcd68f937c32b1e3cf32e8..21b12a56e4a8015312002d6f858aff55e9a163e8 100644 (file)
@@ -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):
 
index 2d6f5edf05c2b8cbb70e39405e56db0570da7fb2..8f8d71ce85e3e4a6e5f6121b4a8821a7e9cc1669 100644 (file)
@@ -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
index e1875c0e9919131264f7b23d4303b73e3a8b888e..d60aeb640e0ae2883fc462e2581dec200a2ca852 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,9 +10,10 @@ Release date: tba
 Core and Builtins
 -----------------
 
-- Issue #26204: The compiler now ignores constant statements (ex: "def f(): 1")
-  and emit a SyntaxWarning warning. The warning is not emitted for string and
-  ellipsis (...) statements.
+- Issue #26204: The compiler now ignores all constant statements: bytes, str,
+  int, float, complex, name constants (None, False, True), Ellipsis
+  and ast.Constant; not only str and int. For example, ``1.0`` is now ignored
+  in ``def f(): 1.0``.
 
 - Issue #4806: Avoid masking the original TypeError exception when using star
   (*) unpacking in function calls.  Based on patch by Hagen Fürstenau and
index 84b79a2fffd5e8b76653ea547ff47c1f9f0075b2..ca1d8656c731ecac3c415ee3ef90d922bf148773 100644 (file)
@@ -2619,33 +2619,13 @@ compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
     switch (value->kind)
     {
     case Str_kind:
+    case Num_kind:
     case Ellipsis_kind:
-        /* Issue #26204: ignore string statement, but don't emit a
-         * SyntaxWarning. Triple quoted strings is a common syntax for
-         * multiline comments.
-         *
-         * Don't emit warning on "def f(): ..." neither. It's a legit syntax
-         * for abstract function. */
-        return 1;
-
     case Bytes_kind:
-    case Num_kind:
     case NameConstant_kind:
     case Constant_kind:
-    {
-        PyObject *msg = PyUnicode_FromString("ignore constant statement");
-        if (msg == NULL)
-            return 0;
-        if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning,
-                                     msg,
-                                     c->c_filename, c->u->u_lineno,
-                                     NULL, NULL) == -1) {
-            Py_DECREF(msg);
-            return 0;
-        }
-        Py_DECREF(msg);
+        /* ignore constant statement */
         return 1;
-    }
 
     default:
         break;