Skip to content

Commit e61b1e7

Browse files
committed
ref: Add include_source_context option
Some users do not like the source context to be there, and so add `include_source_context` option to opt-out. Fixes getsentry#2017
1 parent 7af9c8b commit e61b1e7

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

sentry_sdk/utils.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -594,8 +594,10 @@ def filename_for_module(module, abs_path):
594594
return abs_path
595595

596596

597-
def serialize_frame(frame, tb_lineno=None, include_local_variables=True):
598-
# type: (FrameType, Optional[int], bool) -> Dict[str, Any]
597+
def serialize_frame(
598+
frame, tb_lineno=None, include_local_variables=True, include_source_context=True
599+
):
600+
# type: (FrameType, Optional[int], bool, bool) -> Dict[str, Any]
599601
f_code = getattr(frame, "f_code", None)
600602
if not f_code:
601603
abs_path = None
@@ -611,18 +613,24 @@ def serialize_frame(frame, tb_lineno=None, include_local_variables=True):
611613
if tb_lineno is None:
612614
tb_lineno = frame.f_lineno
613615

614-
pre_context, context_line, post_context = get_source_context(frame, tb_lineno)
615-
616616
rv = {
617617
"filename": filename_for_module(module, abs_path) or None,
618618
"abs_path": os.path.abspath(abs_path) if abs_path else None,
619619
"function": function or "<unknown>",
620620
"module": module,
621621
"lineno": tb_lineno,
622-
"pre_context": pre_context,
623-
"context_line": context_line,
624-
"post_context": post_context,
625622
} # type: Dict[str, Any]
623+
624+
if include_source_context:
625+
pre_context, context_line, post_context = get_source_context(frame, tb_lineno)
626+
rv.update(
627+
{
628+
"pre_context": pre_context,
629+
"context_line": context_line,
630+
"post_context": post_context,
631+
}
632+
)
633+
626634
if include_local_variables:
627635
rv["vars"] = frame.f_locals
628636

@@ -1240,7 +1248,6 @@ def sanitize_url(url, remove_authority=True, remove_query_values=True):
12401248

12411249

12421250
def parse_url(url, sanitize=True):
1243-
12441251
# type: (str, bool) -> ParsedUrl
12451252
"""
12461253
Splits a URL into a url (including path), query and fragment. If sanitize is True, the query

0 commit comments

Comments
 (0)