Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b4be87a

Browse files
gpsheadmiss-islington
authored andcommitted
bpo-32912: Revert SyntaxWarning on invalid escape sequences. (GH-15195)
DeprecationWarning will continue to be emitted for invalid escape sequences in string and bytes literals just as it did in 3.7. SyntaxWarning may be emitted in the future. But per mailing list discussion, we don't yet know when because we haven't settled on how to do so in a non-disruptive manner. (Applies 4c5b6ba to the master branch). (This is #15142 for master/3.9) https://bugs.python.org/issue32912 Automerge-Triggered-By: @gpshead
1 parent 92c7e30 commit b4be87a

File tree

6 files changed

+43
-18
lines changed

6 files changed

+43
-18
lines changed

Doc/reference/lexical_analysis.rst

+3-5
Original file line numberDiff line numberDiff line change
@@ -594,11 +594,9 @@ escape sequences only recognized in string literals fall into the category of
594594
unrecognized escapes for bytes literals.
595595

596596
.. versionchanged:: 3.6
597-
Unrecognized escape sequences produce a :exc:`DeprecationWarning`.
598-
599-
.. versionchanged:: 3.8
600-
Unrecognized escape sequences produce a :exc:`SyntaxWarning`. In
601-
some future version of Python they will be a :exc:`SyntaxError`.
597+
Unrecognized escape sequences produce a :exc:`DeprecationWarning`. In
598+
a future Python version they will be a :exc:`SyntaxWarning` and
599+
eventually a :exc:`SyntaxError`.
602600

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

Doc/whatsnew/3.8.rst

-5
Original file line numberDiff line numberDiff line change
@@ -414,11 +414,6 @@ Other Language Changes
414414
and :keyword:`return` statements.
415415
(Contributed by David Cuthbert and Jordan Chapman in :issue:`32117`.)
416416

417-
* A backslash-character pair that is not a valid escape sequence generates
418-
a :exc:`DeprecationWarning` since Python 3.6. In Python 3.8 it generates
419-
a :exc:`SyntaxWarning` instead.
420-
(Contributed by Serhiy Storchaka in :issue:`32912`.)
421-
422417
* The compiler now produces a :exc:`SyntaxWarning` in some cases when a comma
423418
is missed before tuple or list. For example::
424419

Lib/test/test_fstring.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ def test_backslashes_in_string_part(self):
649649
self.assertEqual(f'2\x203', '2 3')
650650
self.assertEqual(f'\x203', ' 3')
651651

652-
with self.assertWarns(SyntaxWarning): # invalid escape sequence
652+
with self.assertWarns(DeprecationWarning): # invalid escape sequence
653653
value = eval(r"f'\{6*7}'")
654654
self.assertEqual(value, '\\42')
655655
self.assertEqual(f'\\{6*7}', '\\42')

Lib/test/test_string_literals.py

+33-4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import shutil
3333
import tempfile
3434
import unittest
35+
import warnings
3536

3637

3738
TEMPLATE = r"""# coding: %s
@@ -110,10 +111,24 @@ def test_eval_str_invalid_escape(self):
110111
for b in range(1, 128):
111112
if b in b"""\n\r"'01234567NU\\abfnrtuvx""":
112113
continue
113-
with self.assertWarns(SyntaxWarning):
114+
with self.assertWarns(DeprecationWarning):
114115
self.assertEqual(eval(r"'\%c'" % b), '\\' + chr(b))
115116

116-
self.check_syntax_warning("'''\n\\z'''")
117+
with warnings.catch_warnings(record=True) as w:
118+
warnings.simplefilter('always', category=DeprecationWarning)
119+
eval("'''\n\\z'''")
120+
self.assertEqual(len(w), 1)
121+
self.assertEqual(w[0].filename, '<string>')
122+
self.assertEqual(w[0].lineno, 1)
123+
124+
with warnings.catch_warnings(record=True) as w:
125+
warnings.simplefilter('error', category=DeprecationWarning)
126+
with self.assertRaises(SyntaxError) as cm:
127+
eval("'''\n\\z'''")
128+
exc = cm.exception
129+
self.assertEqual(w, [])
130+
self.assertEqual(exc.filename, '<string>')
131+
self.assertEqual(exc.lineno, 1)
117132

118133
def test_eval_str_raw(self):
119134
self.assertEqual(eval(""" r'x' """), 'x')
@@ -145,10 +160,24 @@ def test_eval_bytes_invalid_escape(self):
145160
for b in range(1, 128):
146161
if b in b"""\n\r"'01234567\\abfnrtvx""":
147162
continue
148-
with self.assertWarns(SyntaxWarning):
163+
with self.assertWarns(DeprecationWarning):
149164
self.assertEqual(eval(r"b'\%c'" % b), b'\\' + bytes([b]))
150165

151-
self.check_syntax_warning("b'''\n\\z'''")
166+
with warnings.catch_warnings(record=True) as w:
167+
warnings.simplefilter('always', category=DeprecationWarning)
168+
eval("b'''\n\\z'''")
169+
self.assertEqual(len(w), 1)
170+
self.assertEqual(w[0].filename, '<string>')
171+
self.assertEqual(w[0].lineno, 1)
172+
173+
with warnings.catch_warnings(record=True) as w:
174+
warnings.simplefilter('error', category=DeprecationWarning)
175+
with self.assertRaises(SyntaxError) as cm:
176+
eval("b'''\n\\z'''")
177+
exc = cm.exception
178+
self.assertEqual(w, [])
179+
self.assertEqual(exc.filename, '<string>')
180+
self.assertEqual(exc.lineno, 1)
152181

153182
def test_eval_bytes_raw(self):
154183
self.assertEqual(eval(""" br'x' """), b'x')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Reverted :issue:`32912`: emitting :exc:`SyntaxWarning` instead of
2+
:exc:`DeprecationWarning` for invalid escape sequences in string and bytes
3+
literals.

Python/ast.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -4671,12 +4671,12 @@ warn_invalid_escape_sequence(struct compiling *c, const node *n,
46714671
if (msg == NULL) {
46724672
return -1;
46734673
}
4674-
if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg,
4674+
if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg,
46754675
c->c_filename, LINENO(n),
46764676
NULL, NULL) < 0)
46774677
{
4678-
if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
4679-
/* Replace the SyntaxWarning exception with a SyntaxError
4678+
if (PyErr_ExceptionMatches(PyExc_DeprecationWarning)) {
4679+
/* Replace the DeprecationWarning exception with a SyntaxError
46804680
to get a more accurate error report */
46814681
PyErr_Clear();
46824682
ast_error(c, n, "%U", msg);

0 commit comments

Comments
 (0)