[IPython-user] 0.4.0 ready for Monday

Fernando Perez fperez at colorado.edu
Mon May 19 16:04:11 CDT 2003

Prabhu Ramachandran wrote:
>>>>>>"GB" == Gary Bishop <gb at cs.unc.edu> writes:
>     GB> Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)]
>     GB> IPython 0.2.15pre5 -- An enhanced Interactive Python.
>     GB> This is from CVS
> When I get the time I'll try and check if the CVS version fixes this.

I doubt it does, as the problem is that the cvar objects generate a funny 
NameError exception which trhows the standard lib's inspect.py for a loop. 
Here's the relevant part of the traceback from ipython's crash report:

/usr/lib/python2.2/inspect.py in getdoc(object=<Global variables>)
     252         return tuple(result)
     254 # -------------------------------------------------- source code 
     255 def indentsize(line):
     256     """Return the indent size, in spaces, at the start of a line of 
     257     expline = string.expandtabs(line)
     258     return len(expline) - len(string.lstrip(expline))
     260 def getdoc(object):
     261     """Get the documentation string for an object.
     263     All tabs are expanded to spaces.  To clean up docstrings that are
     264     indented to line up with blocks of code, any whitespace than can be
     265     uniformly removed from the second line onwards is removed."""
     266     try:
--> 267         doc = object.__doc__
         doc = undefined, object = <Global variables>, global __doc__ = 'Get 
useful information
from live Python objects....info about frames on the stack or in a traceback\n'
     268     except AttributeError:
     269         return None
     270     if not isinstance(doc, (str, unicode)):
     271         return None
     272     try:
     273         lines = string.split(string.expandtabs(doc), '\n')
     274     except UnicodeError:
     275         return None
     276     else:
     277         margin = None
     278         for line in lines[1:]:
     279             content = len(string.lstrip(line))
     280             if not content: continue
     281             indent = len(line) - content
     282             if margin is None: margin = indent

NameError: Unknown C global variable

Now, I can definitely harden ipytho against this case, but I have a strong 
feeling that this is indeed a problem of the underlying SWIG code.  inspect.py 
is written with:

     266     try:
--> 267         doc = object.__doc__
     268     except AttributeError:
     269         return None

It doesn't expect a NameError there, and it's getting confused by that fact. 
The SWIG-wrapped cvar object, upon access to a non-existing variable, should 
trhow an attribute error, because the variables are being accessed as 
attributes of the cvar object.

As I said, implementing a workaround in ipython is trivial, and I can do it. 
But I do think that the bug lies really in the way the SWIG code works.  I can 
see  why they chose to raise a NameError, since it seems natural for missing 
variable names.  But since they are implementing name access via attributes, 
the proper exception is an AttributeError.  Otherwise they end up  breaking 
even code in the python standard library, such as in the inspect.py case above.

Please keep me posted on how this goes, and if you need further work on my 
side for ipython, we can discuss it again.



More information about the IPython-user mailing list