Skip to content

Commit 434c62d

Browse files
committed
Address review feedback: trim NEWS wording, generalize the single-call test
Per skirpichev's review on GH-154567: - Trim the NEWS entry to his suggested wording. - Move test_byteswap_single_call_result from ComplexDoubleTest into the shared CFPTest base class, using self.example and computing itemsize/half dynamically instead of hardcoded values, so it now covers both ComplexFloatTest ('Zf') and ComplexDoubleTest ('Zd') rather than just one.
1 parent 7b1b994 commit 434c62d

2 files changed

Lines changed: 17 additions & 17 deletions

File tree

Lib/test/test_array.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,6 +1569,22 @@ def test_byteswap(self):
15691569
b.byteswap()
15701570
self.assertEqual(a, b)
15711571

1572+
def test_byteswap_single_call_result(self):
1573+
# A single byteswap() must swap each item's two halves (real,
1574+
# imag) independently. test_byteswap above only checks that
1575+
# byteswap() twice round-trips to the original, which passes
1576+
# even if a single call scrambles multi-item arrays.
1577+
a = array.array(self.typecode, self.example)
1578+
original = a.tobytes()
1579+
a.byteswap()
1580+
itemsize = a.itemsize
1581+
half = itemsize // 2
1582+
expected = bytearray()
1583+
for i in range(0, len(original), itemsize):
1584+
item = original[i:i + itemsize]
1585+
expected += item[half - 1::-1] + item[itemsize - 1:half - 1:-1]
1586+
self.assertEqual(a.tobytes(), bytes(expected))
1587+
15721588

15731589
class HalfFloatTest(FPTest, unittest.TestCase):
15741590
example = [-42.0, 0, 42, 1e2, -1e4]
@@ -1611,20 +1627,6 @@ class ComplexDoubleTest(CFPTest, unittest.TestCase):
16111627
typecode = 'Zd'
16121628
minitemsize = 16
16131629

1614-
def test_byteswap_single_call_result(self):
1615-
# A single byteswap() must swap each 16-byte item's two 8-byte
1616-
# halves (real, imag) independently. The pre-existing test only
1617-
# checked that byteswap() twice round-trips to the original,
1618-
# which passes even if a single call scrambles multi-item arrays.
1619-
a = array.array(self.typecode, [1+2j, 3+4j, 5+6j])
1620-
original = a.tobytes()
1621-
a.byteswap()
1622-
expected = bytearray()
1623-
for i in range(0, len(original), 16):
1624-
item = original[i:i + 16]
1625-
expected += item[7::-1] + item[15:7:-1]
1626-
self.assertEqual(a.tobytes(), bytes(expected))
1627-
16281630

16291631
class LargeArrayTest(unittest.TestCase):
16301632
typecode = 'b'
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
Fix :meth:`array.array.byteswap` corrupting data for ``'Zd'`` (complex
22
double) arrays with more than one element: the 16-byte item loop advanced
33
the buffer pointer by only 8 bytes per iteration, causing items after the
4-
first to be scrambled. A single :meth:`!byteswap` call produced incorrect
5-
results while a double call happened to round-trip back to the original
6-
value, which is why the existing test suite did not catch it.
4+
first to be scrambled.

0 commit comments

Comments
 (0)