Skip to content

Commit 21ab5dd

Browse files
committed
simpligy logic and edit docs
1 parent 97ace7d commit 21ab5dd

5 files changed

Lines changed: 26 additions & 18 deletions

File tree

Doc/whatsnew/3.16.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,8 +499,12 @@ symtable
499499
(Contributed by Serhiy Storchaka in :gh:`153844`.)
500500

501501
* Inlined list, set and dict comprehensions (:pep:`709`) are now represented
502-
as their own symbol tables, of type
503-
:attr:`~symtable.SymbolTableType.INLINED_COMPREHENSION`.
502+
as their own symbol table entries, of type
503+
:attr:`~symtable.SymbolTableType.INLINED_COMPREHENSION`. This entry type
504+
represents a sub-scope, and holds information only on the symbols whose
505+
scopes are different in the comprehension and the enclosing scope.
506+
Sub-scopes are a new mechanism that can be used when a symbol's scope
507+
changes within the same compilation unit.
504508
(Contributed by Irit Katriel in :gh:`124697`.)
505509

506510

Include/internal/pycore_compile.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,10 @@ typedef struct {
181181
PySTEntryObject *saved_ste;
182182
} _PyCompile_InlinedComprehensionState;
183183

184-
int _PyCompile_EnterInlinedComprehensionScope(struct _PyCompiler *c, _Py_SourceLocation loc,
184+
int _PyCompile_EnterInlinedComprehensionScope(struct _PyCompiler *c,
185185
PySTEntryObject *entry,
186186
_PyCompile_InlinedComprehensionState *state);
187-
int _PyCompile_ExitInlinedComprehensionScope(struct _PyCompiler *c, _Py_SourceLocation loc,
187+
int _PyCompile_ExitInlinedComprehensionScope(struct _PyCompiler *c,
188188
_PyCompile_InlinedComprehensionState *state);
189189
int _PyCompile_AddDeferredAnnotation(struct _PyCompiler *c, stmt_ty s,
190190
PyObject **conditional_annotation_index);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
The :mod:`symtable` module now represents inlined list, set and dict
2-
comprehensions (:pep:`709`) as their own symbol tables, of type
2+
comprehensions (:pep:`709`) as their own symbol table entries of type
33
:attr:`~symtable.SymbolTableType.INLINED_COMPREHENSION`.

Python/codegen.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4982,7 +4982,7 @@ push_inlined_comprehension_state(compiler *c, location loc,
49824982
_PyCompile_InlinedComprehensionState *state)
49834983
{
49844984
RETURN_IF_ERROR(
4985-
_PyCompile_EnterInlinedComprehensionScope(c, loc, comp, state));
4985+
_PyCompile_EnterInlinedComprehensionScope(c, comp, state));
49864986
RETURN_IF_ERROR(
49874987
codegen_push_inlined_comprehension_locals(c, loc, comp, state));
49884988
return SUCCESS;
@@ -5040,7 +5040,7 @@ pop_inlined_comprehension_state(compiler *c, location loc,
50405040
_PyCompile_InlinedComprehensionState *state)
50415041
{
50425042
RETURN_IF_ERROR(codegen_pop_inlined_comprehension_locals(c, loc, state));
5043-
RETURN_IF_ERROR(_PyCompile_ExitInlinedComprehensionScope(c, loc, state));
5043+
RETURN_IF_ERROR(_PyCompile_ExitInlinedComprehensionScope(c, state));
50445044
return SUCCESS;
50455045
}
50465046

Python/compile.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,10 +1018,6 @@ _PyCompile_ResolveNameop(compiler *c, PyObject *mangled, int scope,
10181018

10191019
PySTEntryObject *ste = c->u->u_ste;
10201020
assert(ste != NULL);
1021-
while (ste->ste_parent != NULL) {
1022-
assert(ste->ste_type == InlinedComprehensionBlock);
1023-
ste = ste->ste_parent;
1024-
}
10251021

10261022
assert(scope >= 0);
10271023
switch (scope) {
@@ -1034,15 +1030,24 @@ _PyCompile_ResolveNameop(compiler *c, PyObject *mangled, int scope,
10341030
*optype = COMPILE_OP_DEREF;
10351031
break;
10361032
case LOCAL:
1037-
if (_PyST_IsFunctionLike(ste) || c->u->u_ste->ste_type == InlinedComprehensionBlock) {
1033+
/* Inlined comprehensions isolate their locals as FAST, even when
1034+
* nested in class or module scope. */
1035+
if (_PyST_IsFunctionLike(ste) || ste->ste_type == InlinedComprehensionBlock) {
10381036
*optype = COMPILE_OP_FAST;
10391037
}
10401038
break;
1041-
case GLOBAL_IMPLICIT:
1042-
if (_PyST_IsFunctionLike(ste)) {
1039+
case GLOBAL_IMPLICIT: {
1040+
/* Opcode depends on the enclosing non-inlined scope. */
1041+
PySTEntryObject *enclosing = ste;
1042+
while (enclosing->ste_parent != NULL) {
1043+
assert(enclosing->ste_type == InlinedComprehensionBlock);
1044+
enclosing = enclosing->ste_parent;
1045+
}
1046+
if (_PyST_IsFunctionLike(enclosing)) {
10431047
*optype = COMPILE_OP_GLOBAL;
10441048
}
10451049
break;
1050+
}
10461051
case GLOBAL_EXPLICIT:
10471052
*optype = COMPILE_OP_GLOBAL;
10481053
break;
@@ -1058,8 +1063,7 @@ _PyCompile_ResolveNameop(compiler *c, PyObject *mangled, int scope,
10581063
}
10591064

10601065
int
1061-
_PyCompile_EnterInlinedComprehensionScope(compiler *c, location loc,
1062-
PySTEntryObject *entry,
1066+
_PyCompile_EnterInlinedComprehensionScope(compiler *c, PySTEntryObject *entry,
10631067
_PyCompile_InlinedComprehensionState *state)
10641068
{
10651069
assert(state->saved_ste == NULL);
@@ -1069,8 +1073,8 @@ _PyCompile_EnterInlinedComprehensionScope(compiler *c, location loc,
10691073
}
10701074

10711075
int
1072-
_PyCompile_ExitInlinedComprehensionScope(compiler *c, location loc,
1073-
_PyCompile_InlinedComprehensionState *state)
1076+
_PyCompile_ExitInlinedComprehensionScope(compiler *c,
1077+
_PyCompile_InlinedComprehensionState *state)
10741078
{
10751079
assert(state->saved_ste != NULL);
10761080
Py_DECREF(c->u->u_ste);

0 commit comments

Comments
 (0)