[Python-checkins] bpo-32912: Replace a DeprecationWarning with a SyntaxWarning (GH-9652)

Serhiy Storchaka webhook-mailer at python.org
Fri Oct 19 10:42:13 EDT 2018


https://github.com/python/cpython/commit/6543912c90ffa579dc4c01e811f9609cf92197d3
commit: 6543912c90ffa579dc4c01e811f9609cf92197d3
branch: master
author: Serhiy Storchaka <storchaka at gmail.com>
committer: GitHub <noreply at github.com>
date: 2018-10-19T17:42:06+03:00
summary:

bpo-32912: Replace a DeprecationWarning with a SyntaxWarning (GH-9652)

for invalid escape sequences in string and bytes literals.

files:
A Misc/NEWS.d/next/Core and Builtins/2018-10-01-10-41-53.bpo-32912.JeIOdM.rst
M Doc/reference/lexical_analysis.rst
M Doc/whatsnew/3.8.rst
M Lib/test/test_fstring.py
M Lib/test/test_string_literals.py
M Python/ast.c

diff --git a/Doc/reference/lexical_analysis.rst b/Doc/reference/lexical_analysis.rst
index 84e8c783838b..e1d88eff959f 100644
--- a/Doc/reference/lexical_analysis.rst
+++ b/Doc/reference/lexical_analysis.rst
@@ -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
diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst
index bd3283caadb8..e522addf391f 100644
--- a/Doc/whatsnew/3.8.rst
+++ b/Doc/whatsnew/3.8.rst
@@ -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
 ===========
 
diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py
index fec72e008e5b..09b5ae1fdaee 100644
--- a/Lib/test/test_fstring.py
+++ b/Lib/test/test_fstring.py
@@ -626,7 +626,7 @@ def test_backslashes_in_string_part(self):
         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')
diff --git a/Lib/test/test_string_literals.py b/Lib/test/test_string_literals.py
index aba4fc466762..55bcde4c43fb 100644
--- a/Lib/test/test_string_literals.py
+++ b/Lib/test/test_string_literals.py
@@ -109,18 +109,18 @@ def test_eval_str_invalid_escape(self):
         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 @@ def test_eval_bytes_invalid_escape(self):
         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
index 000000000000..9cb35998a2b5
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2018-10-01-10-41-53.bpo-32912.JeIOdM.rst	
@@ -0,0 +1,2 @@
+A :exc:`SyntaxWarning` is now emitted instead of a :exc:`DeprecationWarning`
+for invalid escape sequences in string and bytes literals.
diff --git a/Python/ast.c b/Python/ast.c
index 587f838f8d8e..184e33b4b506 100644
--- a/Python/ast.c
+++ b/Python/ast.c
@@ -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();
 



More information about the Python-checkins mailing list