Assignment to element of 2D memoryview fails with NotImplementedError

Create issue
Issue #3028 resolved
Jack Lovell created an issue

When trying to assign to a single element in a 2D memoryview as in the attached example, PyPy raises a NotImplementedError. CPython runs the example correctly. Reading the element works correctly in both CPython and PyPy.

Using PyPy 7.1.1-beta0

Comments (6)

  1. mattip

    Should be fixed by the setitem2d branch and bfa7f1269de9. Not worth backporting to python2.7 since cpython does not support nD indexing of memoryviews at all.

  2. Jack Lovell reporter

    Seems to work for memoryviews on single byte dtypes, but not for other numeric types such as ints and floats. See for example:

    import struct
    a = [i / 2 for i in range(10)]
    ba = bytearray(struct.pack(f"{len(a)}d", *a))
    mv = memoryview(ba).cast("d", shape=(2, 5))
    print(mv[1, 3])  # prints 4.0
    mv[1, 3] = 10.0
    print(mv[1, 3])  # still prints 4.0
    a = list(range(10))
    ba = bytearray(struct.pack(f"{len(a)}i", *a))
    mv = memoryview(ba).cast("i", shape=(2, 5))
    print(mv[1, 3])  # prints 8
    mv[1, 3] = 10
    print(mv[1, 3])  # still prints 8
    ba = bytearray(b"abcdabcdabcd")
    mv = memoryview(ba).cast("b", shape=(3, 4))
    print(mv[1, 3])  # prints 100 (ord("d")))
    mv[1, 3] = ord("z")
    print(mv[1, 3])  # prints 122 (ord("z"))

    Note that at line 20 I can also write mv[1, 3] = 123 and it will update successfully, so it seems to be related to the type of the memoryview rather than the value being assigned.

  3. Jack Lovell reporter

    Works fine for me with pypy-c-jit-96849-6c4d7cc5d649-linux64. Thanks, feel free to close the issue.

  4. mattip

    Thanks for the report. We are always looking for user feedback, especially real-world use cases and benchmarked comparisons. Our blog is a nice place to post user stories.

  5. Log in to comment