Skip to content

gh-158660: Use a tagged pointer for PyListBuilder* - #162956

Closed
vstinner wants to merge 1 commit into
python:mainfrom
vstinner:list_builder_tag_ptr
Closed

vstinner wants to merge 1 commit into
python:mainfrom
vstinner:list_builder_tag_ptr

Conversation

@vstinner

@vstinner vstinner commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

In debug mode, use a tagged pointer for PyListBuilder* to make sure that the pointer cannot be passed to an arbitrary Python C API function.

Change list_new_prealloc() return type from PyObject* to PyListObject*.

In debug mode, use a tagged pointer for PyListBuilder* to make sure
that the pointer cannot be passed to an arbitrary Python C API
function.

Change list_new_prealloc() return type from PyObject* to
PyListObject*.
@vstinner

vstinner commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

@serhiy-storchaka @encukou: Do you think that such safety change would be useful in release mode as well? Or should it be restricted to the debug mode to avoid any performance overhead? I expected ptr | 3 and ptr & ~3 to be very cheap on performance.

@vstinner

vstinner commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

@ngoldbaum: I'm also curious about your opinion on using tagged pointers in release mode to detect API misuse.

@encukou

encukou commented Dec 2, 2026

Copy link
Copy Markdown
Member

I'm a bit afraid that this'll make debugging harder, but, I won't block this.
My preference would be keeping ob_type NULL until the list is built.

@vstinner

vstinner commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

I'm no longer sure that this change is worth it. It causes practical issues. I prefer to close this PR.

I just added _testlimitedcapi.PyListBuilder.get_allocated() which requires accessing the internal list object. Using tagged pointers break this method. And I would prefer to not expose internals of the tagged pointers in the limited C API.

I'm a bit afraid that this'll make debugging harder

Oh right, that can be annoying when debugging bugs in C extensions.

@vstinner vstinner closed this Dec 2, 2026
@vstinner
vstinner deleted the list_builder_tag_ptr branch December 2, 2026 23:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants