Skip to content

gh-161635: Add PyList_AsTupleAndClear() function - #161637

Open
vstinner wants to merge 3 commits into
python:mainfrom
vstinner:list_as_tuple_and_clear
Open

vstinner wants to merge 3 commits into
python:mainfrom
vstinner:list_as_tuple_and_clear

Conversation

@vstinner

@vstinner vstinner commented Nov 8, 2026 •

Copy link
Copy Markdown
Member
  • Rename _PyList_AsTupleAndClear() to PyList_AsTupleAndClear().
  • The function now raises TypeError if the argument is not a list.
  • Add documentation and tests.

* Rename _PyList_AsTupleAndClear() to PyList_AsTupleAndClear().
* The function now raises TypeError if the argument is not a list.
* Add documentation and tests.
@read-the-docs-community

read-the-docs-community Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #35738480 | 📁 Comparing fa0dea3 against main (c8eabcc)

  🔍 Preview build  

3 files changed
± c-api/list.html
± whatsnew/3.16.html
± whatsnew/changelog.html

@sobolevn sobolevn 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.

Great stuff! This would be useful for us in msgspec's type convertion :)

Comment thread Lib/test/test_capi/test_list.py Resolved
self.assertEqual(lst, [])

lst = [] # empty list
self.assertIs(list_as_tuple_and_clear(lst), ())

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.

question: is () being a singleton a contract or implementation detail? if the former, it would be better to compare the equality.

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.

Ah, that's a tricky question. It's more an implementation detail. But I would like to test this implementation detail, to check that we don't loose this optimization in a future refatoring.

@vstinner
vstinner requested a review from AA-Turner as a code owner November 8, 2026 19:49
@sobolevn

sobolevn commented Nov 8, 2026

Copy link
Copy Markdown
Member

@vstinner should we do the same with PySet_AsFrozenSetAndClear()?

@vstinner

vstinner commented Nov 8, 2026

Copy link
Copy Markdown
Member Author

@vstinner should we do the same with PySet_AsFrozenSetAndClear()?

I found a few functions calling PyFrozenSet_New(iterable) and then discards iterable:

  • _PyCode_ConstantKey(): from tuple
  • make_union(): from set
  • load_frozenset(): from tuple

So only make_union() with its unionbuilder API may benefit from adding a PySet_AsFrozenSetAndClear() function. Maybe we can start by adding a private function? Do you want to add such private function?

I'm not sure that it's worth it to add a public PySet_AsFrozenSetAndClear() function right now.

... Also, I'm working on PyFrozenSetWriter API which should cover more use cases: #160154 :-)

@sobolevn

sobolevn commented Nov 8, 2026

Copy link
Copy Markdown
Member

Do you want to add such private function?

No, I would prefer to have a well-designed public API :)
PyFrozenSetWriter seems promising! 🎉

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

Labels

awaiting core review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants