Skip to content

Commit

Permalink
Use asttokens less to account for nodes that don't get position infor…
Browse files Browse the repository at this point in the history
…mation (#897)
  • Loading branch information
alexmojaki committed Oct 28, 2020
1 parent 7d2f2dc commit 881b8e1
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions sentry_sdk/integrations/pure_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,29 @@ def pure_eval_frame(frame):
expressions = evaluator.interesting_expressions_grouped(scope)

def closeness(expression):
# type: (Tuple[List[Any], Any]) -> int
# type: (Tuple[List[Any], Any]) -> Tuple[int, int]
# Prioritise expressions with a node closer to the statement executed
# without being after that statement
# A higher return value is better - the expression will appear
# earlier in the list of values and is less likely to be trimmed
nodes, _value = expression

def start(n):
# type: (ast.expr) -> Tuple[int, int]
return (n.lineno, n.col_offset)

nodes_before_stmt = [
node for node in nodes if node.first_token.startpos < stmt.last_token.endpos
node for node in nodes if start(node) < stmt.last_token.end
]
if nodes_before_stmt:
# The position of the last node before or in the statement
return max(node.first_token.startpos for node in nodes_before_stmt)
return max(start(node) for node in nodes_before_stmt)
else:
# The position of the first node after the statement
# Negative means it's always lower priority than nodes that come before
# Less negative means closer to the statement and higher priority
return -min(node.first_token.startpos for node in nodes)
lineno, col_offset = min(start(node) for node in nodes)
return (-lineno, -col_offset)

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

0 comments on commit 881b8e1

Please sign in to comment.