Reject invalid escape sequences (and octal escape sequences) in bytes and Unicode strings

vstinner · GitHub

In Python 3.6, invalid escape sequence were deprecated in string literals (bytes and str): issue

#71551

, commit

110b6fe

.

What's New in Python 3.6: Deprecated Python behavior

:

A backslash-character pair that is not a valid escape sequence now generates a DeprecationWarning. Although this will eventually become a SyntaxError, that will not be for several Python releases. (Contributed by Emanuel Barry in bpo-27364.)

I propose now raises a SyntaxError, rather than a DeprecationWarning (which is silent in most cases).

Example:

$ python3 -W default -c 'print(list("\z"))' <string>:1: DeprecationWarning: invalid escape sequence '\z' ['\\', 'z'] $ python3 -W default -c 'print(list(b"\z"))' <string>:1: DeprecationWarning: invalid escape sequence '\z' [92, 122] Note: Python REPL ate some DeprecationWarning which makes manual testing harder. It was fixed last month by commit

426d72e

in issue

gh-96052

.

———
Python 3.11 now emits a deprecation warning for invalid octal escape sequence (issue

gh-81548

):

Octal escapes in string and bytes literals with value larger than 0o377 now produce DeprecationWarning. In a future Python version they will be a SyntaxWarning and eventually a SyntaxError. (Contributed by Serhiy Storchaka in

gh-81548

.)

Example:

$ python3.11 -Wdefault -c 'print(list(b"\777"))' <string>:1: DeprecationWarning: invalid octal escape sequence '\777' [255] PR:

gh-98401: Invalid escape sequences emits SyntaxWarning #99011