@@ -205,8 +205,9 @@ 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
+ try :
209
+ obj = safe_eval (dexpr , locals_ )
210
+ except EvaluationError :
210
211
return []
211
212
if obj and isinstance (obj , type ({})) and obj .keys ():
212
213
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):
288
289
return [match for match in matches if not match .startswith ('_' )]
289
290
return matches
290
291
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
+
294
296
295
297
def safe_eval (expr , namespace ):
296
298
"""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
301
299
try :
302
- obj = eval (expr , namespace )
303
- return obj
300
+ return eval (expr , namespace )
304
301
except (NameError , AttributeError , SyntaxError ):
305
302
# If debugging safe_eval, raise this!
306
303
# raise
307
- return SafeEvalFailed
304
+ raise EvaluationError
305
+
308
306
309
307
def attr_matches (text , namespace , autocomplete_mode ):
310
308
"""Taken from rlcompleter.py and bent to my will.
@@ -318,8 +316,13 @@ def attr_matches(text, namespace, autocomplete_mode):
318
316
return []
319
317
320
318
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 :
323
326
return []
324
327
with inspection .AttrCleaner (obj ):
325
328
matches = attr_lookup (obj , expr , attr , autocomplete_mode )
0 commit comments