@@ -136,10 +136,47 @@ def test_debug_specifier(self):
136136 # Test white space in debug specifier
137137 t = t"Value: { value = } "
138138 self .assertTStringEqual (
139- t , ("Value: value = " , "" ), [(value , "value" , "r" )]
139+ t , ("Value: value = " , "" ), [(value , "value " , "r" )]
140140 )
141141 self .assertEqual (fstring (t ), "Value: value = 42" )
142142
143+ # Explicit line continuations after the debug marker are part of
144+ # the debug text, not the interpolation expression.
145+ for template , strings , interpolation , rendered in (
146+ (
147+ t"""Value: { value = \
148+ } """ ,
149+ ("Value: value =\\ \n " , "" ),
150+ (value , "value " , "r" ),
151+ "Value: value =\\ \n 42" ,
152+ ),
153+ (
154+ t"""Value: { value = \
155+ !r} """ ,
156+ ("Value: value =\\ \n " , "" ),
157+ (value , "value " , "r" ),
158+ "Value: value =\\ \n 42" ,
159+ ),
160+ (
161+ t"""Value: { value = \
162+ :04} """ ,
163+ ("Value: value =\\ \n " , "" ),
164+ (value , "value " , None , "04" ),
165+ "Value: value =\\ \n 0042" ,
166+ ),
167+ (
168+ t"""Value: { value = \
169+ \
170+ } """ ,
171+ ("Value: value =\\ \n \\ \n " , "" ),
172+ (value , "value " , "r" ),
173+ "Value: value =\\ \n \\ \n 42" ,
174+ ),
175+ ):
176+ with self .subTest (template = template ):
177+ self .assertTStringEqual (template , strings , [interpolation ])
178+ self .assertEqual (fstring (template ), rendered )
179+
143180 class C :
144181 def __format__ (self , spec ):
145182 return f"FORMAT-{ spec } "
@@ -149,6 +186,41 @@ def __format__(self, spec):
149186 self .assertEqual (t .interpolations [0 ].format_spec ,
150187 "FORMAT-value=42" )
151188
189+ def test_interpolation_expression_whitespace (self ):
190+ x = 42
191+ for template , expected in (
192+ (t"{ x } " , "x" ),
193+ (t"{ x } " , "x " ),
194+ (t"{ x } " , " x" ),
195+ (t"{ x } " , " x " ),
196+ (t"{ x } " , " x " ),
197+ (t"""{
198+ x
199+ } """ , "\n x\n " ),
200+ (t"{ x !r} " , " x " ),
201+ (t"{ x :.2f} " , " x " ),
202+ (t"{ x = } " , " x " ),
203+ (t"{ x = !r} " , " x " ),
204+ (t"{ x = :.2f} " , " x " ),
205+ (t"{ x == 42 = } " , "x == 42 " ),
206+ ):
207+ with self .subTest (template = template ):
208+ self .assertEqual (
209+ template .interpolations [0 ].expression ,
210+ expected ,
211+ )
212+
213+ def test_interpolation_expression_with_reconstructed_metadata (self ):
214+ regular = t'''{ (
215+ 1 , # Force lexer metadata reconstruction.
216+ "\" #" )} '''
217+ debug = t'''{ (
218+ 1 , # Force lexer metadata reconstruction.
219+ "\" #" )= } '''
220+ expected = '(\n 1, \n "\\ "#")'
221+ self .assertEqual (regular .interpolations [0 ].expression , expected )
222+ self .assertEqual (debug .interpolations [0 ].expression , expected )
223+
152224 def test_raw_tstrings (self ):
153225 path = r"C:\Users"
154226 t = rt"{ path } \Documents"
@@ -314,12 +386,12 @@ def test_triple_quoted(self):
314386
315387 t = t'{ """a""" "#" # outside
316388} '
317- self .assertEqual (t .interpolations [0 ].expression , '"""a""""#"' )
389+ self .assertEqual (t .interpolations [0 ].expression , '"""a""""#" \n ' )
318390
319391 x , y = 1 , 2
320392 t = t'{ x != y # outside
321393} '
322- self .assertEqual (t .interpolations [0 ].expression , 'x != y' )
394+ self .assertEqual (t .interpolations [0 ].expression , 'x != y \n ' )
323395
324396 d = {'a#b' : 42 }
325397 t = t'''{ f"{ d ["a#b" ]} " } '''
0 commit comments