Skip to content

Commit

Permalink
bpo-39158: ast.literal_eval() doesn't support empty sets (pythonGH-17742
Browse files Browse the repository at this point in the history
)
  • Loading branch information
rhettinger authored Jan 3, 2020
1 parent 32f1443 commit 4fcf5c1
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Doc/library/ast.rst
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ and classes for traversing abstract syntax trees:
.. versionchanged:: 3.2
Now allows bytes and set literals.

.. versionchanged:: 3.9
Now supports creating empty sets with ``'set()'``.


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

Expand Down
3 changes: 3 additions & 0 deletions Lib/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ def _convert(node):
return list(map(_convert, node.elts))
elif isinstance(node, Set):
return set(map(_convert, node.elts))
elif (isinstance(node, Call) and isinstance(node.func, Name) and
node.func.id == 'set' and node.args == node.keywords == []):
return set()
elif isinstance(node, Dict):
return dict(zip(map(_convert, node.keys),
map(_convert, node.values)))
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,7 @@ def test_literal_eval(self):
self.assertEqual(ast.literal_eval('(True, False, None)'), (True, False, None))
self.assertEqual(ast.literal_eval('{1, 2, 3}'), {1, 2, 3})
self.assertEqual(ast.literal_eval('b"hi"'), b"hi")
self.assertEqual(ast.literal_eval('set()'), set())
self.assertRaises(ValueError, ast.literal_eval, 'foo()')
self.assertEqual(ast.literal_eval('6'), 6)
self.assertEqual(ast.literal_eval('+6'), 6)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ast.literal_eval() now supports empty sets.

0 comments on commit 4fcf5c1

Please sign in to comment.