150150 gc_collect )
151151from test .support .script_helper import assert_python_ok
152152from test .support import threading_helper
153- from opcode import opmap
153+ from opcode import opmap , opname
154154COPY_FREE_VARS = opmap ['COPY_FREE_VARS' ]
155155
156156
@@ -357,6 +357,32 @@ def func():
357357 new_code = code = func .__code__ .replace (co_linetable = b'' )
358358 self .assertEqual (list (new_code .co_lines ()), [])
359359
360+ # TODO: RUSTPYTHON
361+ @unittest .expectedFailure
362+ def test_co_lnotab_is_deprecated (self ): # TODO: remove in 3.14
363+ def func ():
364+ pass
365+
366+ with self .assertWarns (DeprecationWarning ):
367+ func .__code__ .co_lnotab
368+
369+ # TODO: RUSTPYTHON
370+ @unittest .expectedFailure
371+ def test_invalid_bytecode (self ):
372+ def foo ():
373+ pass
374+
375+ # assert that opcode 229 is invalid
376+ self .assertEqual (opname [229 ], '<229>' )
377+
378+ # change first opcode to 0xeb (=229)
379+ foo .__code__ = foo .__code__ .replace (
380+ co_code = b'\xe5 ' + foo .__code__ .co_code [1 :])
381+
382+ msg = "unknown opcode 229"
383+ with self .assertRaisesRegex (SystemError , msg ):
384+ foo ()
385+
360386 # TODO: RUSTPYTHON
361387 @unittest .expectedFailure
362388 # @requires_debug_ranges()
@@ -479,6 +505,34 @@ def f():
479505 self .assertNotEqual (code_b , code_d )
480506 self .assertNotEqual (code_c , code_d )
481507
508+ def test_code_hash_uses_firstlineno (self ):
509+ c1 = (lambda : 1 ).__code__
510+ c2 = (lambda : 1 ).__code__
511+ self .assertNotEqual (c1 , c2 )
512+ self .assertNotEqual (hash (c1 ), hash (c2 ))
513+ c3 = c1 .replace (co_firstlineno = 17 )
514+ self .assertNotEqual (c1 , c3 )
515+ self .assertNotEqual (hash (c1 ), hash (c3 ))
516+
517+ def test_code_hash_uses_order (self ):
518+ # Swapping posonlyargcount and kwonlyargcount should change the hash.
519+ c = (lambda x , y , * , z = 1 , w = 1 : 1 ).__code__
520+ self .assertEqual (c .co_argcount , 2 )
521+ self .assertEqual (c .co_posonlyargcount , 0 )
522+ self .assertEqual (c .co_kwonlyargcount , 2 )
523+ swapped = c .replace (co_posonlyargcount = 2 , co_kwonlyargcount = 0 )
524+ self .assertNotEqual (c , swapped )
525+ self .assertNotEqual (hash (c ), hash (swapped ))
526+
527+ # TODO: RUSTPYTHON
528+ @unittest .expectedFailure
529+ def test_code_hash_uses_bytecode (self ):
530+ c = (lambda x , y : x + y ).__code__
531+ d = (lambda x , y : x * y ).__code__
532+ c1 = c .replace (co_code = d .co_code )
533+ self .assertNotEqual (c , c1 )
534+ self .assertNotEqual (hash (c ), hash (c1 ))
535+
482536
483537def isinterned (s ):
484538 return s is sys .intern (('_' + s + '_' )[1 :- 1 ])
@@ -692,7 +746,8 @@ def test_positions(self):
692746
693747 def check_lines (self , func ):
694748 co = func .__code__
695- lines1 = list (dedup (l for (_ , _ , l ) in co .co_lines ()))
749+ lines1 = [line for _ , _ , line in co .co_lines ()]
750+ self .assertEqual (lines1 , list (dedup (lines1 )))
696751 lines2 = list (lines_from_postions (positions_from_location_table (co )))
697752 for l1 , l2 in zip (lines1 , lines2 ):
698753 self .assertEqual (l1 , l2 )
@@ -714,6 +769,7 @@ def f():
714769 pass
715770 PY_CODE_LOCATION_INFO_NO_COLUMNS = 13
716771 f .__code__ = f .__code__ .replace (
772+ co_stacksize = 1 ,
717773 co_firstlineno = 42 ,
718774 co_code = bytes (
719775 [
@@ -742,15 +798,15 @@ def f():
742798 py = ctypes .pythonapi
743799 freefunc = ctypes .CFUNCTYPE (None ,ctypes .c_voidp )
744800
745- RequestCodeExtraIndex = py ._PyEval_RequestCodeExtraIndex
801+ RequestCodeExtraIndex = py .PyUnstable_Eval_RequestCodeExtraIndex
746802 RequestCodeExtraIndex .argtypes = (freefunc ,)
747803 RequestCodeExtraIndex .restype = ctypes .c_ssize_t
748804
749- SetExtra = py ._PyCode_SetExtra
805+ SetExtra = py .PyUnstable_Code_SetExtra
750806 SetExtra .argtypes = (ctypes .py_object , ctypes .c_ssize_t , ctypes .c_voidp )
751807 SetExtra .restype = ctypes .c_int
752808
753- GetExtra = py ._PyCode_GetExtra
809+ GetExtra = py .PyUnstable_Code_GetExtra
754810 GetExtra .argtypes = (ctypes .py_object , ctypes .c_ssize_t ,
755811 ctypes .POINTER (ctypes .c_voidp ))
756812 GetExtra .restype = ctypes .c_int
0 commit comments