From 492f3fc272a3b3e941f0911a1ef722065c58452e Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Sun, 11 Jul 2010 09:41:21 +0000 Subject: Allow set literals in literal_eval(). --- Doc/library/ast.rst | 7 +++++-- Lib/ast.py | 2 ++ Misc/NEWS | 4 +++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Doc/library/ast.rst b/Doc/library/ast.rst index 9bf374f..43c9add 100644 --- a/Doc/library/ast.rst +++ b/Doc/library/ast.rst @@ -119,12 +119,15 @@ and classes for traversing abstract syntax trees: Safely evaluate an expression node or a string containing a Python expression. The string or node provided may only consist of the following - Python literal structures: strings, numbers, tuples, lists, dicts, booleans, - and ``None``. + Python literal structures: strings, numbers, tuples, lists, dicts, sets, + booleans, and ``None``. This can be used for safely evaluating strings containing Python expressions from untrusted sources without the need to parse the values oneself. + .. versionchanged:: 3.2 + Now allows set literals. + .. function:: get_docstring(node, clean=True) diff --git a/Lib/ast.py b/Lib/ast.py index 0b8baf7..092f077 100644 --- a/Lib/ast.py +++ b/Lib/ast.py @@ -58,6 +58,8 @@ def literal_eval(node_or_string): return tuple(map(_convert, node.elts)) elif isinstance(node, List): return list(map(_convert, node.elts)) + elif isinstance(node, Set): + return set(map(_convert, node.elts)) elif isinstance(node, Dict): return dict((_convert(k), _convert(v)) for k, v in zip(node.keys, node.values)) diff --git a/Misc/NEWS b/Misc/NEWS index b72e78a..dbfc4c5 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -470,7 +470,9 @@ C-API 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. -- cgit v0.12