[NumPy-Tickets] [NumPy] #1968: PY3K bug in numpy.info (numpy/lib/utils.py)

NumPy Trac numpy-tickets@scipy....
Tue Nov 20 20:27:31 CST 2012


#1968: PY3K bug in numpy.info (numpy/lib/utils.py)
------------------------------+---------------------------------------------
 Reporter:  warren.weckesser  |       Owner:  somebody   
     Type:  defect            |      Status:  new        
 Priority:  normal            |   Milestone:  Unscheduled
Component:  numpy.lib         |     Version:  1.6.0      
 Keywords:                    |  
------------------------------+---------------------------------------------
Changes (by michaelbnewman):

 * cc: michael.b.newman@… (added)


Comment:

 Recap of problem:

 In Python 2.7 and earlier, numpy.info works as expected:
 {{{
 Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)]
 on win32
 >>> import numpy
 >>> numpy.info("add")
      *** Found in numpy ***
 add(x1, x2[, out])
 Add arguments element-wise.
 (etc.)
 }}}

 But in Python 3.2, numpy.info generates the following error:
 {{{
 Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)]
 on win32
 >>> import numpy
 >>> numpy.info("add")
      *** Found in numpy ***
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "C:\Python32\lib\site-packages\numpy\lib\utils.py", line 533, in
 info
     info(obj)
   File "C:\Python32\lib\site-packages\numpy\lib\utils.py", line 592, in
 info
     elif type(object) is types.InstanceType: ## check for __call__ method
 AttributeError: 'module' object has no attribute 'InstanceType'
 }}}

 Line 592 corresponds to the code block:
 {{{
     elif type(object) is types.InstanceType: ## check for __call__ method
         print("Instance of class: ", object.__class__.__name__,
 file=output)
         print(file=output)
         if hasattr(object, '__call__'):
             arguments =
 inspect.formatargspec(*inspect.getargspec(object.__call__.__func__))
             arglist = arguments.split(', ')
             if len(arglist) > 1:
                 arglist[1] = "("+arglist[1]
                 arguments = ", ".join(arglist[1:])
             else:
                 arguments = "()"

             if hasattr(object,'name'):
                 name = "%s" % object.name
             else:
                 name = "<name>"
             if len(name+arguments) > maxwidth:
                 argstr = _split_line(name, arguments, maxwidth)
             else:
                 argstr = name + arguments

             print(" " + argstr + "\n", file=output)
             doc = inspect.getdoc(object.__call__)
             if doc is not None:
                 print(inspect.getdoc(object.__call__), file=output)
             print(inspect.getdoc(object), file=output)

         else:
             print(inspect.getdoc(object), file=output)
 }}}

 According to:
 http://docs.python.org/2/library/types.html
 "types.InstanceType" is "The type of instances of user-defined classes."

 Furthermore:
 http://docs.python.org/2/reference/datamodel.html#new-style-and-classic-
 classes
 Classes and instances come in two flavors: old-style (or classic) and new-
 style.
 Old-style classes are removed in Python 3, leaving only the semantics of
 new-style classes.

 So the code block starting at Line 592 of util.py is testing for old-style
 classes, which don't exist in Python 3. So to fix the bug, you need to
 strip out the entire code block shown above starting on Line 592, since
 its no longer necessary to test for old-style classes. I have attached a
 "before" and "after" example files for clarity.

-- 
Ticket URL: <http://projects.scipy.org/numpy/ticket/1968#comment:1>
NumPy <http://projects.scipy.org/numpy>
My example project


More information about the NumPy-Tickets mailing list