mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-06-17 00:07:37 +02:00
add input type checking
This commit is contained in:
@ -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
|
||||
|
@ -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'
|
||||
|
Reference in New Issue
Block a user