Skip to content

Commit

Permalink
errorreporting: Remove explicit canvas window parameter to ExceptHook
Browse files Browse the repository at this point in the history
Try to retrieve the workflow file via the OWWidget instance extracted
from the stack.
  • Loading branch information
ales-erjavec committed Dec 22, 2017
1 parent e9a5ecd commit 501442e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Orange/canvas/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ def show_message(message):
log.info("Entering main event loop.")
try:
with patch('sys.excepthook',
ExceptHook(stream=stderr, canvas=canvas_window,
ExceptHook(stream=stderr,
handledException=handle_exception)),\
patch('sys.stderr', stderr),\
patch('sys.stdout', stdout):
Expand Down
38 changes: 22 additions & 16 deletions Orange/canvas/application/errorreporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
)

from Orange.util import try_

from Orange.canvas.scheme import Scheme
try:
from Orange.widgets.widget import OWWidget
from Orange.version import full_version as VERSION_STR
Expand Down Expand Up @@ -179,7 +179,7 @@ def _post_report(data):
@patch('sys.excepthook', sys.__excepthook__) # Prevent recursion
@pyqtSlot(object)
def handle_exception(cls, exc):
(etype, evalue, tb), canvas = exc
etype, evalue, tb = exc
exception = traceback.format_exception_only(etype, evalue)[-1].strip()
stacktrace = ''.join(traceback.format_exception(etype, evalue, tb))

Expand All @@ -204,10 +204,19 @@ def _find_widget_frame(tb):
return tb
tb = tb.tb_next

widget_module, widget, frame = None, None, _find_widget_frame(tb)
if frame:
widget = frame.tb_frame.f_locals['self'].__class__
widget_module = '{}:{}'.format(widget.__module__, frame.tb_lineno)
widget_module = widget_class = widget = workflow = None
frame = _find_widget_frame(tb)
if frame is not None:
widget = frame.tb_frame.f_locals['self'] # type: OWWidget
widget_class = widget_class.__class__
widget_module = '{}:{}'.format(widget_class.__module__, frame.tb_lineno)
if widget is not None:
try:
workflow = widget.signalManager.parent()
if not isinstance(workflow, Scheme):
raise TypeError
except Exception:
workflow = None

packages = ', '.join(sorted(get_installed_distributions()))

Expand All @@ -218,7 +227,8 @@ def _find_widget_frame(tb):
if (err_module, widget_module) in cls._cache:
QMessageBox(QMessageBox.Warning, 'Error Encountered',
'Error encountered{}:<br><br><tt>{}</tt>'.format(
' in widget <b>{}</b>'.format(widget.name) if widget else '',
(' in widget <b>{}</b>'.format(widget_class.name)
if widget_class else ''),
stacktrace.replace('\n', '<br>').replace(' ', '&nbsp;')),
QMessageBox.Ignore).exec()
return
Expand All @@ -227,19 +237,15 @@ def _find_widget_frame(tb):
data = OrderedDict()
data[F.EXCEPTION] = exception
data[F.MODULE] = err_module
if widget:
data[F.WIDGET_NAME] = widget.name
if widget_class:
data[F.WIDGET_NAME] = widget_class.name
data[F.WIDGET_MODULE] = widget_module
if canvas:
if workflow is not None:
fd, filename = mkstemp(prefix='ows-', suffix='.ows.xml')
os.close(fd)
# Prevent excepthook printing the same exception when
# canvas tries to instantiate the broken widget again
with patch('sys.excepthook', lambda *_: None), \
open(filename, "wb") as f:
scheme = canvas.current_document().scheme()
with open(filename, "wb") as f:
try:
scheme.save_to(f, pretty=True, pickle_fallback=True)
workflow.save_to(f, pretty=True, pickle_fallback=True)
except Exception:
pass
data[F.WIDGET_SCHEME] = filename
Expand Down
5 changes: 2 additions & 3 deletions Orange/canvas/application/outputview.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,9 @@ def flush(self):
class ExceptHook(QObject):
handledException = Signal(object)

def __init__(self, parent=None, stream=None, canvas=None, **kwargs):
def __init__(self, parent=None, stream=None, **kwargs):
QObject.__init__(self, parent, **kwargs)
self._stream = stream
self._canvas = canvas

def __call__(self, exc_type, exc_value, tb):
if self._stream:
Expand All @@ -260,4 +259,4 @@ def __call__(self, exc_type, exc_value, tb):
text.append('-' * 79 + '\n')
self._stream.writelines(text)

self.handledException.emit(((exc_type, exc_value, tb), self._canvas))
self.handledException.emit((exc_type, exc_value, tb))

0 comments on commit 501442e

Please sign in to comment.