Skip to content

Expose uv_loop_t pointer for integration with other C-extensions #310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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