8000 gh-109114: Relax the check for invalid lambdas inside f-strings to av… · python/cpython@fa6cc94 · GitHub
[go: up one dir, main page]

Skip to content

Commit fa6cc94

Browse files
committed
gh-109114: Relax the check for invalid lambdas inside f-strings to avoid false positives
Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
1 parent b9831e5 commit fa6cc94

File tree

6 files changed

+3245
-1309
lines changed

6 files changed

+3245
-1309
lines changed

Grammar/python.gram

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,7 @@ invalid_expression:
11701170
_PyPegen_check_legacy_stmt(p, a) ? NULL : p->tokens[p->mark-1]->level == 0 ? NULL :
11711171
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Perhaps you forgot a comma?") }
11721172
| a=disjunction 'if' b=disjunction !('else'|':') { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "expected 'else' after 'if' expression") }
1173-
| a='lambda' [lambda_params] b=':' &(FSTRING_MIDDLE | fstring_replacement_field) {
1173+
| a='lambda' [lambda_params] b=':' &FSTRING_MIDDLE {
11741174
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "f-string: lambda expressions are not allowed without parentheses") }
11751175

11761176
invalid_named_expression(memo):

Lib/test/test_fstring.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,10 @@ def test_lambda(self):
10271027
"f'{lambda x:}'",
10281028
"f'{lambda :}'",
10291029
])
1030+
# Ensure the detection of invalid lambdas doesn't trigger detection
1031+
# for valid lambdas in the second error pass
1032+
with self.assertRaisesRegex(SyntaxError, "invalid syntax"):
1033+
compile("lambda name_3=f'{name_4}': {name_3}\n1 $ 1", "<string>", "exec")
10301034

10311035
# but don't emit the paren warning in general cases
10321036
wit 10000 h self.assertRaisesRegex(SyntaxError, "f-string: expecting a valid expression after '{'"):

Lib/test/test_tix.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import sys
2+
import unittest
3+
from test import support
4+
from test.support import import_helper
5+
from test.support import check_sanitizer
6+
7+
if check_sanitizer(address=True, memory=True):
8+
raise unittest.SkipTest("Tests involving libX11 can SEGFAULT on ASAN/MSAN builds")
9+
10+
11+
# Skip this test if the _tkinter module wasn't built.
12+
_tkinter = import_helper.import_module('_tkinter')
13+
14+
# Skip test if tk cannot be initialized.
15+
support.requires('gui')
16+
17+
# Suppress the deprecation warning
18+
tix = import_helper.import_module('tkinter.tix', deprecated=True)
19+
from tkinter import TclError
20+
21+
22+
class TestTix(unittest.TestCase):
23+
24+
def setUp(self):
25+
try:
26+
self.root = tix.Tk()
27+
except TclError:
28+
if sys.platform.startswith('win'):
29+
self.fail('Tix should always be available on Windows')
30+
self.skipTest('Tix not available')
31+
else:
32+
self.addCleanup(self.root.destroy)
33+
34+
def test_tix_available(self):
35+
# this test is just here to make setUp run
36+
pass
37+
38+
39+
if __name__ == '__main__':
40+
unittest.main()

0 commit comments

Comments
 (0)
0