1919 Type ,
2020 TypeVar ,
2121 Union ,
22+ cast ,
2223)
2324
2425# alias to keep the 'bytecode' variable free
6768# - dis displays bytes
6869OFFSET_AS_INSTRUCTION = PY310
6970
71+ HAS_CONST = set (_opcode .hasconst )
72+ HAS_LOCAL = set (_opcode .haslocal )
73+ HAS_NAME = set (_opcode .hasname )
74+ HAS_FREE = set (_opcode .hasfree )
75+ HAS_COMPARE = set (_opcode .hascompare )
76+
7077
7178def _set_docstring (code : _bytecode .BaseBytecode , consts : Sequence ) -> None :
7279 if not consts :
@@ -478,9 +485,6 @@ def _assemble_lnotab(
478485 doff = 0
479486 dlineno -= 127
480487
481- assert 0 <= doff <= 255
482- assert - 128 <= dlineno <= 127
483-
484488 lnotab .append (struct .pack ("Bb" , doff , dlineno ))
485489
486490 return b"" .join (lnotab )
@@ -500,7 +504,6 @@ def _pack_linetable(
500504 linetable .append (struct .pack ("Bb" , 0 , 127 ))
501505 dlineno -= 127
502506
503- assert - 127 <= dlineno <= 127
504507 else :
505508 dlineno = - 128
506509
@@ -520,8 +523,6 @@ def _pack_linetable(
520523 else :
521524 linetable .append (struct .pack ("Bb" , doff , dlineno ))
522525
523- assert 0 <= doff <= 254
524-
525526 # Used on 3.10
526527 def _assemble_linestable (
527528 self ,
@@ -640,7 +641,6 @@ def _pack_location(
640641
641642 # We enforce the end_lineno to be defined
642643 else :
643- assert end_lineno is not None
644644 assert end_col_offset is not None
645645
646646 # Short forms
@@ -674,6 +674,8 @@ def _pack_location(
674674
675675 # Long form
676676 else :
677+ assert end_lineno is not None
678+
677679 packed .extend (
678680 (
679681 self ._pack_location_header (14 , size ),
@@ -812,7 +814,6 @@ def _parse_varint(except_table_iterator: Iterator[int]) -> int:
812814 def _parse_exception_table (
813815 self , exception_table : bytes
814816 ) -> List [ExceptionTableEntry ]:
815- assert PY311
816817 table = []
817818 iterator = iter (exception_table )
818819 try :
@@ -833,7 +834,6 @@ def _encode_varint(value: int, set_begin_marker: bool = False) -> Iterator[int]:
833834 # Encode value as a varint on 7 bits (MSB should come first) and set
834835 # the begin marker if requested.
835836 temp : List [int ] = []
836- assert value >= 0
837837 while value :
838838 temp .append (value & 63 | (64 if temp else 0 ))
839839 value >>= 6
@@ -967,7 +967,6 @@ def to_bytecode(
967967 for entry in self .exception_table :
968968 # Ensure we do not have more than one entry with identical starting
969969 # offsets
970- assert entry .start_offset not in ex_start
971970 ex_start [entry .start_offset ] = entry
972971 ex_end .setdefault (entry .stop_offset , []).append (entry )
973972
@@ -1046,7 +1045,9 @@ def to_bytecode(
10461045 # We are careful to first advance the offset and check that the CACHE
10471046 # is not a jump target. It should never be the case but we double check.
10481047 if prune_caches and c_instr .name == "CACHE" :
1049- assert jump_target is None
1048+ if jump_target is not None :
1049+ msg = "cache instruction cannot have jump target"
1050+ raise ValueError (msg )
10501051
10511052 # We may need to insert a TryEnd after a CACHE so we need to run the
10521053 # through the last block.
@@ -1055,14 +1056,14 @@ def to_bytecode(
10551056 arg : InstrArg
10561057 c_arg = c_instr .arg
10571058 # FIXME: better error reporting
1058- if opcode in _opcode . hasconst :
1059+ if opcode in HAS_CONST :
10591060 arg = self .consts [c_arg ]
10601061 elif opcode in _opcode .haslocal :
10611062 if opcode in DUAL_ARG_OPCODES :
10621063 arg = (locals_lookup [c_arg >> 4 ], locals_lookup [c_arg & 15 ])
10631064 else :
10641065 arg = locals_lookup [c_arg ]
1065- elif opcode in _opcode . hasname :
1066+ elif opcode in HAS_NAME :
10661067 if opcode in BITFLAG_OPCODES :
10671068 arg = (
10681069 bool (c_arg & 1 ),
@@ -1072,7 +1073,7 @@ def to_bytecode(
10721073 arg = (bool (c_arg & 1 ), bool (c_arg & 2 ), self .names [c_arg >> 2 ])
10731074 else :
10741075 arg = self .names [c_arg ]
1075- elif opcode in _opcode . hasfree :
1076+ elif opcode in HAS_FREE :
10761077 if c_arg < ncells :
10771078 n_or_cell = cells_lookup [c_arg ]
10781079 arg = (
@@ -1083,7 +1084,7 @@ def to_bytecode(
10831084 else :
10841085 name = self .freevars [c_arg - ncells ]
10851086 arg = FreeVar (name )
1086- elif opcode in _opcode . hascompare :
1087+ elif opcode in HAS_COMPARE :
10871088 arg = Compare (
10881089 (c_arg >> 5 ) + ((1 << 4 ) if (c_arg & 16 ) else 0 )
10891090 if PY313
@@ -1175,7 +1176,6 @@ class _ConvertBytecodeToConcrete:
11751176 _compute_jumps_passes = 10
11761177
11771178 def __init__ (self , code : _bytecode .Bytecode ) -> None :
1178- assert isinstance (code , _bytecode .Bytecode )
11791179 self .bytecode = code
11801180
11811181 # temporary variables
@@ -1248,8 +1248,8 @@ def concrete_instructions(self) -> None:
12481248 ConcreteInstr (
12491249 "CACHE" , 0 , location = self .instructions [- 1 ].location
12501250 )
1251- for i in range (self .required_caches )
12521251 ]
1252+ * self .required_caches
12531253 )
12541254 self .required_caches = 0
12551255 self .seen_manual_cache = False
@@ -1272,7 +1272,6 @@ def concrete_instructions(self) -> None:
12721272
12731273 if isinstance (instr , TryBegin ):
12741274 # We expect the stack depth to have be provided or computed earlier
1275- assert instr .stack_depth is not UNSET
12761275 # NOTE here we store the index of the instruction at which the
12771276 # exception table entry starts. This is not the final value we want,
12781277 # we want the offset in the bytecode but that requires to compute
@@ -1306,18 +1305,13 @@ def concrete_instructions(self) -> None:
13061305 # fake value, real value is set in compute_jumps()
13071306 c_arg = 0
13081307 is_jump = True
1309- elif opcode in _opcode . hasconst :
1308+ elif opcode in HAS_CONST :
13101309 c_arg = self .add_const (arg )
1311- elif opcode in _opcode . haslocal :
1310+ elif opcode in HAS_LOCAL :
13121311 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- )
1319- arg1_index = self .add (self .varnames , arg [0 ])
1320- arg2_index = self .add (self .varnames , arg [1 ])
1312+ _arg2 = cast (Tuple [str , str ], arg )
1313+ arg1_index = self .add (self .varnames , _arg2 [0 ])
1314+ arg2_index = self .add (self .varnames , _arg2 [1 ])
13211315 if arg1_index > 16 or arg2_index > 16 :
13221316 n1 , n2 = DUAL_ARG_OPCODES_SINGLE_OPS [opcode ]
13231317 c_instr = ConcreteInstr (n1 , arg1_index , location = location )
@@ -1335,7 +1329,7 @@ def concrete_instructions(self) -> None:
13351329 else :
13361330 assert isinstance (arg , str )
13371331 c_arg = self .add (self .varnames , arg )
1338- elif opcode in _opcode . hasname :
1332+ elif opcode in HAS_NAME :
13391333 if opcode in BITFLAG_OPCODES :
13401334 assert (
13411335 isinstance (arg , tuple )
@@ -1350,27 +1344,21 @@ def concrete_instructions(self) -> None:
13501344 assert False , arg # noqa
13511345 c_arg = int (arg [0 ]) + (index << 1 )
13521346 elif opcode in BITFLAG2_OPCODES :
1353- assert (
1354- isinstance (arg , tuple )
1355- and len (arg ) == 3
1356- and isinstance (arg [0 ], bool )
1357- and isinstance (arg [1 ], bool )
1358- and isinstance (arg [2 ], str )
1359- ), arg
1360- index = self .add (self .names , arg [2 ])
1361- c_arg = int (arg [0 ]) + 2 * int (arg [1 ]) + (index << 2 )
1347+ _arg3 = cast (Tuple [bool , bool , str ], arg )
1348+ index = self .add (self .names , _arg3 [2 ])
1349+ c_arg = int (_arg3 [0 ]) + 2 * int (_arg3 [1 ]) + (index << 2 )
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 :
13701358 assert isinstance (arg , FreeVar )
13711359 free_instrs .append (len (self .instructions ))
13721360 c_arg = self .bytecode .freevars .index (arg .name )
1373- elif opcode in _opcode . hascompare :
1361+ elif opcode in HAS_COMPARE :
13741362 if isinstance (arg , Compare ):
13751363 # In Python 3.13 the 4 lowest bits are used for caching
13761364 # and the 5th one indicate a cast to bool
0 commit comments