Skip to content

gh-111489: Add PyTuple_FromArray() function - #139691

Merged
vstinner merged 4 commits into
python:mainfrom
vstinner:tuple_fromarray
Oct 10, 2025
Merged

vstinner merged 4 commits into
python:mainfrom
vstinner:tuple_fromarray

Conversation

@vstinner

@vstinner vstinner commented Oct 7, 2025 •

Copy link
Copy Markdown
Member
@vstinner

vstinner commented Oct 7, 2025

Copy link
Copy Markdown
Member Author
Comment thread Lib/test/test_capi/test_tuple.py Resolved
Comment thread Lib/test/test_capi/test_tuple.py Resolved
Comment thread Modules/_testcapi/tuple.c Resolved
Comment thread Modules/_testcapi/tuple.c Resolved
Comment thread Modules/_testcapi/tuple.c Resolved
Comment thread Modules/_testcapi/tuple.c Resolved
Comment thread Doc/c-api/tuple.rst Resolved
@vstinner

vstinner commented Oct 7, 2025

Copy link
Copy Markdown
Member Author

@serhiy-storchaka: I addressed your comments. Please review the updated PR.


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

Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Member Author

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?

Comment thread Lib/test/test_capi/test_tuple.py Resolved
Comment thread Modules/_testcapi/tuple.c Resolved
Comment thread Objects/tupleobject.c Resolved
Use also UNINITIALIZED_SIZE in test code.
@vstinner

vstinner commented Oct 7, 2025

Copy link
Copy Markdown
Member Author

I updated the PR for the latest review.

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. 👍

Comment thread Doc/c-api/tuple.rst
Comment on lines +42 to +43
Create a tuple of *size* items and copy references from *array* to the new
tuple.

Copy link
Copy Markdown
Member

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

encukou commented Oct 7, 2025

Copy link
Copy Markdown
Member

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

@vstinner

vstinner commented Oct 7, 2025

Copy link
Copy Markdown
Member Author

Do you want to open a WG vote for this?

I created capi-workgroup/decisions#79.

@vstinner

vstinner commented Oct 7, 2025 •

Copy link
Copy Markdown
Member Author

I exchanged array and size parameters, new API:

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

@vstinner

vstinner commented Oct 8, 2025

Copy link
Copy Markdown
Member Author

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

Copy link
Copy Markdown
Member Author

I created capi-workgroup/decisions#79.

The C API Working Group accepted this API.

@vstinner
vstinner merged commit e31c22d into python:main Oct 10, 2025
@vstinner
vstinner deleted the tuple_fromarray branch October 10, 2025 06:54
@vstinner

Copy link
Copy Markdown
Member Author

Merged. Thanks for your review @serhiy-storchaka and @encukou!

@vstinner

Copy link
Copy Markdown
Member Author

I added the function to pythoncapi-compat: python/pythoncapi-compat@89e023e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

3 participants