@@ -1060,8 +1060,10 @@ def test_show_caches(self):
10601060 caches = list (self .get_cached_values (quickened , adaptive ))
10611061 for cache in caches :
10621062 self .assertRegex (cache , pattern )
1063- self .assertEqual (caches .count ("" ), 8 )
1064- self .assertEqual (len (caches ), 25 )
1063+ total_caches = 25
1064+ empty_caches = 8 if adaptive and quickened else total_caches
1065+ self .assertEqual (caches .count ("" ), empty_caches )
1066+ self .assertEqual (len (caches ), total_caches )
10651067
10661068
10671069class DisWithFileTests (DisTests ):
@@ -1629,6 +1631,36 @@ def test_co_positions_missing_info(self):
16291631 self .assertIsNone (positions .col_offset )
16301632 self .assertIsNone (positions .end_col_offset )
16311633
1634+ @requires_debug_ranges ()
1635+ def test_co_positions_with_lots_of_caches (self ):
1636+ def roots (a , b , c ):
1637+ d = b ** 2 - 4 * a * c
1638+ yield (- b - cmath .sqrt (d )) / (2 * a )
1639+ if d :
1640+ yield (- b + cmath .sqrt (d )) / (2 * a )
1641+ code = roots .__code__
1642+ ops = code .co_code [::2 ]
1643+ cache_opcode = opcode .opmap ["CACHE" ]
1644+ caches = sum (op == cache_opcode for op in ops )
1645+ non_caches = len (ops ) - caches
1646+ # Make sure we have "lots of caches". If not, roots should be changed:
1647+ assert 1 / 3 <= caches / non_caches , "this test needs more caches!"
1648+ for show_caches in (False , True ):
1649+ for adaptive in (False , True ):
1650+ with self .subTest (f"{ adaptive = } , { show_caches = } " ):
1651+ co_positions = [
1652+ positions
1653+ for op , positions in zip (ops , code .co_positions (), strict = True )
1654+ if show_caches or op != cache_opcode
1655+ ]
1656+ dis_positions = [
1657+ instruction .positions
1658+ for instruction in dis .get_instructions (
1659+ code , adaptive = adaptive , show_caches = show_caches
1660+ )
1661+ ]
1662+ self .assertEqual (co_positions , dis_positions )
1663+
16321664# get_instructions has its own tests above, so can rely on it to validate
16331665# the object oriented API
16341666class BytecodeTests (InstructionTestCase , DisTestBase ):
0 commit comments