GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-161635: Add PyList_AsTupleAndClear() function by vstinner · Pull Request #161637 · python/cpython

Conversation

@vstinner

@vstinner vstinner commented Nov 8, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

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.
  • Issue: [C API] Add PyList_AsTupleAndClear() function #161635

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@vstinner
pythongh-161635: Add PyList_AsTupleAndClear() function
0823270
* Rename _PyList_AsTupleAndClear() to PyList_AsTupleAndClear().
* The function now raises TypeError if the argument is not a list.
* Add documentation and tests.
@vstinner
vstinner requested a review from ZeroIntensity as a code owner November 8, 2026 18:44
@bedevere-app bedevere-app Bot added awaiting core review type-feature A feature request or enhancement labels Nov 8, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Nov 8, 2026
[C API] Add PyList_AsTupleAndClear() function #161635
Open
@read-the-docs-community

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

Uh oh!

There was an error while loading. Please reload this page.

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
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

sobolevn
sobolevn reviewed Nov 8, 2026
View reviewed changes

@sobolevn sobolevn left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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 :)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/test/test_capi/test_list.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

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

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

@sobolevn sobolevn Nov 8, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@vstinner vstinner Nov 8, 2026

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

sobolevn reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
vstinner added 2 commits November 8, 2026 20:28
@vstinner
Test also list subclass
c8b5153
@vstinner
Add What's New and Changelog entries
fa0dea3
@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()?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@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 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@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! 🎉

vstinner reacted with hooray emoji
All reactions
  • 🎉 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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

Reviewers

@sobolevn sobolevn sobolevn left review comments
@ZeroIntensity ZeroIntensity Awaiting requested review from ZeroIntensity ZeroIntensity is a code owner
@AA-Turner AA-Turner Awaiting requested review from AA-Turner AA-Turner is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting core review type-feature A feature request or enhancement

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@vstinner @sobolevn
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.