diff --git a/sentry_sdk/utils.py b/sentry_sdk/utils.py index d5bd10addd..4c152484d1 100644 --- a/sentry_sdk/utils.py +++ b/sentry_sdk/utils.py @@ -305,7 +305,7 @@ def frame_from_traceback(tb, with_locals=True): rv = { "filename": abs_path and os.path.basename(abs_path) or None, - "abs_path": abs_path, + "abs_path": os.path.abspath(abs_path), "function": function or "", "module": module, "lineno": tb.tb_lineno, diff --git a/tests/test_utils.py b/tests/test_utils.py index 562c7ddd73..d272669d5a 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,7 +1,10 @@ +import sys +import os + from hypothesis import given, assume import hypothesis.strategies as st -from sentry_sdk.utils import safe_repr +from sentry_sdk.utils import safe_repr, exceptions_from_error_tuple from sentry_sdk._compat import text_type any_string = st.one_of(st.binary(), st.text()) @@ -23,3 +26,23 @@ def test_safe_repr_never_leaves_escapes_in(x): r = safe_repr(x) assert isinstance(r, text_type) assert u"\\u" not in r and u"\\x" not in r + + +def test_abs_path(): + """Check if abs_path is actually an absolute path. This can happen either + with eval/exec like here, or when the file in the frame is relative to + __main__""" + + code = compile("1/0", "test.py", "exec") + try: + exec(code, {}) + except Exception: + exceptions = exceptions_from_error_tuple(sys.exc_info()) + + exception, = exceptions + frames = exception["stacktrace"]["frames"] + assert len(frames) == 2 + + for frame in frames: + assert os.path.abspath(frame["abs_path"]) == frame["abs_path"] + assert os.path.basename(frame["filename"]) == frame["filename"]