Skip to content

Add configuration for blacklisted repr types (closes #77) #78

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 1 commit into from
Jul 15, 2018
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
39 changes: 29 additions & 10 deletions backslash/contrib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

from .._compat import PY2

try:
from slash import config as slash_config
except ImportError:
slash_config = None


_SPECIAL_DIRS = ('.git', '.hg', '.svn')

Expand Down Expand Up @@ -42,12 +47,23 @@ def _is_ipython_frame(frame):
return 'ipython-input' in frame['filename'] and frame['lineno'] == 1

def distill_object_attributes(obj, truncate=True):
repr_blacklisted_types = _get_repr_blacklisted_types()

return {attr: value if isinstance(value, _ALLOWED_ATTRIBUTE_TYPES) else _safe_repr(value, truncate=truncate)
return {attr: value
if isinstance(value, _ALLOWED_ATTRIBUTE_TYPES) else _safe_repr(value, repr_blacklisted_types, truncate=truncate)
for attr, value in _iter_distilled_object_attributes(obj)}

def _get_repr_blacklisted_types():
if slash_config is None:
return ()

log_config = slash_config.root.log
return getattr(log_config, 'repr_blacklisted_types', ())


def distill_slash_traceback(err, line_radius=5):
returned = _extract_frames(err.traceback)
repr_blacklisted_types = _get_repr_blacklisted_types()
returned = _extract_frames(err.traceback, repr_blacklisted_types)
for frame in returned:
if _is_ipython_frame(frame):
commands = frame['locals'].get('In', {}).get('value', None)
Expand All @@ -65,18 +81,18 @@ def distill_slash_traceback(err, line_radius=5):
frame['code_lines_before'], _, frame['code_lines_after'] = _splice_lines(lines, lineno - 1, line_radius)
return returned

def _extract_frames(traceback):
def _extract_frames(traceback, repr_blacklisted_types):
returned = traceback.to_list()
for frame, distilled_dict in zip(traceback.frames, returned):
if 'locals' not in distilled_dict and hasattr(frame, 'python_frame'): # Slash 1.5.x deprecates the `locals` and `globals` attributes
distilled_dict['locals'] = _capture_locals(frame.python_frame)
distilled_dict['globals'] = _capture_globals(frame.python_frame)
distilled_dict['locals'] = _capture_locals(frame.python_frame, repr_blacklisted_types)
distilled_dict['globals'] = _capture_globals(frame.python_frame, repr_blacklisted_types)
return returned

def _capture_locals(frame):
def _capture_locals(frame, repr_blacklisted_types):
if frame is None:
return None
return {local_name: {"value": _safe_repr(local_value)}
return {local_name: {"value": _safe_repr(local_value, repr_blacklisted_types)}
for key, value in frame.f_locals.items()
if "@" not in key
for local_name, local_value in _unwrap_object_variable(key, value)}
Expand Down Expand Up @@ -106,11 +122,11 @@ def _iter_distilled_object_attributes(obj):
continue
yield attr, value

def _capture_globals(frame):
def _capture_globals(frame, repr_blacklisted_types):
if frame is None:
return None
used_globals = set(frame.f_code.co_names)
return dict((global_name, {"value": _safe_repr(value)})
return dict((global_name, {"value": _safe_repr(value, repr_blacklisted_types)})
for global_name, value in frame.f_globals.items()
if global_name in used_globals and _is_global_included(value))

Expand Down Expand Up @@ -144,7 +160,10 @@ def _nested_assign(dictionary, key, value):
else:
dictionary = dictionary.setdefault(part, {})

def _safe_repr(value, truncate=True):
def _safe_repr(value, repr_blacklisted_types, truncate=True):
if isinstance(value, repr_blacklisted_types):
returned = "<{!r} object {:x}>".format(type(value).__name__, id(value))

try:
returned = repr(value)
except Exception: # pylint: disable=broad-except
Expand Down
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Changelog
=========

* :feature:`77` backslash-python now honors Slash's blacklist for performing ``repr`` on traceback distilling, if available
* :release:`2.36.0 <7-5-2018>`
* :feature:`72` Avoid reporting session_start once a session already exists
* :release:`2.35.0 <3-5-2018>`
Expand Down