Skip to content

Commit b332eb8

Browse files
pranavtbhatfantix
andauthored
Expose uv_loop_t pointer for integration with other C-extensions (#310)
Co-authored-by: Fantix King <[email protected]>
1 parent a130375 commit b332eb8

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

Diff for: setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
'pycodestyle~=2.7.0',
3535
'pyOpenSSL~=19.0.0',
3636
'mypy>=0.800',
37+
CYTHON_DEPENDENCY,
3738
]
3839

3940
# Dependencies required to build documentation.

Diff for: tests/cython_helper.pyx

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from cpython.pycapsule cimport PyCapsule_GetPointer
2+
3+
4+
def capsule_equals(cap1, cap2):
5+
return PyCapsule_GetPointer(cap1, NULL) == PyCapsule_GetPointer(cap2, NULL)

Diff for: tests/test_pointers.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from uvloop import _testbase as tb
2+
3+
4+
class Test_UV_Pointers(tb.UVTestCase):
5+
def test_get_uv_loop_t_ptr(self):
6+
loop = self.new_loop()
7+
cap1 = loop.get_uv_loop_t_ptr()
8+
cap2 = loop.get_uv_loop_t_ptr()
9+
cap3 = self.new_loop().get_uv_loop_t_ptr()
10+
11+
import pyximport
12+
13+
pyximport.install()
14+
15+
from tests import cython_helper
16+
17+
self.assertTrue(cython_helper.capsule_equals(cap1, cap2))
18+
self.assertFalse(cython_helper.capsule_equals(cap1, cap3))

Diff for: uvloop/loop.pyx

+5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ from cpython cimport (
3838
PyBytes_AsStringAndSize,
3939
Py_SIZE, PyBytes_AS_STRING, PyBUF_WRITABLE
4040
)
41+
from cpython.pycapsule cimport PyCapsule_New
4142

4243
from . import _noop
4344

@@ -3180,6 +3181,10 @@ cdef class Loop:
31803181
except Exception as ex:
31813182
self.call_soon_threadsafe(future.set_exception, ex)
31823183

3184+
# Expose pointer for integration with other C-extensions
3185+
def get_uv_loop_t_ptr(self):
3186+
return PyCapsule_New(<void *>self.uvloop, NULL, NULL)
3187+
31833188

31843189
cdef void __loop_alloc_buffer(uv.uv_handle_t* uvhandle,
31853190
size_t suggested_size,

0 commit comments

Comments
 (0)