python / cpython

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

\\N in f-string causes next { to be literal if not escaped #75138

Closed d5b9ee74-a580-412c-b8f5-619ccd976881 closed 7 years ago

d5b9ee74-a580-412c-b8f5-619ccd976881 commented 7 years ago
BPO 30955
Nosy @ned-deily, @MitalAshok

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields: ```python assignee = None closed_at = created_at = labels = ['3.7'] title = '\\\\N in f-string causes next { to be literal if not escaped' updated_at = user = 'https://github.com/MitalAshok' ``` bugs.python.org fields: ```python activity = actor = 'ned.deily' assignee = 'none' closed = True closed_date = closer = 'ned.deily' components = [] creation = creator = 'Mital Ashok' dependencies = [] files = [] hgrepos = [] issue_num = 30955 keywords = [] message_count = 2.0 messages = ['298563', '298564'] nosy_count = 2.0 nosy_names = ['ned.deily', 'Mital Ashok'] pr_nums = [] priority = 'normal' resolution = 'out of date' stage = 'resolved' status = 'closed' superseder = None type = None url = 'https://bugs.python.org/issue30955' versions = ['Python 3.6', 'Python 3.7'] ```

d5b9ee74-a580-412c-b8f5-619ccd976881 commented 7 years ago

Take this format python code:

    import unicodedata
    c = chr(0x012345)

To print that character as a string literal, you would expect to do:

print(f"'\\N{{{unicodedata.name(c)}}}'")

Which should print a literal quote ('), a backwards slash (\\ -> \), an N, and the two {{ should escape and print {, followed by the f-expression unicodedata.name(c), then the }} would print one }, and then another literal quote (').

However, this raises a SyntaxError: f-string: single '}' is not allowed. The way to do this without a syntax error is like so:

print(f"'\\N{{unicodedata.name(c)}}}'")

Which prints the expected:

'\N{CUNEIFORM SIGN URU TIMES KI}'

The shortest way to reproduce this is:

f'\\N{'

Which works, and:

f'\\N{{'

which raises an error, even though the first one should raise an error (SyntaxError: f-string: expecting '}').

ned-deily commented 7 years ago

Thanks for the report. This problem has been fixed in Python 3.6.2 which was just released. I believe it was fixed by the changes for bpo-29104.