|
| 1 | +from __future__ import absolute_import |
| 2 | + |
| 3 | +import ast |
| 4 | + |
| 5 | +from sentry_sdk import Hub |
| 6 | +from sentry_sdk._types import MYPY |
| 7 | +from sentry_sdk.integrations import Integration, DidNotEnable |
| 8 | +from sentry_sdk.scope import add_global_event_processor |
| 9 | +from sentry_sdk.utils import walk_exception_chain, iter_stacks |
| 10 | + |
| 11 | +if MYPY: |
| 12 | + from typing import Optional, Dict, Any |
| 13 | + from types import FrameType |
| 14 | + |
| 15 | + from sentry_sdk._types import Event, Hint |
| 16 | + |
| 17 | +try: |
| 18 | + import executing |
| 19 | +except ImportError: |
| 20 | + raise DidNotEnable("executing is not installed") |
| 21 | + |
| 22 | +try: |
| 23 | + import pure_eval |
| 24 | +except ImportError: |
| 25 | + raise DidNotEnable("pure_eval is not installed") |
| 26 | + |
| 27 | +try: |
| 28 | + # Used implicitly, just testing it's available |
| 29 | + import asttokens # noqa |
| 30 | +except ImportError: |
| 31 | + raise DidNotEnable("asttokens is not installed") |
| 32 | + |
| 33 | + |
| 34 | +class PureEvalIntegration(Integration): |
| 35 | + identifier = "pure_eval" |
| 36 | + |
| 37 | + @staticmethod |
| 38 | + def setup_once(): |
| 39 | + # type: () -> None |
| 40 | + |
| 41 | + @add_global_event_processor |
| 42 | + def add_executing_info(event, hint): |
| 43 | + # type: (Event, Optional[Hint]) -> Optional[Event] |
| 44 | + if Hub.current.get_integration(PureEvalIntegration) is None: |
| 45 | + return event |
| 46 | + |
| 47 | + if hint is None: |
| 48 | + return event |
| 49 | + |
| 50 | + exc_info = hint.get("exc_info", None) |
| 51 | + |
| 52 | + if exc_info is None: |
| 53 | + return event |
| 54 | + |
| 55 | + exception = event.get("exception", None) |
| 56 | + |
| 57 | + if exception is None: |
| 58 | + return event |
| 59 | + |
| 60 | + values = exception.get("values", None) |
| 61 | + |
| 62 | + if values is None: |
| 63 | + return event |
| 64 | + |
| 65 | + for exception, (_exc_type, _exc_value, exc_tb) in zip( |
| 66 | + reversed(values), walk_exception_chain(exc_info) |
| 67 | + ): |
| 68 | + sentry_frames = [ |
| 69 | + frame |
| 70 | + for frame in exception.get("stacktrace", {}).get("frames", []) |
| 71 | + if frame.get("function") |
| 72 | + ] |
| 73 | + tbs = list(iter_stacks(exc_tb)) |
| 74 | + if len(sentry_frames) != len(tbs): |
| 75 | + continue |
| 76 | + |
| 77 | + for sentry_frame, tb in zip(sentry_frames, tbs): |
| 78 | + sentry_frame["vars"].update(pure_eval_frame(tb.tb_frame)) |
| 79 | + return event |
| 80 | + |
| 81 | + |
| 82 | +def pure_eval_frame(frame): |
| 83 | + # type: (FrameType) -> Dict[str, Any] |
| 84 | + source = executing.Source.for_frame(frame) |
| 85 | + if not source.tree: |
| 86 | + return {} |
| 87 | + |
| 88 | + statements = source.statements_at_line(frame.f_lineno) |
| 89 | + if not statements: |
| 90 | + return {} |
| 91 | + |
| 92 | + stmt = list(statements)[0] |
| 93 | + while True: |
| 94 | + # Get the parent first in case the original statement is already |
| 95 | + # a function definition, e.g. if we're calling a decorator |
| 96 | + # In that case we still want the surrounding scope, not that function |
| 97 | + stmt = stmt.parent |
| 98 | + if isinstance(stmt, (ast.FunctionDef, ast.ClassDef, ast.Module)): |
| 99 | + break |
| 100 | + |
| 101 | + evaluator = pure_eval.Evaluator.from_frame(frame) |
| 102 | + expressions = evaluator.interesting_expressions_grouped(stmt) |
| 103 | + atok = source.asttokens() |
| 104 | + return {atok.get_text(nodes[0]): value for nodes, value in expressions} |
0 commit comments