From 0a3511fb9789b2e9f0d58739fccffde8db9f2635 Mon Sep 17 00:00:00 2001 From: Leonardo Lazzaro Date: Thu, 19 Jan 2023 20:47:38 +0100 Subject: [PATCH] fix #524 check for type list and improve error msg --- src/tablib/core.py | 9 +++++++-- tests/files/issue_524.yaml | 1 + tests/test_tablib.py | 7 +++++++ 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 tests/files/issue_524.yaml diff --git a/src/tablib/core.py b/src/tablib/core.py index be59ad2d..0a67570d 100644 --- a/src/tablib/core.py +++ b/src/tablib/core.py @@ -333,9 +333,14 @@ def _set_dict(self, pickle): data.dict = [{'age': 90, 'first_name': 'Kenneth', 'last_name': 'Reitz'}] """ - + error_details = "Please check format documentation https://tablib.readthedocs.io/en/stable/formats.html#yaml" if not len(pickle): return + + if not isinstance(pickle, list): + # sometimes pickle is a dict and len(pickle) returns True. + # since we access index 0 we should check if the type is list + raise UnsupportedFormat(error_details) # if list of rows if isinstance(pickle[0], list): @@ -350,7 +355,7 @@ def _set_dict(self, pickle): for row in pickle: self.append(Row(list(row.values()))) else: - raise UnsupportedFormat + raise UnsupportedFormat(error_details) dict = property(_get_dict, _set_dict) diff --git a/tests/files/issue_524.yaml b/tests/files/issue_524.yaml new file mode 100644 index 00000000..b0cf7100 --- /dev/null +++ b/tests/files/issue_524.yaml @@ -0,0 +1 @@ +title: Voice of Miki VanouĊĦek \ No newline at end of file diff --git a/tests/test_tablib.py b/tests/test_tablib.py index 8cc3bb60..9704845e 100755 --- a/tests/test_tablib.py +++ b/tests/test_tablib.py @@ -1229,6 +1229,13 @@ def test_yaml_export(self): output = self.founders.yaml self.assertEqual(output, expected) + def test_yaml_load(self): + """ test issue 524: invalid format """ + yaml_source = Path(__file__).parent / 'files' / 'issue_524.yaml' + with yaml_source.open(mode='rb') as fh: + with self.assertRaises(UnsupportedFormat): + tablib.Dataset().load(fh, 'yaml') + class LatexTests(BaseTestCase): def test_latex_export(self):