139
139
import unittest
140
140
import textwrap
141
141
import weakref
142
+ import dis
142
143
143
144
try :
144
145
import ctypes
145
146
except ImportError :
146
147
ctypes = None
147
148
from test .support import (cpython_only ,
148
- check_impl_detail ,
149
+ check_impl_detail , requires_debug_ranges ,
149
150
gc_collect )
150
151
from test .support .script_helper import assert_python_ok
151
152
from test .support import threading_helper
@@ -165,9 +166,8 @@ def consts(t):
165
166
def dump (co ):
166
167
"""Print out a text representation of a code object."""
167
168
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" ]:
171
171
print ("%s: %s" % (attr , getattr (co , "co_" + attr )))
172
172
print ("consts:" , tuple (consts (co .co_consts )))
173
173
@@ -357,18 +357,6 @@ def func():
357
357
new_code = code = func .__code__ .replace (co_linetable = b'' )
358
358
self .assertEqual (list (new_code .co_lines ()), [])
359
359
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
-
372
360
# TODO: RUSTPYTHON
373
361
@unittest .expectedFailure
374
362
# @requires_debug_ranges()
@@ -717,6 +705,38 @@ def test_lines(self):
717
705
self .check_lines (misshappen )
718
706
self .check_lines (bug93662 )
719
707
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
+
720
740
721
741
if check_impl_detail (cpython = True ) and ctypes is not None :
722
742
py = ctypes .pythonapi
@@ -811,6 +831,7 @@ def run(self):
811
831
tt .join ()
812
832
self .assertEqual (LAST_FREED , 500 )
813
833
834
+
814
835
def load_tests (loader , tests , pattern ):
815
836
tests .addTest (doctest .DocTestSuite ())
816
837
return tests
0 commit comments