Skip to content

Commit 8b728d9

Browse files
committed
Be verbose about Python versions
1 parent 2a3cde4 commit 8b728d9

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

bpython/simpleeval.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,21 @@
2323
"""simple evaluation of side-effect free code
2424
2525
In order to provide fancy completion, some code can be executed safely.
26-
2726
"""
2827

29-
3028
import ast
3129
import sys
3230
import builtins
3331

3432
from . import line as line_properties
3533
from .inspection import getattr_safe
3634

35+
_is_py38 = sys.version_info[:2] >= (3, 8)
36+
_is_py39 = sys.version_info[:2] >= (3, 9)
37+
3738
_string_type_nodes = (ast.Str, ast.Bytes)
3839
_numeric_types = (int, float, complex)
39-
_name_type_nodes = (ast.Name, ast.NameConstant)
40+
_name_type_nodes = (ast.Name,) if _is_py38 else (ast.Name, ast.NameConstant)
4041

4142

4243
class EvaluationError(Exception):
@@ -88,9 +89,9 @@ def simple_eval(node_or_string, namespace=None):
8889
def _convert(node):
8990
if isinstance(node, ast.Constant):
9091
return node.value
91-
elif isinstance(node, _string_type_nodes):
92+
elif not _is_py38 and isinstance(node, _string_type_nodes):
9293
return node.s
93-
elif isinstance(node, ast.Num):
94+
elif not _is_py38 and isinstance(node, ast.Num):
9495
return node.n
9596
elif isinstance(node, ast.Tuple):
9697
return tuple(map(_convert, node.elts))
@@ -149,14 +150,16 @@ def _convert(node):
149150
return left - right
150151

151152
# this is a deviation from literal_eval: we allow indexing
152-
elif isinstance(node, ast.Subscript) and isinstance(
153-
node.slice, ast.Index
153+
elif (
154+
not _is_py39
155+
and isinstance(node, ast.Subscript)
156+
and isinstance(node.slice, ast.Index)
154157
):
155158
obj = _convert(node.value)
156159
index = _convert(node.slice.value)
157160
return safe_getitem(obj, index)
158161
elif (
159-
sys.version_info[:2] >= (3, 9)
162+
_is_py39
160163
and isinstance(node, ast.Subscript)
161164
and isinstance(node.slice, (ast.Constant, ast.Name))
162165
):

0 commit comments

Comments
 (0)