Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Lib/test/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Speed up writes to floating-point and complex :class:`array.array` objects.
Patch by Maurycy Pawłowski-Wieroński.
24 changes: 12 additions & 12 deletions Modules/arraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {

@skirpichev skirpichev Nov 23, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP7 nit:

Suggested change
if (x == -1.0 && PyErr_Occurred()) {
if (x == -1.0 && PyErr_Occurred()) {

(ditto other cases)

return -1;
}

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe

Suggested change
if (PyErr_Occurred()) {
if (x.real == -1.0 && PyErr_Occurred()) {

@skirpichev skirpichev Nov 23, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect this was copied from Python/getargs.c. We also check errors this way in the AC.

Not sure if there is a measurable difference, though. PyErr_Occurred() is cheap.

Edit:
Probably it's not worth for AC, but for array you can see measurable difference (default configure options, gcc 14.2):

Benchmark ref patch
array('Zd', <10>) 1.91 us 1.63 us: 1.17x faster
array('Zd', <100>) 9.32 us 7.65 us: 1.22x faster
Geometric mean (ref) 1.19x faster
Details
from array import array
import pyperf
runner = pyperf.Runner()
for n in [10, 100]:
    xs = [i + 1j for i in range(n)]
    s = f"array('Zd', <{n}>)"
    tp = "Zd"
    runner.bench_func(s, array, tp, xs)
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
index a484dba703c..5d1deb1e4e7 100644
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -691,7 +691,7 @@ cd_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
 {
     Py_complex x = PyComplex_AsCComplex(v);
     double f[2];
-    if (PyErr_Occurred()) {
+    if (x.real == -1.0 && PyErr_Occurred()) {
         return -1;
     }
 

return -1;
}

Expand Down Expand Up @@ -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;
}

Expand Down
Loading