bpo-39382: Avoid dangling object use in abstract_issubclass() (GH-18530)

Hold reference of __bases__ tuple until tuple item is done with, because by
dropping the reference the item may be destroyed.
This commit is contained in:
Yonatan Goldschmidt 2020-02-22 15:11:48 +02:00 committed by GitHub
parent a025d4ca99
commit 1c56f8ffad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 3 deletions

View file

@ -251,6 +251,27 @@ class TestIsInstanceIsSubclass(unittest.TestCase):
# blown
self.assertRaises(RecursionError, blowstack, isinstance, '', str)
def test_issubclass_refcount_handling(self):
# bpo-39382: abstract_issubclass() didn't hold item reference while
# peeking in the bases tuple, in the single inheritance case.
class A:
@property
def __bases__(self):
return (int, )
class B:
def __init__(self):
# setting this here increases the chances of exhibiting the bug,
# probably due to memory layout changes.
self.x = 1
@property
def __bases__(self):
return (A(), )
self.assertEqual(True, issubclass(B(), int))
def blowstack(fxn, arg, compare_to):
# Make sure that calling isinstance with a deeply nested tuple for its
# argument will raise RecursionError eventually.