mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +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
|
@ -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