4444else :
4545 _name_type_nodes = (ast .Name ,)
4646
47+
4748class EvaluationError (Exception ):
4849 """Raised if an exception occurred in safe_eval."""
4950
@@ -113,31 +114,32 @@ def _convert(node):
113114 raise EvaluationError ("can't lookup %s" % node .id )
114115
115116 # 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
119120 operand = _convert (node .operand )
120121 if not type (operand ) in _numeric_types :
121122 raise ValueError ("unary + and - only allowed on builtin nums" )
122123 if isinstance (node .op , ast .UAdd ):
123124 return + operand
124125 else :
125126 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 ) )):
128129 # ast.literal_eval does ast typechecks here, we use type checks
129130 left = _convert (node .left )
130131 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 ):
132134 raise ValueError ("binary + and - only allowed on builtin nums" )
133135 if isinstance (node .op , ast .Add ):
134136 return left + right
135137 else :
136138 return left - right
137139
138140 # 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 ) ):
141143 obj = _convert (node .value )
142144 index = _convert (node .slice .value )
143145 return safe_getitem (obj , index )
@@ -187,7 +189,7 @@ def evaluate_current_expression(cursor_offset, line, namespace=None):
187189 attr_before_cursor = temp_line [temp_attribute .start :temp_cursor ]
188190
189191 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 ):
191193 try :
192194 tree = ast .parse (line [i :cursor_offset ])
193195 yield tree
@@ -201,15 +203,16 @@ def parse_trees(cursor_offset, line):
201203 largest_ast = attribute_access .value
202204
203205 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" )
205208 try :
206209 return simple_eval (largest_ast , namespace )
207210 except ValueError :
208211 raise EvaluationError ("Could not safely evaluate" )
209212
210213
211214def 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 """
213216 # this function runs user code in case of custom descriptors,
214217 # so could fail in any way
215218
@@ -220,4 +223,5 @@ def evaluate_current_attribute(cursor_offset, line, namespace=None):
220223 try :
221224 return getattr (obj , attr .word )
222225 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