diff --git a/HISTORY.md b/HISTORY.md index a36732d..aaccbb0 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -6,6 +6,10 @@ - Dropped Python 2 support +### Bugfixes + +- Fixed a crash when exporting an empty string with the ReST format (#368) + ## 0.14.0 (2019-10-19) ### Deprecations diff --git a/src/tablib/formats/_rst.py b/src/tablib/formats/_rst.py index 27151a3..8b5cfa0 100644 --- a/src/tablib/formats/_rst.py +++ b/src/tablib/formats/_rst.py @@ -34,7 +34,7 @@ def _max_word_len(text): 8 """ - return max((len(word) for word in text.split())) + return max((len(word) for word in text.split())) if text else 0 def _get_column_string_lengths(dataset): diff --git a/tests/test_tablib.py b/tests/test_tablib.py index 04cba69..77f2ef6 100755 --- a/tests/test_tablib.py +++ b/tests/test_tablib.py @@ -486,17 +486,6 @@ class TablibTestCase(BaseTestCase): self.founders.append(('First\nSecond', 'Name', 42)) self.founders.export('xlsx') - def test_rst_force_grid(self): - data.append(self.john) - data.append(self.george) - data.headers = self.headers - - simple = tablib.formats._rst.export_set(data) - grid = tablib.formats._rst.export_set(data, force_grid=True) - self.assertNotEqual(simple, grid) - self.assertNotIn('+', simple) - self.assertIn('+', grid) - class HTMLTests(BaseTestCase): def test_html_export(self): @@ -538,6 +527,35 @@ class HTMLTests(BaseTestCase): self.assertEqual(html, d.html) +class RSTTests(BaseTestCase): + def test_rst_force_grid(self): + data = tablib.Dataset() + data.append(self.john) + data.append(self.george) + data.headers = self.headers + + simple = tablib.formats._rst.export_set(data) + grid = tablib.formats._rst.export_set(data, force_grid=True) + self.assertNotEqual(simple, grid) + self.assertNotIn('+', simple) + self.assertIn('+', grid) + + def test_empty_string(self): + data = tablib.Dataset() + data.headers = self.headers + data.append(self.john) + data.append(('Wendy', '', 43)) + self.assertEqual( + data.export('rst'), + '========== ========= ===\n' + 'first_name last_name gpa\n' + '========== ========= ===\n' + 'John Adams 90 \n' + 'Wendy 43 \n' + '========== ========= ===' + ) + + class CSVTests(BaseTestCase): def test_csv_format_detect(self): """Test CSV format detection."""