Skip to content

Commit 6543912

Browse files
bpo-32912: Replace a DeprecationWarning with a SyntaxWarning (GH-9652)
for invalid escape sequences in string and bytes literals.
1 parent 2091448 commit 6543912

File tree

6 files changed

+23
-12
lines changed

6 files changed

+23
-12
lines changed

Doc/reference/lexical_analysis.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,8 +559,11 @@ escape sequences only recognized in string literals fall into the category of
559559
unrecognized escapes for bytes literals.
560560

561561
.. versionchanged:: 3.6
562-
Unrecognized escape sequences produce a DeprecationWarning. In
563-
some future version of Python they will be a SyntaxError.
562+
Unrecognized escape sequences produce a :exc:`DeprecationWarning`.
563+
564+
.. versionchanged:: 3.8
565+
Unrecognized escape sequences produce a :exc:`SyntaxWarning`. In
566+
some future version of Python they will be a :exc:`SyntaxError`.
564567

565568
Even in a raw literal, quotes can be escaped with a backslash, but the
566569
backslash remains in the result; for example, ``r"\""`` is a valid string

Doc/whatsnew/3.8.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ Other Language Changes
107107
and :keyword:`return` statements.
108108
(Contributed by David Cuthbert and Jordan Chapman in :issue:`32117`.)
109109

110+
* A backslash-character pair that is not a valid escape sequence generates
111+
a :exc:`DeprecationWarning` since Python 3.6. In Python 3.8 it generates
112+
a :exc:`SyntaxWarning` instead.
113+
(Contributed by Serhiy Storchaka in :issue:`32912`.)
114+
115+
110116
New Modules
111117
===========
112118

Lib/test/test_fstring.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ def test_backslashes_in_string_part(self):
626626
self.assertEqual(f'2\x203', '2 3')
627627
self.assertEqual(f'\x203', ' 3')
628628

629-
with self.assertWarns(DeprecationWarning): # invalid escape sequence
629+
with self.assertWarns(SyntaxWarning): # invalid escape sequence
630630
value = eval(r"f'\{6*7}'")
631631
self.assertEqual(value, '\\42')
632632
self.assertEqual(f'\\{6*7}', '\\42')

Lib/test/test_string_literals.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,18 @@ def test_eval_str_invalid_escape(self):
109109
for b in range(1, 128):
110110
if b in b"""\n\r"'01234567NU\\abfnrtuvx""":
111111
continue
112-
with self.assertWarns(DeprecationWarning):
112+
with self.assertWarns(SyntaxWarning):
113113
self.assertEqual(eval(r"'\%c'" % b), '\\' + chr(b))
114114

115115
with warnings.catch_warnings(record=True) as w:
116-
warnings.simplefilter('always', category=DeprecationWarning)
116+
warnings.simplefilter('always', category=SyntaxWarning)
117117
eval("'''\n\\z'''")
118118
self.assertEqual(len(w), 1)
119119
self.assertEqual(w[0].filename, '<string>')
120120
self.assertEqual(w[0].lineno, 2)
121121

122122
with warnings.catch_warnings(record=True) as w:
123-
warnings.simplefilter('error', category=DeprecationWarning)
123+
warnings.simplefilter('error', category=SyntaxWarning)
124124
with self.assertRaises(SyntaxError) as cm:
125125
eval("'''\n\\z'''")
126126
exc = cm.exception
@@ -158,18 +158,18 @@ def test_eval_bytes_invalid_escape(self):
158158
for b in range(1, 128):
159159
if b in b"""\n\r"'01234567\\abfnrtvx""":
160160
continue
161-
with self.assertWarns(DeprecationWarning):
161+
with self.assertWarns(SyntaxWarning):
162162
self.assertEqual(eval(r"b'\%c'" % b), b'\\' + bytes([b]))
163163

164164
with warnings.catch_warnings(record=True) as w:
165-
warnings.simplefilter('always', category=DeprecationWarning)
165+
warnings.simplefilter('always', category=SyntaxWarning)
166166
eval("b'''\n\\z'''")
167167
self.assertEqual(len(w), 1)
168168
self.assertEqual(w[0].filename, '<string>')
169169
self.assertEqual(w[0].lineno, 2)
170170

171171
with warnings.catch_warnings(record=True) as w:
172-
warnings.simplefilter('error', category=DeprecationWarning)
172+
warnings.simplefilter('error', category=SyntaxWarning)
173173
with self.assertRaises(SyntaxError) as cm:
174174
eval("b'''\n\\z'''")
175175
exc = cm.exception
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
A :exc:`SyntaxWarning` is now emitted instead of a :exc:`DeprecationWarning`
2+
for invalid escape sequences in string and bytes literals.

Python/ast.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4128,14 +4128,14 @@ warn_invalid_escape_sequence(struct compiling *c, const node *n,
41284128
if (msg == NULL) {
41294129
return -1;
41304130
}
4131-
if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg,
4131+
if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg,
41324132
c->c_filename, LINENO(n),
41334133
NULL, NULL) < 0)
41344134
{
4135-
if (PyErr_ExceptionMatches(PyExc_DeprecationWarning)) {
4135+
if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
41364136
const char *s;
41374137

4138-
/* Replace the DeprecationWarning exception with a SyntaxError
4138+
/* Replace the SyntaxWarning exception with a SyntaxError
41394139
to get a more accurate error report */
41404140
PyErr_Clear();
41414141

0 commit comments

Comments
 (0)