Skip to content

Commit 418e720

Browse files
[3.12] gh-112006: Fix inspect.unwrap() for types where __wrapped__ is a data descriptor (GH-115540) (GH-115966)
(cherry picked from commit 68c79d2) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 3af945f commit 418e720

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

Lib/inspect.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -760,18 +760,14 @@ def unwrap(func, *, stop=None):
760760
:exc:`ValueError` is raised if a cycle is encountered.
761761
762762
"""
763-
if stop is None:
764-
def _is_wrapper(f):
765-
return hasattr(f, '__wrapped__')
766-
else:
767-
def _is_wrapper(f):
768-
return hasattr(f, '__wrapped__') and not stop(f)
769763
f = func # remember the original func for error reporting
770764
# Memoise by id to tolerate non-hashable objects, but store objects to
771765
# ensure they aren't destroyed, which would allow their IDs to be reused.
772766
memo = {id(f): f}
773767
recursion_limit = sys.getrecursionlimit()
774-
while _is_wrapper(func):
768+
while not isinstance(func, type) and hasattr(func, '__wrapped__'):
769+
if stop is not None and stop(func):
770+
break
775771
func = func.__wrapped__
776772
id_func = id(func)
777773
if (id_func in memo) or (len(memo) >= recursion_limit):

Lib/test/test_inspect/test_inspect.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3488,16 +3488,20 @@ class Bar(Spam, Foo):
34883488
((('a', ..., ..., "positional_or_keyword"),),
34893489
...))
34903490

3491-
class Wrapped:
3492-
pass
3493-
Wrapped.__wrapped__ = lambda a: None
3494-
self.assertEqual(self.signature(Wrapped),
3491+
def test_signature_on_wrapper(self):
3492+
class Wrapper:
3493+
def __call__(self, b):
3494+
pass
3495+
wrapper = Wrapper()
3496+
wrapper.__wrapped__ = lambda a: None
3497+
self.assertEqual(self.signature(wrapper),
34953498
((('a', ..., ..., "positional_or_keyword"),),
34963499
...))
34973500
# wrapper loop:
3498-
Wrapped.__wrapped__ = Wrapped
3501+
wrapper = Wrapper()
3502+
wrapper.__wrapped__ = wrapper
34993503
with self.assertRaisesRegex(ValueError, 'wrapper loop'):
3500-
self.signature(Wrapped)
3504+
self.signature(wrapper)
35013505

35023506
def test_signature_on_lambdas(self):
35033507
self.assertEqual(self.signature((lambda a=10: a)),
@@ -4672,6 +4676,14 @@ def test_recursion_limit(self):
46724676
with self.assertRaisesRegex(ValueError, 'wrapper loop'):
46734677
inspect.unwrap(obj)
46744678

4679+
def test_wrapped_descriptor(self):
4680+
self.assertIs(inspect.unwrap(NTimesUnwrappable), NTimesUnwrappable)
4681+
self.assertIs(inspect.unwrap(staticmethod), staticmethod)
4682+
self.assertIs(inspect.unwrap(classmethod), classmethod)
4683+
self.assertIs(inspect.unwrap(staticmethod(classmethod)), classmethod)
4684+
self.assertIs(inspect.unwrap(classmethod(staticmethod)), staticmethod)
4685+
4686+
46754687
class TestMain(unittest.TestCase):
46764688
def test_only_source(self):
46774689
module = importlib.import_module('unittest')
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :func:`inspect.unwrap` for types with the ``__wrapper__`` data
2+
descriptor.

0 commit comments

Comments
 (0)