File tree Expand file tree Collapse file tree 1 file changed +16
-8
lines changed Expand file tree Collapse file tree 1 file changed +16
-8
lines changed Original file line number Diff line number Diff 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
4149else : # pragma: nocover-py2
4250 from inspect import signature , Parameter , unwrap
You can’t perform that action at this time.
0 commit comments