44
44
else :
45
45
_name_type_nodes = (ast .Name ,)
46
46
47
+
47
48
class EvaluationError (Exception ):
48
49
"""Raised if an exception occurred in safe_eval."""
49
50
@@ -113,31 +114,32 @@ def _convert(node):
113
114
raise EvaluationError ("can't lookup %s" % node .id )
114
115
115
116
# unary + and - are allowed on any type
116
- elif isinstance (node , ast .UnaryOp ) and \
117
- isinstance (node .op , (ast .UAdd , ast .USub )):
118
- # ast.literal_eval does ast typechecks here, we use type checks
117
+ elif ( isinstance (node , ast .UnaryOp ) and
118
+ isinstance (node .op , (ast .UAdd , ast .USub ) )):
119
+ # ast.literal_eval does ast typechecks here, we use type checks
119
120
operand = _convert (node .operand )
120
121
if not type (operand ) in _numeric_types :
121
122
raise ValueError ("unary + and - only allowed on builtin nums" )
122
123
if isinstance (node .op , ast .UAdd ):
123
124
return + operand
124
125
else :
125
126
return - operand
126
- elif isinstance (node , ast .BinOp ) and \
127
- isinstance (node .op , (ast .Add , ast .Sub )):
127
+ elif ( isinstance (node , ast .BinOp ) and
128
+ isinstance (node .op , (ast .Add , ast .Sub ) )):
128
129
# ast.literal_eval does ast typechecks here, we use type checks
129
130
left = _convert (node .left )
130
131
right = _convert (node .right )
131
- if not (type (left ) in _numeric_types and type (right ) in _numeric_types ):
132
+ if not (type (left ) in _numeric_types and
133
+ type (right ) in _numeric_types ):
132
134
raise ValueError ("binary + and - only allowed on builtin nums" )
133
135
if isinstance (node .op , ast .Add ):
134
136
return left + right
135
137
else :
136
138
return left - right
137
139
138
140
# this is a deviation from literal_eval: we allow indexing
139
- elif isinstance (node , ast .Subscript ) and \
140
- isinstance (node .slice , ast .Index ):
141
+ elif ( isinstance (node , ast .Subscript ) and
142
+ isinstance (node .slice , ast .Index ) ):
141
143
obj = _convert (node .value )
142
144
index = _convert (node .slice .value )
143
145
return safe_getitem (obj , index )
@@ -187,7 +189,7 @@ def evaluate_current_expression(cursor_offset, line, namespace=None):
187
189
attr_before_cursor = temp_line [temp_attribute .start :temp_cursor ]
188
190
189
191
def parse_trees (cursor_offset , line ):
190
- for i in range (cursor_offset - 1 , - 1 , - 1 ):
192
+ for i in range (cursor_offset - 1 , - 1 , - 1 ):
191
193
try :
192
194
tree = ast .parse (line [i :cursor_offset ])
193
195
yield tree
@@ -201,15 +203,16 @@ def parse_trees(cursor_offset, line):
201
203
largest_ast = attribute_access .value
202
204
203
205
if largest_ast is None :
204
- raise EvaluationError ("Corresponding ASTs to right of cursor are invalid" )
206
+ raise EvaluationError (
207
+ "Corresponding ASTs to right of cursor are invalid" )
205
208
try :
206
209
return simple_eval (largest_ast , namespace )
207
210
except ValueError :
208
211
raise EvaluationError ("Could not safely evaluate" )
209
212
210
213
211
214
def evaluate_current_attribute (cursor_offset , line , namespace = None ):
212
- """Safely evaluates the expression attribute lookup currently occuring on """
215
+ """Safely evaluates the expression having an attributed accesssed """
213
216
# this function runs user code in case of custom descriptors,
214
217
# so could fail in any way
215
218
@@ -220,4 +223,5 @@ def evaluate_current_attribute(cursor_offset, line, namespace=None):
220
223
try :
221
224
return getattr (obj , attr .word )
222
225
except AttributeError :
223
- raise EvaluationError ("can't lookup attribute %s on %r" % (attr .word , obj ))
226
+ raise EvaluationError (
227
+ "can't lookup attribute %s on %r" % (attr .word , obj ))
0 commit comments