From b9b8f448868b6d7335ecd9bd29d18c660861ab47 2011-06-28 19:46:33 From: Jörgen Stenarson Date: 2011-06-28 19:46:33 Subject: [PATCH] don't rely on hasattr in utils.dir2 Some remote object libraries (e.g. Pyro) will lie to hasattr, not raising AttributeError until the attr is actually requested. This prevents these AttributeErrors from crashing IPython. closes gh-543 Signed-off-by: MinRK --- diff --git a/IPython/utils/dir2.py b/IPython/utils/dir2.py index 363e579..b82b62c 100644 --- a/IPython/utils/dir2.py +++ b/IPython/utils/dir2.py @@ -20,8 +20,14 @@ def get_class_members(cls): ret = dir(cls) if hasattr(cls,'__bases__'): - for base in cls.__bases__: - ret.extend(get_class_members(base)) + try: + bases = cls.__bases__ + except AttributeError: + # `obj` lied to hasattr (e.g. Pyro), ignore + pass + else: + for base in bases: + ret.extend(get_class_members(base)) return ret @@ -59,6 +65,9 @@ def dir2(obj): except TypeError: # This will happen if `obj` is a class and not an instance. pass + except AttributeError: + # `obj` lied to hasatter (e.g. Pyro), ignore + pass # Support for PyCrust-style _getAttributeNames magic method. if hasattr(obj, '_getAttributeNames'): @@ -69,6 +78,9 @@ def dir2(obj): # `obj` is a class and not an instance. Ignore # this error. pass + except AttributeError: + # `obj` lied to hasatter (e.g. Pyro), ignore + pass if may_have_dupes: # eliminate possible duplicates, as some traits may also