Skip to content

Commit 492f3fc

Browse files
committed
Allow set literals in literal_eval().
1 parent e40ee50 commit 492f3fc

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

Doc/library/ast.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,15 @@ and classes for traversing abstract syntax trees:
119119

120120
Safely evaluate an expression node or a string containing a Python
121121
expression. The string or node provided may only consist of the following
122-
Python literal structures: strings, numbers, tuples, lists, dicts, booleans,
123-
and ``None``.
122+
Python literal structures: strings, numbers, tuples, lists, dicts, sets,
123+
booleans, and ``None``.
124124

125125
This can be used for safely evaluating strings containing Python expressions
126126
from untrusted sources without the need to parse the values oneself.
127127

128+
.. versionchanged:: 3.2
129+
Now allows set literals.
130+
128131

129132
.. function:: get_docstring(node, clean=True)
130133

Lib/ast.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ def _convert(node):
5858
return tuple(map(_convert, node.elts))
5959
elif isinstance(node, List):
6060
return list(map(_convert, node.elts))
61+
elif isinstance(node, Set):
62+
return set(map(_convert, node.elts))
6163
elif isinstance(node, Dict):
6264
return dict((_convert(k), _convert(v)) for k, v
6365
in zip(node.keys, node.values))

Misc/NEWS

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,9 @@ C-API
470470
Library
471471
-------
472472

473-
- Issue #9164: Ensure that sysconfig handles duplicate -arch flags in CFLAGS
473+
- ``ast.literal_eval()`` now allows set literals.
474+
475+
- Issue #9164: Ensure that sysconfig handles duplicate -arch flags in CFLAGS.
474476

475477
- Issue #7646: The fnmatch pattern cache no longer grows without bound.
476478

0 commit comments

Comments
 (0)