Permalink
Browse files

Show regex parse error messages from libc.c.

Also removes stderr spew from dev scripts -- fixes issue #32.
  • Loading branch information...
Andy Chu
Andy Chu committed Sep 2, 2018
1 parent a550217 commit 70941c5ebe2c00fdf6221e0d68c0fadfa5089ad9
Showing with 20 additions and 18 deletions.
  1. +3 −3 native/libc.c
  2. +11 −13 native/libc_test.py
  3. +5 −2 osh/bool_parse.py
  4. +1 −0 test/parse-errors.sh
View
@@ -227,9 +227,9 @@ func_regex_parse(PyObject *self, PyObject *args) {
}
if (err_str) {
// TODO: return a proper value?
fprintf(stderr, "Error compiling regex: %s\n", err_str);
Py_RETURN_FALSE;
// When the regex contains a variable, it can't be checked at compile-time.
PyErr_SetString(PyExc_RuntimeError, err_str);
return NULL;
} else {
Py_RETURN_TRUE;
}
View
@@ -16,7 +16,7 @@
class LibcTest(unittest.TestCase):
def testFnmatch(self):
print(dir(libc))
#print(dir(libc))
# pattern, string, result
cases = [
@@ -43,20 +43,20 @@ def testFnmatch(self):
self.assertEqual(expected, actual)
def testGlob(self):
print('GLOB')
print(libc.glob('*.py'))
# This will not match anything!
print(libc.glob('\\'))
# This one will match a file named \
print(libc.glob('\\\\'))
def testRegex(self):
#print(libc.regcomp(r'.*\.py'))
def testRegexParse(self):
self.assertEqual(True, libc.regex_parse(r'.*\.py'))
self.assertEqual(False, libc.regex_parse(r'*'))
self.assertEqual(False, libc.regex_parse('\\'))
self.assertEqual(False, libc.regex_parse('{'))
# Syntax errors
self.assertRaises(RuntimeError, libc.regex_parse, r'*')
self.assertRaises(RuntimeError, libc.regex_parse, '\\')
self.assertRaises(RuntimeError, libc.regex_parse, '{')
cases = [
('([a-z]+)([0-9]+)', 'foo123', ['foo123', 'foo', '123']),
@@ -66,17 +66,15 @@ def testRegex(self):
(r'bc', 'abcd', ['bc']),
# The match is unanchored
(r'.c', 'abcd', ['bc'])
]
]
for pat, s, expected in cases:
print('CASE %s' % pat)
#print('CASE %s' % pat)
actual = libc.regex_match(pat, s)
self.assertEqual(expected, actual)
# Syntax Error
self.assertRaises(
RuntimeError, libc.regex_match, r'*', 'abcd')
def testRegexMatch(self):
self.assertRaises(RuntimeError, libc.regex_match, r'*', 'abcd')
def testRegexFirstGroupMatch(self):
s='oXooXoooXoX'
View
@@ -225,8 +225,11 @@ def ParseFactor(self):
# TODO: Should raise exception with error?
# doesn't contain $foo, etc.
if ok and not libc.regex_parse(regex_str):
p_die("Invalid regex: %r" % regex_str, word=right)
if ok:
try:
libc.regex_parse(regex_str)
except RuntimeError as e:
p_die("Error parsing regex %r: %s", regex_str, e, word=right)
self._Next()
return ast.BoolBinary(op, left, right)
View
@@ -154,6 +154,7 @@ bool-expr() {
# Invalid regex
_error-case '[[ $var =~ * ]]'
_error-case '[[ $var =~ \\ ]]'
# Unbalanced parens
_error-case '[[ ( 1 == 2 - ]]'

0 comments on commit 70941c5

Please sign in to comment.