Evaluate an expression node or a string containing only a Python expression. The string or node provided may only consist of the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts, sets, booleans, and None. Caution: A complex expression can over
(node_or_string)
| 49 | |
| 50 | |
| 51 | def literal_eval(node_or_string): |
| 52 | """ |
| 53 | Evaluate an expression node or a string containing only a Python |
| 54 | expression. The string or node provided may only consist of the following |
| 55 | Python literal structures: strings, bytes, numbers, tuples, lists, dicts, |
| 56 | sets, booleans, and None. |
| 57 | |
| 58 | Caution: A complex expression can overflow the C stack and cause a crash. |
| 59 | """ |
| 60 | if isinstance(node_or_string, str): |
| 61 | node_or_string = parse(node_or_string.lstrip(" \t"), mode='eval').body |
| 62 | elif isinstance(node_or_string, Expression): |
| 63 | node_or_string = node_or_string.body |
| 64 | return _convert_literal(node_or_string) |
| 65 | |
| 66 | |
| 67 | def _convert_literal(node): |
no test coverage detected
searching dependent graphs…