bytes() doesn't behave correctly for strided memoryviews
Originally filed as https://github.com/numpy/numpy/issues/19536. It looks like this might be a PyPy bug, so re-filing here.
Here's a test script:
import numpy
import inspect
def memory_view_fields(m):
return [v for v in inspect.getmembers(m) if not callable(v[1]) and not v[0].startswith('__')]
unstrided = numpy.array([1, 2, 3, 4], numpy.uint8)
strided = numpy.lib.stride_tricks.as_strided(unstrided, shape=[2], strides=[2])
print(strided)
print(list(strided))
print(bytes(strided))
print(bytes(memoryview(strided)))
print(memory_view_fields(memoryview(strided)))
Under the default CPython 3.9.6 on Arch Linux I see this output:
[1 3]
[1, 3]
b'\x01\x03'
b'\x01\x03'
[('c_contiguous', False), ('contiguous', False), ('f_contiguous', False), ('format', 'B'), ('itemsize', 1), ('nbytes', 2), ('ndim', 1), ('obj', array([1, 3], dtype=uint8)), ('readonly', False), ('shape', (2,)), ('strides', (2,)), ('suboffsets', ())]
But under PyPy 7.3.5 I see:
[1 3]
[1, 3]
b'\x01\x02' <-- BUG
b'\x01\x02' <-- BUG
[('c_contiguous', False), ('contiguous', False), ('f_contiguous', False), ('format', 'B'), ('itemsize', 1), ('nbytes', 2), ('ndim', 1), ('obj', array([1, 3], dtype=uint8)), ('readonly', False), ('shape', (2,)), ('strides', (2,)), ('suboffsets', ())]
Can anyone suggest other test cases I could add to these?