77import random
88import string
99import sys
10+ import textwrap
1011import types
1112import unittest
1213import warnings
1516from copy import deepcopy
1617from contextlib import redirect_stdout
1718from test import support
19+ from test .support .script_helper import assert_python_ok
1820
1921try :
2022 import _testcapi
@@ -5230,6 +5232,7 @@ def test_type_lookup_mro_reference(self):
52305232 # Issue #14199: _PyType_Lookup() has to keep a strong reference to
52315233 # the type MRO because it may be modified during the lookup, if
52325234 # __bases__ is set during the lookup for example.
5235+ code = textwrap .dedent ("""
52335236 class MyKey(object):
52345237 def __hash__(self):
52355238 return hash('mykey')
@@ -5245,12 +5248,29 @@ class Base2(object):
52455248 mykey = 'from Base2'
52465249 mykey2 = 'from Base2'
52475250
5248- with self .assertWarnsRegex (RuntimeWarning , 'X' ):
5249- X = type ('X' , (Base ,), {MyKey (): 5 })
5250- # mykey is read from Base
5251- self .assertEqual (X .mykey , 'from Base' )
5252- # mykey2 is read from Base2 because MyKey.__eq__ has set __bases__
5253- self .assertEqual (X .mykey2 , 'from Base2' )
5251+ X = type('X', (Base,), {MyKey(): 5})
5252+
5253+ bases_before = ",".join([c.__name__ for c in X.__bases__])
5254+ print(f"before={bases_before}")
5255+
5256+ # mykey is initially read from Base, however, the lookup will be perfomed
5257+ # again if specialization fails. The second lookup will use the new
5258+ # mro set by __eq__.
5259+ print(X.mykey)
5260+
5261+ bases_after = ",".join([c.__name__ for c in X.__bases__])
5262+ print(f"after={bases_after}")
5263+
5264+ # mykey2 is read from Base2 because MyKey.__eq__ has set __bases_
5265+ print(f"mykey2={X.mykey2}")
5266+ """ )
5267+ _ , out , err = assert_python_ok ("-c" , code )
5268+ err = err .decode ()
5269+ self .assertRegex (err , "RuntimeWarning: .*X" )
5270+ out = out .decode ()
5271+ self .assertRegex (out , "before=Base" )
5272+ self .assertRegex (out , "after=Base2" )
5273+ self .assertRegex (out , "mykey2=from Base2" )
52545274
52555275
52565276class PicklingTests (unittest .TestCase ):
0 commit comments