Skip to content

Commit 881b8e1

Browse files
authored
Use asttokens less to account for nodes that don't get position information (getsentry#897)
1 parent 7d2f2dc commit 881b8e1

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

sentry_sdk/integrations/pure_eval.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,23 +104,29 @@ def pure_eval_frame(frame):
104104
expressions = evaluator.interesting_expressions_grouped(scope)
105105

106106
def closeness(expression):
107-
# type: (Tuple[List[Any], Any]) -> int
107+
# type: (Tuple[List[Any], Any]) -> Tuple[int, int]
108108
# Prioritise expressions with a node closer to the statement executed
109109
# without being after that statement
110110
# A higher return value is better - the expression will appear
111111
# earlier in the list of values and is less likely to be trimmed
112112
nodes, _value = expression
113+
114+
def start(n):
115+
# type: (ast.expr) -> Tuple[int, int]
116+
return (n.lineno, n.col_offset)
117+
113118
nodes_before_stmt = [
114-
node for node in nodes if node.first_token.startpos < stmt.last_token.endpos
119+
node for node in nodes if start(node) < stmt.last_token.end
115120
]
116121
if nodes_before_stmt:
117122
# The position of the last node before or in the statement
118-
return max(node.first_token.startpos for node in nodes_before_stmt)
123+
return max(start(node) for node in nodes_before_stmt)
119124
else:
120125
# The position of the first node after the statement
121126
# Negative means it's always lower priority than nodes that come before
122127
# Less negative means closer to the statement and higher priority
123-
return -min(node.first_token.startpos for node in nodes)
128+
lineno, col_offset = min(start(node) for node in nodes)
129+
return (-lineno, -col_offset)
124130

125131
# This adds the first_token and last_token attributes to nodes
126132
atok = source.asttokens()

0 commit comments

Comments
 (0)