mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
gh-121130: Fix f-string format specifiers with debug expressions (#121150)
This commit is contained in:
parent
69c68de43a
commit
c46d64e0ef
8 changed files with 75 additions and 31 deletions
|
@ -3638,7 +3638,7 @@ eval_results = [
|
|||
('Expression', ('Subscript', (1, 0, 1, 10), ('List', (1, 0, 1, 3), [('Constant', (1, 1, 1, 2), 5, None)], ('Load',)), ('Slice', (1, 4, 1, 9), ('Constant', (1, 4, 1, 5), 1, None), ('Constant', (1, 6, 1, 7), 1, None), ('Constant', (1, 8, 1, 9), 1, None)), ('Load',))),
|
||||
('Expression', ('IfExp', (1, 0, 1, 21), ('Name', (1, 9, 1, 10), 'x', ('Load',)), ('Call', (1, 0, 1, 5), ('Name', (1, 0, 1, 3), 'foo', ('Load',)), [], []), ('Call', (1, 16, 1, 21), ('Name', (1, 16, 1, 19), 'bar', ('Load',)), [], []))),
|
||||
('Expression', ('JoinedStr', (1, 0, 1, 6), [('FormattedValue', (1, 2, 1, 5), ('Name', (1, 3, 1, 4), 'a', ('Load',)), -1, None)])),
|
||||
('Expression', ('JoinedStr', (1, 0, 1, 10), [('FormattedValue', (1, 2, 1, 9), ('Name', (1, 3, 1, 4), 'a', ('Load',)), -1, ('JoinedStr', (1, 4, 1, 8), [('Constant', (1, 5, 1, 8), '.2f', None)]))])),
|
||||
('Expression', ('JoinedStr', (1, 0, 1, 10), [('FormattedValue', (1, 2, 1, 9), ('Name', (1, 3, 1, 4), 'a', ('Load',)), -1, ('Constant', (1, 5, 1, 8), '.2f', None))])),
|
||||
('Expression', ('JoinedStr', (1, 0, 1, 8), [('FormattedValue', (1, 2, 1, 7), ('Name', (1, 3, 1, 4), 'a', ('Load',)), 114, None)])),
|
||||
('Expression', ('JoinedStr', (1, 0, 1, 11), [('Constant', (1, 2, 1, 6), 'foo(', None), ('FormattedValue', (1, 6, 1, 9), ('Name', (1, 7, 1, 8), 'a', ('Load',)), -1, None), ('Constant', (1, 9, 1, 10), ')', None)])),
|
||||
]
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
# Unicode identifiers in tests is allowed by PEP 3131.
|
||||
|
||||
import ast
|
||||
import datetime
|
||||
import dis
|
||||
import os
|
||||
import re
|
||||
|
@ -1601,6 +1602,12 @@ x = (
|
|||
self.assertEqual(f'{f(a=4)}', '3=')
|
||||
self.assertEqual(x, 4)
|
||||
|
||||
# Check debug expressions in format spec
|
||||
y = 20
|
||||
self.assertEqual(f"{2:{y=}}", "yyyyyyyyyyyyyyyyyyy2")
|
||||
self.assertEqual(f"{datetime.datetime.now():h1{y=}h2{y=}h3{y=}}",
|
||||
'h1y=20h2y=20h3y=20')
|
||||
|
||||
# Make sure __format__ is being called.
|
||||
class C:
|
||||
def __format__(self, s):
|
||||
|
@ -1614,9 +1621,11 @@ x = (
|
|||
self.assertEqual(f'{C()=: }', 'C()=FORMAT- ')
|
||||
self.assertEqual(f'{C()=:x}', 'C()=FORMAT-x')
|
||||
self.assertEqual(f'{C()=!r:*^20}', 'C()=********REPR********')
|
||||
self.assertEqual(f"{C():{20=}}", 'FORMAT-20=20')
|
||||
|
||||
self.assertRaises(SyntaxError, eval, "f'{C=]'")
|
||||
|
||||
|
||||
# Make sure leading and following text works.
|
||||
x = 'foo'
|
||||
self.assertEqual(f'X{x=}Y', 'Xx='+repr(x)+'Y')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue