Skip to content

Commit

Permalink
Added warning traceback function
Browse files Browse the repository at this point in the history
  • Loading branch information
rgerkin committed May 8, 2019
1 parent b643584 commit 6a144e8
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion sciunit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import json
import re
import contextlib
import traceback
from io import TextIOWrapper, StringIO
from datetime import datetime
try:
Expand Down Expand Up @@ -47,6 +48,25 @@
'CWD': os.path.realpath(sciunit.__path__[0])}


def warn_with_traceback(message, category, filename, lineno, file=None, line=None):
"""A function to use with `warnings.showwarning` to show a traceback."""
log = file if hasattr(file,'write') else sys.stderr
traceback.print_stack(file=log)
log.write(warnings.formatwarning(message, category, filename, lineno, line))


def set_warnings_traceback(tb=True):
"""Set to `True` to give tracebacks for all warnings, or `False` to restore
default behavior."""
if tb:
warnings._showwarning = warnings.showwarning
warnings.showwarning = warn_with_traceback
warnings.simplefilter("always")
else:
warnings.showwarning = warnings._showwarning
warnings.simplefilter("default")


def rec_apply(func, n):
"""
Used to determine parent directory n levels up
Expand Down Expand Up @@ -153,7 +173,7 @@ def fix_display(self):
except ImportError:
pass
else:
print("Setting matplotlib backend to Agg")
printd("Setting matplotlib backend to Agg")
mpl.use('Agg')

def load_notebook(self, name):
Expand Down

0 comments on commit 6a144e8

Please sign in to comment.