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)
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"""))
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))
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"""))
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"""))