Skip to content
Closed
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
9 changes: 9 additions & 0 deletions src/tablib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,13 +364,22 @@ def _set_dict(self, pickle):
if isinstance(pickle[0], list):
self.wipe()
for row in pickle:
# only the first element's type is checked above, so a later
# row of a different type (e.g. a null item in a YAML list)
# would otherwise raise a bare TypeError.
if not isinstance(row, list):
raise UnsupportedFormat(error_details)
self.append(Row(row))

# if list of objects
elif isinstance(pickle[0], dict):
self.wipe()
self.headers = list(pickle[0].keys())
for row in pickle:
# guard against a later row that is not a dict, which would
# otherwise raise a bare AttributeError from row.values().
if not isinstance(row, dict):
raise UnsupportedFormat(error_details)
self.append(Row(list(row.values())))
else:
raise UnsupportedFormat(error_details)
Expand Down
8 changes: 8 additions & 0 deletions tests/test_tablib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1801,6 +1801,14 @@ def test_yaml_import_set(self):

self.assertEqual(_yaml, data.yaml)

def test_yaml_import_set_with_non_dict_row(self):
"""A YAML list whose first item is a dict but a later item is not
(e.g. a trailing bare ``-`` which parses to null) used to raise a bare
AttributeError from row.values(); it should raise UnsupportedFormat."""
_yaml = '- {id: 0, name: n0}\n- {id: 1, name: n1}\n-\n'
with self.assertRaises(UnsupportedFormat):
tablib.Dataset().load(_yaml, 'yaml')

def test_yaml_export(self):
"""YAML export"""

Expand Down