Skip to content

Fix varmap to apply func before recursion #1593

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 2 commits into from
Jul 22, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix varmap to apply func before recursion
  • Loading branch information
basepi committed Jul 21, 2022
commit 09c0f6c3f3876a06d3fbb2ab0d389e36853dfc27
8 changes: 6 additions & 2 deletions elasticapm/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,14 @@ def varmap(func, var, context=None, name=None, **kwargs):
return func(name, "<...>", **kwargs)
context.add(objid)
if isinstance(var, dict):
# Apply func() before recursion, so that `shorten()` doesn't have to iterate over all the trimmed values
ret = func(name, var, **kwargs)
# iterate over a copy of the dictionary to avoid "dictionary changed size during iteration" issues
ret = func(name, dict((k, varmap(func, v, context, k, **kwargs)) for k, v in var.copy().items()), **kwargs)
ret = dict((k, varmap(func, v, context, k, **kwargs)) for k, v in ret.copy().items())
elif isinstance(var, (list, tuple)):
ret = func(name, [varmap(func, f, context, name, **kwargs) for f in var], **kwargs)
# Apply func() before recursion, so that `shorten()` doesn't have to iterate over all the trimmed values
ret = func(name, var, **kwargs)
ret = [varmap(func, f, context, name, **kwargs) for f in ret]
else:
ret = func(name, var, **kwargs)
context.remove(objid)
Expand Down