Skip to content

Tweak get_stack_trace() API - #1637

Merged
matthiask merged 3 commits into
django-commons:mainfrom
living180:stack_trace_api
Jun 1, 2022
Merged

Tweak get_stack_trace() API#1637
matthiask merged 3 commits into
django-commons:mainfrom
living180:stack_trace_api

Conversation

@living180

Copy link
Copy Markdown
Contributor

Replace the depth argument to get_stack_trace() with a more intuitive skip argument. Update the cache and SQL panels to supply a skip argument to get_stack_trace() to ensure that the stack traces are clean even when HIDE_IN_STACKTRACES is empty.

@matthiask

Copy link
Copy Markdown
Member

I'm not completely sure if skip is clearer than depth. Maybe skip_stack_levels but in this case we could copy the stacklevel name from warnings.warn?

@living180

Copy link
Copy Markdown
Contributor Author

How about skip_frames? I don't want to copy stacklevel because my skip argument has slightly different semantics. stacklevel from warnings.warn() and depth from sys._getframe() both default to 1, and the reference point is from the frame of warnings.warn() or sys._getframe(). But the caller doesn't care about the frames of the functions it is calling to get the trace. It wants either a trace starting with itself, or a lower frame if it (or possibly one or more of its callers) shouldn't be displayed to the user. That's why I switched to a default value of 0 for skip. When I first added the depth argument to the get_stack_trace() calls for the cache and SQL panels, it was depth=3, which felt confusing and I really had to think about it, and that was right when I was writing the code (not coming back and trying to understand it weeks or months later). Now with skip=2, the meaning seems clear to me: skip the current frame (which calls get_stack_trace()) plus one more frame (the caller of the current frame).

Does this make sense?

@matthiask matthiask left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, makes sense. Thanks for the explanation!

I think the change is fine as-is.

Comment thread debug_toolbar/utils.py Outdated
living180 added 2 commits June 1, 2022 16:16
Rename the `depth` argument to `skip` and change the semantics so that
now `skip=0` has the same meaning as `depth=1`.
Ensure that stack traces do not include any of the cache or SQL panel
tracking infrastructure even if HIDE_IN_STACKTRACES is empty.
@matthiask
matthiask merged commit e59a8ec into django-commons:main Jun 1, 2022
@living180
living180 deleted the stack_trace_api branch June 2, 2022 11:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants