diff options
author | Stefan Behnel <stefan_ml@behnel.de> | 2023-03-31 13:45:22 +0200 |
---|---|---|
committer | Stefan Behnel <stefan_ml@behnel.de> | 2023-03-31 13:52:04 +0200 |
commit | 1177bd64a3f5ce7fc9cf3e7c1421bc9aef2937dc (patch) | |
tree | 1f4a159d69aa1f5ae8f313798ea3f7619501abc6 /tests | |
parent | eaf0b3d7f51830f182fc503f5722b6f8ea0c71ac (diff) | |
download | cython-1177bd64a3f5ce7fc9cf3e7c1421bc9aef2937dc.tar.gz |
Add an explicit (although unnecessary) "noexcept" marker to the "PyCapsule_Destructor" function type to document explicitly that it must not emit exceptions.
See https://github.com/scipy/scipy/issues/17234
Diffstat (limited to 'tests')
-rw-r--r-- | tests/run/pycapsule.pyx | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/tests/run/pycapsule.pyx b/tests/run/pycapsule.pyx new file mode 100644 index 000000000..3bba08d88 --- /dev/null +++ b/tests/run/pycapsule.pyx @@ -0,0 +1,33 @@ +# mode: run + +import gc +from cpython cimport pycapsule + +cdef int value = 5 +cdef bint destructed = False + +cdef void destructor(object obj) noexcept: + # PyPy's GC does not guarantee immediate execution. + global destructed + destructed = True + + +def was_destructed(): + return destructed + + +def test_capsule(): + """ + >>> test_capsule() + True + >>> _ = gc.collect() + >>> was_destructed() # let's assume that gc.collect() is enough to assert this + True + """ + capsule = pycapsule.PyCapsule_New(&value, b"simple value", &destructor) + + assert pycapsule.PyCapsule_GetName(capsule) == b"simple value" + assert pycapsule.PyCapsule_GetPointer(capsule, b"simple value") is &value + assert pycapsule.PyCapsule_GetDestructor(capsule) is &destructor + + return pycapsule.PyCapsule_IsValid(capsule, b"simple value") |