139139import unittest
140140import textwrap
141141import weakref
142+ import dis
142143
143144try :
144145 import ctypes
145146except ImportError :
146147 ctypes = None
147148from test .support import (cpython_only ,
148- check_impl_detail ,
149+ check_impl_detail , requires_debug_ranges ,
149150 gc_collect )
150151from test .support .script_helper import assert_python_ok
151152from test .support import threading_helper
@@ -165,9 +166,8 @@ def consts(t):
165166def dump (co ):
166167 """Print out a text representation of a code object."""
167168 for attr in ["name" , "argcount" , "posonlyargcount" ,
168- "kwonlyargcount" , "names" , "varnames" ,]:
169- # TODO: RUSTPYTHON
170- # "cellvars","freevars", "nlocals", "flags"]:
169+ "kwonlyargcount" , "names" , "varnames" ,
170+ "cellvars" , "freevars" , "nlocals" , "flags" ]:
171171 print ("%s: %s" % (attr , getattr (co , "co_" + attr )))
172172 print ("consts:" , tuple (consts (co .co_consts )))
173173
@@ -357,18 +357,6 @@ 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_invalid_bytecode (self ):
363- def foo (): pass
364- foo .__code__ = co = foo .__code__ .replace (co_code = b'\xee \x00 d\x00 S\x00 ' )
365-
366- with self .assertRaises (SystemError ) as se :
367- foo ()
368- self .assertEqual (
369- f"{ co .co_filename } :{ co .co_firstlineno } : unknown opcode 238" ,
370- str (se .exception ))
371-
372360 # TODO: RUSTPYTHON
373361 @unittest .expectedFailure
374362 # @requires_debug_ranges()
@@ -717,6 +705,38 @@ def test_lines(self):
717705 self .check_lines (misshappen )
718706 self .check_lines (bug93662 )
719707
708+ @cpython_only
709+ def test_code_new_empty (self ):
710+ # If this test fails, it means that the construction of PyCode_NewEmpty
711+ # needs to be modified! Please update this test *and* PyCode_NewEmpty,
712+ # so that they both stay in sync.
713+ def f ():
714+ pass
715+ PY_CODE_LOCATION_INFO_NO_COLUMNS = 13
716+ f .__code__ = f .__code__ .replace (
717+ co_firstlineno = 42 ,
718+ co_code = bytes (
719+ [
720+ dis .opmap ["RESUME" ], 0 ,
721+ dis .opmap ["LOAD_ASSERTION_ERROR" ], 0 ,
722+ dis .opmap ["RAISE_VARARGS" ], 1 ,
723+ ]
724+ ),
725+ co_linetable = bytes (
726+ [
727+ (1 << 7 )
728+ | (PY_CODE_LOCATION_INFO_NO_COLUMNS << 3 )
729+ | (3 - 1 ),
730+ 0 ,
731+ ]
732+ ),
733+ )
734+ self .assertRaises (AssertionError , f )
735+ self .assertEqual (
736+ list (f .__code__ .co_positions ()),
737+ 3 * [(42 , 42 , None , None )],
738+ )
739+
720740
721741if check_impl_detail (cpython = True ) and ctypes is not None :
722742 py = ctypes .pythonapi
@@ -811,6 +831,7 @@ def run(self):
811831 tt .join ()
812832 self .assertEqual (LAST_FREED , 500 )
813833
834+
814835def load_tests (loader , tests , pattern ):
815836 tests .addTest (doctest .DocTestSuite ())
816837 return tests
0 commit comments