From 97ec355648eb1db3e6d1cfe69fc2e34e8e5f5631 Mon Sep 17 00:00:00 2001 From: astaric Date: Thu, 8 Sep 2016 21:30:09 +0200 Subject: [PATCH 1/2] CSVReader: Keep original exceptions stack trace --- Orange/data/io.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Orange/data/io.py b/Orange/data/io.py index c3d90308ef4..e491f251dd0 100644 --- a/Orange/data/io.py +++ b/Orange/data/io.py @@ -688,7 +688,7 @@ def read(self): except Exception as e: error = e continue - raise ValueError('Cannot parse dataset {}: {}'.format(self.filename, error)) + raise ValueError('Cannot parse dataset {}: {}'.format(self.filename, error)) from error @classmethod def write_file(cls, filename, data): From f04649a4e77df60de510b1598408233a4d02af51 Mon Sep 17 00:00:00 2001 From: astaric Date: Thu, 8 Sep 2016 21:55:03 +0200 Subject: [PATCH 2/2] Flags: Do not crash if attribute cannot be parsed Fixes a problem, where files containing paths as attribute values could not be read. Reading failed when the value of the attribute was parsed as a python literal. --- Orange/data/io.py | 10 +++++++--- Orange/tests/test_tab_reader.py | 11 +++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Orange/data/io.py b/Orange/data/io.py index e491f251dd0..c8942a87866 100644 --- a/Orange/data/io.py +++ b/Orange/data/io.py @@ -122,9 +122,13 @@ def __init__(self, flags): if self._RE_ALL.match(flag): if '=' in flag: k, v = flag.split('=', 1) - self.attributes[k] = (v if Flags._RE_ATTR_UNQUOTED_STR(v) else - literal_eval(v) if v else - '') + if not Flags._RE_ATTR_UNQUOTED_STR(v): + try: + v = literal_eval(v) + except SyntaxError: + # If parsing failed, treat value as string + pass + self.attributes[k] = v else: setattr(self, flag, True) setattr(self, self.ALL.get(flag, ''), True) diff --git a/Orange/tests/test_tab_reader.py b/Orange/tests/test_tab_reader.py index 56209c47e99..6a80217d17d 100644 --- a/Orange/tests/test_tab_reader.py +++ b/Orange/tests/test_tab_reader.py @@ -83,6 +83,17 @@ def test_read_and_save_attributes(self): self.assertEqual(c1.name, "Class 1") self.assertEqual(c1.attributes, {'x': 'a longer string'}) + path = "/path/to/somewhere" + c1.attributes["path"] = path + outf = io.StringIO() + outf.close = lambda: None + TabReader.write_file(outf, table) + outf.seek(0) + + table = read_tab_file(outf) + f1, f2, c1, c2 = table.domain.variables + self.assertEqual(c1.attributes["path"], path) + def test_read_data_oneline_header(self): samplefile = """\ data1\tdata2\tdata3