Allow set literals in literal_eval().

This commit is contained in:
Georg Brandl 2010-07-11 09:41:21 +00:00
parent e40ee509e3
commit 492f3fc272
3 changed files with 10 additions and 3 deletions

View file

@ -119,12 +119,15 @@ and classes for traversing abstract syntax trees:
Safely evaluate an expression node or a string containing a Python Safely evaluate an expression node or a string containing a Python
expression. The string or node provided may only consist of the following expression. The string or node provided may only consist of the following
Python literal structures: strings, numbers, tuples, lists, dicts, booleans, Python literal structures: strings, numbers, tuples, lists, dicts, sets,
and ``None``. booleans, and ``None``.
This can be used for safely evaluating strings containing Python expressions This can be used for safely evaluating strings containing Python expressions
from untrusted sources without the need to parse the values oneself. from untrusted sources without the need to parse the values oneself.
.. versionchanged:: 3.2
Now allows set literals.
.. function:: get_docstring(node, clean=True) .. function:: get_docstring(node, clean=True)

View file

@ -58,6 +58,8 @@ def literal_eval(node_or_string):
return tuple(map(_convert, node.elts)) return tuple(map(_convert, node.elts))
elif isinstance(node, List): elif isinstance(node, List):
return list(map(_convert, node.elts)) return list(map(_convert, node.elts))
elif isinstance(node, Set):
return set(map(_convert, node.elts))
elif isinstance(node, Dict): elif isinstance(node, Dict):
return dict((_convert(k), _convert(v)) for k, v return dict((_convert(k), _convert(v)) for k, v
in zip(node.keys, node.values)) in zip(node.keys, node.values))

View file

@ -470,7 +470,9 @@ C-API
Library Library
------- -------
- Issue #9164: Ensure that sysconfig handles duplicate -arch flags in CFLAGS - ``ast.literal_eval()`` now allows set literals.
- Issue #9164: Ensure that sysconfig handles duplicate -arch flags in CFLAGS.
- Issue #7646: The fnmatch pattern cache no longer grows without bound. - Issue #7646: The fnmatch pattern cache no longer grows without bound.