FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-155525: Cover quadratic f-string tokenization regression by gwosti · Pull Request #156756 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
28 changes: 28 additions & 0 deletions Lib/test/test_fstring.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,18 @@ def build_fstr(n, extra=''):
s = "f'{1}' 'x' 'y'" * 1024
self.assertEqual(eval(s), '1xy' * 1024)

@support.requires_resource('cpu')
def test_many_fstrings_in_module(self):
fields = ''.join(f'{{x{i}}}' for i in range(100))
source = ''.join(
f"value_{i} = f'{fields}'\n" for i in range(1_000)
)
namespace = {f'x{i}': str(i) for i in range(100)}
expected = ''.join(str(i) for i in range(100))
exec(source, namespace)
self.assertEqual(namespace['value_0'], expected)
self.assertEqual(namespace['value_999'], expected)

def test_format_specifier_expressions(self):
width = 10
precision = 4
Expand Down Expand Up @@ -1348,6 +1360,9 @@ def test_not_equal(self):
self.assertEqual(f'{3!=4:}', 'True')
self.assertEqual(f'{3!=4!s}', 'True')
self.assertEqual(f'{3!=4!s:.3}', 'Tru')
a = 3
b = 4
self.assertEqual(f'{a!=b=:>10}', 'a!=b= 1')

def test_equal_equal(self):
# Because an expression ending in = has special meaning,
Expand Down Expand Up @@ -1819,6 +1834,19 @@ def test_debug_in_file(self):
self.assertEqual(stdout.decode('utf-8').strip().replace('\r\n', '\n').replace('\r', '\n'),
"3\n=3")

def test_debug_in_file_after_buffer_resize(self):
expression = "(\n" + (" " * 64 + "\n") * 256 + "1\n)"
expected = expression + "=1"
with temp_cwd():
script = 'script.py'
source = (
f"result = f'''{{{expression}=}}'''\n"
f"assert result == {expected!r}\n"
)
with open(script, 'w') as f:
f.write(source)
assert_python_ok(script)

def test_syntax_warning_infinite_recursion_in_file(self):
with temp_cwd():
script = 'script.py'
Expand Down
36 changes: 36 additions & 0 deletions Lib/test/test_tstring.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import unittest

from test import support
from test.support.os_helper import temp_cwd
from test.support.script_helper import assert_python_ok
from test.test_string._support import TStringBaseCase, fstring


Expand Down Expand Up @@ -79,6 +82,31 @@ def upper(self):
)
self.assertEqual(fstring(t), "Name: Bob, Age: 30")

def test_interpolation_expression_in_file_after_buffer_resize(self):
expression = "(\n" + (" " * 64 + "\n") * 256 + "1\n)"
with temp_cwd():
script = 'script.py'
source = (
f"template = t'''{{{expression}}}'''\n"
"interpolation = template.interpolations[0]\n"
f"assert interpolation.expression == {expression!r}\n"
)
with open(script, 'w') as f:
f.write(source)
assert_python_ok(script)

@support.requires_resource('cpu')
def test_many_tstrings_in_module(self):
fields = ''.join(f'{{x{i}}}' for i in range(100))
source = ''.join(
f"value_{i} = t'{fields}'\n" for i in range(1_000)
)
namespace = {f'x{i}': str(i) for i in range(100)}
expected = ''.join(str(i) for i in range(100))
exec(source, namespace)
self.assertEqual(fstring(namespace['value_0']), expected)
self.assertEqual(fstring(namespace['value_999']), expected)

def test_format_specifiers(self):
# Test basic format specifiers
value = 3.14159
Expand All @@ -88,6 +116,14 @@ def test_format_specifiers(self):
)
self.assertEqual(fstring(t), "Pi: 3.14")

a = 3
b = 4
t = t"{a!=b:>10}"
self.assertTStringEqual(
t, ("", ""), [(a != b, "a!=b", None, ">10")]
)
self.assertEqual(fstring(t), " 1")

def test_conversions(self):
# Test !s conversion (str)
obj = object()
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix quadratic-time tokenization of modules containing many f-strings or
t-strings.
Loading

Back | FazBrowse Home | New Git URL