8000 gh-130907: Treat all module-level annotations as conditional by JelleZijlstra · Pull Request #131550 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-130907: Treat all module-level annotations as conditional #131550

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Approach suggested by Irit
  • Loading branch information
JelleZijlstra committed Apr 21, 2025
commit ee8e8570e756d719ed7fa3ec577fb2919eccd7bc
7 changes: 5 additions & 2 deletions Include/internal/pycore_instruction_sequence.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ typedef struct instruction_sequence {

/* PyList of instruction sequences of nested functions */
PyObject *s_nested;

/* Code for creating annotations, spliced into the main sequence later */
struct instruction_sequence *s_annotations_code;
} _PyInstructionSequence;

typedef struct {
Expand All @@ -66,8 +69,8 @@ _PyJumpTargetLabel _PyInstructionSequence_NewLabel(_PyInstructionSequence *seq);
int _PyInstructionSequence_ApplyLabelMap(_PyInstructionSequence *seq);
int _PyInstructionSequence_InsertInstruction(_PyInstructionSequence *seq, int pos,
int opcode, int oparg, _Py_SourceLocation loc);
int _PyInstructionSequence_InjectSequence(_PyInstructionSequence *seq, int pos,
_PyInstructionSequence *injected);
int _PyInstructionSequence_SetAnnotationsCode(_PyInstructionSequence *seq,
_PyInstructionSequence *annotations);
int _PyInstructionSequence_AddNested(_PyInstructionSequence *seq, _PyInstructionSequence *nested);
void PyInstructionSequence_Fini(_PyInstructionSequence *seq);

Expand Down
24 changes: 16 additions & 8 deletions Include/internal/pycore_opcode_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 11 additions & 10 deletions Include/opcode_ids.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 11 additions & 10 deletions Lib/_opcode_metadata.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Lib/test/test_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,8 @@ def test_boundaries(self):
def test_widths(self):
long_opcodes = set(['JUMP_BACKWARD_NO_INTERRUPT',
'LOAD_FAST_BORROW_LOAD_FAST_BORROW',
'INSTRUMENTED_CALL_FUNCTION_EX'])
'INSTRUMENTED_CALL_FUNCTION_EX',
'ANNOTATIONS_PLACEHOLDER'])
for op, opname in enumerate(dis.opname):
if opname in long_opcodes or opname.startswith("INSTRUMENTED"):
continue
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Raise an error if the ``__annotations__`` attribute is accessed on a module
that has not finished executing.
If the ``__annotations__`` of a module object are accessed while the
module is executing, return the annotations that have been defined so far,
without caching them.
4 changes: 4 additions & 0 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2026,6 +2026,10 @@ dummy_func(
}
}

pseudo(ANNOTATIONS_PLACEHOLDER, (--)) = {
NOP,
};

inst(DICT_UPDATE, (dict, unused[oparg - 1], update -- dict, unused[oparg - 1])) {
PyObject *dict_o = PyStackRef_AsPyObjectBorrow(dict);
PyObject *update_o = PyStackRef_AsPyObjectBorrow(update);
Expand Down
6 changes: 4 additions & 2 deletions Python/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,9 @@ codegen_enter_scope(compiler *c, identifier name, int scope_type,
loc.lineno = 0;
}
ADDOP_I(c, loc, RESUME, RESUME_AT_FUNC_START);
if (scope_type == COMPILE_SCOPE_MODULE) {
ADDOP(c, loc, ANNOTATIONS_PLACEHOLDER);
}
return SUCCESS;
}

Expand Down Expand Up @@ -832,9 +835,8 @@ codegen_process_deferred_annotations(compiler *c, location loc)

if (nested_instr_seq != NULL) {
RETURN_IF_ERROR(
_PyInstructionSequence_InjectSequence(old_instr_seq, 1, nested_instr_seq));
_PyInstructionSequence_SetAnnotationsCode(old_instr_seq, nested_instr_seq));
_PyCompile_SetInstrSequence(c, old_instr_seq);
PyInstructionSequence_Fini(nested_instr_seq);
}

