|
14 | 14 | import re |
15 | 15 | import types |
16 | 16 | import decimal |
| 17 | +import subprocess |
17 | 18 | import unittest |
18 | 19 | import warnings |
19 | 20 | from test import support |
20 | 21 | from test.support.os_helper import temp_cwd |
21 | | -from test.support.script_helper import assert_python_failure, assert_python_ok |
| 22 | +from test.support.script_helper import ( |
| 23 | + assert_python_failure, assert_python_ok, spawn_python) |
22 | 24 |
|
23 | 25 | a_global = 'global variable' |
24 | 26 |
|
@@ -822,6 +824,18 @@ def build_fstr(n, extra=''): |
822 | 824 | s = "f'{1}' 'x' 'y'" * 1024 |
823 | 825 | self.assertEqual(eval(s), '1xy' * 1024) |
824 | 826 |
|
| 827 | + @support.requires_resource('cpu') |
| 828 | + def test_many_fstrings_in_module(self): |
| 829 | + fields = ''.join(f'{{x{i}}}' for i in range(100)) |
| 830 | + source = ''.join( |
| 831 | + f"value_{i} = f'{fields}'\n" for i in range(1_000) |
| 832 | + ) |
| 833 | + namespace = {f'x{i}': str(i) for i in range(100)} |
| 834 | + expected = ''.join(str(i) for i in range(100)) |
| 835 | + exec(source, namespace) |
| 836 | + self.assertEqual(namespace['value_0'], expected) |
| 837 | + self.assertEqual(namespace['value_999'], expected) |
| 838 | + |
825 | 839 | def test_format_specifier_expressions(self): |
826 | 840 | width = 10 |
827 | 841 | precision = 4 |
@@ -1338,6 +1352,9 @@ def test_not_equal(self): |
1338 | 1352 | self.assertEqual(f'{3!=4:}', 'True') |
1339 | 1353 | self.assertEqual(f'{3!=4!s}', 'True') |
1340 | 1354 | self.assertEqual(f'{3!=4!s:.3}', 'Tru') |
| 1355 | + a = 3 |
| 1356 | + b = 4 |
| 1357 | + self.assertEqual(f'{a!=b=:>10}', 'a!=b= 1') |
1341 | 1358 |
|
1342 | 1359 | def test_equal_equal(self): |
1343 | 1360 | # Because an expression ending in = has special meaning, |
@@ -1792,6 +1809,32 @@ def test_debug_in_file(self): |
1792 | 1809 | self.assertEqual(stdout.decode('utf-8').strip().replace('\r\n', '\n').replace('\r', '\n'), |
1793 | 1810 | "3\n=3") |
1794 | 1811 |
|
| 1812 | + @support.requires_subprocess() |
| 1813 | + def test_expression_in_interactive_after_buffer_resize(self): |
| 1814 | + expression = "(\n" + (" " * 64 + "\n") * 256 + "1\n)" |
| 1815 | + source = ( |
| 1816 | + f"result = f'''{{{expression}=}}'''\n" |
| 1817 | + "print(repr(result))\n" |
| 1818 | + ) |
| 1819 | + with spawn_python('-i', '-q', stderr=subprocess.PIPE) as process: |
| 1820 | + stdout, stderr = process.communicate( |
| 1821 | + source.encode(), timeout=support.SHORT_TIMEOUT) |
| 1822 | + self.assertEqual(process.returncode, 0, stderr) |
| 1823 | + self.assertEqual(stdout.decode().strip(), repr(expression + "=1")) |
| 1824 | + |
| 1825 | + def test_debug_in_file_after_buffer_resize(self): |
| 1826 | + expression = "(\n" + (" " * 64 + "\n") * 256 + "1\n)" |
| 1827 | + expected = expression + "=1" |
| 1828 | + with temp_cwd(): |
| 1829 | + script = 'script.py' |
| 1830 | + source = ( |
| 1831 | + f"result = f'''{{{expression}=}}'''\n" |
| 1832 | + f"assert result == {expected!r}\n" |
| 1833 | + ) |
| 1834 | + with open(script, 'w') as f: |
| 1835 | + f.write(source) |
| 1836 | + assert_python_ok(script) |
| 1837 | + |
1795 | 1838 | def test_syntax_warning_infinite_recursion_in_file(self): |
1796 | 1839 | with temp_cwd(): |
1797 | 1840 | script = 'script.py' |
|
0 commit comments