gh-108308: Replace PyDict_GetItem() with PyDict_GetItemRef() by vstinner · Pull Request #108309 · python/cpython · GitHub
Skip to content
Merged
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
49 changes: 38 additions & 11 deletions Python/assemble.c
34 changes: 27 additions & 7 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -4194,9 +4194,20 @@ compiler_nameop(struct compiler *c, location loc,
optype = OP_DEREF;
break;
case LOCAL:
if (_PyST_IsFunctionLike(c->u->u_ste) ||
(PyDict_GetItem(c->u->u_metadata.u_fasthidden, mangled) == Py_True))
if (_PyST_IsFunctionLike(c->u->u_ste)) {
optype = OP_FAST;
}
else {
PyObject *item;
if (PyDict_GetItemRef(c->u->u_metadata.u_fasthidden, mangled,
&item) < 0) {
goto error;
}
if (item == Py_True) {
optype = OP_FAST;
}
Py_XDECREF(item);
}
break;
case GLOBAL_IMPLICIT:
if (_PyST_IsFunctionLike(c->u->u_ste))
Expand All @@ -4221,15 +4232,15 @@ compiler_nameop(struct compiler *c, location loc,
op = LOAD_FROM_DICT_OR_DEREF;
// First load the locals
if (codegen_addop_noarg(INSTR_SEQUENCE(c), LOAD_LOCALS, loc) < 0) {
return ERROR;
goto error;
}
}
else if (c->u->u_ste->ste_can_see_class_scope) {
op = LOAD_FROM_DICT_OR_DEREF;
// First load the classdict
if (compiler_addop_o(c->u, loc, LOAD_DEREF,
c->u->u_metadata.u_freevars, &_Py_ID(__classdict__)) < 0) {
return ERROR;
goto error;
}
}
else {
Expand All @@ -4256,7 +4267,7 @@ compiler_nameop(struct compiler *c, location loc,
// First load the classdict
if (compiler_addop_o(c->u, loc, LOAD_DEREF,
c->u->u_metadata.u_freevars, &_Py_ID(__classdict__)) < 0) {
return ERROR;
goto error;
}
} else {
op = LOAD_GLOBAL;
Expand Down Expand Up @@ -4290,6 +4301,10 @@ compiler_nameop(struct compiler *c, location loc,
arg <<= 1;
}
return codegen_addop_i(INSTR_SEQUENCE(c), op, arg, loc);

error:
Py_DECREF(mangled);
return ERROR;
}

static int
Expand Down Expand Up @@ -5518,8 +5533,13 @@ push_inlined_comprehension_state(struct compiler *c, location loc,
if ((symbol & DEF_LOCAL && !(symbol & DEF_NONLOCAL)) || in_class_block) {
if (!_PyST_IsFunctionLike(c->u->u_ste)) {
// non-function scope: override this name to use fast locals
PyObject *orig = PyDict_GetItem(c->u->u_metadata.u_fasthidden, k);
if (orig != Py_True) {
PyObject *orig;
if (PyDict_GetItemRef(c->u->u_metadata.u_fasthidden, k, &orig) < 0) {
return ERROR;
}
int orig_is_true = (orig == Py_True);
Py_XDECREF(orig);
if (!orig_is_true) {
if (PyDict_SetItem(c->u->u_metadata.u_fasthidden, k, Py_True) < 0) {
return ERROR;
}
Expand Down
30 changes: 22 additions & 8 deletions Python/flowgraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -2404,17 +2404,31 @@ build_cellfixedoffsets(_PyCompile_CodeUnitMetadata *umd)
PyObject *varname, *cellindex;
Py_ssize_t pos = 0;
while (PyDict_Next(umd->u_cellvars, &pos, &varname, &cellindex)) {
PyObject *varindex = PyDict_GetItem(umd->u_varnames, varname);
if (varindex != NULL) {
assert(PyLong_AS_LONG(cellindex) < INT_MAX);
assert(PyLong_AS_LONG(varindex) < INT_MAX);
int oldindex = (int)PyLong_AS_LONG(cellindex);
int argoffset = (int)PyLong_AS_LONG(varindex);
fixed[oldindex] = argoffset;
PyObject *varindex;
if (PyDict_GetItemRef(umd->u_varnames, varname, &varindex) < 0) {
goto error;
}
if (varindex == NULL) {
continue;
}

int argoffset = _PyLong_AsInt(varindex);
Py_DECREF(varindex);
if (argoffset == -1 && PyErr_Occurred()) {
goto error;
}
}

int oldindex = _PyLong_AsInt(cellindex);
if (oldindex == -1 && PyErr_Occurred()) {
goto error;
}
fixed[oldindex] = argoffset;
}
return fixed;

error:
PyMem_Free(fixed);
return NULL;
}

#define IS_GENERATOR(CF) \
Expand Down
24 changes: 18 additions & 6 deletions Python/pylifecycle.c