-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: PyCapsule_GetDestructor is allowed to return a nullptr destructor #4221
Conversation
Previously, this code would error out if the destructor happened to be a nullptr. This is incorrect. nullptrs are allowed for capsule destructors. "It is legal for a capsule to have a NULL destructor. This makes a NULL return code somewhat ambiguous; use PyCapsule_IsValid() or PyErr_Occurred() to disambiguate." See: https://docs.python.org/3/c-api/capsule.html#c.PyCapsule_GetDestructor I noticed this while working on a type caster related to pybind#3858 DLPack happens to allow the destructor not to be defined on a capsule, and I encountered such a case. See: https://github.com/dmlc/dlpack/blob/e2bdd3bee8cb6501558042633fa59144cc8b7f5f/include/dlpack/dlpack.h#L219
Thanks @galv, oversight on my part when rewriting the error handling of capsules a few months ago. We also have an open issue to include dlpack casters as officially supported in pybind11 so consider making a PR once you finish writing the casters. |
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.
Thanks for the fix! (Sorry I missed this, too.)
Oh, wait, I'll add a test (to be sure we don't miss this again). |
f626d7d adds a simple test. Remark regarding implementation: I tried to use
To be sure the test is actually exercising the fix: I cherry-picked f626d7d on top of master, then ran test_pytypes:
|
@Skylion007 Does the added test look good to you? |
I tried this locally and it works! I never knew that there are cases where `reinterpret_cast` does not work but `static_cast` does. Let's see if all compilers are happy with this. Co-authored-by: Aaron Gokaslan <[email protected]>
Thanks! Waiting a moment before merging this, until we have #4201 stable again. |
Thanks for the fix! |
Oh, same question, sorry I didn't see this before. https://github.com/pybind/pybind11/pull/4232/files#r991640142 |
Description
Previously, this code would error out if the destructor happened to be a nullptr. This is incorrect. nullptrs are allowed for capsule destructors.
"It is legal for a capsule to have a NULL destructor. This makes a NULL return code somewhat ambiguous; use PyCapsule_IsValid() or PyErr_Occurred() to disambiguate."
See:
https://docs.python.org/3/c-api/capsule.html#c.PyCapsule_GetDestructor
I noticed this while working on a type caster related to #3858 DLPack happens to allow the destructor not to be defined on a capsule, and I encountered such a case. See:
https://github.com/dmlc/dlpack/blob/e2bdd3bee8cb6501558042633fa59144cc8b7f5f/include/dlpack/dlpack.h#L219
Suggested changelog entry: