对由空格分隔的数字字符串进行 ast 文字评估
ast literal eval on string of numbers separated by spaces
不同版本的ast有不同的行为:
python2, python3.7:
ast.literal_eval("3 -1")
>> ValueError: malformed node or string
python3.4
ast.literal_eval("3 -1")
>> 2
有相关文档吗?
这记录在 bug 31778 中。更严格行为的基本原理是旧行为可能导致某些值被错误评估:例如,日期字符串 2019-12-18
可以被评估为算术表达式而不是被保留为字符串。
不计算表达式更符合 documented behaviour
The string or node provided may only consist of the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts, sets, booleans, and None.
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.
根据错误报告。
不同版本的ast有不同的行为:
python2, python3.7:
ast.literal_eval("3 -1")
>> ValueError: malformed node or string
python3.4
ast.literal_eval("3 -1")
>> 2
有相关文档吗?
这记录在 bug 31778 中。更严格行为的基本原理是旧行为可能导致某些值被错误评估:例如,日期字符串 2019-12-18
可以被评估为算术表达式而不是被保留为字符串。
不计算表达式更符合 documented behaviour
The string or node provided may only consist of the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts, sets, booleans, and None.
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.
根据错误报告。