From 5a9649cf2ee2ad0a2f7cbc73359e878084b66968 Mon Sep 17 00:00:00 2001 From: Aron Bierbaum Date: Wed, 7 Sep 2022 08:26:20 -0500 Subject: [PATCH] Add basic support for functools.partial Update robustApply() for functools.partials so that we can connect to functool.partial objects. --- pydispatch/robustapply.py | 5 +++++ tests/test_robustapply.py | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/pydispatch/robustapply.py b/pydispatch/robustapply.py index 698b9ff..03e09c8 100644 --- a/pydispatch/robustapply.py +++ b/pydispatch/robustapply.py @@ -5,6 +5,7 @@ and subset the given arguments to match only those which are acceptable. """ +import functools import sys if sys.hexversion >= 0x3000000: im_func = '__func__' @@ -25,6 +26,10 @@ def function( receiver ): If fromMethod is true, then the callable already has its first argument bound """ + if isinstance(receiver, functools.partial): + # Get the code object and start index from bound function. + _, code_obj, start_idx = function(receiver.func) + return receiver, code_obj, start_idx if hasattr(receiver, '__call__'): # Reassign receiver to the actual method that will be called. if hasattr( receiver.__call__, im_func) or hasattr( receiver.__call__, im_code): diff --git a/tests/test_robustapply.py b/tests/test_robustapply.py index 29c64d2..6313057 100644 --- a/tests/test_robustapply.py +++ b/tests/test_robustapply.py @@ -17,6 +17,15 @@ def test03( self ): def test04( self ): """Raise error on duplication of a particular argument""" self.assertRaises( TypeError, robustApply, oneArgument, "this", blah = "that" ) + def testPartials( self ): + func0 = functools.partial(noArgument) + robustApply(func0) + + func1 = functools.partial(oneArgument, blah = 10) + robustApply(func1) + + func2 = functools.partial(twoArgument, other = 10) + robustApply(func2, "first") def getSuite(): return unittest.makeSuite(TestCases,'test')