mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
create NameConstant AST class for None, True, and False literals (closes #16619)
This commit is contained in:
parent
4b237e3b11
commit
442f20996d
15 changed files with 152 additions and 72 deletions
|
@ -42,7 +42,6 @@ def literal_eval(node_or_string):
|
|||
Python literal structures: strings, bytes, numbers, tuples, lists, dicts,
|
||||
sets, booleans, and None.
|
||||
"""
|
||||
_safe_names = {'None': None, 'True': True, 'False': False}
|
||||
if isinstance(node_or_string, str):
|
||||
node_or_string = parse(node_or_string, mode='eval')
|
||||
if isinstance(node_or_string, Expression):
|
||||
|
@ -61,9 +60,8 @@ def literal_eval(node_or_string):
|
|||
elif isinstance(node, Dict):
|
||||
return dict((_convert(k), _convert(v)) for k, v
|
||||
in zip(node.keys, node.values))
|
||||
elif isinstance(node, Name):
|
||||
if node.id in _safe_names:
|
||||
return _safe_names[node.id]
|
||||
elif isinstance(node, NameConstant):
|
||||
return node.value
|
||||
elif isinstance(node, UnaryOp) and \
|
||||
isinstance(node.op, (UAdd, USub)) and \
|
||||
isinstance(node.operand, (Num, UnaryOp, BinOp)):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue