Skip to content

Commit 02c3cdc

Browse files
[3.6] bpo-25532: Protect against infinite loops in inspect.unwrap() (GH-1717) (#3778)
Some objects (like test mocks) auto-generate new objects on attribute access, which can lead to an infinite loop in inspect.unwrap(). Ensuring references are retained to otherwise temporary objects and capping the size of the memo dict turns this case into a conventional exception instead.. (cherry picked from commit f9169ce)
1 parent 680429b commit 02c3cdc

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

Lib/inspect.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -505,13 +505,16 @@ def _is_wrapper(f):
505505
def _is_wrapper(f):
506506
return hasattr(f, '__wrapped__') and not stop(f)
507507
f = func # remember the original func for error reporting
508-
memo = {id(f)} # Memoise by id to tolerate non-hashable objects
508+
# Memoise by id to tolerate non-hashable objects, but store objects to
509+
# ensure they aren't destroyed, which would allow their IDs to be reused.
510+
memo = {id(f): f}
511+
recursion_limit = sys.getrecursionlimit()
509512
while _is_wrapper(func):
510513
func = func.__wrapped__
511514
id_func = id(func)
512-
if id_func in memo:
515+
if (id_func in memo) or (len(memo) >= recursion_limit):
513516
raise ValueError('wrapper loop when unwrapping {!r}'.format(f))
514-
memo.add(id_func)
517+
memo[id_func] = func
515518
return func
516519

517520
# -------------------------------------------------- source code extraction

Lib/test/test_inspect.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3557,6 +3557,19 @@ def test_builtins_have_signatures(self):
35573557
self.assertIsNone(obj.__text_signature__)
35583558

35593559

3560+
class NTimesUnwrappable:
3561+
def __init__(self, n):
3562+
self.n = n
3563+
self._next = None
3564+
3565+
@property
3566+
def __wrapped__(self):
3567+
if self.n <= 0:
3568+
raise Exception("Unwrapped too many times")
3569+
if self._next is None:
3570+
self._next = NTimesUnwrappable(self.n - 1)
3571+
return self._next
3572+
35603573
class TestUnwrap(unittest.TestCase):
35613574

35623575
def test_unwrap_one(self):
@@ -3612,6 +3625,11 @@ class C:
36123625
__wrapped__ = func
36133626
self.assertIsNone(inspect.unwrap(C()))
36143627

3628+
def test_recursion_limit(self):
3629+
obj = NTimesUnwrappable(sys.getrecursionlimit() + 1)
3630+
with self.assertRaisesRegex(ValueError, 'wrapper loop'):
3631+
inspect.unwrap(obj)
3632+
36153633
class TestMain(unittest.TestCase):
36163634
def test_only_source(self):
36173635
module = importlib.import_module('unittest')
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
inspect.unwrap() will now only try to unwrap an object
2+
sys.getrecursionlimit() times, to protect against objects which create a new
3+
object on every attribute access.

0 commit comments

Comments
 (0)