parse understands that strings are not macros (#315)

This commit is contained in:
Tobi 2022-02-26 13:44:38 +01:00 committed by GitHub
parent 216b9d730d
commit fcfc58287c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 0 deletions

View File

@ -421,6 +421,12 @@ def parse(macro, context=None, return_errors=False):
try: try:
macro_object = _parse_recurse(macro, context) macro_object = _parse_recurse(macro, context)
if not isinstance(macro_object, Macro):
# someone put a single parameter like a string into this function, and
# it was most likely returned without modification. Not a macro
raise ValueError("The provided code was not a macro")
return macro_object if not return_errors else None return macro_object if not return_errors else None
except Exception as error: except Exception as error:
logger.error('Failed to parse macro "%s": %s', macro, error.__repr__()) logger.error('Failed to parse macro "%s": %s', macro, error.__repr__())

View File

@ -360,6 +360,10 @@ class TestMacros(MacroTestBase):
self.assertIsNone(parse("r(a, k(b))", self.context)) self.assertIsNone(parse("r(a, k(b))", self.context))
self.assertIsNone(parse("m(a, b)", self.context)) self.assertIsNone(parse("m(a, b)", self.context))
# passing a string parameter. This is not a macro, even though
# it might look like it without the string quotes.
self.assertIsNone(parse('"m(a, b)"', self.context))
async def test_0(self): async def test_0(self):
macro = parse("k(1)", self.context) macro = parse("k(1)", self.context)
one_code = system_mapping.get("1") one_code = system_mapping.get("1")