Skip to content

Commit a32170b

Browse files
authored
Merge pull request #1085 from douglasjacobsen/fix-ast-escape-warning
Add warning capture to ast.parse calls
2 parents 0480b2e + 17f8e37 commit a32170b

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

lib/ramble/ramble/expander.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import random
1313
import re
1414
import string
15+
import warnings
1516
from contextlib import contextmanager
1617
from typing import Dict, FrozenSet, List, Union
1718

@@ -805,15 +806,23 @@ def perform_math_eval(self, in_str):
805806
unmodified (if unsuccessful)
806807
807808
"""
808-
try:
809-
math_ast = ast.parse(in_str, mode="eval")
810-
out_str = self.eval_math(math_ast.body)
811-
return out_str
812-
except MathEvaluationError as e:
813-
logger.debug(f' Math input is: "{in_str}"')
814-
logger.debug(e)
815-
except RambleSyntaxError as e:
816-
raise RambleSyntaxError(f'{str(e)} in "{in_str}"')
809+
with warnings.catch_warnings(record=True) as wal:
810+
try:
811+
math_ast = ast.parse(in_str, mode="eval")
812+
out_str = self.eval_math(math_ast.body)
813+
return out_str
814+
except MathEvaluationError as e:
815+
logger.debug(f' Math input is: "{in_str}"')
816+
logger.debug(e)
817+
except RambleSyntaxError as e:
818+
raise RambleSyntaxError(f'{str(e)} in "{in_str}"')
819+
except Exception as e:
820+
logger.debug(f"ast.parse hit the following exception on input: {in_str}")
821+
logger.debug(f"{e}")
822+
823+
for warn in wal:
824+
if r"invalid escape sequence '\{'" not in str(warn.message):
825+
logger.warn(str(warn.message))
817826

818827
return in_str
819828

0 commit comments

Comments
 (0)