asarray() with __array_interface__ doesn't set .base

Create issue
Issue #52 resolved
Andreas Kloeckner created an issue
import numpy as np

class _DummyArray(object):
    """ Dummy object that just exists to hang __array_interface__ dictionaries
    and possibly keep alive a reference to a base array.
    """
    def __init__(self, interface, base=None):
        self.__array_interface__ = interface
        self.base = base

x = np.zeros(10000)
d = _DummyArray(x.__array_interface__, base=x)
y = np.asarray(d)

assert y.base is d

succeeds on numpy, fails on numpypy

Comments (3)

  1. mattip

    It seems we do not call __init__ for a class with an __array_interface__ attribute like numpy does. I linked in the test in issue #53 and rewrote a bit (to allow running on a vanilla PyPy without the numpy fork)

    try:
        import _numpypy.multiarray as np
    except:
        import numpy as np
    
    class _DummyArray(object):
        """ Dummy object that just exists to hang __array_interface__ dictionaries
        and possibly keep alive a reference to a base array.
        """
        def __init__(self, interface, base=None):
            self.__array_interface__ = interface
            print 'in init'
            self.base = base
    
    x = np.zeros(10000)
    d = _DummyArray(x.__array_interface__, base=x)
    print 'calling array of d'
    y = np.array(d, copy=False)
    
    if not y.base is d:
        print 'y.base is', type(y.base), 'not d'
    
    x = np.zeros((0,), dtype='float32')
    d = _DummyArray(x.__array_interface__, base=x)
    print 'd.strides', d.__array_interface__['strides']
    d.__array_interface__["strides"] = x.strides
    print(d.__array_interface__)
    y = np.array(d, copy=False)
    

    PyPy does not print in init like CPython does

  2. Log in to comment