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.

Title: ast.literal_eval doesn't support the Python ternary operator
Type: behavior Stage: resolved
Components: Versions:
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, georg.brandl, larry, mark.dickinson, r.david.murray, serhiy.storchaka
Priority: normal Keywords:

Created on 2015-05-08 13:22 by larry, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg242760 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2015-05-08 13:22
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.
msg242765 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-05-08 14:16
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/", line 84, in literal_eval
    return _convert(node_or_string)
  File "/usr/lib/python3.4/", 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.
msg242767 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-05-08 14:25
Right, this isn't a bug, it is that literal_eval is intended only to support *literals*, not expressions.  See also issue 22525.
Date User Action Args
2022-04-11 14:58:16adminsetgithub: 68334
2015-05-08 14:25:28r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg242767

resolution: not a bug
stage: needs patch -> resolved
2015-05-08 14:16:11serhiy.storchakasetnosy: + mark.dickinson, serhiy.storchaka
messages: + msg242765
2015-05-08 13:22:43larrycreate