5555# - dis displays bytes
5656OFFSET_AS_INSTRUCTION = sys .version_info >= (3 , 10 )
5757
58+ HAS_CONST = set (_opcode .hasconst )
59+ HAS_LOCAL = set (_opcode .haslocal )
60+ HAS_NAME = set (_opcode .hasname )
61+ HAS_FREE = set (_opcode .hasfree )
62+ HAS_COMPARE = set (_opcode .hascompare )
63+
5864
5965def _set_docstring (code : _bytecode .BaseBytecode , consts : Sequence ) -> None :
6066 if not consts :
@@ -458,9 +464,6 @@ def _assemble_lnotab(
458464 doff = 0
459465 dlineno -= 127
460466
461- assert 0 <= doff <= 255
462- assert - 128 <= dlineno <= 127
463-
464467 lnotab .append (struct .pack ("Bb" , doff , dlineno ))
465468
466469 return b"" .join (lnotab )
@@ -480,7 +483,6 @@ def _pack_linetable(
480483 linetable .append (struct .pack ("Bb" , 0 , 127 ))
481484 dlineno -= 127
482485
483- assert - 127 <= dlineno <= 127
484486 else :
485487 dlineno = - 128
486488
@@ -500,8 +502,6 @@ def _pack_linetable(
500502 else :
501503 linetable .append (struct .pack ("Bb" , doff , dlineno ))
502504
503- assert 0 <= doff <= 254
504-
505505 # Used on 3.10
506506 def _assemble_linestable (
507507 self ,
@@ -620,9 +620,6 @@ def _pack_location(
620620
621621 # We enforce the end_lineno to be defined
622622 else :
623- assert end_lineno is not None
624- assert end_col_offset is not None
625-
626623 # Short forms
627624 if (
628625 end_lineno == l_lineno
@@ -792,7 +789,6 @@ def _parse_varint(except_table_iterator: Iterator[int]) -> int:
792789 def _parse_exception_table (
793790 self , exception_table : bytes
794791 ) -> List [ExceptionTableEntry ]:
795- assert sys .version_info >= (3 , 11 )
796792 table = []
797793 iterator = iter (exception_table )
798794 try :
@@ -813,7 +809,6 @@ def _encode_varint(value: int, set_begin_marker: bool = False) -> Iterator[int]:
813809 # Encode value as a varint on 7 bits (MSB should come first) and set
814810 # the begin marker if requested.
815811 temp : List [int ] = []
816- assert value >= 0
817812 while value :
818813 temp .append (value & 63 | (64 if temp else 0 ))
819814 value >>= 6
@@ -949,7 +944,6 @@ def to_bytecode(
949944 for entry in self .exception_table :
950945 # Ensure we do not have more than one entry with identical starting
951946 # offsets
952- assert entry .start_offset not in ex_start
953947 ex_start [entry .start_offset ] = entry
954948 ex_end .setdefault (entry .stop_offset , []).append (entry )
955949
@@ -1018,33 +1012,35 @@ def to_bytecode(
10181012 # We are careful to first advance the offset and check that the CACHE
10191013 # is not a jump target. It should never be the case but we double check.
10201014 if prune_caches and c_instr .name == "CACHE" :
1021- assert jump_target is None
1015+ if jump_target is not None :
1016+ msg = "cache instruction cannot have jump target"
1017+ raise ValueError (msg )
10221018
10231019 # We may need to insert a TryEnd after a CACHE so we need to run the
10241020 # through the last block.
10251021 else :
10261022 arg : InstrArg
10271023 c_arg = c_instr .arg
10281024 # FIXME: better error reporting
1029- if c_instr .opcode in _opcode . hasconst :
1025+ if c_instr .opcode in HAS_CONST :
10301026 arg = self .consts [c_arg ]
1031- elif c_instr .opcode in _opcode . haslocal :
1027+ elif c_instr .opcode in HAS_LOCAL :
10321028 arg = self .varnames [c_arg ]
1033- elif c_instr .opcode in _opcode . hasname :
1029+ elif c_instr .opcode in HAS_NAME :
10341030 if c_instr .name in BITFLAG_INSTRUCTIONS :
10351031 arg = (bool (c_arg & 1 ), self .names [c_arg >> 1 ])
10361032 elif c_instr .name in BITFLAG2_INSTRUCTIONS :
10371033 arg = (bool (c_arg & 1 ), bool (c_arg & 2 ), self .names [c_arg >> 2 ])
10381034 else :
10391035 arg = self .names [c_arg ]
1040- elif c_instr .opcode in _opcode . hasfree :
1036+ elif c_instr .opcode in HAS_FREE :
10411037 if c_arg < ncells :
10421038 name = cells_lookup [c_arg ]
10431039 arg = CellVar (name )
10441040 else :
10451041 name = self .freevars [c_arg - ncells ]
10461042 arg = FreeVar (name )
1047- elif c_instr .opcode in _opcode . hascompare :
1043+ elif c_instr .opcode in HAS_COMPARE :
10481044 arg = Compare (
10491045 (c_arg >> 4 ) if sys .version_info >= (3 , 12 ) else c_arg
10501046 )
@@ -1120,7 +1116,6 @@ class _ConvertBytecodeToConcrete:
11201116 _compute_jumps_passes = 10
11211117
11221118 def __init__ (self , code : _bytecode .Bytecode ) -> None :
1123- assert isinstance (code , _bytecode .Bytecode )
11241119 self .bytecode = code
11251120
11261121 # temporary variables
@@ -1180,8 +1175,8 @@ def concrete_instructions(self) -> None:
11801175 ConcreteInstr (
11811176 "CACHE" , 0 , location = self .instructions [- 1 ].location
11821177 )
1183- for i in range (self .required_caches )
11841178 ]
1179+ * self .required_caches
11851180 )
11861181 self .required_caches = 0
11871182 self .seen_manual_cache = False
@@ -1201,7 +1196,6 @@ def concrete_instructions(self) -> None:
12011196
12021197 if isinstance (instr , TryBegin ):
12031198 # We expect the stack depth to have be provided or computed earlier
1204- assert instr .stack_depth is not UNSET
12051199 # NOTE here we store the index of the instruction at which the
12061200 # exception table entry starts. This is not the final value we want,
12071201 # we want the offset in the bytecode but that requires to compute
@@ -1235,43 +1229,28 @@ def concrete_instructions(self) -> None:
12351229 # fake value, real value is set in compute_jumps()
12361230 arg = 0
12371231 is_jump = True
1238- elif instr .opcode in _opcode . hasconst :
1232+ elif instr .opcode in HAS_CONST :
12391233 arg = self .add_const (arg )
1240- elif instr .opcode in _opcode . haslocal :
1234+ elif instr .opcode in HAS_LOCAL :
12411235 assert isinstance (arg , str )
12421236 arg = self .add (self .varnames , arg )
1243- elif instr .opcode in _opcode . hasname :
1237+ elif instr .opcode in HAS_NAME :
12441238 if instr .name in BITFLAG_INSTRUCTIONS :
1245- assert (
1246- isinstance (arg , tuple )
1247- and len (arg ) == 2
1248- and isinstance (arg [0 ], bool )
1249- and isinstance (arg [1 ], str )
1250- ), arg
12511239 index = self .add (self .names , arg [1 ])
12521240 arg = int (arg [0 ]) + (index << 1 )
12531241 elif instr .name in BITFLAG2_INSTRUCTIONS :
1254- assert (
1255- isinstance (arg , tuple )
1256- and len (arg ) == 3
1257- and isinstance (arg [0 ], bool )
1258- and isinstance (arg [1 ], bool )
1259- and isinstance (arg [2 ], str )
1260- ), arg
12611242 index = self .add (self .names , arg [2 ])
12621243 arg = int (arg [0 ]) + 2 * int (arg [1 ]) + (index << 2 )
12631244 else :
1264- assert isinstance (arg , str ), f"Got { arg } , expected a str"
12651245 arg = self .add (self .names , arg )
1266- elif instr .opcode in _opcode . hasfree :
1246+ elif instr .opcode in HAS_FREE :
12671247 if isinstance (arg , CellVar ):
12681248 cell_instrs .append (len (self .instructions ))
12691249 arg = self .bytecode .cellvars .index (arg .name )
12701250 else :
1271- assert isinstance (arg , FreeVar )
12721251 free_instrs .append (len (self .instructions ))
12731252 arg = self .bytecode .freevars .index (arg .name )
1274- elif instr .opcode in _opcode . hascompare :
1253+ elif instr .opcode in HAS_COMPARE :
12751254 if isinstance (arg , Compare ):
12761255 # In Python 3.12 the 4 lowest bits are used for caching
12771256 # See compare_masks in compile.c
@@ -1284,7 +1263,6 @@ def concrete_instructions(self) -> None:
12841263 arg = arg .value
12851264
12861265 # The above should have performed all the necessary conversion
1287- assert isinstance (arg , int )
12881266 c_instr = ConcreteInstr (instr .name , arg , location = instr .location )
12891267 if is_jump :
12901268 self .jumps .append ((len (self .instructions ), label , c_instr ))
0 commit comments