This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ast.literal_eval fails on sets
Type: Stage:
Components: Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder: ast.literal_eval does not handled new set literals
View: 10091
Assigned To: Nosy List: alex, benjamin.peterson, brett.cannon, brian.curtin, georg.brandl, ncoghlan
Priority: low Keywords: patch

Created on 2011-12-06 01:02 by alex, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
x.diff alex, 2011-12-06 01:05 review
Messages (6)
msg148897 - (view) Author: Alex Gaynor (alex) * (Python committer) Date: 2011-12-06 01:02
In 2.7 ast.literal_eval blows up with a set for input:


>>> import ast
>>> ast.literal_eval("{1}")
msg148898 - (view) Author: Alex Gaynor (alex) * (Python committer) Date: 2011-12-06 01:05
Patch with tests
msg148899 - (view) Author: Brian Curtin (brian.curtin) * (Python committer) Date: 2011-12-06 01:14
I don't profess to have any special ast knowledge, but given the context around there and the fact that it works...it looks fine to me.
msg148900 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2011-12-06 01:29
Dict and Set comprehensions are also broken:

>>> {1 for x in ()}
set([])
>>> {1:2 for x in ()}
{}
>>> ast.literal_eval("{1 for x in ()}")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/ast.py", line 80, in literal_eval
    return _convert(node_or_string)
  File "/usr/lib64/python2.7/ast.py", line 79, in _convert
    raise ValueError('malformed string')
ValueError: malformed string
>>> ast.literal_eval("{1:2 for x in ()}")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/ast.py", line 80, in literal_eval
    return _convert(node_or_string)
  File "/usr/lib64/python2.7/ast.py", line 79, in _convert
    raise ValueError('malformed string')
ValueError: malformed string
msg148901 - (view) Author: Alex Gaynor (alex) * (Python committer) Date: 2011-12-06 01:32
There's no support for comprehensions of any sort, and confusingly limited support for arithmetic ops, I'd like to keep the scope of this issue small, basically backporting 90bf0631bfb8 and adding the tests (which I can also add to default).
msg148910 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2011-12-06 11:51
Alex: IMO the operator support is only required for complex "literals".
History
Date User Action Args
2022-04-11 14:57:24adminsetgithub: 57745
2011-12-06 11:51:13georg.brandlsetmessages: + msg148910
2011-12-06 01:47:59benjamin.petersonsetstatus: open -> closed
resolution: not a bug
superseder: ast.literal_eval does not handled new set literals
2011-12-06 01:32:12alexsetmessages: + msg148901
2011-12-06 01:29:19ncoghlansetmessages: + msg148900
2011-12-06 01:14:47brian.curtinsetnosy: + brian.curtin
messages: + msg148899
2011-12-06 01:08:58pitrousetnosy: + brett.cannon, georg.brandl, ncoghlan, benjamin.peterson
2011-12-06 01:05:24alexsetfiles: + x.diff
keywords: + patch
messages: + msg148898
2011-12-06 01:02:39alexcreate