return SUCCESS;
Expand Down
24 changes: 23 additions & 1 deletion Python/flowgraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -3847,16 +3847,38 @@ _PyCfg_FromInstructionSequence(_PyInstructionSequence *seq)
seq->s_instrs[instr->i_oparg].i_target = 1;
}
}
int offset = 0;
for (int i = 0; i < seq->s_used; i++) {
_PyInstruction *instr = &seq->s_instrs[i];
if (instr->i_opcode == ANNOTATIONS_PLACEHOLDER) {
if (seq->s_annotations_code != NULL) {
assert(seq->s_annotations_code->s_labelmap_size == 0
&& seq->s_annotations_code->s_nested == NULL);
for (int j = 0; j < seq->s_annotations_code->s_used; j++) {
_PyInstruction *ann_instr = &seq->s_annotations_code->s_instrs[j];
assert(!HAS_TARGET(ann_instr->i_opcode));
if (_PyCfgBuilder_Addop(g, ann_instr->i_opcode, ann_instr->i_oparg, ann_instr->i_loc) < 0) {
goto error;
}
}
offset += seq->s_annotations_code->s_used - 1;
}
else {
offset -= 1;
}
continue;
}
if (instr->i_target) {
jump_target_label lbl_ = {i};
jump_target_label lbl_ = {i + offset};
if (_PyCfgBuilder_UseLabel(g, lbl_) < 0) {
goto error;
}
}
int opcode = instr->i_opcode;
int oparg = instr->i_oparg;
if (HAS_TARGET(opcode)) {
oparg += offset;
}
if (_PyCfgBuilder_Addop(g, opcode, oparg, instr->i_loc) < 0) {
goto error;
}
Expand Down
47 changes: 17 additions & 30 deletions Python/instruction_sequence.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,24 @@ typedef _Py_SourceLocation location;
}

static int
instr_sequence_grow(instr_sequence *seq, int size) {
instr_sequence_next_inst(instr_sequence *seq) {
assert(seq->s_instrs != NULL || seq->s_used == 0);


_Py_c_array_t array = {
.array = (void*)seq->s_instrs,
.allocated_entries = seq->s_allocated,
.item_size = sizeof(instruction),
.initial_num_entries = INITIAL_INSTR_SEQUENCE_SIZE,
};

RETURN_IF_ERROR(_Py_CArray_EnsureCapacity(&array, seq->s_used + size));
RETURN_IF_ERROR(_Py_CArray_EnsureCapacity(&array, seq->s_used + 1));
seq->s_instrs = array.array;
seq->s_allocated = array.allocated_entries;

assert(seq->s_allocated >= 0);
assert(seq->s_used < seq->s_allocated);
seq->s_used += size;
return seq->s_used - 1;
}

static int
instr_sequence_next_inst(instr_sequence *seq) {
return instr_sequence_grow(seq, 1);
return seq->s_used++;
}

_PyJumpTargetLabel
Expand Down Expand Up @@ -160,28 +155,11 @@ _PyInstructionSequence_InsertInstruction(instr_sequence *seq, int pos,
}

int
_PyInstructionSequence_InjectSequence(instr_sequence *seq, int pos,
instr_sequence *injected)
_PyInstructionSequence_SetAnnotationsCode(instr_sequence *seq,
instr_sequence *annotations)
{
assert(pos >= 0 && pos <= seq->s_used);
// Merging labelmaps is not supported
assert(injected->s_labelmap_size == 0 && injected->s_nested == NULL);
if (injected->s_used == 0) {
return SUCCESS;
}

int last_idx = instr_sequence_grow(seq, injected->s_used);

RETURN_IF_ERROR(last_idx);
for (int i = last_idx - injected->s_used; i >= pos; i--) {
seq->s_instrs[i + injected->s_used] = seq->s_instrs[i];
}
for (int i=0; i < injected->s_used; i++) {
seq->s_instrs[i + pos] = injected->s_instrs[i];
}
for(int lbl=0; lbl < seq->s_labelmap_size; lbl++) {
seq->s_labelmap[lbl] += injected->s_used;
}
assert(seq->s_annotations_code == NULL);
seq->s_annotations_code = annotations;
return SUCCESS;
}

Expand Down Expand Up @@ -209,6 +187,12 @@ PyInstructionSequence_Fini(instr_sequence *seq) {

PyMem_Free(seq->s_instrs);
seq->s_instrs = NULL;

if (seq->s_annotations_code != NULL) {
PyInstructionSequence_Fini(seq->s_annotations_code);
Py_CLEAR(seq->s_annotations_code);
}

}

/*[clinic input]
Expand All @@ -231,6 +215,7 @@ inst_seq_create(void)
seq->s_labelmap = NULL;
seq->s_labelmap_size = 0;
seq->s_nested = NULL;
seq->s_annotations_code = NULL;

PyObject_GC_Track(seq);
return seq;
Expand Down Expand Up @@ -445,6 +430,7 @@ inst_seq_traverse(PyObject *op, visitproc visit, void *arg)
{
_PyInstructionSequence *seq = (_PyInstructionSequence *)op;
Py_VISIT(seq->s_nested);
Py_VISIT((PyObject *)seq->s_annotations_code);
return 0;
}

Expand All @@ -453,6 +439,7 @@ inst_seq_clear(PyObject *op)
{
_PyInstructionSequence *seq = (_PyInstructionSequence *)op;
Py_CLEAR(seq->s_nested);
Py_CLEAR(seq->s_annotations_code);
return 0;
}

Expand Down
Loading
0