Skip to content

Commit 50f874b

Browse files
authored
fix: Make filename relative to sys.path (#121)
* fix: Make filename relative to sys.path * fix: Add suggestion from armin * fix: replace .pyc with .py
1 parent 69c084f commit 50f874b

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

sentry_sdk/utils.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,23 @@ def extract_locals(frame):
304304
return rv
305305

306306

307+
def filename_for_module(module, abs_path):
308+
try:
309+
if abs_path.endswith(".pyc"):
310+
abs_path = abs_path[:-1]
311+
312+
base_module = module.split(".", 1)[0]
313+
if base_module == module:
314+
return os.path.basename(abs_path)
315+
316+
base_module_path = sys.modules[base_module].__file__
317+
return abs_path.split(base_module_path.rsplit(os.sep, 2)[0], 1)[-1].lstrip(
318+
os.sep
319+
)
320+
except Exception:
321+
return abs_path
322+
323+
307324
def serialize_frame(frame, tb_lineno=None, with_locals=True):
308325
f_code = getattr(frame, "f_code", None)
309326
if f_code:
@@ -323,8 +340,8 @@ def serialize_frame(frame, tb_lineno=None, with_locals=True):
323340
pre_context, context_line, post_context = get_source_context(frame, tb_lineno)
324341

325342
rv = {
326-
"filename": abs_path and os.path.basename(abs_path) or None,
327-
"abs_path": os.path.abspath(abs_path),
343+
"filename": filename_for_module(module, abs_path) or None,
344+
"abs_path": os.path.abspath(abs_path) if abs_path else None,
328345
"function": function or "<unknown>",
329346
"module": module,
330347
"lineno": tb_lineno,

tests/test_utils.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
exceptions_from_error_tuple,
1313
format_and_strip,
1414
strip_string,
15+
filename_for_module,
1516
)
1617
from sentry_sdk._compat import text_type
1718

@@ -41,12 +42,13 @@ def test_abs_path():
4142
exceptions = exceptions_from_error_tuple(sys.exc_info())
4243

4344
exception, = exceptions
44-
frames = exception["stacktrace"]["frames"]
45-
assert len(frames) == 2
45+
frame1, frame2 = frames = exception["stacktrace"]["frames"]
4646

4747
for frame in frames:
4848
assert os.path.abspath(frame["abs_path"]) == frame["abs_path"]
49-
assert os.path.basename(frame["filename"]) == frame["filename"]
49+
50+
assert frame1["filename"] == "tests/test_utils.py"
51+
assert frame2["filename"] == "test.py"
5052

5153

5254
def test_non_string_variables():
@@ -96,3 +98,16 @@ def x(template, params):
9698
"len": 14,
9799
"rem": [["!limit", "x", 3, 6], ["!limit", "x", 9, 12]],
98100
}
101+
102+
103+
def test_filename():
104+
x = filename_for_module
105+
106+
assert x("bogus", "bogus") == "bogus"
107+
108+
assert x("os", os.__file__) == "os.py"
109+
assert x("pytest", pytest.__file__) == "pytest.py"
110+
111+
import sentry_sdk.utils
112+
113+
assert x("sentry_sdk.utils", sentry_sdk.utils.__file__) == "sentry_sdk/utils.py"

0 commit comments

Comments
 (0)