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
|
Iterator object
|
||||||
"""
|
"""
|
||||||
if not quiet:
|
if not quiet: jc.utils.compatibility(__name__, info.compatible)
|
||||||
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
|
procs = None
|
||||||
buff_cache = None
|
buff_cache = None
|
||||||
disk = None
|
disk = None
|
||||||
@ -156,7 +156,10 @@ def parse(data, raw=False, quiet=False, ignore_exceptions=False):
|
|||||||
tz = None
|
tz = None
|
||||||
|
|
||||||
for line in data:
|
for line in data:
|
||||||
|
output_line = {}
|
||||||
try:
|
try:
|
||||||
|
if not isinstance(line, str): raise TypeError("Input line must be a 'str' object.")
|
||||||
|
|
||||||
# skip blank lines
|
# skip blank lines
|
||||||
if line.strip() == '':
|
if line.strip() == '':
|
||||||
continue
|
continue
|
||||||
|
@ -75,7 +75,7 @@ class MyTests(unittest.TestCase):
|
|||||||
"""
|
"""
|
||||||
Test 'vmstat' with no data
|
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):
|
def test_vmstat_s_unparsable(self):
|
||||||
data = 'unparsable data'
|
data = 'unparsable data'
|
||||||
|
Reference in New Issue
Block a user