fix for exception in console auto-completing an object with __getitem__ but no __len__ (BMEdge).

This commit is contained in:
Campbell Barton 2013-04-30 23:01:12 +00:00
parent 6af6c96e85
commit 56e6c14e1e

@ -96,12 +96,22 @@ def complete_indices(word, namespace, obj=None, base=None):
if not hasattr(obj, '__getitem__'): if not hasattr(obj, '__getitem__'):
# obj is not a list or dictionary # obj is not a list or dictionary
return [] return []
if is_dict(obj):
obj_is_dict = is_dict(obj)
# rare objects have a __getitem__ but no __len__ (eg. BMEdge)
if not obj_is_dict:
try:
obj_len = len(obj)
except TypeError:
return []
if obj_is_dict:
# dictionary type # dictionary type
matches = ['%s[%r]' % (base, key) for key in sorted(obj.keys())] matches = ['%s[%r]' % (base, key) for key in sorted(obj.keys())]
else: else:
# list type # list type,
matches = ['%s[%d]' % (base, idx) for idx in range(len(obj))] matches = ['%s[%d]' % (base, idx) for idx in range(obj_len)]
if word != base: if word != base:
matches = [match for match in matches if match.startswith(word)] matches = [match for match in matches if match.startswith(word)]
return matches return matches