Skip to content

Commit

Permalink
fix(er): include nonlocals in snapshots
Browse files Browse the repository at this point in the history
We include nonlocal variables in snapshots to provide for better
visibility into exception occurrences.
  • Loading branch information
P403n1x87 committed Jan 10, 2025
1 parent afe439e commit 01cff99
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
9 changes: 5 additions & 4 deletions ddtrace/debugging/_safety.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from inspect import CO_VARARGS
from inspect import CO_VARKEYWORDS
from itertools import chain
from types import FrameType
from typing import Any
from typing import Dict
Expand All @@ -23,11 +24,11 @@ def get_args(frame: FrameType) -> Iterator[Tuple[str, Any]]:

def get_locals(frame: FrameType) -> Iterator[Tuple[str, Any]]:
code = frame.f_code
_locals = frame.f_locals
nargs = code.co_argcount + bool(code.co_flags & CO_VARARGS) + bool(code.co_flags & CO_VARKEYWORDS)
names = code.co_varnames[nargs:]
values = (frame.f_locals.get(name) for name in names)

return zip(names, values)
return (
(name, _locals.get(name)) for name in chain(code.co_varnames[nargs:], code.co_freevars, code.co_cellvars)
) # include freevars and cellvars


def get_globals(frame: FrameType) -> Iterator[Tuple[str, Any]]:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
exception replay: include missing nonlocal variables in snapshot log messages.
20 changes: 20 additions & 0 deletions tests/debugging/exception/test_replay.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,23 @@ def c(foo=42):
self.assert_span_count(6)
# no new snapshots
assert len(uploader.collector.queue) == 3

def test_debugger_exception_in_closure(self):
def b():
with self.trace("b"):
nonloc = 4

def a(v):
if nonloc:
raise ValueError("hello", v)

a(nonloc)

with exception_replay() as uploader:
with with_rate_limiter(RateLimiter(limit_rate=1, raise_on_exceed=False)):
with pytest.raises(ValueError):
b()

assert all(
s.line_capture["locals"]["nonloc"] == {"type": "int", "value": "4"} for s in uploader.collector.queue
)

0 comments on commit 01cff99

Please sign in to comment.