From 353aa4244500659937506c5ba75521b345f3a559 Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Mon, 16 Jan 2023 09:55:44 +0100 Subject: [PATCH 1/2] Add minimal reproducer --- Lib/test/test_array.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py index 5b2c107a6044bd..1eed409d7f1690 100755 --- a/Lib/test/test_array.py +++ b/Lib/test/test_array.py @@ -192,6 +192,12 @@ def test_unicode(self): self.assertEqual(a, b, msg="{0!r} != {1!r}; testcase={2!r}".format(a, b, testcase)) + def test_memoryview(self): + a = array.array("Q", [1, 2, 3]) + untyped_buf = memoryview(a).cast("B") + b = array.array("Q", untyped_buf) + self.assertEqual(a, b) + class BaseTest: # Required class attributes (provided by subclasses From f3b1a71917a4902f7bb7c7a2108fb7b4a945c3be Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Mon, 16 Jan 2023 10:31:11 +0100 Subject: [PATCH 2/2] Include memoryviews as 'bytes' for the array.array initializer --- Modules/arraymodule.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index 114c69a033593c..c355297f9e6f11 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -2637,6 +2637,7 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds) if (!(initial == NULL || PyList_Check(initial) || PyByteArray_Check(initial) || PyBytes_Check(initial) + || PyMemoryView_Check(initial) || PyTuple_Check(initial) || ((c=='u') && PyUnicode_Check(initial)) || (array_Check(initial, state) @@ -2687,7 +2688,8 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds) } } else if (initial != NULL && (PyByteArray_Check(initial) || - PyBytes_Check(initial))) { + PyBytes_Check(initial) || + PyMemoryView_Check(initial))) { PyObject *v; v = array_array_frombytes((arrayobject *)a, initial);