Skip to content

Commit e203d6b

Browse files
author
rocky
committed
Adminisitrivia
Makefile: add ChangeLog-without-correction to facilitate diff changes black, 25.12.0 xdis/bytecode.py
1 parent b3bc2d7 commit e203d6b

File tree

2 files changed

+76
-20
lines changed

2 files changed

+76
-20
lines changed

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,17 @@ verbose-install:
127127
install:
128128
$(PYTHON) ./setup.py install >/dev/null
129129

130+
#: Remove ChangeLog
130131
rmChangeLog:
131132
rm ChangeLog || true
132133

134+
#: Create ChangeLog from version control without corrections
135+
ChangeLog-without-corrections:
136+
git log --pretty --numstat --summary | $(GIT2CL) >ChangeLog
137+
133138
#: Create a ChangeLog from git via git log and git2cl
134139
ChangeLog: rmChangeLog
135-
git log --pretty --numstat --summary | $(GIT2CL) >$@
140+
ChangeLog-without-corrections
136141
patch ChangeLog < ChangeLog-spell-corrected.diff
137142

138143
.PHONY: $(PHONY)

xdis/bytecode.py

Lines changed: 70 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,18 @@ def get_const_info(const_index, const_list):
7070
if const_list is not None:
7171
arg_val = const_list[const_index]
7272

73-
arg_repr = prefer_double_quote(repr(arg_val)) if isinstance(arg_val, str) else repr(arg_val)
73+
arg_repr = (
74+
prefer_double_quote(repr(arg_val))
75+
if isinstance(arg_val, str)
76+
else repr(arg_val)
77+
)
7478

7579
# Float values "nan" and "inf" are not directly representable in Python at least
7680
# before 3.5 and even there it is via a library constant.
7781
# So we will canonicalize their representation as float('nan') and float('inf')
78-
if isinstance(arg_val, float) and str(arg_val) in frozenset(["nan", "-nan", "inf", "-inf"]):
82+
if isinstance(arg_val, float) and str(arg_val) in frozenset(
83+
["nan", "-nan", "inf", "-inf"]
84+
):
7985
return arg_val, f"float('{arg_val}')"
8086
return arg_val, arg_repr
8187

@@ -172,7 +178,9 @@ def _parse_varint(iterator: Iterator[int]) -> int:
172178
return val
173179

174180

175-
_ExceptionTableEntry = collections.namedtuple("_ExceptionTableEntry", "start end target depth lasti")
181+
_ExceptionTableEntry = collections.namedtuple(
182+
"_ExceptionTableEntry", "start end target depth lasti"
183+
)
176184

177185

