-1

When I execute code ast.literal_eval('1+1') in python2.7, the result as follows: enter image description here And I try it in python3.6, it works correctly. So what's the reason?

Jedore
  • 333
  • 3
  • 13

1 Answers1

3

Both the 2.7 and 3.6 docs say the following:

This can be used for safely evaluating strings containing Python values from untrusted sources without the need to parse the values oneself. It is not capable of evaluating arbitrarily complex expressions, for example involving operators or indexing.

Addition is an operator, so this is documented not to work. The fact that it does work in Python 3.6 is surprising to me. Searching the bug tracker, this discrepancy is listed as Python bug #31778.

Kevin
  • 28,963
  • 9
  • 62
  • 81