@@ -62,11 +62,12 @@ def literal_eval(node_or_string):
62
62
node_or_string = parse (node_or_string , mode = 'eval' )
63
63
if isinstance (node_or_string , Expression ):
64
64
node_or_string = node_or_string .body
65
+ def _raise_malformed_node (node ):
66
+ raise ValueError (f'malformed node or string: { node !r} ' )
65
67
def _convert_num (node ):
66
- if isinstance (node , Constant ):
67
- if type (node .value ) in (int , float , complex ):
68
- return node .value
69
- raise ValueError ('malformed node or string: ' + repr (node ))
68
+ if not isinstance (node , Constant ) or type (node .value ) not in (int , float , complex ):
69
+ _raise_malformed_node (node )
70
+ return node .value
70
71
def _convert_signed_num (node ):
71
72
if isinstance (node , UnaryOp ) and isinstance (node .op , (UAdd , USub )):
72
73
operand = _convert_num (node .operand )
@@ -88,6 +89,8 @@ def _convert(node):
88
89
node .func .id == 'set' and node .args == node .keywords == []):
89
90
return set ()
90
91
elif isinstance (node , Dict ):
92
+ if len (node .keys ) != len (node .values ):
93
+ _raise_malformed_node (node )
91
94
return dict (zip (map (_convert , node .keys ),
92
95
map (_convert , node .values )))
93
96
elif isinstance (node , BinOp ) and isinstance (node .op , (Add , Sub )):
0 commit comments