Skip to content

Stacktrace cleanups #1630

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 4 commits into from
May 29, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
32 changes: 7 additions & 25 deletions debug_toolbar/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import inspect
import os.path
import re
import sys
from importlib import import_module
from pprint import pformat
Expand Down Expand Up @@ -47,12 +46,11 @@ def omit_path(path):

def tidy_stacktrace(stack):
"""
Clean up stacktrace and remove all entries that:
1. Are part of Django (except contrib apps)
2. Are part of socketserver (used by Django's dev server)
3. Are the last entry (which is part of our stacktracing code)
Clean up stacktrace and remove all entries that are excluded by the
HIDE_IN_STACKTRACES setting.

``stack`` should be a list of frame tuples from ``inspect.stack()``
``stack`` should be a list of frame tuples from ``inspect.stack()`` or
``debug_toolbar.utils.get_stack()``.
"""
trace = []
for frame, path, line_no, func_name, text in (f[:5] for f in stack):
Expand Down Expand Up @@ -201,32 +199,16 @@ def getframeinfo(frame, context=1):
try:
lines, lnum = inspect.findsource(frame)
except Exception: # findsource raises platform-dependant exceptions
first_lines = lines = index = None
lines = index = None
else:
start = max(start, 1)
start = max(0, min(start, len(lines) - context))
first_lines = lines[:2]
lines = lines[start : (start + context)]
index = lineno - 1 - start
else:
first_lines = lines = index = None

# Code taken from Django's ExceptionReporter._get_lines_from_file
if first_lines and isinstance(first_lines[0], bytes):
encoding = "ascii"
for line in first_lines[:2]:
# File coding may be specified. Match pattern from PEP-263
# (https://www.python.org/dev/peps/pep-0263/)
match = re.search(rb"coding[:=]\s*([-\w.]+)", line)
if match:
encoding = match.group(1).decode("ascii")
break
lines = [line.decode(encoding, "replace") for line in lines]
lines = index = None

if hasattr(inspect, "Traceback"):
return inspect.Traceback(filename, lineno, frame.f_code.co_name, lines, index)
else:
return (filename, lineno, frame.f_code.co_name, lines, index)
return inspect.Traceback(filename, lineno, frame.f_code.co_name, lines, index)


def get_sorted_request_variable(variable):
Expand Down
1 change: 1 addition & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Change log
* Added pyflame (for flame graphs) to the list of third-party panels.
* Fixed the cache panel to correctly count cache misses from the get_many()
cache method.
* Removed some obsolete compatibility code from the stack trace recording code.

3.4.0 (2022-05-03)
------------------
Expand Down