[New-bugs-announce] [issue28590] fstring's '{' from escape sequences does not start an expression

尤立宇 report at bugs.python.org
Wed Nov 2 10:58:03 EDT 2016


New submission from 尤立宇:

PEP 498 says: (https://www.python.org/dev/peps/pep-0498/#escape-sequences)

Scanning an f-string for expressions happens after escape sequences are decoded. Because hex(ord('{')) == 0x7b , the f-string f'\u007b4*10}' is decoded to f'{4*10}' , which evaluates as the integer 40:

    >>> f'\u007b4*10}'
    '40'

However, in python3.6.0b3, the '{' from '\u007b4' does not start an expression, making the remaining '}' invalid:

    >>> f'\u007b4*10}'
      File "<stdin>", line 1
    SyntaxError: f-string: single '}' is not allowed

There's even a test case for it: (Lib/test/test_fstring.py:383)

    def test_no_escapes_for_braces(self):
        # \x7b is '{'.  Make sure it doesn't start an expression.
        self.assertEqual(f'\x7b2}}', '{2}')
        self.assertEqual(f'\x7b2', '{2')
        self.assertEqual(f'\u007b2', '{2')
        self.assertEqual(f'\N{LEFT CURLY BRACKET}2\N{RIGHT CURLY BRACKET}', '{2}')

----------
components: Interpreter Core
messages: 279927
nosy: 尤立宇
priority: normal
severity: normal
status: open
title: fstring's '{' from escape sequences does not start an expression
type: behavior
versions: Python 3.6

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


More information about the New-bugs-announce mailing list