]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-32912: Replace a DeprecationWarning with a SyntaxWarning (GH-9652)
authorSerhiy Storchaka <storchaka@gmail.com>
Fri, 19 Oct 2018 14:42:06 +0000 (17:42 +0300)
committerGitHub <noreply@github.com>
Fri, 19 Oct 2018 14:42:06 +0000 (17:42 +0300)
for invalid escape sequences in string and bytes literals.

Doc/reference/lexical_analysis.rst
Doc/whatsnew/3.8.rst
Lib/test/test_fstring.py
Lib/test/test_string_literals.py
Misc/NEWS.d/next/Core and Builtins/2018-10-01-10-41-53.bpo-32912.JeIOdM.rst [new file with mode: 0644]
Python/ast.c

index 84e8c783838b641fc790acf7621bef43fd5867a6..e1d88eff959f631c0c79040b7ddf38ca53e88368 100644 (file)
@@ -559,8 +559,11 @@ escape sequences only recognized in string literals fall into the category of
 unrecognized escapes for bytes literals.
 
    .. versionchanged:: 3.6
-      Unrecognized escape sequences produce a DeprecationWarning.  In
-      some future version of Python they will be a SyntaxError.
+      Unrecognized escape sequences produce a :exc:`DeprecationWarning`.
+
+   .. versionchanged:: 3.8
+      Unrecognized escape sequences produce a :exc:`SyntaxWarning`.  In
+      some future version of Python they will be a :exc:`SyntaxError`.
 
 Even in a raw literal, quotes can be escaped with a backslash, but the
 backslash remains in the result; for example, ``r"\""`` is a valid string
index bd3283caadb8b3e5e8662d426cab22e209955dc6..e522addf391fa56a7fe1d71e83950115a733d28b 100644 (file)
@@ -107,6 +107,12 @@ Other Language Changes
   and :keyword:`return` statements.
   (Contributed by David Cuthbert and Jordan Chapman in :issue:`32117`.)
 
+* A backslash-character pair that is not a valid escape sequence generates
+  a :exc:`DeprecationWarning` since Python 3.6. In Python 3.8 it generates
+  a :exc:`SyntaxWarning` instead.
+  (Contributed by Serhiy Storchaka in :issue:`32912`.)
+
+
 New Modules
 ===========
 
index fec72e008e5bc7671f4f4594ea9b1b3714f8b209..09b5ae1fdaee642e5e3b38d54004c03bc29972be 100644 (file)
@@ -626,7 +626,7 @@ non-important content
         self.assertEqual(f'2\x203', '2 3')
         self.assertEqual(f'\x203', ' 3')
 
-        with self.assertWarns(DeprecationWarning):  # invalid escape sequence
+        with self.assertWarns(SyntaxWarning):  # invalid escape sequence
             value = eval(r"f'\{6*7}'")
         self.assertEqual(value, '\\42')
         self.assertEqual(f'\\{6*7}', '\\42')
index aba4fc46676245409954fb216b98a6db49ada5b2..55bcde4c43fb068bb6732aaa36340877be78c700 100644 (file)
@@ -109,18 +109,18 @@ class TestLiterals(unittest.TestCase):
         for b in range(1, 128):
             if b in b"""\n\r"'01234567NU\\abfnrtuvx""":
                 continue
-            with self.assertWarns(DeprecationWarning):
+            with self.assertWarns(SyntaxWarning):
                 self.assertEqual(eval(r"'\%c'" % b), '\\' + chr(b))
 
         with warnings.catch_warnings(record=True) as w:
-            warnings.simplefilter('always', category=DeprecationWarning)
+            warnings.simplefilter('always', category=SyntaxWarning)
             eval("'''\n\\z'''")
         self.assertEqual(len(w), 1)
         self.assertEqual(w[0].filename, '<string>')
         self.assertEqual(w[0].lineno, 2)
 
         with warnings.catch_warnings(record=True) as w:
-            warnings.simplefilter('error', category=DeprecationWarning)
+            warnings.simplefilter('error', category=SyntaxWarning)
             with self.assertRaises(SyntaxError) as cm:
                 eval("'''\n\\z'''")
             exc = cm.exception
@@ -158,18 +158,18 @@ class TestLiterals(unittest.TestCase):
         for b in range(1, 128):
             if b in b"""\n\r"'01234567\\abfnrtvx""":
                 continue
-            with self.assertWarns(DeprecationWarning):
+            with self.assertWarns(SyntaxWarning):
                 self.assertEqual(eval(r"b'\%c'" % b), b'\\' + bytes([b]))
 
         with warnings.catch_warnings(record=True) as w:
-            warnings.simplefilter('always', category=DeprecationWarning)
+            warnings.simplefilter('always', category=SyntaxWarning)
             eval("b'''\n\\z'''")
         self.assertEqual(len(w), 1)
         self.assertEqual(w[0].filename, '<string>')
         self.assertEqual(w[0].lineno, 2)
 
         with warnings.catch_warnings(record=True) as w:
-            warnings.simplefilter('error', category=DeprecationWarning)
+            warnings.simplefilter('error', category=SyntaxWarning)
             with self.assertRaises(SyntaxError) as cm:
                 eval("b'''\n\\z'''")
             exc = cm.exception
diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-10-01-10-41-53.bpo-32912.JeIOdM.rst b/Misc/NEWS.d/next/Core and Builtins/2018-10-01-10-41-53.bpo-32912.JeIOdM.rst
new file mode 100644 (file)
index 0000000..9cb3599
--- /dev/null
@@ -0,0 +1,2 @@
+A :exc:`SyntaxWarning` is now emitted instead of a :exc:`DeprecationWarning`
+for invalid escape sequences in string and bytes literals.
index 587f838f8d8ee26822221127f5fba5fce44950a7..184e33b4b5064bdc03d16cbc6d9b006b9973888f 100644 (file)
@@ -4128,14 +4128,14 @@ warn_invalid_escape_sequence(struct compiling *c, const node *n,
     if (msg == NULL) {
         return -1;
     }
-    if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg,
+    if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg,
                                    c->c_filename, LINENO(n),
                                    NULL, NULL) < 0)
     {
-        if (PyErr_ExceptionMatches(PyExc_DeprecationWarning)) {
+        if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
             const char *s;
 
-            /* Replace the DeprecationWarning exception with a SyntaxError
+            /* Replace the SyntaxWarning exception with a SyntaxError
                to get a more accurate error report */
             PyErr_Clear();