[IPython-user] That trait_names attribute bit me!

Matthew Wilson matt@tplus1....
Sun Dec 14 15:39:44 CST 2008


A friend wrote a neat mock-object library that among other things,
always replies True to any hasattr requests.

It also can be used as an infinitely long iterable.

So when I tried to do tab completion on an instance of this mock Class,
I got stuck in an infinite loop because of these lines in dir2 (defined
in genutils.py):

    # this is the 'dir' function for objects with Enthought's traits
    if hasattr(obj, 'trait_names'):
        try:
            words.extend(obj.trait_names())
            may_have_dupes = True
        except TypeError:
            # This will happen if `obj` is a class and not an instance.
            pass

    print "did that stuff"

Just to be clear, my object is obj, and can be used as an iterable and
will NEVER raise a StopIteration.  Also, you can ask it if it has ANY
attribute, and it will always say yes.

So, the line

            words.extend(obj.trait_names())

goes and goes and goes, as words keeps appending more elements.

I really like the suggestion in this ticket, to disable this feature by
default:

http://projects.scipy.org/ipython/ipython/ticket/229

Alternately, in the short run, can we combine the 

    hasattr(obj, 'trait_names') 

test with an 

    isinstance(obj, Enthought)

test (or something similar)?

IPython is great.  I can't imagine programming without it.

Matt



More information about the IPython-user mailing list