Skip to content

Commit 0964e44

Browse files
untitakermitsuhiko
authored andcommitted
fix: Make abs_path always an abs path (#66)
1 parent c61c6bb commit 0964e44

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

sentry_sdk/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ def frame_from_traceback(tb, with_locals=True):
305305

306306
rv = {
307307
"filename": abs_path and os.path.basename(abs_path) or None,
308-
"abs_path": abs_path,
308+
"abs_path": os.path.abspath(abs_path),
309309
"function": function or "<unknown>",
310310
"module": module,
311311
"lineno": tb.tb_lineno,

tests/test_utils.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import sys
2+
import os
3+
14
from hypothesis import given, assume
25
import hypothesis.strategies as st
36

4-
from sentry_sdk.utils import safe_repr
7+
from sentry_sdk.utils import safe_repr, exceptions_from_error_tuple
58
from sentry_sdk._compat import text_type
69

710
any_string = st.one_of(st.binary(), st.text())
@@ -23,3 +26,23 @@ def test_safe_repr_never_leaves_escapes_in(x):
2326
r = safe_repr(x)
2427
assert isinstance(r, text_type)
2528
assert u"\\u" not in r and u"\\x" not in r
29+
30+
31+
def test_abs_path():
32+
"""Check if abs_path is actually an absolute path. This can happen either
33+
with eval/exec like here, or when the file in the frame is relative to
34+
__main__"""
35+
36+
code = compile("1/0", "test.py", "exec")
37+
try:
38+
exec(code, {})
39+
except Exception:
40+
exceptions = exceptions_from_error_tuple(sys.exc_info())
41+
42+
exception, = exceptions
43+
frames = exception["stacktrace"]["frames"]
44+
assert len(frames) == 2
45+
46+
for frame in frames:
47+
assert os.path.abspath(frame["abs_path"]) == frame["abs_path"]
48+
assert os.path.basename(frame["filename"]) == frame["filename"]

0 commit comments

Comments
 (0)