From 8a46a259a36efc55bb0b2141acb9dd5ae52b7725 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Tue, 30 Nov 2021 09:19:51 -0800 Subject: [PATCH] add input type checks --- jc/parsers/ping_s.py | 8 +++++--- tests/test_ping_s.py | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/jc/parsers/ping_s.py b/jc/parsers/ping_s.py index 1f335c4e..68677fe8 100644 --- a/jc/parsers/ping_s.py +++ b/jc/parsers/ping_s.py @@ -468,14 +468,16 @@ def parse(data, raw=False, quiet=False, ignore_exceptions=False): """ s = _state() - 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.") for line in data: - output_line = {} try: + if not isinstance(line, str): raise TypeError("Input line must be a 'str' object.") + # skip blank lines if line.strip() == '': continue diff --git a/tests/test_ping_s.py b/tests/test_ping_s.py index 579fef3f..c1eaa63b 100644 --- a/tests/test_ping_s.py +++ b/tests/test_ping_s.py @@ -397,7 +397,7 @@ class MyTests(unittest.TestCase): """ Test 'ping' with no data """ - self.assertEqual(list(jc.parsers.ping_s.parse('', quiet=True)), []) + self.assertEqual(list(jc.parsers.ping_s.parse([], quiet=True)), []) def test_ping_s_unparsable(self): data = 'unparsable data'