Skip to content

Commit

Permalink
Merge pull request #356 from European-XFEL/editor-eval
Browse files Browse the repository at this point in the history
Handle get_context_file() throwing an exception in the editor
  • Loading branch information
JamesWrigley authored Nov 27, 2024
2 parents 8f1decb + 23a2595 commit 332408d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
21 changes: 18 additions & 3 deletions damnit/gui/editor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
import traceback
from enum import Enum
from io import StringIO
from pathlib import Path
Expand Down Expand Up @@ -26,12 +27,17 @@ class ContextFileCheckerThread(QThread):
# ContextTestResult, traceback, lineno, offset, checked_code
check_result = pyqtSignal(object, str, int, int, str)

def __init__(self, code, db_dir, context_python, parent=None):
def __init__(self, code, db_dir, context_python, context_getter, parent=None):
super().__init__(parent)
self.code = code
self.db_dir = db_dir
self.context_python = context_python

# This is a hack to allow us to test throwing an exception when checking
# the context file. It'll always be get_context_file() except when
# replaced with a Mock by the tests.
self.context_getter = context_getter

def run(self):
error_info = None

Expand All @@ -51,7 +57,16 @@ def run(self):
ctx_path = Path(ctx_file.name)
ctx_path.write_text(self.code)

ctx, error_info = get_context_file(ctx_path, self.context_python)
try:
ctx, error_info = self.context_getter(ctx_path, self.context_python)
except:
# Not a failure to evalute the context file, but a failure
# to *attempt* to evaluate the context file (e.g. because of
# a missing dependency).
help_msg = "# This is a partial error, please check the terminal for the full error message and ask for help from DA or the DOC."
traceback_str = f"{help_msg}\n\n{traceback.format_exc()}"
self.check_result.emit(ContextTestResult.ERROR, traceback_str, 0, 0, self.code)
return

if error_info is not None:
stacktrace, lineno, offset = error_info
Expand Down Expand Up @@ -110,7 +125,7 @@ def __init__(self):

def launch_test_context(self, db):
context_python = db.metameta.get("context_python")
thread = ContextFileCheckerThread(self.text(), db.path.parent, context_python, parent=self)
thread = ContextFileCheckerThread(self.text(), db.path.parent, context_python, get_context_file, parent=self)
thread.check_result.connect(self.on_test_result)
thread.finished.connect(thread.deleteLater)
thread.start()
Expand Down
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Fixed:
- Added back grid lines for plots of `DataArray`'s (!334).
- Fixed thumbnails of 2D `DataArray`'s to match what is displayed when the
variable is plotted (!355).
- Fixed crashes when the context file environment is missing dependencies (!356).

## [0.1.4]

Expand Down
10 changes: 10 additions & 0 deletions tests/test_gui.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
import os
import sys
import textwrap
from contextlib import contextmanager
from pathlib import Path
Expand Down Expand Up @@ -151,6 +152,15 @@ def test_editor(mock_db, mock_ctx, qtbot):
win.save_context()
assert ctx_path.read_text() == warning_code

# Throwing an exception when evaluating the context file in a different
# environment should be handled gracefully. This can happen if running the
# ctxrunner itself fails, e.g. because of a missing dependency.
db.metameta["context_python"] = sys.executable
with qtbot.waitSignal(editor.check_result) as sig, \
patch("damnit.gui.editor.get_context_file", side_effect=Exception("foo")):
editor.launch_test_context(db)
assert sig.args[0] == ContextTestResult.ERROR

def test_settings(mock_db_with_data, mock_ctx, tmp_path, monkeypatch, qtbot):
db_dir, db = mock_db_with_data
monkeypatch.chdir(db_dir)
Expand Down

0 comments on commit 332408d

Please sign in to comment.