Specifying strides on zero-sized array leads to error, unlike numpy

Issue #53 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((0,), dtype=np.float32)
d = _DummyArray(x.__array_interface__, base=x)
d.__array_interface__["strides"] = x.strides
print(d.__array_interface__)
y = np.asarray(d)

succeeds on numpy, crashes on numpypy with

Traceback (most recent call last):
  File "x.py", line 15, in <module>
    y = np.asarray(d)
  File "/home/andreas/src/env-pypy/site-packages/numpy/core/numeric.py", line 474, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: strides is incompatible with shape of requested array and size of buffer

Comments (4)

  1. mattip

    Reading the documentation on __array_interface__ here it seems to imply that the API is read-only. Is it a common usecase to (ab)use it to be read-write? (linked to issue #52 as well)

  2. Andreas Kloeckner reporter

    Oh. Yeah, I probably should have made a copy of x.__array_interface__. Here's a cleaner version that exhibits the same symptom:

    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((0,), dtype=np.float32)
    intf = x.__array_interface__.copy()
    intf["strides"] = x.strides
    d = _DummyArray(intf, base=x)
    print(d.__array_interface__)
    y = np.asarray(d)
    
  3. Log in to comment