=== modified file 'IPython/extensions/ipy_traits_completer.py' --- IPython/extensions/ipy_traits_completer.py 2009-07-02 03:36:02 +0000 +++ IPython/extensions/ipy_traits_completer.py 2009-08-19 22:53:09 +0000 @@ -44,13 +44,14 @@ ===== - This requires Python 2.4 to work (I use sets). I don't think anyone is - using traits with 2.3 anyway, so that's OK. + using traits with 2.3 anyway, so that's OK. + + - Imports from enthought.traits are deferred until an object with a class that + looks like it subclasses from HasTraits comes along. This test is done by + looking at the name of the class and its superclasses. """ ############################################################################# -# External imports -from enthought.traits import api as T - # IPython imports from IPython.core.ipapi import TryNext, get as ipget from IPython.utils.genutils import dir2 @@ -70,11 +71,37 @@ # Set of names that Traits automatically adds to ANY traits-inheriting object. # These are the names we'll filter out. -TRAIT_NAMES = set( dir2(T.HasTraits()) ) - set( dir2(object()) ) +TRAIT_NAMES = None +def get_trait_names(): + global TRAIT_NAMES + from enthought.traits.api import HasTraits + if TRAIT_NAMES is None: + TRAIT_NAMES = set( dir2(HasTraits()) ) - set( dir2(object()) ) + else: + return TRAIT_NAMES ############################################################################# # Code begins +def looks_like_isinstance(obj, classname): + """ Return True if the object has a class or superclass with the given class + name. + + Ignores old-style classes. + """ + from types import InstanceType + + t = type(obj) + if t is InstanceType: + # Old-style classes. + return False + elif t.__name__ == classname: + return True + for klass in t.__mro__: + if klass.__name__ == classname: + return True + return False + def trait_completer(self,event): """A custom IPython tab-completer that is traits-aware. @@ -93,7 +120,13 @@ obj = oinfo['obj'] # OK, we got the object. See if it's traits, else punt - if not isinstance(obj,T.HasTraits): + if not looks_like_isinstance(obj, 'HasTraits'): + raise TryNext + + # Defer import until here so as not to require Traits until we get something + # that looks like it might be a HasTraits instance. + from enthought.traits.api import HasTraits + if not isinstance(obj, HasTraits): raise TryNext # it's a traits object, don't show the tr* attributes unless the completion @@ -114,7 +147,7 @@ #print '\nastart:<%r>' % attr_start # dbg if len(attr_start)