Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ast.literal_eval fails on sets #57745

Closed
alex opened this issue Dec 6, 2011 · 6 comments
Closed

ast.literal_eval fails on sets #57745

alex opened this issue Dec 6, 2011 · 6 comments

Comments

@alex
Copy link
Member

alex commented Dec 6, 2011

BPO 13536
Nosy @brettcannon, @birkenfeld, @ncoghlan, @benjaminp, @alex, @briancurtin
Superseder
  • bpo-10091: ast.literal_eval does not handled new set literals
  • Files
  • x.diff
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2011-12-06.01:47:59.785>
    created_at = <Date 2011-12-06.01:02:39.729>
    labels = ['invalid']
    title = 'ast.literal_eval fails on sets'
    updated_at = <Date 2011-12-06.11:51:13.603>
    user = 'https://github.com/alex'

    bugs.python.org fields:

    activity = <Date 2011-12-06.11:51:13.603>
    actor = 'georg.brandl'
    assignee = 'none'
    closed = True
    closed_date = <Date 2011-12-06.01:47:59.785>
    closer = 'benjamin.peterson'
    components = []
    creation = <Date 2011-12-06.01:02:39.729>
    creator = 'alex'
    dependencies = []
    files = ['23859']
    hgrepos = []
    issue_num = 13536
    keywords = ['patch']
    message_count = 6.0
    messages = ['148897', '148898', '148899', '148900', '148901', '148910']
    nosy_count = 6.0
    nosy_names = ['brett.cannon', 'georg.brandl', 'ncoghlan', 'benjamin.peterson', 'alex', 'brian.curtin']
    pr_nums = []
    priority = 'low'
    resolution = 'not a bug'
    stage = None
    status = 'closed'
    superseder = '10091'
    type = None
    url = 'https://bugs.python.org/issue13536'
    versions = ['Python 2.7']

    @alex
    Copy link
    Member Author

    alex commented Dec 6, 2011

    In 2.7 ast.literal_eval blows up with a set for input:

    >> import ast
    >> ast.literal_eval("{1}")

    @alex
    Copy link
    Member Author

    alex commented Dec 6, 2011

    Patch with tests

    @briancurtin
    Copy link
    Member

    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.

    @ncoghlan
    Copy link
    Contributor

    ncoghlan commented Dec 6, 2011

    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

    @alex
    Copy link
    Member Author

    alex commented Dec 6, 2011

    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).

    @birkenfeld
    Copy link
    Member

    Alex: IMO the operator support is only required for complex "literals".

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    None yet
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants