Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit057a41e

Browse files
loic-simonmiss-islington
authored andcommitted
pythongh-134582: Fix t-strings untokenize() roundtrip removing space between braces (pythonGH-134603)
(cherry picked from commit52509cc)Co-authored-by: Loïc Simon <loic.pano@gmail.com>
1 parentcbf4ccf commit057a41e

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

‎Lib/test/test_tokenize.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1975,6 +1975,10 @@ def test_roundtrip(self):
19751975
forcaseincases:
19761976
self.check_roundtrip(case)
19771977

1978+
self.check_roundtrip(r"t'{ {}}'")
1979+
self.check_roundtrip(r"t'{f'{ {}}'}{ {}}'")
1980+
self.check_roundtrip(r"f'{t'{ {}}'}{ {}}'")
1981+
19781982

19791983
deftest_continuation(self):
19801984
# Balancing continuation

‎Lib/tokenize.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def compat(self, token, iterable):
274274
toks_append=self.tokens.append
275275
startline=token[0]in (NEWLINE,NL)
276276
prevstring=False
277-
in_fstring=0
277+
in_fstring_or_tstring=0
278278

279279
fortokin_itertools.chain([token],iterable):
280280
toknum,tokval=tok[:2]
@@ -293,10 +293,10 @@ def compat(self, token, iterable):
293293
else:
294294
prevstring=False
295295

296-
iftoknum==FSTRING_START:
297-
in_fstring+=1
298-
eliftoknum==FSTRING_END:
299-
in_fstring-=1
296+
iftoknumin {FSTRING_START,TSTRING_START}:
297+
in_fstring_or_tstring+=1
298+
eliftoknumin {FSTRING_END,TSTRING_END}:
299+
in_fstring_or_tstring-=1
300300
iftoknum==INDENT:
301301
indents.append(tokval)
302302
continue
@@ -311,8 +311,8 @@ def compat(self, token, iterable):
311311
eliftoknumin {FSTRING_MIDDLE,TSTRING_MIDDLE}:
312312
tokval=self.escape_brackets(tokval)
313313

314-
# Insert a space between two consecutive brackets if we are in an f-string
315-
iftokvalin {"{","}"}andself.tokensandself.tokens[-1]==tokvalandin_fstring:
314+
# Insert a space between two consecutive brackets if we are in an f-string or t-string
315+
iftokvalin {"{","}"}andself.tokensandself.tokens[-1]==tokvalandin_fstring_or_tstring:
316316
tokval=' '+tokval
317317

318318
# Insert a space between two consecutive f-strings
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix tokenize.untokenize() round-trip errors related to t-strings braces escaping

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp