Skip to content

Commit b460a9b

Browse files
author
Joe Jevnik
committed
MAINT: backport CPython definition of unwrap
1 parent a069f61 commit b460a9b

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

interface/compat.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,23 @@ def viewkeys(d):
2828
return d.viewkeys()
2929

3030
def unwrap(func, stop=None):
31+
# NOTE: implementation is taken from CPython/Lib/inspect.py, Python 3.6
3132
if stop is None:
32-
def stop(f):
33-
return False
34-
35-
while not stop(func):
36-
try:
37-
func = func.__wrapped__
38-
except AttributeError:
39-
return func
33+
def _is_wrapper(f):
34+
return hasattr(f, '__wrapped__')
35+
else:
36+
def _is_wrapper(f):
37+
return hasattr(f, '__wrapped__') and not stop(f)
38+
f = func # remember the original func for error reporting
39+
memo = {id(f)} # Memoise by id to tolerate non-hashable objects
40+
while _is_wrapper(func):
41+
func = func.__wrapped__
42+
id_func = id(func)
43+
if id_func in memo:
44+
raise ValueError('wrapper loop when unwrapping {!r}'.format(f))
45+
memo.add(id_func)
46+
return func
47+
4048

4149
else: # pragma: nocover-py2
4250
from inspect import signature, Parameter, unwrap

0 commit comments

Comments
 (0)