[Numpy-discussion] subclassing ndaray

Christopher Hanley chanley at stsci.edu
Mon Feb 27 11:57:02 CST 2006


Travis Oliphant wrote:
> So, this seems like a workable compromise.  It should work a bit better 
> now that I've changed it so that __array_finalize__ is called on every 
> sub-class creation.  If there is no "parent" then parent will be None.

Hi Travis,

The change you made just broke the following bit of code:

class FITS_rec(rec.recarray):
     def __new__(subtype, input):
         """Construct a FITS record array from a recarray."""
         # input should be a record array
         self = rec.recarray.__new__(subtype, input.shape, input.dtype,
             buf=input.data, strides=input.strides)

         self._nfields = len(self.dtype.fields[-1])
         self._convert = [None]*len(self.dtype.fields[-1])
         self._coldefs = None
         return self

     def __array_finalize__(self,obj):
         self._convert = obj._convert
         self._coldefs = obj._coldefs
         self._nfields = obj._nfields


In [1]: from pyfits import FITS_rec

In [2]: from numpy import rec

In [3]: data = 
FITS_rec(rec.array(None,formats="i4,i4,i4",names="c1,c2,c3",shape=3))
---------------------------------------------------------------------------
exceptions.AttributeError                            Traceback (most 
recent call last)

/data/sparty1/dev/pyfits-numpy/test/<console>

/data/sparty1/dev/site-packages/lib/python/pyfits.py in __new__(subtype, 
input)
    3136 #        self.__setstate__(input.__getstate__())
    3137         self = rec.recarray.__new__(subtype, input.shape, 
input.dtype,
-> 3138             buf=input.data, strides=input.strides)
    3139
    3140         # _parent is the original (storage) array,

/data/sparty1/dev/site-packages/lib/python/numpy/core/records.py in 
__new__(subtype, shape, formats, names, titles, buf, offset, strides, 
byteorder, aligned)
     153             self = sb.ndarray.__new__(subtype, shape, (record, 
descr),
     154                                       buffer=buf, offset=offset,
--> 155                                       strides=strides)
     156         return self
     157

/data/sparty1/dev/site-packages/lib/python/pyfits.py in 
__array_finalize__(self, obj)
    3150     def __array_finalize__(self,obj):
    3151 #        self._parent = obj._parent
-> 3152         self._convert = obj._convert
    3153         self._coldefs = obj._coldefs
    3154         self._nfields = obj._nfields

AttributeError: 'NoneType' object has no attribute '_convert'


Given what you have just said I would not have expected this to be broken.

Chris






More information about the Numpy-discussion mailing list