Skip to content

ast.literal_eval doesn't support the Python ternary operator #68334

Closed

Description

@larryhastings
BPO 24146
Nosy @birkenfeld, @mdickinson, @larryhastings, @benjaminp, @bitdancer, @serhiy-storchaka

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 2015-05-08.14:25:28.768>
created_at = <Date 2015-05-08.13:22:43.292>
labels = ['type-bug', 'invalid']
title = "ast.literal_eval doesn't support the Python ternary operator"
updated_at = <Date 2015-05-08.14:25:28.766>
user = 'https://github.com/larryhastings'

bugs.python.org fields:

activity = <Date 2015-05-08.14:25:28.766>
actor = 'r.david.murray'
assignee = 'none'
closed = True
closed_date = <Date 2015-05-08.14:25:28.768>
closer = 'r.david.murray'
components = []
creation = <Date 2015-05-08.13:22:43.292>
creator = 'larry'
dependencies = []
files = []
hgrepos = []
issue_num = 24146
keywords = []
message_count = 3.0
messages = ['242760', '242765', '242767']
nosy_count = 6.0
nosy_names = ['georg.brandl', 'mark.dickinson', 'larry', 'benjamin.peterson', 'r.david.murray', 'serhiy.storchaka']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue24146'
versions = []

Activity

  1. larryhastings commented on May 8, 2015

    @larryhastings
    ContributorAuthor

    ast.literal_eval() supports all Python operators, yes? No. It doesn't support "if/else", Python's ternary operator.

    Is there a reason it does not? I think it probably should.

  2. serhiy-storchaka commented on May 8, 2015

    @serhiy-storchaka
    Member

    No it doesn't support all Python operators.

    >>> ast.literal_eval('2*3')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib/python3.4/ast.py", line 84, in literal_eval
        return _convert(node_or_string)
      File "/usr/lib/python3.4/ast.py", line 83, in _convert
        raise ValueError('malformed node or string: ' + repr(node))
    ValueError: malformed node or string: <_ast.BinOp object at 0xb6f8446c>

    And shouldn't. It supports "+" and "-" only because they are needed for support of complex "literals". It is unintentional side effect, that ast.literal_eval() supports not only "2+3j", but "2+3" too.

  3. bitdancer commented on May 8, 2015

    @bitdancer
    Member

    Right, this isn't a bug, it is that literal_eval is intended only to support *literals*, not expressions. See also bpo-22525.

  4. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions