Skip to content

Commit 7a7446d

Browse files
author
rocky
committed
small variable-name change
local Variable is_python36 changed to is_fixed_wordsize_bytecode. This clarifies *why* this distinction is importants. Also, add more annotations.
1 parent 73bcf41 commit 7a7446d

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

xdis/bytecode.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def get_const_info(const_index, const_list):
9292
_get_const_info = get_const_info
9393

9494

95-
def get_name_info(name_index, name_list):
95+
def get_name_info(name_index: int, name_list) -> tuple:
9696
"""Helper to get optional details about named references
9797
9898
Returns the dereferenced name as both value and repr if the name
@@ -189,7 +189,7 @@ def _parse_varint(iterator: Iterator[int]) -> int:
189189
)
190190

191191

192-
def parse_exception_table(exception_table: bytes):
192+
def parse_exception_table(exception_table: bytes) -> list:
193193
iterator = iter(exception_table)
194194
entries = []
195195
try:
@@ -248,15 +248,17 @@ def get_logical_instruction_at_offset(
248248
# PERFORMANCE FIX: Only add exception labels if we're building labels ourselves
249249
# When called from get_instructions_bytes, labels already includes exception targets
250250
if exception_entries is not None:
251-
for start, end, target, _, _ in exception_entries:
251+
for _start, _end, target, _, _ in exception_entries:
252252
if target not in labels:
253253
labels.append(target)
254254

255255
# label_maps = get_jump_target_maps(bytecode, opc)
256256

257257
# FIXME: We really need to distinguish 3.6.0a1 from 3.6.a3.
258-
# See below FIXME
259-
python_36 = True if opc.python_version >= (3, 6) else False
258+
# See below FIXME.
259+
# Python 3.6 starts fixed-length bytecode of 2 bytes per instruction.
260+
# Before that and initially, bytecode was either 1 or 3 bytes.
261+
is_fixed_wordsize_bytecode = True if opc.python_version >= (3, 6) else False
260262

261263
starts_line = None
262264

@@ -295,7 +297,7 @@ def get_logical_instruction_at_offset(
295297
argrepr = ""
296298
has_arg = op_has_argument(op, opc)
297299
if has_arg:
298-
if python_36:
300+
if is_fixed_wordsize_bytecode:
299301
arg = code2num(bytecode, i) | extended_arg
300302
extended_arg = (arg << 8) if opname == "EXTENDED_ARG" else 0
301303
# FIXME: Python 3.6.0a1 is 2, for 3.6.a3 we have 1
@@ -392,15 +394,18 @@ def get_logical_instruction_at_offset(
392394
argrepr = argval
393395
elif op in opc.NARGS_OPS:
394396
opname = opname
395-
if python_36 and opname in ("CALL_FUNCTION", "CALL_FUNCTION_EX"):
397+
if is_fixed_wordsize_bytecode and opname in (
398+
"CALL_FUNCTION",
399+
"CALL_FUNCTION_EX",
400+
):
396401
if opname == "CALL_FUNCTION":
397402
argrepr = format_CALL_FUNCTION(code2num(bytecode, i - 1))
398403
else:
399404
assert opname == "CALL_FUNCTION_EX"
400405
argrepr = format_CALL_FUNCTION_EX(code2num(bytecode, i - 1))
401406
else:
402407
if not (
403-
python_36
408+
is_fixed_wordsize_bytecode
404409
or opname in ("RAISE_VARARGS", "DUP_TOPX", "MAKE_FUNCTION")
405410
):
406411
argrepr = "%d positional, %d named" % (
@@ -410,7 +415,7 @@ def get_logical_instruction_at_offset(
410415
if hasattr(opc, "opcode_arg_fmt") and opname in opc.opcode_arg_fmt:
411416
argrepr = opc.opcode_arg_fmt[opname](arg)
412417
else:
413-
if python_36:
418+
if is_fixed_wordsize_bytecode:
414419
i += 1
415420
if hasattr(opc, "opcode_arg_fmt") and opname in opc.opcode_arg_fmt:
416421
argrepr = opc.opcode_arg_fmt[opname](arg)

0 commit comments

Comments
 (0)