MCPcopy
hub / github.com/rocky/python-uncompyle6 / detect_control_flow

Method detect_control_flow

uncompyle6/scanners/scanner30.py:29–470  ·  view source on GitHub ↗

Detect structures and their boundaries to fix optimized jumps Python 3.0 is more like Python 2.6 than it is Python 3.x. So we have a special routine here.

(self, offset, targets, inst_index)

Source from the content-addressed store, hash-verified

27 pass
28
29 def detect_control_flow(self, offset, targets, inst_index):
30 """
31 Detect structures and their boundaries to fix optimized jumps
32 Python 3.0 is more like Python 2.6 than it is Python 3.x.
33 So we have a special routine here.
34 """
35
36 code = self.code
37 op = self.insts[inst_index].opcode
38
39 # Detect parent structure
40 parent = self.structs[0]
41 start = parent["start"]
42 end = parent["end"]
43
44 # Pick innermost parent for our offset
45 for struct in self.structs:
46 current_start = struct["start"]
47 current_end = struct["end"]
48 if (current_start <= offset < current_end) and (
49 current_start >= start and current_end <= end
50 ):
51 start = current_start
52 end = current_end
53 parent = struct
54
55 if op == self.opc.SETUP_LOOP:
56 # We categorize loop types: 'for', 'while', 'while 1' with
57 # possibly suffixes '-loop' and '-else'
58 # Try to find the jump_back instruction of the loop.
59 # It could be a return instruction.
60
61 start += instruction_size(op, self.opc)
62 target = self.get_target(offset)
63 end = self.restrict_to_parent(target, parent)
64 self.setup_loops[target] = offset
65
66 if target != end:
67 self.fixed_jumps[offset] = end
68
69 (line_no, next_line_byte) = self.lines[offset]
70 jump_back = self.last_instr(
71 start, end, self.opc.JUMP_ABSOLUTE, next_line_byte, False
72 )
73
74 if jump_back:
75 jump_forward_offset = xdis.next_offset(
76 code[jump_back], self.opc, jump_back
77 )
78 else:
79 jump_forward_offset = None
80
81 return_val_offset1 = self.prev[self.prev[end]]
82
83 if (
84 jump_back
85 and jump_back != self.prev_op[end]
86 and self.is_jump_forward(jump_forward_offset)

Callers

nothing calls this directly

Calls 13

get_targetMethod · 0.80
restrict_to_parentMethod · 0.80
last_instrMethod · 0.80
next_offsetMethod · 0.80
is_jump_forwardMethod · 0.80
get_instMethod · 0.80
addMethod · 0.80
remove_mid_line_ifsMethod · 0.80
inst_matchesMethod · 0.80
is_jump_backMethod · 0.80
appendMethod · 0.45
rem_orMethod · 0.45

Tested by

no test coverage detected