1
1
# encoding: utf-8
2
+
3
+ # The MIT License
4
+ #
5
+ # Copyright (c) 2015 the bpython authors.
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in
15
+ # all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ # THE SOFTWARE.
24
+ #
2
25
"""simple evaluation of side-effect free code
3
26
4
27
In order to provide fancy completion, some code can be executed safely.
@@ -26,6 +49,16 @@ def safe_eval(expr, namespace):
26
49
raise EvaluationError
27
50
28
51
52
+ # This function is under the Python License, Version 2
53
+ # This license requires modifications to the code be reported.
54
+ # Based on ast.literal_eval in Python 2 and Python 3
55
+ # Modifications:
56
+ # * Python 2 and Python 3 versions of the function are combined
57
+ # * checks that objects used as operands of + and - are numbers
58
+ # instead of checking they are constructed with number literals
59
+ # * new docstring describing different functionality
60
+ # * looks up names from namespace
61
+ # * indexing syntax is allowed
29
62
def simple_eval (node_or_string , namespace = None ):
30
63
"""
31
64
Safely evaluate an expression node or a string containing a Python
@@ -40,7 +73,6 @@ def simple_eval(node_or_string, namespace=None):
40
73
41
74
The optional namespace dict-like ought not to cause side effects on lookup
42
75
"""
43
- # Based heavily on stdlib ast.literal_eval
44
76
if namespace is None :
45
77
namespace = {}
46
78
if isinstance (node_or_string , string_types ):
@@ -118,11 +150,18 @@ def safe_getitem(obj, index):
118
150
raise ValueError ('unsafe to lookup on object of type %s' % (type (obj ), ))
119
151
120
152
153
+ # This function is under the Python License, Version 2
154
+ # This license requires modifications to the code be reported.
155
+ # Based on ast.NodeVisitor.generic_visit
156
+ # Modifications:
157
+ # * Now a standalone function instead of method
158
+ # * now hardcoded to look for Attribute node with given attr name
159
+ # * returns values back up the recursive call stack to stop once target found
121
160
def find_attribute_with_name (node , name ):
122
- """Based on ast.NodeVisitor """
161
+ """Search depth-first for getitem indexing with name """
123
162
if isinstance (node , ast .Attribute ) and node .attr == name :
124
163
return node
125
- for field , value in ast .iter_fields (node ):
164
+ for _ , value in ast .iter_fields (node ):
126
165
if isinstance (value , list ):
127
166
for item in value :
128
167
if isinstance (item , ast .AST ):
@@ -134,7 +173,6 @@ def find_attribute_with_name(node, name):
134
173
if r :
135
174
return r
136
175
137
-
138
176
def evaluate_current_expression (cursor_offset , line , namespace = None ):
139
177
"""
140
178
Return evaluated expression to the right of the dot of current attribute.
0 commit comments