Skip to content

Commit

Permalink
fix #524 check for type list and improve error msg
Browse files Browse the repository at this point in the history
  • Loading branch information
llazzaro authored and claudep committed Jan 20, 2023
1 parent bbc2739 commit 0a3511f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/tablib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)

Expand Down
1 change: 1 addition & 0 deletions tests/files/issue_524.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
title: Voice of Miki Vanoušek
7 changes: 7 additions & 0 deletions tests/test_tablib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 0a3511f

Please sign in to comment.