@@ -535,6 +535,7 @@ def test_code_equal_with_instrumentation(self):
535535 self .assertNotEqual (code1 , code2 )
536536 sys .settrace (None )
537537
538+ @unittest .skipUnless (ctypes , "requires ctypes" )
538539 @unittest .skipUnless (_testcapi , "requires _testcapi" )
539540 @unittest .skipUnless (_testinternalcapi , "requires _testinternalcapi" )
540541 def test_co_framesize_overflow (self ):
@@ -545,28 +546,17 @@ def foo(a, b):
545546 return x
546547
547548 c = foo .__code__
548- co_nlocalsplus = len ({* c .co_varnames , * c .co_cellvars , * c .co_freevars })
549- # co_framesize = co_stacksize + co_nlocalsplus + FRAME_SPECIALS_SIZE
550- co_framesize = _testinternalcapi .get_co_framesize (c )
551- FRAME_SPECIALS_SIZE = co_framesize - c .co_stacksize - co_nlocalsplus
552549
550+ fss = support .get_frame_specials_size ()
553551 ps = ctypes .sizeof (ctypes .c_void_p ) # sizeof(PyObject *)
554- smallest_evil_co_stacksize = (
555- (_testcapi .INT_MAX - co_nlocalsplus - FRAME_SPECIALS_SIZE ) // ps
556- )
552+ co_nlocalsplus = len ({* c .co_varnames , * c .co_cellvars , * c .co_freevars })
553+ # anything below that limit is a valid co_stacksize
554+ evil_stacksize = int (_testcapi .INT_MAX / ps - fss - co_nlocalsplus )
555+ self .assertLessEqual (evil_stacksize , _testcapi .INT_MAX // ps )
557556
558- for evil_co_stacksize in [
559- _testcapi .INT_MAX ,
560- _testcapi .INT_MAX // ps ,
561- smallest_evil_co_stacksize ,
562- ]:
563- with (
564- self .subTest (evil_co_stacksize ),
565- self .assertRaisesRegex (OverflowError , "co_stacksize" )
566- ):
567- c .__replace__ (co_stacksize = evil_co_stacksize )
568-
569- c .__replace__ (co_stacksize = smallest_evil_co_stacksize - 1 )
557+ with self .assertRaisesRegex (OverflowError , "co_stacksize" ):
558+ c .__replace__ (co_stacksize = evil_stacksize )
559+ c .__replace__ (co_stacksize = evil_stacksize - 1 )
570560
571561
572562def isinterned (s ):
0 commit comments