gh-111489: Add PyTuple_FromArray() function by vstinner · Pull Request #139691 · python/cpython

@vstinner

@vstinner vstinner commented

Oct 7, 2025

edited by github-actions bot

Loading

@vstinner

@vstinner

@vstinner

serhiy-storchaka

@vstinner

@vstinner

serhiy-storchaka


tup = ()
copy = tuple_fromarray(tup)
self.assertIs(copy, tup)

Choose a reason for hiding this comment

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

Is not the empty tuple singleton a CPython implementation detail?

Choose a reason for hiding this comment

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

The whole file is a CPython implementation detail (_testcapi), no?

@vstinner

Use also UNINITIALIZED_SIZE in test code.

@vstinner

I updated the PR for the latest review.

serhiy-storchaka

Choose a reason for hiding this comment

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

LGTM. 👍

Comment on lines +42 to +43

Create a tuple of *size* items and copy references from *array* to the new
tuple.

Choose a reason for hiding this comment

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

I would prefer to formulate this in words more similar to PyTuple_New() or PyTuple_Pack() ("Return a new tuple object of size ... "), but this is not so important.

@encukou

Implementation looks good, thanks! Do you want to open a WG vote for this?

@vstinner

@vstinner

I exchanged array and size parameters, new API:

PyObject* PyTuple_FromArray(Py_ssize_t size, PyObject *const *array)

@vstinner

I exchanged array and size parameters, new API: (...)

Sorry, I changed my preference back to (array, size) as before :-) See the API discussion at: capi-workgroup/decisions#79

@vstinner

@vstinner

@vstinner