forked frombpython/bpython
- Notifications
You must be signed in to change notification settings - Fork0
Expand file tree
/
Copy pathsimpleeval.py
More file actions
281 lines (233 loc) · 10.3 KB
/
simpleeval.py
File metadata and controls
281 lines (233 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# encoding: utf-8
# The MIT License
#
# Copyright (c) 2015 the bpython authors.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
"""simple evaluation of side-effect free code
In order to provide fancy completion, some code can be executed safely.
"""
from __future__importabsolute_import
importast
importinspect
fromsiximportstring_types
fromsix.movesimportbuiltins
importsys
importtypes
from .importlineasline_properties
from ._py3compatimportpy3
from .inspectionimportis_new_style,AttrCleaner
_string_type_nodes= (ast.Str,ast.Bytes)ifpy3else (ast.Str,)
_numeric_types= (int,float,complex)+ (()ifpy3else (long,))
# added in Python 3.4
ifhasattr(ast,'NameConstant'):
_name_type_nodes= (ast.Name,ast.NameConstant)
else:
_name_type_nodes= (ast.Name,)
classEvaluationError(Exception):
"""Raised if an exception occurred in safe_eval."""
defsafe_eval(expr,namespace):
"""Not all that safe, just catches some errors"""
try:
returneval(expr,namespace)
except (NameError,AttributeError,SyntaxError):
# If debugging safe_eval, raise this!
# raise
raiseEvaluationError
# This function is under the Python License, Version 2
# This license requires modifications to the code be reported.
# Based on ast.literal_eval in Python 2 and Python 3
# Modifications:
# * Python 2 and Python 3 versions of the function are combined
# * checks that objects used as operands of + and - are numbers
# instead of checking they are constructed with number literals
# * new docstring describing different functionality
# * looks up names from namespace
# * indexing syntax is allowed
defsimple_eval(node_or_string,namespace=None):
"""
Safely evaluate an expression node or a string containing a Python
expression without triggering any user code.
The string or node provided may only consist of:
* the following Python literal structures: strings, numbers, tuples,
lists, and dicts
* variable names causing lookups in the passed in namespace or builtins
* getitem calls using the [] syntax on objects of the types above
Like the Python 3 (and unlike the Python 2) literal_eval, unary and binary
+ and - operations are allowed on all builtin numeric types.
The optional namespace dict-like ought not to cause side effects on lookup
"""
ifnamespaceisNone:
namespace= {}
ifisinstance(node_or_string,string_types):
node_or_string=ast.parse(node_or_string,mode='eval')
ifisinstance(node_or_string,ast.Expression):
node_or_string=node_or_string.body
def_convert(node):
ifisinstance(node,_string_type_nodes):
returnnode.s
elifisinstance(node,ast.Num):
returnnode.n
elifisinstance(node,ast.Tuple):
returntuple(map(_convert,node.elts))
elifisinstance(node,ast.List):
returnlist(map(_convert,node.elts))
elifisinstance(node,ast.Dict):
returndict((_convert(k),_convert(v))fork,v
inzip(node.keys,node.values))
# this is a deviation from literal_eval: we allow non-literals
elifisinstance(node,_name_type_nodes):
try:
returnnamespace[node.id]
exceptKeyError:
try:
returngetattr(builtins,node.id)
exceptAttributeError:
raiseEvaluationError("can't lookup %s"%node.id)
# unary + and - are allowed on any type
elif (isinstance(node,ast.UnaryOp)and
isinstance(node.op, (ast.UAdd,ast.USub))):
# ast.literal_eval does ast typechecks here, we use type checks
operand=_convert(node.operand)
ifnottype(operand)in_numeric_types:
raiseValueError("unary + and - only allowed on builtin nums")
ifisinstance(node.op,ast.UAdd):
return+operand
else:
return-operand
elif (isinstance(node,ast.BinOp)and
isinstance(node.op, (ast.Add,ast.Sub))):
# ast.literal_eval does ast typechecks here, we use type checks
left=_convert(node.left)
right=_convert(node.right)
ifnot (type(left)in_numeric_typesand
type(right)in_numeric_types):
raiseValueError("binary + and - only allowed on builtin nums")
ifisinstance(node.op,ast.Add):
returnleft+right
else:
returnleft-right
# this is a deviation from literal_eval: we allow indexing
elif (isinstance(node,ast.Subscript)and
isinstance(node.slice,ast.Index)):
obj=_convert(node.value)
index=_convert(node.slice.value)
returnsafe_getitem(obj,index)
# this is a deviation from literal_eval: we allow attribute access
ifisinstance(node,ast.Attribute):
obj=_convert(node.value)
attr=node.attr
returnsafe_get_attribute(obj,attr)
raiseValueError('malformed string')
return_convert(node_or_string)
defsafe_getitem(obj,index):
iftype(obj)in (list,tuple,dict,bytes)+string_types:
try:
returnobj[index]
except (KeyError,IndexError):
raiseEvaluationError("can't lookup key %r on %r"% (index,obj))
raiseValueError('unsafe to lookup on object of type %s'% (type(obj), ))
deffind_attribute_with_name(node,name):
ifisinstance(node,ast.Attribute)andnode.attr==name:
returnnode
foriteminast.iter_child_nodes(node):
r=find_attribute_with_name(item,name)
ifr:
returnr
defevaluate_current_expression(cursor_offset,line,namespace=None):
"""
Return evaluated expression to the right of the dot of current attribute.
Only evaluates builtin objects, and do any attribute lookup.
"""
# Builds asts from with increasing numbers of characters back from cursor.
# Find the biggest valid ast.
# Once our attribute access is found, return its .value subtree
ifnamespaceisNone:
namespace= {}
# in case attribute is blank, e.g. foo.| -> foo.xxx|
temp_line=line[:cursor_offset]+'xxx'+line[cursor_offset:]
temp_cursor=cursor_offset+3
temp_attribute=line_properties.current_expression_attribute(
temp_cursor,temp_line)
iftemp_attributeisNone:
raiseEvaluationError("No current attribute")
attr_before_cursor=temp_line[temp_attribute.start:temp_cursor]
defparse_trees(cursor_offset,line):
foriinrange(cursor_offset-1,-1,-1):
try:
tree=ast.parse(line[i:cursor_offset])
yieldtree
exceptSyntaxError:
continue
largest_ast=None
fortreeinparse_trees(temp_cursor,temp_line):
attribute_access=find_attribute_with_name(tree,attr_before_cursor)
ifattribute_access:
largest_ast=attribute_access.value
iflargest_astisNone:
raiseEvaluationError(
"Corresponding ASTs to right of cursor are invalid")
try:
returnsimple_eval(largest_ast,namespace)
exceptValueError:
raiseEvaluationError("Could not safely evaluate")
defevaluate_current_attribute(cursor_offset,line,namespace=None):
"""Safely evaluates the expression having an attributed accessed"""
# this function runs user code in case of custom descriptors,
# so could fail in any way
obj=evaluate_current_expression(cursor_offset,line,namespace)
attr=line_properties.current_expression_attribute(cursor_offset,line)
ifattrisNone:
raiseEvaluationError("No attribute found to look up")
try:
returngetattr(obj,attr.word)
exceptAttributeError:
raiseEvaluationError(
"can't lookup attribute %s on %r"% (attr.word,obj))
defsafe_get_attribute(obj,attr):
"""Gets attributes without triggering descriptors on new-style classes"""
ifis_new_style(obj):
withAttrCleaner(obj):
result=safe_get_attribute_new_style(obj,attr)
ifisinstance(result,member_descriptor):
# will either be the same slot descriptor or the value
returngetattr(obj,attr)
returnresult
returngetattr(obj,attr)
class_ClassWithSlots(object):
__slots__= ['a']
member_descriptor=type(_ClassWithSlots.a)
defsafe_get_attribute_new_style(obj,attr):
"""Returns approximately the attribute returned by getattr(obj, attr)
The object returned ought to be callable if getattr(obj, attr) was.
Fake callable objects may be returned instead, in order to avoid executing
arbitrary code in descriptors.
If the object is an instance of a class that uses __slots__, will return
the member_descriptor object instead of the value.
"""
ifnotis_new_style(obj):
raiseValueError("%r is not a new-style class or object"%obj)
to_look_through= (obj.__mro__
ifinspect.isclass(obj)
else (obj,)+type(obj).__mro__)
forclsinto_look_through:
ifhasattr(cls,'__dict__')andattrincls.__dict__:
returncls.__dict__[attr]
raiseAttributeError()