diff --git a/import_export/formats/base_formats.py b/import_export/formats/base_formats.py index fb71b11..7933760 100644 --- a/import_export/formats/base_formats.py +++ b/import_export/formats/base_formats.py @@ -164,6 +164,12 @@ class TSV(TextFormat): TABLIB_MODULE = 'tablib.formats._tsv' CONTENT_TYPE = 'text/tab-separated-values' + def create_dataset(self, in_stream, **kwargs): + if sys.version_info[0] < 3: + # python 2.7 csv does not do unicode + return super(TSV, self).create_dataset(in_stream.encode('utf-8'), **kwargs) + return super(TSV, self).create_dataset(in_stream, **kwargs) + class ODS(TextFormat): TABLIB_MODULE = 'tablib.formats._ods' diff --git a/tests/core/exports/books-mac.tsv b/tests/core/exports/books-mac.tsv new file mode 100644 index 0000000..1cc5de4 --- /dev/null +++ b/tests/core/exports/books-mac.tsv @@ -0,0 +1,2 @@ +id name author_email +1 Some book test@example.com diff --git a/tests/core/exports/books-unicode.tsv b/tests/core/exports/books-unicode.tsv new file mode 100644 index 0000000..e586724 --- /dev/null +++ b/tests/core/exports/books-unicode.tsv @@ -0,0 +1,2 @@ +id name author_email +1 Some bookš test@example.com diff --git a/tests/core/tests/test_base_formats.py b/tests/core/tests/test_base_formats.py index 8245436..3a59377 100644 --- a/tests/core/tests/test_base_formats.py +++ b/tests/core/tests/test_base_formats.py @@ -74,3 +74,31 @@ class CSVTest(TestCase): with open(filename, self.format.get_read_mode()) as in_stream: data = force_text(in_stream.read()) base_formats.CSV().create_dataset(data) + + +class TSVTest(TestCase): + + def setUp(self): + self.format = base_formats.TSV() + + def test_import_mac(self): + filename = os.path.join( + os.path.dirname(__file__), + os.path.pardir, + 'exports', + 'books-mac.tsv') + with open(filename, self.format.get_read_mode()) as in_stream: + actual = in_stream.read() + expected = 'id\tname\tauthor_email\n1\tSome book\ttest@example.com\n' + self.assertEqual(actual, expected) + + def test_import_unicode(self): + # importing tsv UnicodeEncodeError + filename = os.path.join( + os.path.dirname(__file__), + os.path.pardir, + 'exports', + 'books-unicode.tsv') + with open(filename, self.format.get_read_mode()) as in_stream: + data = force_text(in_stream.read()) + base_formats.TSV().create_dataset(data) diff --git a/tests/database.db b/tests/database.db index 29e644c..73a7485 100644 Binary files a/tests/database.db and b/tests/database.db differ