GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-134582: Fix t-strings untokenize() roundtrip removing space betwee… · miss-islington/cpython@52509cc

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • Lib
    • test
      • test_tokenize.py
    • tokenize.py
  • Misc/NEWS.d/next/Library
    • 2025-05-23-23-43-39.gh-issue-134582.9POq3l.rst
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Lib/test/test_tokenize.py‎

Copy file name to clipboardExpand all lines: Lib/test/test_tokenize.py
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1975,6 +1975,10 @@ def test_roundtrip(self):
19751975
for case in cases:
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
def test_continuation(self):
19801984
# Balancing continuation
Collapse file

‎Lib/tokenize.py‎

Copy file name to clipboardExpand all lines: Lib/tokenize.py
+7-7Lines 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
for tok in _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-
if toknum == FSTRING_START:
297-
in_fstring += 1
298-
elif toknum == FSTRING_END:
299-
in_fstring -= 1
296+
if toknum in {FSTRING_START, TSTRING_START}:
297+
in_fstring_or_tstring += 1
298+
elif toknum in {FSTRING_END, TSTRING_END}:
299+
in_fstring_or_tstring -= 1
300300
if toknum == INDENT:
301301
indents.append(tokval)
302302
continue
@@ -311,8 +311,8 @@ def compat(self, token, iterable):
311311
elif toknum in {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-
if tokval in {"{", "}"} and self.tokens and self.tokens[-1] == tokval and in_fstring:
314+
# Insert a space between two consecutive brackets if we are in an f-string or t-string
315+
if tokval in {"{", "}"} and self.tokens and self.tokens[-1] == tokval and in_fstring_or_tstring:
316316
tokval = ' ' + tokval
317317

318318
# Insert a space between two consecutive f-strings
Collapse file

‎Misc/NEWS.d/next/Library/2025-05-23-23-43-39.gh-issue-134582.9POq3l.rst‎

Copy file name to clipboard
+1Lines changed: 1 addition & 0 deletions
  • Display the source diff
  • Display the rich diff
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 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.