From 0fd363408791db76c96edd58479145fefeb2439f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Sat, 4 Feb 2023 15:24:14 +0000 Subject: [PATCH] Add uvloop.loop.Handle.get_callback() --- tests/test_base.py | 13 +++++++++++++ uvloop/cbhandles.pyx | 10 ++++++++++ 2 files changed, 23 insertions(+) diff --git a/tests/test_base.py b/tests/test_base.py index 3265e03d..03c9b0ba 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -879,6 +879,19 @@ def test_get_ready_queue(self): l2 = len(self.loop.get_ready_queue()) self.assertEqual(l1, l2-1) + def test_ready_handle(self): + def cb(a, b): + return None + args = 1, 2 + self.loop.call_soon(cb, *args) + handle = list(self.loop.get_ready_queue())[-1] + self.assertIsInstance(handle, uvloop.loop.Handle) + cb2, args2 = handle.get_callback() + self.assertIs(cb, cb2) + self.assertEqual(args, args2) + #import uvloop.handles + #self.assertIsInstance(last, uvloop.handles.Handle) + class TestBaseAIO(_TestBase, AIOTestCase): pass diff --git a/uvloop/cbhandles.pyx b/uvloop/cbhandles.pyx index bc67b73d..acaf082f 100644 --- a/uvloop/cbhandles.pyx +++ b/uvloop/cbhandles.pyx @@ -161,6 +161,16 @@ cdef class Handle: def cancelled(self): return self._cancelled + def get_callback(self): + """ + Allow access to a python callback and its args. + Return a (callback, args) tuple or None if it is an + internal callback. + """ + if self.cb_type == 1: + return self.arg1, self.arg2 + return None + @cython.no_gc_clear @cython.freelist(DEFAULT_FREELIST_SIZE)