From 14247adb0ae007924ca551a706eb0cfdbae97a41 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Tue, 30 Nov 2021 10:03:59 -0800 Subject: [PATCH] add input type checks --- jc/parsers/csv_s.py | 7 ++++--- tests/test_csv_s.py | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/jc/parsers/csv_s.py b/jc/parsers/csv_s.py index acde6825..eecb7de3 100644 --- a/jc/parsers/csv_s.py +++ b/jc/parsers/csv_s.py @@ -48,7 +48,7 @@ from jc.exceptions import ParseError class info(): """Provides parser metadata (version, author, etc.)""" - version = '1.0' + version = '1.1' description = 'CSV file streaming parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -95,8 +95,9 @@ def parse(data, raw=False, quiet=False, ignore_exceptions=False): Iterator object """ - if not quiet: - jc.utils.compatibility(__name__, info.compatible) + if not quiet: jc.utils.compatibility(__name__, info.compatible) + if not hasattr(data, '__iter__') or isinstance(data, (str, bytes)): + raise TypeError("Input data must be a non-string iterable object.") # convert data to an iterable in case a sequence like a list is used as input. # this allows the exhaustion of the input so we don't double-process later. diff --git a/tests/test_csv_s.py b/tests/test_csv_s.py index 4de2aec6..5a9707ce 100644 --- a/tests/test_csv_s.py +++ b/tests/test_csv_s.py @@ -74,7 +74,7 @@ class MyTests(unittest.TestCase): """ Test CSV parser with no data """ - self.assertEqual(list(jc.parsers.csv_s.parse('', quiet=True)), []) + self.assertEqual(list(jc.parsers.csv_s.parse([], quiet=True)), []) def test_csv_unparsable(self): """