diff --git a/bashlex/parser.py b/bashlex/parser.py index 6980ee75..d406d551 100644 --- a/bashlex/parser.py +++ b/bashlex/parser.py @@ -647,6 +647,8 @@ def parse(s, strictmode=True, expansionlimit=None, convertpos=False, proceedoner ''' p = _parser(s, strictmode=strictmode, expansionlimit=expansionlimit, proceedonerror=proceedonerror) parts = [p.parse()] + if parts[0] is None or isinstance(parts[0], str): + return [] class endfinder(ast.nodevisitor): def __init__(self): diff --git a/tests/test_parser.py b/tests/test_parser.py index edffe9ca..fd6f7bda 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -125,6 +125,22 @@ def assertASTsEquals(self, s, expectedlist, **parserargs): msg = 'ASTs not equal for %r\n\nresult:\n\n%s\n\n!=\n\nexpected:\n\n%s' % (s, result.dump(), expected.dump()) self.assertEqual(result, expected, msg) + def test_empty(self): + s = '' + self.assertASTsEquals(s, []) + + s = '\n' + self.assertASTsEquals(s, []) + + s = ' ' + self.assertASTsEquals(s, []) + + s = '# Comment' + self.assertASTsEquals(s, []) + + s = '# Comment\n# Another comment' + self.assertASTsEquals(s, []) + def test_command(self): s = 'a b c' self.assertASTEquals(s,