Skip to content

Commit

Permalink
Expose uv_loop_t pointer for integration with other C-extensions (#310)
Browse files Browse the repository at this point in the history
Co-authored-by: Fantix King <fantix.king@gmail.com>
  • Loading branch information
pranavtbhat and fantix committed Aug 13, 2022
1 parent a130375 commit b332eb8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -34,6 +34,7 @@
'pycodestyle~=2.7.0',
'pyOpenSSL~=19.0.0',
'mypy>=0.800',
CYTHON_DEPENDENCY,
]

# Dependencies required to build documentation.
Expand Down
5 changes: 5 additions & 0 deletions 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)
18 changes: 18 additions & 0 deletions 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))
5 changes: 5 additions & 0 deletions uvloop/loop.pyx
Expand Up @@ -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

Expand Down Expand Up @@ -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(<void *>self.uvloop, NULL, NULL)


cdef void __loop_alloc_buffer(uv.uv_handle_t* uvhandle,
size_t suggested_size,
Expand Down

0 comments on commit b332eb8

Please sign in to comment.