예제 #1
0
def _user_syntax_error_string(source_code):
    try:
        ast.parse(source_code)
    except SyntaxError as e:
        return error_strings.user_syntax_error(e, source_code)
    else:
        raise AssertionError("Didn't fail to parse: %s" % source_code)
예제 #2
0
 def test_synthetic_error_long(self):
     """User-synthesized SyntaxErrors still give nice output."""
     self.assertEqual(
         error_strings.user_syntax_error(SyntaxError('message'), 'a\nb'),
         textwrap.dedent("""\
                      Failed to parse Python-like source code (message).
                          a
                          b"""))
예제 #3
0
def compile_matcher(user_input: str) -> matcher.Matcher:
    """Creates a :class:`~refex.python.matcher.Matcher` from a string."""
    user_input = textwrap.dedent(user_input).strip('\n')
    try:
        return semiliteral_eval.Eval(user_input,
                                     callables=_ALL_MATCHERS,
                                     constants=matcher.registered_constants)
    except SyntaxError as e:
        raise ValueError(error_strings.user_syntax_error(e, user_input))
예제 #4
0
 def test_synthetic_error_no_msg(self):
     self.assertEqual(
         error_strings.user_syntax_error(SyntaxError(''), 'a b'),
         textwrap.dedent("""\
                      Failed to parse Python-like source code (<unknown reason>).
                          a b"""))
예제 #5
0
 def test_synthetic_error_short(self):
     self.assertEqual(
         error_strings.user_syntax_error(SyntaxError('message'), 'a b'),
         textwrap.dedent("""\
                      Failed to parse Python-like source code (message).
                          a b"""))