mirror of
https://github.com/python/cpython.git
synced 2025-10-09 08:31:26 +00:00
bpo-28964: add line number of node (if available) to ast.literal_eval error messages (GH-23677)
This commit is contained in:
parent
bb70b2afe3
commit
586f3dbe15
3 changed files with 17 additions and 1 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue