Skip to content

Commit

Permalink
Fix issues when using keyword argument named self with weak function …
Browse files Browse the repository at this point in the history
…proxy.
  • Loading branch information
GrahamDumpleton committed Jan 12, 2023
1 parent 4e09f7a commit 32a0720
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ Version 1.15.0
Python version of wrapt and did not occur when using the C extension based
implementation.

* When using ``WeakFunctionProxy`` as a wrapper for a function, when calling the
function via the proxy object, it was not possible to pass a keyword argument
named ``self``.

Version 1.14.1
--------------

Expand Down
7 changes: 6 additions & 1 deletion src/wrapt/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,12 @@ def __init__(self, wrapped, callback=None):
super(WeakFunctionProxy, self).__init__(
weakref.proxy(wrapped, _callback))

def __call__(self, *args, **kwargs):
def __call__(*args, **kwargs):
def _unpack_self(self, *args):
return self, args

self, args = _unpack_self(*args)

# We perform a boolean check here on the instance and wrapped
# function as that will trigger the reference error prior to
# calling if the reference had expired.
Expand Down
10 changes: 10 additions & 0 deletions tests/test_weak_function_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,15 @@ def squeal(self):

self.assertEqual(method(), 'bark')

class TestArgumentUnpackingWeakFunctionProxy(unittest.TestCase):

def test_self_keyword_argument(self):
def function(self, *args, **kwargs):
return self, args, kwargs

proxy = wrapt.wrappers.WeakFunctionProxy(function)

self.assertEqual(proxy(self='self', arg1='arg1'), ('self', (), dict(arg1='arg1')))

if __name__ == '__main__':
unittest.main()

0 comments on commit 32a0720

Please sign in to comment.