@@ -205,8 +205,10 @@ def matches(cls, cursor_offset, line, locals_, **kwargs):
205
205
return None
206
206
start , end , orig = r
207
207
_ , _ , dexpr = lineparts .current_dict (cursor_offset , line )
208
- obj = safe_eval (dexpr , locals_ )
209
- if obj is SafeEvalFailed :
208
+
209
+ try :
210
+ obj = safe_eval (dexpr , locals_ )
211
+ except EvaluationError :
210
212
return []
211
213
if obj and isinstance (obj , type ({})) and obj .keys ():
212
214
return ["{!r}]" .format (k ) for k in obj .keys () if repr (k ).startswith (orig )]
@@ -328,23 +330,17 @@ def matches(cls, cursor_offset, line, **kwargs):
328
330
return [match for match in matches if not match .startswith ('_' )]
329
331
return matches
330
332
331
- class SafeEvalFailed (Exception ):
332
- """If this object is returned, safe_eval failed"""
333
- # Because every normal Python value is a possible return value of safe_eval
333
+ class EvaluationError (Exception ):
334
+ """safe_eval failed"""
334
335
335
336
def safe_eval (expr , namespace ):
336
337
"""Not all that safe, just catches some errors"""
337
- if expr .isdigit ():
338
- # Special case: float literal, using attrs here will result in
339
- # a SyntaxError
340
- return SafeEvalFailed
341
338
try :
342
- obj = eval (expr , namespace )
343
- return obj
344
- except (NameError , AttributeError , SyntaxError ) as e :
345
- # If debugging safe_eval, raise this!
346
- # raise e
347
- return SafeEvalFailed
339
+ return eval (expr , namespace )
340
+ except (NameError , AttributeError , SyntaxError ):
341
+ # If debugging safe_eval, reraise!
342
+ # raise
343
+ raise EvaluationError
348
344
349
345
def attr_matches (text , namespace , autocomplete_mode ):
350
346
"""Taken from rlcompleter.py and bent to my will.
@@ -358,8 +354,13 @@ def attr_matches(text, namespace, autocomplete_mode):
358
354
return []
359
355
360
356
expr , attr = m .group (1 , 3 )
361
- obj = safe_eval (expr , namespace )
362
- if obj is SafeEvalFailed :
357
+ if expr .isdigit ():
358
+ # Special case: float literal, using attrs here will result in
359
+ # a SyntaxError
360
+ return []
361
+ try :
362
+ obj = safe_eval (expr , namespace )
363
+ except EvaluationError :
363
364
return []
364
365
with inspection .AttrCleaner (obj ):
365
366
matches = attr_lookup (obj , expr , attr , autocomplete_mode )
0 commit comments