diff --git a/setup.py b/setup.py index 2e1a164c..336085cc 100644 --- a/setup.py +++ b/setup.py @@ -34,6 +34,7 @@ 'pycodestyle~=2.7.0', 'pyOpenSSL~=19.0.0', 'mypy>=0.800', + CYTHON_DEPENDENCY, ] # Dependencies required to build documentation. diff --git a/tests/cython_helper.pyx b/tests/cython_helper.pyx new file mode 100644 index 00000000..05c691da --- /dev/null +++ b/tests/cython_helper.pyx @@ -0,0 +1,5 @@ +from cpython.pycapsule cimport PyCapsule_GetPointer + + +def capsule_equals(cap1, cap2): + return PyCapsule_GetPointer(cap1, NULL) == PyCapsule_GetPointer(cap2, NULL) diff --git a/tests/test_pointers.py b/tests/test_pointers.py new file mode 100644 index 00000000..5e526b06 --- /dev/null +++ b/tests/test_pointers.py @@ -0,0 +1,18 @@ +from uvloop import _testbase as tb + + +class Test_UV_Pointers(tb.UVTestCase): + def test_get_uv_loop_t_ptr(self): + loop = self.new_loop() + cap1 = loop.get_uv_loop_t_ptr() + cap2 = loop.get_uv_loop_t_ptr() + cap3 = self.new_loop().get_uv_loop_t_ptr() + + import pyximport + + pyximport.install() + + from tests import cython_helper + + self.assertTrue(cython_helper.capsule_equals(cap1, cap2)) + self.assertFalse(cython_helper.capsule_equals(cap1, cap3)) diff --git a/uvloop/loop.pyx b/uvloop/loop.pyx index d9b5aaac..d36920b0 100644 --- a/uvloop/loop.pyx +++ b/uvloop/loop.pyx @@ -38,6 +38,7 @@ from cpython cimport ( PyBytes_AsStringAndSize, Py_SIZE, PyBytes_AS_STRING, PyBUF_WRITABLE ) +from cpython.pycapsule cimport PyCapsule_New from . import _noop @@ -3180,6 +3181,10 @@ cdef class Loop: except Exception as ex: self.call_soon_threadsafe(future.set_exception, ex) + # Expose pointer for integration with other C-extensions + def get_uv_loop_t_ptr(self): + return PyCapsule_New(self.uvloop, NULL, NULL) + cdef void __loop_alloc_buffer(uv.uv_handle_t* uvhandle, size_t suggested_size,