diff options
| author | Irit Katriel <iritkatriel@yahoo.com> | 2020-12-25 17:04:31 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-12-25 20:04:31 +0300 |
| commit | 586f3dbe15139cafb2a6ffb82cea146906561844 (patch) | |
| tree | 0de5a1236469ee1a791f11d378d2c65e52f45faa /Lib/ast.py | |
| parent | bb70b2afe39ad4334a9f3449cddd28149bd628b6 (diff) | |
| download | cpython-git-586f3dbe15139cafb2a6ffb82cea146906561844.tar.gz | |
bpo-28964: add line number of node (if available) to ast.literal_eval error messages (GH-23677)
Diffstat (limited to 'Lib/ast.py')
| -rw-r--r-- | Lib/ast.py | 5 |
1 files changed, 4 insertions, 1 deletions
diff --git a/Lib/ast.py b/Lib/ast.py index 7275fe28ba..845c80c2bb 100644 --- a/Lib/ast.py +++ b/Lib/ast.py @@ -63,7 +63,10 @@ def literal_eval(node_or_string): if isinstance(node_or_string, Expression): node_or_string = node_or_string.body def _raise_malformed_node(node): - raise ValueError(f'malformed node or string: {node!r}') + msg = "malformed node or string" + if lno := getattr(node, 'lineno', None): + msg += f' on line {lno}' + raise ValueError(msg + f': {node!r}') def _convert_num(node): if not isinstance(node, Constant) or type(node.value) not in (int, float, complex): _raise_malformed_node(node) |
