Skip to content

Rename 'with_locals' to 'include_local_variables' #1924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion sentry_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,18 @@ def _get_options(*args, **kwargs):

for key, value in iteritems(options):
if key not in rv:
# Option "with_locals" was renamed to "include_local_variables"
if key == "with_locals":
msg = (
"Deprecated: The option 'with_locals' was renamed to 'include_local_variables'. "
"Please use 'include_local_variables'. The option 'with_locals' will be removed in the future."
)
logger.warning(msg)
rv["include_local_variables"] = value
continue

raise TypeError("Unknown option %r" % (key,))

rv[key] = value

if rv["dsn"] is None:
Expand Down Expand Up @@ -213,7 +224,7 @@ def _prepare_event(
"values": [
{
"stacktrace": current_stacktrace(
self.options["with_locals"]
self.options["include_local_variables"]
),
"crashed": False,
"current": True,
Expand Down
2 changes: 1 addition & 1 deletion sentry_sdk/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ class ClientConstructor(object):
def __init__(
self,
dsn=None, # type: Optional[str]
with_locals=True, # type: bool
max_breadcrumbs=DEFAULT_MAX_BREADCRUMBS, # type: int
release=None, # type: Optional[str]
environment=None, # type: Optional[str]
Expand Down Expand Up @@ -125,6 +124,7 @@ def __init__(
before_send_transaction=None, # type: Optional[TransactionProcessor]
project_root=None, # type: Optional[str]
enable_tracing=None, # type: Optional[bool]
include_local_variables=True, # type: Optional[bool]
trace_propagation_targets=[ # noqa: B006
MATCH_ALL
], # type: Optional[Sequence[str]]
Expand Down
2 changes: 1 addition & 1 deletion sentry_sdk/integrations/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def _emit(self, record):
"values": [
{
"stacktrace": current_stacktrace(
client_options["with_locals"]
client_options["include_local_variables"]
),
"crashed": False,
"current": True,
Expand Down
20 changes: 13 additions & 7 deletions sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ def filename_for_module(module, abs_path):
return abs_path


def serialize_frame(frame, tb_lineno=None, with_locals=True):
def serialize_frame(frame, tb_lineno=None, include_local_variables=True):
# type: (FrameType, Optional[int], bool) -> Dict[str, Any]
f_code = getattr(frame, "f_code", None)
if not f_code:
Expand Down Expand Up @@ -620,21 +620,23 @@ def serialize_frame(frame, tb_lineno=None, with_locals=True):
"context_line": context_line,
"post_context": post_context,
} # type: Dict[str, Any]
if with_locals:
if include_local_variables:
rv["vars"] = frame.f_locals

return rv


def current_stacktrace(with_locals=True):
def current_stacktrace(include_local_variables=True):
# type: (bool) -> Any
__tracebackhide__ = True
frames = []

f = sys._getframe() # type: Optional[FrameType]
while f is not None:
if not should_hide_frame(f):
frames.append(serialize_frame(f, with_locals=with_locals))
frames.append(
serialize_frame(f, include_local_variables=include_local_variables)
)
f = f.f_back

frames.reverse()
Expand Down Expand Up @@ -668,12 +670,16 @@ def single_exception_from_error_tuple(
)

if client_options is None:
with_locals = True
include_local_variables = True
else:
with_locals = client_options["with_locals"]
include_local_variables = client_options["include_local_variables"]

frames = [
serialize_frame(tb.tb_frame, tb_lineno=tb.tb_lineno, with_locals=with_locals)
serialize_frame(
tb.tb_frame,
tb_lineno=tb.tb_lineno,
include_local_variables=include_local_variables,
)
for tb in iter_stacks(tb)
]

Expand Down
4 changes: 2 additions & 2 deletions tests/integrations/pure_eval/test_pure_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@


@pytest.mark.parametrize("integrations", [[], [PureEvalIntegration()]])
def test_with_locals_enabled(sentry_init, capture_events, integrations):
sentry_init(with_locals=True, integrations=integrations)
def test_include_local_variables_enabled(sentry_init, capture_events, integrations):
sentry_init(include_local_variables=True, integrations=integrations)
events = capture_events()

def foo():
Expand Down
52 changes: 47 additions & 5 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# coding: utf-8
import os
import json
import mock
import pytest
import subprocess
import sys
Expand All @@ -22,6 +23,7 @@
from sentry_sdk.transport import Transport
from sentry_sdk._compat import reraise, text_type, PY2
from sentry_sdk.utils import HAS_CHAINED_EXCEPTIONS
from sentry_sdk.utils import logger
from sentry_sdk.serializer import MAX_DATABAG_BREADTH
from sentry_sdk.consts import DEFAULT_MAX_BREADCRUMBS

Expand Down Expand Up @@ -291,8 +293,48 @@ def e(exc):
pytest.raises(EventCapturedError, lambda: e(ValueError()))


def test_with_locals_enabled(sentry_init, capture_events):
sentry_init(with_locals=True)
def test_with_locals_deprecation_enabled(sentry_init):
with mock.patch.object(logger, "warning", mock.Mock()) as fake_warning:
sentry_init(with_locals=True)

client = Hub.current.client
assert "with_locals" not in client.options
assert "include_local_variables" in client.options
assert client.options["include_local_variables"]

fake_warning.assert_called_once_with(
"Deprecated: The option 'with_locals' was renamed to 'include_local_variables'. Please use 'include_local_variables'. The option 'with_locals' will be removed in the future."
)


def test_with_locals_deprecation_disabled(sentry_init):
with mock.patch.object(logger, "warning", mock.Mock()) as fake_warning:
sentry_init(with_locals=False)

client = Hub.current.client
assert "with_locals" not in client.options
assert "include_local_variables" in client.options
assert not client.options["include_local_variables"]

fake_warning.assert_called_once_with(
"Deprecated: The option 'with_locals' was renamed to 'include_local_variables'. Please use 'include_local_variables'. The option 'with_locals' will be removed in the future."
)


def test_include_local_variables_deprecation(sentry_init):
with mock.patch.object(logger, "warning", mock.Mock()) as fake_warning:
sentry_init(include_local_variables=False)

client = Hub.current.client
assert "with_locals" not in client.options
assert "include_local_variables" in client.options
assert not client.options["include_local_variables"]

fake_warning.assert_not_called()


def test_include_local_variables_enabled(sentry_init, capture_events):
sentry_init(include_local_variables=True)
events = capture_events()
try:
1 / 0
Expand All @@ -307,8 +349,8 @@ def test_with_locals_enabled(sentry_init, capture_events):
)


def test_with_locals_disabled(sentry_init, capture_events):
sentry_init(with_locals=False)
def test_include_local_variables_disabled(sentry_init, capture_events):
sentry_init(include_local_variables=False)
events = capture_events()
try:
1 / 0
Expand Down Expand Up @@ -372,7 +414,7 @@ def bar():


def test_attach_stacktrace_enabled_no_locals(sentry_init, capture_events):
sentry_init(attach_stacktrace=True, with_locals=False)
sentry_init(attach_stacktrace=True, include_local_variables=False)
events = capture_events()

def foo():
Expand Down