@@ -1197,8 +1197,10 @@ def test_show_caches(self):
1197
1197
caches = list (self .get_cached_values (quickened , adaptive ))
1198
1198
for cache in caches :
1199
1199
self .assertRegex (cache , pattern )
1200
- self .assertEqual (caches .count ("" ), 8 )
1201
- self .assertEqual (len (caches ), 22 )
1200
+ total_caches = 22
1201
+ empty_caches = 8 if adaptive and quickened else total_caches
1202
+ self .assertEqual (caches .count ("" ), empty_caches )
1203
+ self .assertEqual (len (caches ), total_caches )
1202
1204
1203
1205
1204
1206
class DisWithFileTests (DisTests ):
@@ -1751,6 +1753,36 @@ def test_co_positions_missing_info(self):
1751
1753
self .assertIsNone (positions .col_offset )
1752
1754
self .assertIsNone (positions .end_col_offset )
1753
1755
1756
+ @requires_debug_ranges ()
1757
+ def test_co_positions_with_lots_of_caches (self ):
1758
+ def roots (a , b , c ):
1759
+ d = b ** 2 - 4 * a * c
1760
+ yield (- b - cmath .sqrt (d )) / (2 * a )
1761
+ if d :
1762
+ yield (- b + cmath .sqrt (d )) / (2 * a )
1763
+ code = roots .__code__
1764
+ ops = code .co_code [::2 ]
1765
+ cache_opcode = opcode .opmap ["CACHE" ]
1766
+ caches = sum (op == cache_opcode for op in ops )
1767
+ non_caches = len (ops ) - caches
1768
+ # Make sure we have "lots of caches". If not, roots should be changed:
1769
+ assert 1 / 3 <= caches / non_caches , "this test needs more caches!"
1770
+ for show_caches in (False , True ):
1771
+ for adaptive in (False , True ):
1772
+ with self .subTest (f"{ adaptive = } , { show_caches = } " ):
1773
+ co_positions = [
1774
+ positions
1775
+ for op , positions in zip (ops , code .co_positions (), strict = True )
1776
+ if show_caches or op != cache_opcode
1777
+ ]
1778
+ dis_positions = [
1779
+ instruction .positions
1780
+ for instruction in dis .get_instructions (
1781
+ code , adaptive = adaptive , show_caches = show_caches
1782
+ )
1783
+ ]
1784
+ self .assertEqual (co_positions , dis_positions )
1785
+
1754
1786
# get_instructions has its own tests above, so can rely on it to validate
1755
1787
# the object oriented API
1756
1788
class BytecodeTests (InstructionTestCase , DisTestBase ):
0 commit comments