178186
def parse_exception_table(exception_table: bytes) -> list:
@@ -272,7 +280,9 @@ def get_logical_instruction_at_offset(
272280
i = offset
273281

274282
# create a localsplusnames table that resolves duplicates.
275-
localsplusnames = (varnames or tuple()) + tuple(name for name in (cells or tuple()) if name not in varnames)
283+
localsplusnames = (varnames or tuple()) + tuple(
284+
name for name in (cells or tuple()) if name not in varnames
285+
)
276286

277287
while i < n and last_op_was_extended_arg:
278288
op = code2num(bytecode, i)
@@ -299,7 +309,11 @@ def get_logical_instruction_at_offset(
299309
# FIXME: Python 3.6.0a1 is 2, for 3.6.a3 we have 1
300310
i += 1
301311
else:
302-
arg = code2num(bytecode, i) + code2num(bytecode, i + 1) * 0x100 + extended_arg
312+
arg = (
313+
code2num(bytecode, i)
314+
+ code2num(bytecode, i + 1) * 0x100
315+
+ extended_arg
316+
)
303317
i += 2
304318
extended_arg = arg * 0x10000 if opname == "EXTENDED_ARG" else 0
305319

@@ -400,7 +414,10 @@ def get_logical_instruction_at_offset(
400414
assert opname == "CALL_FUNCTION_EX"
401415
argrepr = format_CALL_FUNCTION_EX(code2num(bytecode, i - 1))
402416
else:
403-
if not (fixed_length_instructions or opname in ("RAISE_VARARGS", "DUP_TOPX", "MAKE_FUNCTION")):
417+
if not (
418+
fixed_length_instructions
419+
or opname in ("RAISE_VARARGS", "DUP_TOPX", "MAKE_FUNCTION")
420+
):
404421
argrepr = "%d positional, %d named" % (
405422
code2num(bytecode, i - 2),
406423
code2num(bytecode, i - 1),
@@ -465,9 +482,17 @@ def get_instructions_bytes(
465482
constants: tuple = code_object.co_consts
466483
names: tuple = code_object.co_names
467484
varnames: tuple = code_object.co_varnames
468-
cellvars: tuple = code_object.co_cellvars if hasattr(code_object, "co_cellvars") else tuple()
469-
exception_entries = code_object.exception_entries if hasattr(code_object, "exception_entries") else tuple()
470-
freevars: tuple = code_object.co_freevars if hasattr(code_object, "co_freevars") else tuple()
485+
cellvars: tuple = (
486+
code_object.co_cellvars if hasattr(code_object, "co_cellvars") else tuple()
487+
)
488+
exception_entries = (
489+
code_object.exception_entries
490+
if hasattr(code_object, "exception_entries")
491+
else tuple()
492+
)
493+
freevars: tuple = (
494+
code_object.co_freevars if hasattr(code_object, "co_freevars") else tuple()
495+
)
471496

472497
cells = cellvars + freevars
473498

@@ -521,7 +546,9 @@ class Bytecode:
521546
Iterating over these yields the bytecode operations as Instruction instances.
522547
"""
523548

524-
def __init__(self, x, opc, first_line=None, current_offset=None, dup_lines: bool = True) -> None:
549+
def __init__(
550+
self, x, opc, first_line=None, current_offset=None, dup_lines: bool = True
551+
) -> None:
525552
self.codeobj = co = get_code_object(x)
526553
self._line_offset = 0
527554
self._cell_names = tuple()
@@ -542,7 +569,11 @@ def __init__(self, x, opc, first_line=None, current_offset=None, dup_lines: bool
542569
self.opnames = opc.opname
543570
self.current_offset = current_offset
544571

545-
if opc.version_tuple >= (3, 11) and not opc.is_pypy and hasattr(co, "co_exceptiontable"):
572+
if (
573+
opc.version_tuple >= (3, 11)
574+
and not opc.is_pypy
575+
and hasattr(co, "co_exceptiontable")
576+
):
546577
self.exception_entries = parse_exception_table(co.co_exceptiontable)
547578
else:
548579
self.exception_entries = None
@@ -561,7 +592,9 @@ def from_traceback(cls, tb, opc=None):
561592
opc = get_opcode_module(sys.version_info, PYTHON_IMPLEMENTATION)
562593
while tb.tb_next:
563594
tb = tb.tb_next
564-
return cls(tb.tb_frame.f_code, opc=opc, first_line=None, current_offset=tb.tb_lasti)
595+
return cls(
596+
tb.tb_frame.f_code, opc=opc, first_line=None, current_offset=tb.tb_lasti
597+
)
565598

566599
def info(self) -> str:
567600
"""Return formatted information about the code object."""
@@ -647,7 +680,9 @@ def show_source_text(line_number: Optional[int]) -> None:
647680
if show_source and filename and line_number:
648681
source_text = getline(filename, line_number).lstrip()
649682
if source_text.startswith('"""'):
650-
source_text = get_docstring(filename, line_number + 1, source_text.rstrip())
683+
source_text = get_docstring(
684+
filename, line_number + 1, source_text.rstrip()
685+
)
651686
if source_text:
652687
file.write(" " * 13 + "# " + source_text)
653688

@@ -743,10 +778,18 @@ def show_source_text(line_number: Optional[int]) -> None:
743778
extended_arg_jump_target_offset = None
744779

745780
instructions.append(instr)
746-
new_source_line = show_lineno and (extended_arg_starts_line or instr.starts_line is not None and instr.offset >= 0)
781+
new_source_line = show_lineno and (
782+
extended_arg_starts_line
783+
or instr.starts_line is not None
784+
and instr.offset >= 0
785+
)
747786
if new_source_line:
748787
file.write("\n")
749-
show_source_text(extended_arg_starts_line if extended_arg_starts_line else instr.starts_line)
788+
show_source_text(
789+
extended_arg_starts_line
790+
if extended_arg_starts_line
791+
else instr.starts_line
792+
)
750793

751794
is_current_instr = instr.offset == lasti
752795

@@ -775,7 +818,9 @@ def show_source_text(line_number: Optional[int]) -> None:
775818
# currently we can't track names in this area, but instead use
776819
# locals and hope the two are the same.
777820
if instr.opname == "RESERVE_FAST":
778-
file.write("# Warning: subsequent LOAD_FAST and STORE_FAST after RESERVE_FAST are inaccurate here in Python before 1.5\n")
821+
file.write(
822+
"# Warning: subsequent LOAD_FAST and STORE_FAST after RESERVE_FAST are inaccurate here in Python before 1.5\n"
823+
)
779824
pass
780825
return instructions
781826

@@ -794,7 +839,9 @@ def get_instructions(self, x):
794839
return get_instructions_bytes(co, self.opc)
795840

796841

797-
def list2bytecode(inst_list: Iterable, opc, varnames: str, consts: Tuple[None, int]) -> bytes:
842+
def list2bytecode(
843+
inst_list: Iterable, opc, varnames: str, consts: Tuple[None, int]
844+
) -> bytes:
798845
"""Convert list/tuple of list/tuples to bytecode
799846
_names_ contains a list of name objects
800847
"""
@@ -803,15 +850,19 @@ def list2bytecode(inst_list: Iterable, opc, varnames: str, consts: Tuple[None, i
803850
opname = opcodes[0]
804851
operands = opcodes[1:]
805852
if opname not in opc.opname:
806-
raise TypeError("error at item %d [%s, %s], opcode not valid" % (i, opname, operands))
853+
raise TypeError(
854+
"error at item %d [%s, %s], opcode not valid" % (i, opname, operands)
855+
)
807856
opcode = opc.opmap[opname]
808857
bc.append(opcode)
809858
print(opname, operands)
810859
gen = (j for j in operands if operands)
811860
for j in gen:
812861
k = (consts if opcode in opc.CONST_OPS else varnames).index(j)
813862
if k == -1:
814-
raise TypeError(f"operand {i} [{opname}, {operands}], not found in names")
863+
raise TypeError(
864+
f"operand {i} [{opname}, {operands}], not found in names"
865+
)
815866
else:
816867
bc += num2code(k)
817868
pass

0 commit comments

Comments
 (0)