Skip to content

Commit cad23f2

Browse files
committed
[GR-48140] Write individual array elements of MemoryView's shape, strides and suboffsets.
2 parents 79efed6 + 200f48c commit cad23f2

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_memoryview.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
22
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33
#
44
# The Universal Permissive License (UPL), Version 1.0
@@ -39,6 +39,7 @@
3939

4040
import itertools
4141
import sys
42+
import struct
4243

4344
from . import CPyExtTestCase, CPyExtFunction, CPyExtType, unhandled_error_compare_with_message, unhandled_error_compare
4445

@@ -445,6 +446,29 @@ def compile_module(self, name):
445446

446447

447448
class TestObject(object):
449+
def test_memoryview_fromobject_multidim(self):
450+
TestType = CPyExtType(
451+
"TestMemoryViewMultidim",
452+
"""
453+
PyObject* get_converted_view(PyObject* self, PyObject* obj) {
454+
PyObject *mv = PyMemoryView_FromObject(obj);
455+
if (!mv) {
456+
return NULL;
457+
}
458+
459+
Py_buffer *view = PyMemoryView_GET_BUFFER(mv);
460+
// int i = (int)PyNumber_AsSsize_t(idx, NULL);
461+
return PyMemoryView_FromBuffer(view);
462+
}
463+
""",
464+
tp_methods='{"get_converted_view", get_converted_view, METH_O, ""}',
465+
)
466+
467+
obj = TestType()
468+
t1_buf = struct.pack("d"*12, *[1.5*x for x in range(12)])
469+
t1_mv = memoryview(t1_buf).cast('d', shape=[3,4])
470+
assert t1_mv == obj.get_converted_view(t1_mv)
471+
448472
def test_memoryview_acquire_release(self):
449473
TestType = CPyExtType(
450474
"TestMemoryViewBuffer1",

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/PyMemoryViewWrapper.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ static Object getShape(int[] intArray,
9191
@Cached CStructAccess.AllocateNode alloc,
9292
@Cached CStructAccess.WriteLongNode write) {
9393
Object mem = alloc.alloc(intArray.length * Long.BYTES);
94-
for (int i = 0; i < intArray.length; i++) {
95-
write.write(mem, intArray[i]);
96-
}
94+
write.writeIntArray(mem, intArray);
9795
return mem;
9896
}
9997
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/structs/CStructAccess.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,16 @@ public final void writeLongArray(Object pointer, long[] values, int length, int
10481048
}
10491049
}
10501050

1051+
public final void writeIntArray(Object pointer, int[] values) {
1052+
writeIntArray(pointer, values, values.length, 0, 0);
1053+
}
1054+
1055+
public final void writeIntArray(Object pointer, int[] values, int length, int sourceOffset, int targetOffset) {
1056+
for (int i = 0; i < length; i++) {
1057+
execute(pointer, (i + targetOffset) * Long.BYTES, values[i + sourceOffset]);
1058+
}
1059+
}
1060+
10511061
public final boolean accepts(ArgDescriptor desc) {
10521062
return desc.isI64();
10531063
}

0 commit comments

Comments
 (0)