python / cpython

The Python programming language
https://www.python.org
Other
63.13k stars 30.22k forks source link

Bug in block comments with escape characters. #114865

Closed massibronto closed 8 months ago

massibronto commented 8 months ago

Bug report

Bug description:

print("start")

'''
fdf

escape
r'\Atestescape'

second
re.search(r'\f([a-z])(\d{6})', values["-IN-"])

block comment

'''

print("end")

input("pause..")

The issue happens when having escape characters inside block comments. The block comment will break on the character sequence \A and \x and \d. Error messages: blockcomment.py:4: SyntaxWarning: invalid escape sequence '\A' or blockcomment.py:4: SyntaxWarning: invalid escape sequence '\d' or

blockcomment.py", line 3
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 58-59: truncated \xXX escape

Whereas the block comment will not break on \f or \t

I would expect the block comment to completely ignore the inside, or am I wrong?

CPython versions tested on:

3.12

Operating systems tested on:

Windows

JelleZijlstra commented 8 months ago

Python doesn't have block comments; what you're using is a string, which happens to work something like a block comment because it's ignored at runtime. The warning you cite is raised in strings and is correct.

massibronto commented 8 months ago

Thank you for pointing this out. Sorry for this misconception; I am not so familiar with the language yet.

terryjreedy commented 8 months ago

Please direct questions, including about possible bugs, to a help forum such as https://discuss.python.org/c/users/7