Skip to content

Commit 200f48c

Browse files
committed
Add test case
1 parent 100712a commit 200f48c

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
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",

0 commit comments

Comments
 (0)