From bde7ec77462903325ea266e781ded8e25758343d Mon Sep 17 00:00:00 2001 From: janezd Date: Mon, 14 Nov 2016 23:00:03 +0100 Subject: [PATCH] OWLoadClassifier: Show message about unpickling error --- Orange/widgets/classify/owloadclassifier.py | 11 ++++--- .../classify/tests/test_owloadclassifier.py | 32 +++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 Orange/widgets/classify/tests/test_owloadclassifier.py diff --git a/Orange/widgets/classify/owloadclassifier.py b/Orange/widgets/classify/owloadclassifier.py index 6b151a0807e..6ff02d580c9 100644 --- a/Orange/widgets/classify/owloadclassifier.py +++ b/Orange/widgets/classify/owloadclassifier.py @@ -12,6 +12,7 @@ from Orange.widgets.utils import stdpaths from Orange.widgets.classify import owsaveclassifier +from Orange.widgets.widget import Msg class OWLoadClassifier(widget.OWWidget): @@ -27,6 +28,9 @@ class OWLoadClassifier(widget.OWWidget): #: Current (last selected) filename or None. filename = Setting(None) + class Error(widget.OWWidget.Error): + load_error = Msg("An error occured while reading '{}'") + FILTER = owsaveclassifier.OWSaveClassifier.FILTER want_main_area = False @@ -96,11 +100,10 @@ def load(self, filename): """Load the object from filename and send it to output.""" try: classifier = pickle.load(open(filename, "rb")) - except pickle.UnpicklingError: - raise # TODO: error reporting - except os.error: - raise # TODO: error reporting + except (pickle.UnpicklingError, OSError, EOFError): + self.Error.load_error(os.path.split(filename)[-1]) else: + self.Error.load_error.clear() self._remember(filename) self.send("Classifier", classifier) diff --git a/Orange/widgets/classify/tests/test_owloadclassifier.py b/Orange/widgets/classify/tests/test_owloadclassifier.py new file mode 100644 index 00000000000..0c763e56536 --- /dev/null +++ b/Orange/widgets/classify/tests/test_owloadclassifier.py @@ -0,0 +1,32 @@ +# Test methods with long descriptive names can omit docstrings +# pylint: disable=missing-docstring +import os +import pickle +from tempfile import mkstemp + +from Orange.classification.majority import ConstantModel +from Orange.widgets.classify.owloadclassifier import OWLoadClassifier +from Orange.widgets.tests.base import WidgetTest + + +class TestOWMajority(WidgetTest): + def setUp(self): + self.widget = self.create_widget(OWLoadClassifier) + + def test_show_error(self): + self.widget.load("no-such-file.pckls") + self.assertTrue(self.widget.Error.load_error.is_shown()) + + clsf = ConstantModel([1, 1, 1]) + fd, fname = mkstemp(suffix='.pkcls') + try: + pickle.dump(clsf, open(fname, "wb")) + self.widget.load(fname) + self.assertFalse(self.widget.Error.load_error.is_shown()) + + open(fname, "w").write("X") + self.widget.load(fname) + self.assertTrue(self.widget.Error.load_error.is_shown()) + finally: + os.remove(fname) +