Skip to content

Commit

Permalink
Merge pull request #1752 from janezd/loadclassifier-pickle-error
Browse files Browse the repository at this point in the history
[FIX] OWLoadClassifier: Show message on unpickling error
  • Loading branch information
kernc authored Nov 16, 2016
2 parents 8ea3088 + bde7ec7 commit 5d08c54
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
11 changes: 7 additions & 4 deletions Orange/widgets/classify/owloadclassifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand Down
32 changes: 32 additions & 0 deletions Orange/widgets/classify/tests/test_owloadclassifier.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 5d08c54

Please sign in to comment.