From caf0a5c8713bf0d72951a2c6fd5a2c62c9eb59d3 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Tue, 30 Nov 2021 09:51:27 -0800 Subject: [PATCH] add input type checking --- jc/parsers/vmstat_s.py | 9 ++++++--- tests/test_vmstat_s.py | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/jc/parsers/vmstat_s.py b/jc/parsers/vmstat_s.py index 6697f082..abe8f0d8 100644 --- a/jc/parsers/vmstat_s.py +++ b/jc/parsers/vmstat_s.py @@ -145,10 +145,10 @@ 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.") - output_line = {} procs = None buff_cache = None disk = None @@ -156,7 +156,10 @@ def parse(data, raw=False, quiet=False, ignore_exceptions=False): tz = None 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_vmstat_s.py b/tests/test_vmstat_s.py index f8ccd04e..ec64bd31 100644 --- a/tests/test_vmstat_s.py +++ b/tests/test_vmstat_s.py @@ -75,7 +75,7 @@ class MyTests(unittest.TestCase): """ Test 'vmstat' with no data """ - self.assertEqual(list(jc.parsers.vmstat_s.parse('', quiet=True)), []) + self.assertEqual(list(jc.parsers.vmstat_s.parse([], quiet=True)), []) def test_vmstat_s_unparsable(self): data = 'unparsable data'