Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c6ef5ac

Browse files
[3.13] gh-119698: fix a special case in symtable.Class.get_methods (GH-121802) (#121909)
(cherry picked from commit 6682d91) Co-authored-by: Bénédikt Tran <[email protected]>
1 parent 4395d68 commit c6ef5ac

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

Lib/symtable.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,26 @@ def is_local_symbol(ident):
238238
if is_local_symbol(st.name):
239239
match st.type:
240240
case _symtable.TYPE_FUNCTION:
241+
# generators are of type TYPE_FUNCTION with a ".0"
242+
# parameter as a first parameter (which makes them
243+
# distinguishable from a function named 'genexpr')
244+
if st.name == 'genexpr' and '.0' in st.varnames:
245+
continue
241246
d[st.name] = 1
242247
case _symtable.TYPE_TYPE_PARAMETERS:
243248
# Get the function-def block in the annotation
244249
# scope 'st' with the same identifier, if any.
245250
scope_name = st.name
246251
for c in st.children:
247252
if c.name == scope_name and c.type == _symtable.TYPE_FUNCTION:
248-
d[st.name] = 1
253+
# A generic generator of type TYPE_FUNCTION
254+
# cannot be a direct child of 'st' (but it
255+
# can be a descendant), e.g.:
256+
#
257+
# class A:
258+
# type genexpr[genexpr] = (x for x in [])
259+
assert scope_name != 'genexpr' or '.0' not in c.varnames
260+
d[scope_name] = 1
249261
break
250262
self.__methods = tuple(d)
251263
return self.__methods

Lib/test/test_symtable.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""
22
Test the API of the symtable module.
33
"""
4+
5+
import textwrap
46
import symtable
57
import unittest
68

@@ -354,7 +356,7 @@ def test_name(self):
354356
self.assertEqual(self.spam.lookup("x").get_name(), "x")
355357
self.assertEqual(self.Mine.get_name(), "Mine")
356358

357-
def test_class_info(self):
359+
def test_class_get_methods(self):
358360
self.assertEqual(self.Mine.get_methods(), ('a_method',))
359361

360362
top = symtable.symtable(TEST_COMPLEX_CLASS_CODE, "?", "exec")
@@ -375,6 +377,58 @@ def test_class_info(self):
375377
'glob_assigned_async_meth', 'glob_assigned_async_meth_pep_695',
376378
))
377379

380+
# Test generator expressions that are of type TYPE_FUNCTION
381+
# but will not be reported by get_methods() since they are
382+
# not functions per se.
383+
#
384+
# Other kind of comprehensions such as list, set or dict
385+
# expressions do not have the TYPE_FUNCTION type.
386+
387+
def check_body(body, expected_methods):
388+
indented = textwrap.indent(body, ' ' * 4)
389+
top = symtable.symtable(f"class A:\n{indented}", "?", "exec")
390+
this = find_block(top, "A")
391+
self.assertEqual(this.get_methods(), expected_methods)
392+
393+
# statements with 'genexpr' inside it
394+
GENEXPRS = (
395+
'x = (x for x in [])',
396+
'x = (x async for x in [])',
397+
'type x[genexpr = (x for x in [])] = (x for x in [])',
398+
'type x[genexpr = (x async for x in [])] = (x async for x in [])',
399+
'genexpr = (x for x in [])',
400+
'genexpr = (x async for x in [])',
401+
'type genexpr[genexpr = (x for x in [])] = (x for x in [])',
402+
'type genexpr[genexpr = (x async for x in [])] = (x async for x in [])',
403+
)
404+
405+
for gen in GENEXPRS:
406+
# test generator expression
407+
with self.subTest(gen=gen):
408+
check_body(gen, ())
409+
410+
# test generator expression + variable named 'genexpr'
411+
with self.subTest(gen=gen, isvar=True):
412+
check_body('\n'.join((gen, 'genexpr = 1')), ())
413+
check_body('\n'.join(('genexpr = 1', gen)), ())
414+
415+
for paramlist in ('()', '(x)', '(x, y)', '(z: T)'):
416+
for func in (
417+
f'def genexpr{paramlist}:pass',
418+
f'async def genexpr{paramlist}:pass',
419+
f'def genexpr[T]{paramlist}:pass',
420+
f'async def genexpr[T]{paramlist}:pass',
421+
):
422+
with self.subTest(func=func):
423+
# test function named 'genexpr'
424+
check_body(func, ('genexpr',))
425+
426+
for gen in GENEXPRS:
427+
with self.subTest(gen=gen, func=func):
428+
# test generator expression + function named 'genexpr'
429+
check_body('\n'.join((gen, func)), ('genexpr',))
430+
check_body('\n'.join((func, gen)), ('genexpr',))
431+
378432
def test_filename_correct(self):
379433
### Bug tickler: SyntaxError file name correct whether error raised
380434
### while parsing or building symbol table.

0 commit comments

Comments
 (0)