| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 85ec3b3 commit da699ed
8 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -395,7 +395,6 @@ struct _Py_global_strings { | |||
| 395 | 395 | STRUCT_FOR_ID(deterministic) | |
| 396 | 396 | STRUCT_FOR_ID(device) | |
| 397 | 397 | STRUCT_FOR_ID(dict) | |
| 398 | - STRUCT_FOR_ID(dictcomp) | ||
| 399 | 398 | STRUCT_FOR_ID(difference_update) | |
| 400 | 399 | STRUCT_FOR_ID(digest) | |
| 401 | 400 | STRUCT_FOR_ID(digest_size) | |
@@ -470,7 +469,6 @@ struct _Py_global_strings { | |||
| 470 | 469 | STRUCT_FOR_ID(func) | |
| 471 | 470 | STRUCT_FOR_ID(future) | |
| 472 | 471 | STRUCT_FOR_ID(generation) | |
| 473 | - STRUCT_FOR_ID(genexpr) | ||
| 474 | 472 | STRUCT_FOR_ID(get) | |
| 475 | 473 | STRUCT_FOR_ID(get_debug) | |
| 476 | 474 | STRUCT_FOR_ID(get_event_loop) | |
@@ -546,7 +544,6 @@ struct _Py_global_strings { | |||
| 546 | 544 | STRUCT_FOR_ID(kw2) | |
| 547 | 545 | STRUCT_FOR_ID(kwdefaults) | |
| 548 | 546 | STRUCT_FOR_ID(label) | |
| 549 | - STRUCT_FOR_ID(lambda) | ||
| 550 | 547 | STRUCT_FOR_ID(last) | |
| 551 | 548 | STRUCT_FOR_ID(last_exc) | |
| 552 | 549 | STRUCT_FOR_ID(last_node) | |
@@ -562,7 +559,6 @@ struct _Py_global_strings { | |||
| 562 | 559 | STRUCT_FOR_ID(line) | |
| 563 | 560 | STRUCT_FOR_ID(line_buffering) | |
| 564 | 561 | STRUCT_FOR_ID(lineno) | |
| 565 | - STRUCT_FOR_ID(listcomp) | ||
| 566 | 562 | STRUCT_FOR_ID(little) | |
| 567 | 563 | STRUCT_FOR_ID(lo) | |
| 568 | 564 | STRUCT_FOR_ID(locale) | |
@@ -709,7 +705,6 @@ struct _Py_global_strings { | |||
| 709 | 705 | STRUCT_FOR_ID(server_hostname) | |
| 710 | 706 | STRUCT_FOR_ID(server_side) | |
| 711 | 707 | STRUCT_FOR_ID(session) | |
| 712 | - STRUCT_FOR_ID(setcomp) | ||
| 713 | 708 | STRUCT_FOR_ID(setpgroup) | |
| 714 | 709 | STRUCT_FOR_ID(setsid) | |
| 715 | 710 | STRUCT_FOR_ID(setsigdef) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -255,25 +255,13 @@ def is_local_symbol(ident): | |||
| 255 | 255 | if is_local_symbol(st.name): | |
| 256 | 256 | match st.type: | |
| 257 | 257 | case _symtable.TYPE_FUNCTION: | |
| 258 | - # generators are of type TYPE_FUNCTION with a ".0" | ||
| 259 | - # parameter as a first parameter (which makes them | ||
| 260 | - # distinguishable from a function named 'genexpr') | ||
| 261 | - if st.name == 'genexpr' and '.0' in st.varnames: | ||
| 262 | - continue | ||
| 263 | 258 | d[st.name] = 1 | |
| 264 | 259 | case _symtable.TYPE_TYPE_PARAMETERS: | |
| 265 | 260 | # Get the function-def block in the annotation | |
| 266 | 261 | # scope 'st' with the same identifier, if any. | |
| 267 | 262 | scope_name = st.name | |
| 268 | 263 | for c in st.children: | |
| 269 | 264 | if c.name == scope_name and c.type == _symtable.TYPE_FUNCTION: | |
| 270 | - # A generic generator of type TYPE_FUNCTION | ||
| 271 | - # cannot be a direct child of 'st' (but it | ||
| 272 | - # can be a descendant), e.g.: | ||
| 273 | - # | ||
| 274 | - # class A: | ||
| 275 | - # type genexpr[genexpr] = (x for x in []) | ||
| 276 | - assert scope_name != 'genexpr' or '.0' not in c.varnames | ||
| 277 | 265 | d[scope_name] = 1 | |
| 278 | 266 | break | |
| 279 | 267 | self.__methods = tuple(d) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -527,6 +527,58 @@ def test_symtable_entry_repr(self): | |||
| 527 | 527 | expected = f"<symtable entry top({self.top.get_id()}), line {self.top.get_lineno()}>" | |
| 528 | 528 | self.assertEqual(repr(self.top._table), expected) | |
| 529 | 529 | ||
| 530 | + def test_lambda(self): | ||
| 531 | + st = symtable.symtable("lambda x: x", "?", "exec") | ||
| 532 | + self.assertEqual(len(st.get_children()), 1) | ||
| 533 | + st = st.get_children()[0] | ||
| 534 | + self.assertIs(st.get_type(), symtable.SymbolTableType.FUNCTION) | ||
| 535 | + self.assertEqual(st.get_name(), "<lambda>") | ||
| 536 | + self.assertFalse(st.is_nested()) | ||
| 537 | + self.assertEqual(sorted(st.get_identifiers()), ["x"]) | ||
| 538 | + self.assertEqual(st.get_children(), []) | ||
| 539 | + | ||
| 540 | + def test_nested_lambda(self): | ||
| 541 | + st = symtable.symtable("lambda x: lambda y=x: y", "?", "exec") | ||
| 542 | + self.assertEqual(len(st.get_children()), 1) | ||
| 543 | + st = st.get_children()[0] | ||
| 544 | + self.assertIs(st.get_type(), symtable.SymbolTableType.FUNCTION) | ||
| 545 | + self.assertEqual(st.get_name(), "<lambda>") | ||
| 546 | + self.assertFalse(st.is_nested()) | ||
| 547 | + self.assertEqual(sorted(st.get_identifiers()), ["x"]) | ||
| 548 | + self.assertEqual(len(st.get_children()), 1) | ||
| 549 | + st = st.get_children()[0] | ||
| 550 | + self.assertIs(st.get_type(), symtable.SymbolTableType.FUNCTION) | ||
| 551 | + self.assertEqual(st.get_name(), "<lambda>") | ||
| 552 | + self.assertTrue(st.is_nested()) | ||
| 553 | + self.assertEqual(sorted(st.get_identifiers()), ["y"]) | ||
| 554 | + self.assertEqual(st.get_children(), []) | ||
| 555 | + | ||
| 556 | + def test_genexpr(self): | ||
| 557 | + st = symtable.symtable("(x for x in a)", "?", "exec") | ||
| 558 | + self.assertEqual(len(st.get_children()), 1) | ||
| 559 | + st = st.get_children()[0] | ||
| 560 | + self.assertIs(st.get_type(), symtable.SymbolTableType.FUNCTION) | ||
| 561 | + self.assertEqual(st.get_name(), "<genexpr>") | ||
| 562 | + self.assertFalse(st.is_nested()) | ||
| 563 | + self.assertEqual(sorted(st.get_identifiers()), [".0", "x"]) | ||
| 564 | + self.assertEqual(st.get_children(), []) | ||
| 565 | + | ||
| 566 | + def test_nested_genexpr(self): | ||
| 567 | + st = symtable.symtable("((y for y in x) for x in a)", "?", "exec") | ||
| 568 | + self.assertEqual(len(st.get_children()), 1) | ||
| 569 | + st = st.get_children()[0] | ||
| 570 | + self.assertIs(st.get_type(), symtable.SymbolTableType.FUNCTION) | ||
| 571 | + self.assertEqual(st.get_name(), "<genexpr>") | ||
| 572 | + self.assertFalse(st.is_nested()) | ||
| 573 | + self.assertEqual(sorted(st.get_identifiers()), [".0", "x"]) | ||
| 574 | + self.assertEqual(len(st.get_children()), 1) | ||
| 575 | + st = st.get_children()[0] | ||
| 576 | + self.assertIs(st.get_type(), symtable.SymbolTableType.FUNCTION) | ||
| 577 | + self.assertEqual(st.get_name(), "<genexpr>") | ||
| 578 | + self.assertTrue(st.is_nested()) | ||
| 579 | + self.assertEqual(sorted(st.get_identifiers()), [".0", "y"]) | ||
| 580 | + self.assertEqual(st.get_children(), []) | ||
| 581 | + | ||
| 530 | 582 | ||
| 531 | 583 | class ComprehensionTests(unittest.TestCase): | |
| 532 | 584 | def get_identifiers_recursive(self, st, res): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,3 @@ | |||
| 1 | + Changed the names of the symbol tables for lambda expressions and generator | ||
| 2 | + expressions to "<lambda>" and "<genexpr>" respectively to avoid conflicts | ||
| 3 | + with user-defined names. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2413,7 +2413,7 @@ symtable_visit_expr(struct symtable *st, expr_ty e) | |||
| 2413 | 2413 | VISIT_SEQ(st, expr, e->v.Lambda.args->defaults); | |
| 2414 | 2414 | if (e->v.Lambda.args->kw_defaults) | |
| 2415 | 2415 | VISIT_SEQ_WITH_NULL(st, expr, e->v.Lambda.args->kw_defaults); | |
| 2416 | - if (!symtable_enter_block(st, &_Py_ID(lambda), | ||
| 2416 | + if (!symtable_enter_block(st, &_Py_STR(anon_lambda), | ||
| 2417 | 2417 | FunctionBlock, (void *)e, LOCATION(e))) { | |
| 2418 | 2418 | return 0; | |
| 2419 | 2419 | } | |
@@ -3055,31 +3055,31 @@ symtable_handle_comprehension(struct symtable *st, expr_ty e, | |||
| 3055 | 3055 | static int | |
| 3056 | 3056 | symtable_visit_genexp(struct symtable *st, expr_ty e) | |
| 3057 | 3057 | { | |
| 3058 | - return symtable_handle_comprehension(st, e, &_Py_ID(genexpr), | ||
| 3058 | + return symtable_handle_comprehension(st, e, &_Py_STR(anon_genexpr), | ||
| 3059 | 3059 | e->v.GeneratorExp.generators, | |
| 3060 | 3060 | e->v.GeneratorExp.elt, NULL); | |
| 3061 | 3061 | } | |
| 3062 | 3062 | ||
| 3063 | 3063 | static int | |
| 3064 | 3064 | symtable_visit_listcomp(struct symtable *st, expr_ty e) | |
| 3065 | 3065 | { | |
| 3066 | - return symtable_handle_comprehension(st, e, &_Py_ID(listcomp), | ||
| 3066 | + return symtable_handle_comprehension(st, e, &_Py_STR(anon_listcomp), | ||
| 3067 | 3067 | e->v.ListComp.generators, | |
| 3068 | 3068 | e->v.ListComp.elt, NULL); | |
| 3069 | 3069 | } | |
| 3070 | 3070 | ||
| 3071 | 3071 | static int | |
| 3072 | 3072 | symtable_visit_setcomp(struct symtable *st, expr_ty e) | |
| 3073 | 3073 | { | |
| 3074 | - return symtable_handle_comprehension(st, e, &_Py_ID(setcomp), | ||
| 3074 | + return symtable_handle_comprehension(st, e, &_Py_STR(anon_setcomp), | ||
| 3075 | 3075 | e->v.SetComp.generators, | |
| 3076 | 3076 | e->v.SetComp.elt, NULL); | |
| 3077 | 3077 | } | |
| 3078 | 3078 | ||
| 3079 | 3079 | static int | |
| 3080 | 3080 | symtable_visit_dictcomp(struct symtable *st, expr_ty e) | |
| 3081 | 3081 | { | |
| 3082 | - return symtable_handle_comprehension(st, e, &_Py_ID(dictcomp), | ||
| 3082 | + return symtable_handle_comprehension(st, e, &_Py_STR(anon_dictcomp), | ||
| 3083 | 3083 | e->v.DictComp.generators, | |
| 3084 | 3084 | e->v.DictComp.key, | |
| 3085 | 3085 | e->v.DictComp.value); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments