From 44ce953f3c349dcfa33aebdd27f23b158863a6ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Mon, 23 Nov 2026 22:59:21 +0100 Subject: [PATCH 1/3] avoid parsing queen --- Modules/arraymodule.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index 64dacd1b8f1f61..a484dba703c165 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -584,8 +584,8 @@ e_getitem(arrayobject *ap, Py_ssize_t i) static int e_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) { - double x; - if (!PyArg_Parse(v, "d;array item must be float", &x)) { + double x = PyFloat_AsDouble(v); + if (x == -1.0 && PyErr_Occurred()) { return -1; } @@ -607,9 +607,10 @@ f_getitem(arrayobject *ap, Py_ssize_t i) static int f_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) { - double x; - if (!PyArg_Parse(v, "d;array item must be float", &x)) + double x = PyFloat_AsDouble(v); + if (x == -1.0 && PyErr_Occurred()) { return -1; + } CHECK_ARRAY_BOUNDS(ap, i); @@ -629,9 +630,10 @@ d_getitem(arrayobject *ap, Py_ssize_t i) static int d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) { - double x; - if (!PyArg_Parse(v, "d;array item must be float", &x)) + double x = PyFloat_AsDouble(v); + if (x == -1.0 && PyErr_Occurred()) { return -1; + } CHECK_ARRAY_BOUNDS(ap, i); @@ -652,9 +654,8 @@ cf_getitem(arrayobject *ap, Py_ssize_t i) static int cf_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) { - Py_complex x; - - if (!PyArg_Parse(v, "D;array item must be complex", &x)) { + Py_complex x = PyComplex_AsCComplex(v); + if (PyErr_Occurred()) { return -1; } @@ -688,10 +689,9 @@ cd_getitem(arrayobject *ap, Py_ssize_t i) static int cd_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) { - Py_complex x; + Py_complex x = PyComplex_AsCComplex(v); double f[2]; - - if (!PyArg_Parse(v, "D;array item must be complex", &x)) { + if (PyErr_Occurred()) { return -1; } From bf53f63223261ce22aa237a7a8f1080e0d7c31dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Mon, 23 Nov 2026 22:59:29 +0100 Subject: [PATCH 2/3] test --- Lib/test/test_array.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py index 580966589fdd6b..58e4b33df15689 100755 --- a/Lib/test/test_array.py +++ b/Lib/test/test_array.py @@ -105,6 +105,19 @@ def __index__(self): with self.assertRaises(TypeError): a.fromlist(lst) + @subTests("typecode", ["e", "f", "d", "Zf", "Zd"]) + def test_setitem_conversion_error(self, typecode): + class BadNumber: + def __float__(self): + raise ValueError("conversion failed") + + __complex__ = __float__ + + a = array.array(typecode, [-1, 42]) + with self.assertRaisesRegex(ValueError, "conversion failed"): + a[1] = BadNumber() + self.assertEqual(a.tolist(), [-1, 42]) + def test_typecodes(self): self.assertIsInstance(array.typecodes, tuple) for typecode in array.typecodes: From 51d281daaacc48c12fb3e1666e4afc65fb8ee1a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Mon, 23 Nov 2026 23:11:55 +0100 Subject: [PATCH 3/3] news --- .../next/Library/2026-11-23-23-11-49.gh-issue-162507.hG5bdI.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-11-23-23-11-49.gh-issue-162507.hG5bdI.rst diff --git a/Misc/NEWS.d/next/Library/2026-11-23-23-11-49.gh-issue-162507.hG5bdI.rst b/Misc/NEWS.d/next/Library/2026-11-23-23-11-49.gh-issue-162507.hG5bdI.rst new file mode 100644 index 00000000000000..e22f25d2aab915 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-23-23-11-49.gh-issue-162507.hG5bdI.rst @@ -0,0 +1,2 @@ +Speed up writes to floating-point and complex :class:`array.array` objects. +Patch by Maurycy Pawłowski-Wieroński.