6767# - dis displays bytes
6868OFFSET_AS_INSTRUCTION = PY310
6969
70+ HAS_CONST = set (_opcode .hasconst )
71+ HAS_LOCAL = set (_opcode .haslocal )
72+ HAS_NAME = set (_opcode .hasname )
73+ HAS_FREE = set (_opcode .hasfree )
74+ HAS_COMPARE = set (_opcode .hascompare )
75+
7076
7177def _set_docstring (code : _bytecode .BaseBytecode , consts : Sequence ) -> None :
7278 if not consts :
@@ -478,9 +484,6 @@ def _assemble_lnotab(
478484 doff = 0
479485 dlineno -= 127
480486
481- assert 0 <= doff <= 255
482- assert - 128 <= dlineno <= 127
483-
484487 lnotab .append (struct .pack ("Bb" , doff , dlineno ))
485488
486489 return b"" .join (lnotab )
@@ -500,7 +503,6 @@ def _pack_linetable(
500503 linetable .append (struct .pack ("Bb" , 0 , 127 ))
501504 dlineno -= 127
502505
503- assert - 127 <= dlineno <= 127
504506 else :
505507 dlineno = - 128
506508
@@ -520,8 +522,6 @@ def _pack_linetable(
520522 else :
521523 linetable .append (struct .pack ("Bb" , doff , dlineno ))
522524
523- assert 0 <= doff <= 254
524-
525525 # Used on 3.10
526526 def _assemble_linestable (
527527 self ,
@@ -640,9 +640,6 @@ def _pack_location(
640640
641641 # We enforce the end_lineno to be defined
642642 else :
643- assert end_lineno is not None
644- assert end_col_offset is not None
645-
646643 # Short forms
647644 if (
648645 end_lineno == l_lineno
@@ -812,7 +809,6 @@ def _parse_varint(except_table_iterator: Iterator[int]) -> int:
812809 def _parse_exception_table (
813810 self , exception_table : bytes
814811 ) -> List [ExceptionTableEntry ]:
815- assert PY311
816812 table = []
817813 iterator = iter (exception_table )
818814 try :
@@ -833,7 +829,6 @@ def _encode_varint(value: int, set_begin_marker: bool = False) -> Iterator[int]:
833829 # Encode value as a varint on 7 bits (MSB should come first) and set
834830 # the begin marker if requested.
835831 temp : List [int ] = []
836- assert value >= 0
837832 while value :
838833 temp .append (value & 63 | (64 if temp else 0 ))
839834 value >>= 6
@@ -967,7 +962,6 @@ def to_bytecode(
967962 for entry in self .exception_table :
968963 # Ensure we do not have more than one entry with identical starting
969964 # offsets
970- assert entry .start_offset not in ex_start
971965 ex_start [entry .start_offset ] = entry
972966 ex_end .setdefault (entry .stop_offset , []).append (entry )
973967
@@ -1046,7 +1040,9 @@ def to_bytecode(
10461040 # We are careful to first advance the offset and check that the CACHE
10471041 # is not a jump target. It should never be the case but we double check.
10481042 if prune_caches and c_instr .name == "CACHE" :
1049- assert jump_target is None
1043+ if jump_target is not None :
1044+ msg = "cache instruction cannot have jump target"
1045+ raise ValueError (msg )
10501046
10511047 # We may need to insert a TryEnd after a CACHE so we need to run the
10521048 # through the last block.
@@ -1055,14 +1051,14 @@ def to_bytecode(
10551051 arg : InstrArg
10561052 c_arg = c_instr .arg
10571053 # FIXME: better error reporting
1058- if opcode in _opcode . hasconst :
1054+ if opcode in HAS_CONST :
10591055 arg = self .consts [c_arg ]
10601056 elif opcode in _opcode .haslocal :
10611057 if opcode in DUAL_ARG_OPCODES :
10621058 arg = (locals_lookup [c_arg >> 4 ], locals_lookup [c_arg & 15 ])
10631059 else :
10641060 arg = locals_lookup [c_arg ]
1065- elif opcode in _opcode . hasname :
1061+ elif opcode in HAS_NAME :
10661062 if opcode in BITFLAG_OPCODES :
10671063 arg = (
10681064 bool (c_arg & 1 ),
@@ -1072,7 +1068,7 @@ def to_bytecode(
10721068 arg = (bool (c_arg & 1 ), bool (c_arg & 2 ), self .names [c_arg >> 2 ])
10731069 else :
10741070 arg = self .names [c_arg ]
1075- elif opcode in _opcode . hasfree :
1071+ elif opcode in HAS_FREE :
10761072 if c_arg < ncells :
10771073 n_or_cell = cells_lookup [c_arg ]
10781074 arg = (
@@ -1083,7 +1079,7 @@ def to_bytecode(
10831079 else :
10841080 name = self .freevars [c_arg - ncells ]
10851081 arg = FreeVar (name )
1086- elif opcode in _opcode . hascompare :
1082+ elif opcode in HAS_COMPARE :
10871083 arg = Compare (
10881084 (c_arg >> 5 ) + ((1 << 4 ) if (c_arg & 16 ) else 0 )
10891085 if PY313
@@ -1175,7 +1171,6 @@ class _ConvertBytecodeToConcrete:
11751171 _compute_jumps_passes = 10
11761172
11771173 def __init__ (self , code : _bytecode .Bytecode ) -> None :
1178- assert isinstance (code , _bytecode .Bytecode )
11791174 self .bytecode = code
11801175
11811176 # temporary variables
@@ -1248,8 +1243,8 @@ def concrete_instructions(self) -> None:
12481243 ConcreteInstr (
12491244 "CACHE" , 0 , location = self .instructions [- 1 ].location
12501245 )
1251- for i in range (self .required_caches )
12521246 ]
1247+ * self .required_caches
12531248 )
12541249 self .required_caches = 0
12551250 self .seen_manual_cache = False
@@ -1272,7 +1267,6 @@ def concrete_instructions(self) -> None:
12721267
12731268 if isinstance (instr , TryBegin ):
12741269 # We expect the stack depth to have be provided or computed earlier
1275- assert instr .stack_depth is not UNSET
12761270 # NOTE here we store the index of the instruction at which the
12771271 # exception table entry starts. This is not the final value we want,
12781272 # we want the offset in the bytecode but that requires to compute
@@ -1306,16 +1300,10 @@ def concrete_instructions(self) -> None:
13061300 # fake value, real value is set in compute_jumps()
13071301 c_arg = 0
13081302 is_jump = True
1309- elif opcode in _opcode . hasconst :
1303+ elif opcode in HAS_CONST :
13101304 c_arg = self .add_const (arg )
1311- elif opcode in _opcode . haslocal :
1305+ elif opcode in HAS_LOCAL :
13121306 if opcode in DUAL_ARG_OPCODES :
1313- assert (
1314- isinstance (arg , tuple )
1315- and len (arg ) == 2
1316- and isinstance (arg [0 ], str )
1317- and isinstance (arg [1 ], str )
1318- )
13191307 arg1_index = self .add (self .varnames , arg [0 ])
13201308 arg2_index = self .add (self .varnames , arg [1 ])
13211309 if arg1_index > 16 or arg2_index > 16 :
@@ -1335,7 +1323,7 @@ def concrete_instructions(self) -> None:
13351323 else :
13361324 assert isinstance (arg , str )
13371325 c_arg = self .add (self .varnames , arg )
1338- elif opcode in _opcode . hasname :
1326+ elif opcode in HAS_NAME :
13391327 if opcode in BITFLAG_OPCODES :
13401328 assert (
13411329 isinstance (arg , tuple )
@@ -1362,15 +1350,14 @@ def concrete_instructions(self) -> None:
13621350 else :
13631351 assert isinstance (arg , str ), f"Got { arg } , expected a str"
13641352 c_arg = self .add (self .names , arg )
1365- elif opcode in _opcode . hasfree :
1353+ elif opcode in HAS_FREE :
13661354 if isinstance (arg , CellVar ):
13671355 cell_instrs .append (len (self .instructions ))
13681356 c_arg = self .bytecode .cellvars .index (arg .name )
13691357 else :
1370- assert isinstance (arg , FreeVar )
13711358 free_instrs .append (len (self .instructions ))
13721359 c_arg = self .bytecode .freevars .index (arg .name )
1373- elif opcode in _opcode . hascompare :
1360+ elif opcode in HAS_COMPARE :
13741361 if isinstance (arg , Compare ):
13751362 # In Python 3.13 the 4 lowest bits are used for caching
13761363 # and the 5th one indicate a cast to bool
0 commit comments