-2

Using, python 3.4, that works:

>>> ast.literal_eval("2 - 1")
1

but that doesn't:

>>> ast.literal_eval("1 | 2")
raise ValueError('malformed node or string: ' + repr(node))
ValueError: malformed node or string: <_ast.BinOp object at 0x0000000003338978>

>>> ast.literal_eval("1 ^ 2")
raise ValueError('malformed node or string: ' + repr(node))
ValueError: malformed node or string: <_ast.BinOp object at 0x0000000003338400>

All values are literals, there's no difficulty performing logical operations when it can perform additions, substractions...

Why can't I perform logical operations using ast.literal_eval ?

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • I thought it was related to constant folding, but that does not seem to be a case, as `1|2` seems to be correctly folded to `3` (according to `dis` module). – Łukasz Rogalski Dec 07 '16 at 13:51
  • @ŁukaszRogalski Additionally, >>> ast.dump(ast.parse("1 ^ 2")) 'Module(body=[Expr(value=BinOp(left=Num(n=1), op=BitXor(), right=Num(n=2)))])' – pradyunsg Dec 07 '16 at 13:56
  • @pradyunsg: `ast` does not do any constant folding, so I'm not sure what point you are trying to make – Eric Dec 07 '16 at 13:59
  • @Eric That the constant folding does not happen at the ast level. At least not until the result from `ast.parse`. – pradyunsg Dec 07 '16 at 14:01

2 Answers2

4

Because it's not implemented that way in Python 2.7.

At a glance, addition is only supported in order to enable complex literals like 1 + 2j, since it rejects 2j + 1.

In python 3.5, the implementation was made a bit more lenient, and does not restrict addition in this way.

Eric
  • 95,302
  • 53
  • 242
  • 374
1

It's just not implemented that way. The only operators "white-listed" are Add and Sub. 3.4, 3.5

This could be filed as a bug-report to http://bugs.python.org, asking for support of more operators.

pradyunsg
  • 18,287
  • 11
  • 43
  • 96