Re: backslash in triple quoted string

Thomas Passin <[email protected]>
Newsgroups comp.lang.python
Message-ID <[email protected]>
On 5/8/2025 2:05 AM, Left Right via Python-list wrote:
> Also, it appears that the change linked above is a lie:
> 
> https://docs.python.org/3/reference/lexical_analysis.html#grammar-token-python-grammar-longstringitem
> 
> According to the grammar, any character can follow backslash in a
> valid Python program. The warning / error raised by this code should
> not be a syntax error / warning because the syntax is correct.

"Changed in version 3.12: Unrecognized escape sequences produce a 
SyntaxWarning. In a future Python version they will be eventually a 
SyntaxError."

See https://docs.python.org/3/reference/lexical_analysis.html#strings

Test case for "\" in triple-quoted string:

[code]
>>> def f():
...    """\hello"""
<stdin>:2: SyntaxWarning: invalid escape sequence '\h'
...    print('hi')
...

>>> f()
hi
[/code]

This example fits the documentation exactly. A SyntaxWarning is emitted 
for the unrecognized escape sequence "\h". The program runs as intended.

Please let's drop all the OT back-and-forth.

> On Thu, May 8, 2025 at 7:54 AM Left Right <[email protected]> wrote:
>>
>> I think it could be this:
>>
>> A backslash-character pair that is not a valid escape sequence now
>> generates a SyntaxWarning, instead of DeprecationWarning. For example,
>> re.compile("\d+\.\d+") now emits a SyntaxWarning ("\d" is an invalid
>> escape sequence, use raw strings for regular expression:
>> re.compile(r"\d+\.\d+")). In a future Python version, SyntaxError will
>> eventually be raised, instead of SyntaxWarning. (Contributed by Victor
>> Stinner in gh-98401.)
>>
>> Found in:
>> https://docs.python.org/3/whatsnew/3.12.html#other-language-changes
>>
>> It's not supposed to crash your program though. If the program crashes
>> because of it, it's a bug in Python.
>>
>> On Thu, May 8, 2025 at 7:00 AM Bob van der Poel via Python-list
>> <[email protected]> wrote:
>>>
>>> Did something change in python buggering up my use of a "\ " sequence in a
>>> triple quoted string?
>>>
>>> I have yet to go through my archive on the program, but I tried to run it
>>> today and it crashed quite spectacularly when it hit a """ .... """ line
>>> being used as a comment at the top of a function. I changed the "\" to a
>>> "/" and all is well now.
>>>
>>>
>>> --
>>>
>>> **** Listen to my FREE CD at http://www.mellowood.ca/music/cedars ****
>>> Bob van der Poel ** Wynndel, British Columbia, CANADA **
>>> EMAIL: [email protected]
>>> WWW:   http://www.mellowood.ca
>>> --
>>> https://mail.python.org/mailman/listinfo/python-list
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.