@@ -28,6 +28,13 @@ def wrapper(*args, **kwargs):
28
28
return wrapper
29
29
30
30
31
+ class TestBase (unittest .TestCase ):
32
+ def assert_specialized (self , f , opname ):
33
+ instructions = dis .get_instructions (f , adaptive = True )
34
+ opnames = {instruction .opname for instruction in instructions }
35
+ self .assertIn (opname , opnames )
36
+
37
+
31
38
class TestLoadSuperAttrCache (unittest .TestCase ):
32
39
def test_descriptor_not_double_executed_on_spec_fail (self ):
33
40
calls = []
@@ -479,7 +486,7 @@ def f():
479
486
self .assertFalse (f ())
480
487
481
488
482
- class TestCallCache (unittest . TestCase ):
489
+ class TestCallCache (TestBase ):
483
490
def test_too_many_defaults_0 (self ):
484
491
def f ():
485
492
pass
@@ -507,22 +514,40 @@ def f(x, y):
507
514
f (None )
508
515
f ()
509
516
517
+ @disabling_optimizer
518
+ @requires_specialization
519
+ def test_assign_init_code (self ):
520
+ class MyClass :
521
+ def __init__ (self ):
522
+ pass
523
+
524
+ def instantiate ():
525
+ return MyClass ()
526
+
527
+ # Trigger specialization
528
+ for _ in range (1025 ):
529
+ instantiate ()
530
+ self .assert_specialized (instantiate , "CALL_ALLOC_AND_ENTER_INIT" )
531
+
532
+ def count_args (self , * args ):
533
+ self .num_args = len (args )
534
+
535
+ # Set MyClass.__init__.__code__ to a code object that is incompatible
536
+ # (uses varargs) with the current specialization
537
+ MyClass .__init__ .__code__ = count_args .__code__
538
+ instantiate ()
539
+
510
540
511
541
@threading_helper .requires_working_threading ()
512
542
@requires_specialization
513
- class TestRacesDoNotCrash (unittest . TestCase ):
543
+ class TestRacesDoNotCrash (TestBase ):
514
544
# Careful with these. Bigger numbers have a higher chance of catching bugs,
515
545
# but you can also burn through a *ton* of type/dict/function versions:
516
546
ITEMS = 1000
517
547
LOOPS = 4
518
548
WARMUPS = 2
519
549
WRITERS = 2
520
550
521
- def assert_specialized (self , f , opname ):
522
- instructions = dis .get_instructions (f , adaptive = True )
523
- opnames = {instruction .opname for instruction in instructions }
524
- self .assertIn (opname , opnames )
525
-
526
551
@disabling_optimizer
527
552
def assert_races_do_not_crash (
528
553
self , opname , get_items , read , write , * , check_items = False
0 commit comments