Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] OWLoadClassifier: Show message on unpickling error #1752

Merged
merged 1 commit into from
Nov 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)