Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions Python/assemble.c
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ assemble_emit_instr(struct assembler *a, instruction *instr)
int size = instr_size(instr);
if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
if (len > PY_SSIZE_T_MAX / 2) {
PyErr_NoMemory();
return ERROR;
}
RETURN_IF_ERROR(_PyBytes_Resize(&a->a_bytecode, len * 2));
Expand Down
21 changes: 16 additions & 5 deletions Python/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -667,8 +667,8 @@ codegen_unwind_fblock_stack(compiler *c, location *ploc,
_PyCompile_PopFBlock(c, top->fb_type, top->fb_block);
RETURN_IF_ERROR(codegen_unwind_fblock(c, ploc, &copy, preserve_tos));
RETURN_IF_ERROR(codegen_unwind_fblock_stack(c, ploc, preserve_tos, loop));
_PyCompile_PushFBlock(c, copy.fb_loc, copy.fb_type, copy.fb_block,
copy.fb_exit, copy.fb_datum);
RETURN_IF_ERROR(_PyCompile_PushFBlock(c, copy.fb_loc, copy.fb_type, copy.fb_block,
copy.fb_exit, copy.fb_datum));
return SUCCESS;
}

Expand Down Expand Up @@ -715,10 +715,14 @@ codegen_setup_annotations_scope(compiler *c, location loc,

// if .format > VALUE_WITH_FAKE_GLOBALS: raise NotImplementedError
PyObject *value_with_fake_globals = PyLong_FromLong(_Py_ANNOTATE_FORMAT_VALUE_WITH_FAKE_GLOBALS);
if (value_with_fake_globals == NULL) {
return ERROR;
}

assert(!SYMTABLE_ENTRY(c)->ste_has_docstring);
_Py_DECLARE_STR(format, ".format");
ADDOP_I(c, loc, LOAD_FAST, 0);
ADDOP_LOAD_CONST(c, loc, value_with_fake_globals);
ADDOP_LOAD_CONST_NEW(c, loc, value_with_fake_globals); // macro handles decref of value_with_fake_globals
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that the comment is worth it. We didn't add it on other ADDOP_LOAD_CONST_NEW() calls.

Suggested change
ADDOP_LOAD_CONST_NEW(c, loc, value_with_fake_globals); // macro handles decref of value_with_fake_globals
ADDOP_LOAD_CONST_NEW(c, loc, value_with_fake_globals);

ADDOP_I(c, loc, COMPARE_OP, (Py_GT << 5) | compare_masks[Py_GT]);
NEW_JUMP_TARGET_LABEL(c, body);
ADDOP_JUMP(c, loc, POP_JUMP_IF_FALSE, body);
Expand Down Expand Up @@ -794,6 +798,9 @@ codegen_deferred_annotations_body(compiler *c, location loc,
if (!mangled) {
return ERROR;
}
// NOTE: ref of mangled can be leaked on ADDOP* and VISIT macros due to early returns
// fixing would require an overhaul of these macros

PyObject *cond_index = PyList_GET_ITEM(conditional_annotation_indices, i);
assert(PyLong_CheckExact(cond_index));
long idx = PyLong_AS_LONG(cond_index);
Expand All @@ -816,7 +823,7 @@ codegen_deferred_annotations_body(compiler *c, location loc,

VISIT(c, expr, st->v.AnnAssign.annotation);
ADDOP_I(c, LOC(st), COPY, 2);
ADDOP_LOAD_CONST_NEW(c, LOC(st), mangled);
ADDOP_LOAD_CONST_NEW(c, LOC(st), mangled); // macro handles decref of mangle
// stack now contains <annos> <name> <annos> <value>
ADDOP(c, loc, STORE_SUBSCR);
// stack now contains <annos>
Expand Down Expand Up @@ -3279,7 +3286,11 @@ codegen_nameop(compiler *c, location loc,
}

int scope = _PyST_GetScope(SYMTABLE_ENTRY(c), mangled);
RETURN_IF_ERROR(scope);
if (scope == -1) {
Py_DECREF(mangled);
return ERROR;
Comment on lines +3290 to +3291
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Py_DECREF(mangled);
return ERROR;
goto error;

}

_PyCompile_optype optype;
Py_ssize_t arg = 0;
if (_PyCompile_ResolveNameop(c, mangled, scope, &optype, &arg) < 0) {
Expand Down
4 changes: 4 additions & 0 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1100,18 +1100,22 @@ _PyCompile_TweakInlinedComprehensionScopes(compiler *c, location loc,
assert(orig == NULL || orig == Py_True || orig == Py_False);
if (orig != Py_True) {
if (PyDict_SetItem(c->u->u_metadata.u_fasthidden, k, Py_True) < 0) {
Py_XDECREF(orig);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, it's not needed since True and False are immortal. But adding an explicit DECREF makes the code easier to review/maintain.

return ERROR;
}
if (state->fast_hidden == NULL) {
state->fast_hidden = PySet_New(NULL);
if (state->fast_hidden == NULL) {
Py_XDECREF(orig);
return ERROR;
}
}
if (PySet_Add(state->fast_hidden, k) < 0) {
Py_XDECREF(orig);
return ERROR;
}
}
Py_XDECREF(orig);
}
}
}
Expand Down
Loading