Commit 7c6f2f8
fix: PyCapsule_GetDestructor is allowed to return a nullptr destructor (#4221)
* fix: PyCapsule_GetDestructor is allowed to return a nullptr destructor
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
* Add test for the fix.
* Update tests/test_pytypes.cpp
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 <skylion.aaron@gmail.com>
* style: pre-commit fixes
Co-authored-by: Ralf W. Grosse-Kunstleve <rwgk@google.com>
Co-authored-by: Ralf W. Grosse-Kunstleve <rwgkio@gmail.com>
Co-authored-by: Aaron Gokaslan <skylion.aaron@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>1 parent 4a42156 commit 7c6f2f8
File tree
3 files changed
+23
-6
lines changed- include/pybind11
- tests
3 files changed
+23
-6
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1829 | 1829 | | |
1830 | 1830 | | |
1831 | 1831 | | |
1832 | | - | |
1833 | | - | |
1834 | | - | |
1835 | | - | |
1836 | | - | |
| 1832 | + | |
| 1833 | + | |
1837 | 1834 | | |
1838 | 1835 | | |
1839 | 1836 | | |
1840 | 1837 | | |
1841 | 1838 | | |
1842 | 1839 | | |
1843 | | - | |
| 1840 | + | |
| 1841 | + | |
| 1842 | + | |
| 1843 | + | |
1844 | 1844 | | |
1845 | 1845 | | |
1846 | 1846 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
289 | 289 | | |
290 | 290 | | |
291 | 291 | | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
292 | 298 | | |
293 | 299 | | |
294 | 300 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
299 | 299 | | |
300 | 300 | | |
301 | 301 | | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
302 | 313 | | |
303 | 314 | | |
304 | 315 | | |
| |||
0 commit comments