When I execute code ast.literal_eval('1+1')
in python2.7, the result as follows:
And I try it in python3.6, it works correctly. So what's the reason?
Asked
Active
Viewed 120 times
-1

Jedore
- 333
- 3
- 13
-
Please post text instead of images of text when possible. – Galen Dec 21 '17 at 03:18
1 Answers
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