Skip to content

CALL_LIST_APPEND specialization can make no progress #141367

Description

@Fidget-Spinner

Bug report

Bug description:

CALL_LIST_APPEND requires self to be a PyList_CheckExact in bytecodes.c. However, the specialize.c doesn't check that. This means there's a possible discrepancy between what we specialize for and what we deopt.

The fix is to check that self is a list. It should be as simple as

diff --git a/Python/specialize.c b/Python/specialize.c
index a1c5dedd615..5b44432b8b7 100644
--- a/Python/specialize.c+++ b/Python/specialize.c@@ -2041,8 +2041,12 @@ specialize_method_descriptor(PyMethodDescrObject *descr, _Py_CODEUNIT *instr,
bool pop = (next.op.code == POP_TOP);
int oparg = instr->op.arg;
if ((PyObject *)descr == list_append && oparg == 1 && pop) {
- specialize(instr, CALL_LIST_APPEND);- return 0;+ PyThreadState *tstate = PyThreadState_GET();+ PyObject *self = PyStackRef_AsPyObjectBorrow(tstate->current_frame->stackpointer[-2]);+ if (PyList_CheckExact(self)) {+ specialize(instr, CALL_LIST_APPEND);+ return 0;+ }
}
specialize(instr, CALL_METHOD_DESCRIPTOR_O);
return 0;

The test case should be in test_opcache.py, and test something like

classMyList(list): passx=MyList()
for_inrange(spec_counter):
x.append(1)

and ensure that the append is not specialized to CALL_LIST_APPEND.

@efimov-mikhail can I let you take up this issue please? I'm pretty sure 3.14 is affected too.

CPython versions tested on:

CPython main branch, 3.15, 3.14

Operating systems tested on:

No response

Linked PRs

Metadata

Metadata

Labels

3.15pre-release feature fixes, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions