diff --git a/Orange/widgets/unsupervised/owdistancefile.py b/Orange/widgets/unsupervised/owdistancefile.py index bed14aada18..c851f90656a 100644 --- a/Orange/widgets/unsupervised/owdistancefile.py +++ b/Orange/widgets/unsupervised/owdistancefile.py @@ -124,6 +124,8 @@ def open_file(self): err = str(exc) self.Error.invalid_file(" \n"[len(err) > 40] + err) else: + # If you add any other checks before accepting the file, + # you should probably mirror them in canDropFile if distances.shape[0] != distances.shape[1]: self.Error.non_square_matrix() else: @@ -158,7 +160,12 @@ def parametersFromFile(self, path): return {"recent_paths": stored_recent_paths_prepend(self.WIDGET, r)} def canDropFile(self, path: str) -> bool: - return os.path.splitext(path)[1].lower() in (".dst", ".xlsx") + try: + distances = DistMatrix.from_file(path) + except Exception: # pylint: disable=broad-except + return False + else: + return distances.shape[0] == distances.shape[1] if __name__ == "__main__": # pragma: no cover diff --git a/Orange/widgets/unsupervised/tests/test_owdistancefile.py b/Orange/widgets/unsupervised/tests/test_owdistancefile.py index 9e6232d3e1c..0defa82a487 100644 --- a/Orange/widgets/unsupervised/tests/test_owdistancefile.py +++ b/Orange/widgets/unsupervised/tests/test_owdistancefile.py @@ -35,9 +35,13 @@ def test_nan_to_num(self): class TestOWDistanceFileDropHandler(unittest.TestCase): def test_canDropFile(self): + def fn(name): + return os.path.join(os.path.split(Orange.tests.__file__)[0], name) + handler = OWDistanceFileDropHandler() - self.assertTrue(handler.canDropFile("test.dst")) - self.assertTrue(handler.canDropFile("test.xlsx")) + self.assertTrue(handler.canDropFile(fn("xlsx_files/distances_with_nans.xlsx"))) + self.assertFalse(handler.canDropFile(fn("xlsx_files/distances_nonsquare.xlsx"))) + self.assertFalse(handler.canDropFile(fn("datasets/lenses.tab"))) self.assertFalse(handler.canDropFile("test.bin")) def test_parametersFromFile(self):