@@ -63,7 +63,6 @@ class InstructionWalker:
63
63
First, in strict sequence to visit all the instructions in the object.
64
64
This is `walk(follow_jumps=False)`. Second, we want to follow jumps to
65
65
understand how execution will flow: `walk(follow_jumps=True)`.
66
-
67
66
"""
68
67
69
68
def __init__ (self , code : CodeType ) -> None :
@@ -98,7 +97,8 @@ def walk(
98
97
offset += 2
99
98
100
99
101
- TBranchTrails = dict [TOffset , dict [Optional [TArc ], set [TOffset ]]]
100
+ TBranchTrailsOneSource = dict [Optional [TArc ], set [TOffset ]]
101
+ TBranchTrails = dict [TOffset , TBranchTrailsOneSource ]
102
102
103
103
104
104
def branch_trails (code : CodeType ) -> TBranchTrails :
@@ -117,7 +117,9 @@ def branch_trails(code: CodeType) -> TBranchTrails:
117
117
arc from the original instruction's line to the new source line.
118
118
119
119
"""
120
- the_trails : TBranchTrails = collections .defaultdict (lambda :collections .defaultdict (set ))
120
+ the_trails : TBranchTrails = collections .defaultdict (
121
+ lambda : collections .defaultdict (set )
122
+ )
121
123
iwalker = InstructionWalker (code )
122
124
for inst in iwalker .walk (follow_jumps = False ):
123
125
if not inst .jump_target :
@@ -131,7 +133,10 @@ def branch_trails(code: CodeType) -> TBranchTrails:
131
133
if from_line is None :
132
134
continue
133
135
134
- def walk_one_branch (start_at : TOffset ) -> tuple [Optional [TArc ], set [TOffset ]]:
136
+ def add_one_branch_trail (
137
+ trails : TBranchTrailsOneSource ,
138
+ start_at : TOffset ,
139
+ ) -> None :
135
140
# pylint: disable=cell-var-from-loop
136
141
inst_offsets : set [TOffset ] = set ()
137
142
to_line = None
@@ -146,17 +151,15 @@ def walk_one_branch(start_at: TOffset) -> tuple[Optional[TArc], set[TOffset]]:
146
151
to_line = - code .co_firstlineno
147
152
break
148
153
if to_line is not None :
149
- return (from_line , to_line ), inst_offsets
154
+ trails [ (from_line , to_line )]. update ( inst_offsets )
150
155
else :
151
- return None , set ()
156
+ trails [ None ] = set ()
152
157
153
158
# Calculate two trails: one from the next instruction, and one from the
154
159
# jump_target instruction.
155
- trails = collections .defaultdict (set )
156
- arc , offsets = walk_one_branch (start_at = inst .offset + 2 )
157
- trails [arc ].update (offsets )
158
- arc , offsets = walk_one_branch (start_at = inst .jump_target )
159
- trails [arc ].update (offsets )
160
+ trails : TBranchTrailsOneSource = collections .defaultdict (set )
161
+ add_one_branch_trail (trails , start_at = inst .offset + 2 )
162
+ add_one_branch_trail (trails , start_at = inst .jump_target )
160
163
the_trails [inst .offset ] = trails
161
164
162
165
# Sometimes we get BRANCH_RIGHT or BRANCH_LEFT events from instructions
0 commit comments