[New-bugs-announce] [issue30955] \\N in f-string causes next { to be literal if not escaped

Mital Ashok report at bugs.python.org
Mon Jul 17 18:09:08 EDT 2017


New submission from Mital Ashok:

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 '}'`).

----------
messages: 298563
nosy: Mital Ashok
priority: normal
severity: normal
status: open
title: \\N in f-string causes next { to be literal if not escaped
versions: Python 3.6, Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue30955>
_______________________________________


More information about the New-bugs-announce mailing list