@@ -205,8 +205,9 @@ def matches(cls, cursor_offset, line, locals_, **kwargs):
205205 return None
206206 start , end , orig = r
207207 _ , _ , dexpr = lineparts .current_dict (cursor_offset , line )
208- obj = safe_eval (dexpr , locals_ )
209- if obj is SafeEvalFailed :
208+ try :
209+ obj = safe_eval (dexpr , locals_ )
210+ except EvaluationError :
210211 return []
211212 if obj and isinstance (obj , type ({})) and obj .keys ():
212213 return ["{!r}]" .format (k ) for k in obj .keys () if repr (k ).startswith (orig )]
@@ -288,23 +289,20 @@ def matches(cls, cursor_offset, line, **kwargs):
288289 return [match for match in matches if not match .startswith ('_' )]
289290 return matches
290291
291- class SafeEvalFailed (Exception ):
292- """If this object is returned, safe_eval failed"""
293- # Because every normal Python value is a possible return value of safe_eval
292+
293+ class EvaluationError (Exception ):
294+ """Raised if an exception occurred in safe_eval."""
295+
294296
295297def safe_eval (expr , namespace ):
296298 """Not all that safe, just catches some errors"""
297- if expr .isdigit ():
298- # Special case: float literal, using attrs here will result in
299- # a SyntaxError
300- return SafeEvalFailed
301299 try :
302- obj = eval (expr , namespace )
303- return obj
300+ return eval (expr , namespace )
304301 except (NameError , AttributeError , SyntaxError ):
305302 # If debugging safe_eval, raise this!
306303 # raise
307- return SafeEvalFailed
304+ raise EvaluationError
305+
308306
309307def attr_matches (text , namespace , autocomplete_mode ):
310308 """Taken from rlcompleter.py and bent to my will.
@@ -318,8 +316,13 @@ def attr_matches(text, namespace, autocomplete_mode):
318316 return []
319317
320318 expr , attr = m .group (1 , 3 )
321- obj = safe_eval (expr , namespace )
322- if obj is SafeEvalFailed :
319+ if expr .isdigit ():
320+ # Special case: float literal, using attrs here will result in
321+ # a SyntaxError
322+ return []
323+ try :
324+ obj = safe_eval (expr , namespace )
325+ except EvaluationError :
323326 return []
324327 with inspection .AttrCleaner (obj ):
325328 matches = attr_lookup (obj , expr , attr , autocomplete_mode )
0 commit comments