Skip to content

Commit

Permalink
gh-117139: Replace _PyList_FromArraySteal with stack ref variant
Browse files Browse the repository at this point in the history
This replaces `_PyList_FromArraySteal` with `_PyList_FromStackRefSteal`.
It's functionally equivalent, but takes a `_PyStackRef` array instead of
an array of `PyObject` pointers.

Co-authored-by: Ken Jin <kenjin@python.org>
  • Loading branch information
colesbury and Fidget-Spinner committed Aug 8, 2024
1 parent 2d9d3a9 commit 47d3235
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 34 deletions.
5 changes: 4 additions & 1 deletion Include/internal/pycore_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ typedef struct {
PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
} _PyListIterObject;

PyAPI_FUNC(PyObject *)_PyList_FromArraySteal(PyObject *const *src, Py_ssize_t n);
union _PyStackRef;

PyAPI_FUNC(PyObject *)_PyList_FromStackRefSteal(const union _PyStackRef *src, Py_ssize_t n);


#ifdef __cplusplus
}
Expand Down
8 changes: 5 additions & 3 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3142,7 +3142,7 @@ PyList_AsTuple(PyObject *v)
}

PyObject *
_PyList_FromArraySteal(PyObject *const *src, Py_ssize_t n)
_PyList_FromStackRefSteal(const _PyStackRef *src, Py_ssize_t n)
{
if (n == 0) {
return PyList_New(0);
Expand All @@ -3151,13 +3151,15 @@ _PyList_FromArraySteal(PyObject *const *src, Py_ssize_t n)
PyListObject *list = (PyListObject *)PyList_New(n);
if (list == NULL) {
for (Py_ssize_t i = 0; i < n; i++) {
Py_DECREF(src[i]);
PyStackRef_CLOSE(src[i]);
}
return NULL;
}

PyObject **dst = list->ob_item;
memcpy(dst, src, n * sizeof(PyObject *));
for (Py_ssize_t i = 0; i < n; i++) {
dst[i] = PyStackRef_AsPyObjectSteal(src[i]);
}

return (PyObject *)list;
}
Expand Down
8 changes: 1 addition & 7 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1666,13 +1666,7 @@ dummy_func(
}

inst(BUILD_LIST, (values[oparg] -- list)) {
STACKREFS_TO_PYOBJECTS(values, oparg, values_o);
if (CONVERSION_FAILED(values_o)) {
DECREF_INPUTS();
ERROR_IF(true, error);
}
PyObject *list_o = _PyList_FromArraySteal(values_o, oparg);
STACKREFS_TO_PYOBJECTS_CLEANUP(values_o);
PyObject *list_o = _PyList_FromStackRefSteal(values, oparg);
ERROR_IF(list_o == NULL, error);
list = PyStackRef_FromPyObjectSteal(list_o);
}
Expand Down
10 changes: 1 addition & 9 deletions Python/executor_cases.c.h

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

14 changes: 1 addition & 13 deletions Python/generated_cases.c.h

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

2 changes: 1 addition & 1 deletion Tools/cases_generator/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ def has_error_without_pop(op: parser.InstDef) -> bool:
"STACKREFS_TO_PYOBJECTS",
"STACKREFS_TO_PYOBJECTS_CLEANUP",
"CONVERSION_FAILED",
"_PyList_FromArraySteal",
"_PyList_FromStackRefSteal",
"_PyTuple_FromArraySteal",
"_PyTuple_FromStackRefSteal",
)
Expand Down

0 comments on commit 47d3235

Please sign in to comment.