gh-138325: steal list items in INTRINSIC_LIST_TO_TUPLE opcode (#149960)

This commit is contained in:
Pieter Eendebak 2026-05-18 08:37:12 +02:00 committed by GitHub
parent acefff95ea
commit 6ee879ffa1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 2 deletions

View file

@ -7,6 +7,8 @@
#include "pycore_genobject.h" // _PyAsyncGenValueWrapperNew
#include "pycore_interpframe.h" // _PyFrame_GetLocals()
#include "pycore_intrinsics.h" // INTRINSIC_PRINT
#include "pycore_list.h" // _PyList_AsTupleAndClear()
#include "pycore_object.h" // _PyObject_IsUniquelyReferenced()
#include "pycore_pyerrors.h" // _PyErr_SetString()
#include "pycore_runtime.h" // _Py_ID()
#include "pycore_typevarobject.h" // _Py_make_typevar()
@ -190,8 +192,12 @@ unary_pos(PyThreadState* unused, PyObject *value)
static PyObject *
list_to_tuple(PyThreadState* unused, PyObject *v)
{
assert(PyList_Check(v));
return PyTuple_FromArray(((PyListObject *)v)->ob_item, Py_SIZE(v));
/* INTRINSIC_LIST_TO_TUPLE is only emitted by the compiler for a
freshly-built, uniquely-referenced temporary list, so steal its items
into the tuple instead of copying them. */
assert(PyList_CheckExact(v));
assert(_PyObject_IsUniquelyReferenced(v));
return _PyList_AsTupleAndClear((PyListObject *)v);
}
static PyObject *