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
1 change: 1 addition & 0 deletions Doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@
('c:type', '__int64'),
('c:type', 'unsigned __int64'),
('c:type', 'double'),
('c:type', '_Float16'),
# Standard C structures
('c:struct', 'in6_addr'),
('c:struct', 'in_addr'),
Expand Down
34 changes: 31 additions & 3 deletions Doc/library/array.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
--------------

This module defines an object type which can compactly represent an array of
basic values: characters, integers, floating-point numbers. Arrays are mutable :term:`sequence`
basic values: characters, integers, floating-point numbers, complex numbers. Arrays are mutable :term:`sequence`
types and behave very much like lists, except that the type of objects stored in
them is constrained. The type is specified at object creation time by using a
:dfn:`type code`, which is a single character. The following type codes are
Expand Down Expand Up @@ -42,10 +42,17 @@ defined:
+-----------+--------------------+-------------------+-----------------------+-------+
| ``'Q'`` | unsigned long long | int | 8 | |
+-----------+--------------------+-------------------+-----------------------+-------+
| ``'e'`` | _Float16 | float | 2 | \(3) |
+-----------+--------------------+-------------------+-----------------------+-------+
| ``'f'`` | float | float | 4 | |
+-----------+--------------------+-------------------+-----------------------+-------+
| ``'d'`` | double | float | 8 | |
+-----------+--------------------+-------------------+-----------------------+-------+
| ``'F'`` | float complex | complex | 8 | \(4) |
+-----------+--------------------+-------------------+-----------------------+-------+
| ``'D'`` | double complex | complex | 16 | \(4) |
+-----------+--------------------+-------------------+-----------------------+-------+


Notes:

Expand All @@ -63,6 +70,24 @@ Notes:
(2)
.. versionadded:: 3.13

(3)
The IEEE 754 binary16 "half precision" type was introduced in the 2008
revision of the `IEEE 754 standard <ieee 754 standard_>`_.
This type is not widely supported by C compilers. It's available
as :c:expr:`_Float16` type, if the compiler supports the Annex H
of the C23 standard.

.. versionadded:: 3.15

(4)
Complex types (``F`` and ``D``) are available unconditionally,
regardless on support for complex types (the Annex G of the C11 standard)
by the C compiler.
As specified in the C11 standard, each complex type is represented by a
two-element C array containing, respectively, the real and imaginary parts.

.. versionadded:: 3.15


The actual representation of values is determined by the machine architecture
(strictly speaking, by the C implementation). The actual size can be accessed
Expand Down Expand Up @@ -139,9 +164,10 @@ The module defines the following type:
.. method:: byteswap()

"Byteswap" all items of the array. This is only supported for values which are
1, 2, 4, or 8 bytes in size; for other types of values, :exc:`RuntimeError` is
1, 2, 4, 8 or 16 bytes in size; for other types of values, :exc:`RuntimeError` is
raised. It is useful when reading data from a file written on a machine with a
different byte order.
different byte order. Note, that for complex types the order of
components (the real part, followed by imaginary part) is preserved.


.. method:: count(x)
Expand Down Expand Up @@ -282,3 +308,5 @@ Examples::

`NumPy <https://numpy.org/>`_
The NumPy package defines another array type.

.. _ieee 754 standard: https://en.wikipedia.org/wiki/IEEE_754-2008_revision
9 changes: 9 additions & 0 deletions Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,15 @@ argparse
(Contributed by Savannah Ostrowski in :gh:`142390`.)


array
-----

* Support half-floats (16-bit IEEE 754 binary interchange format, formatting
character ``'e'``), the :c:expr:`float complex` and :c:expr:`double complex`
C types (formatting characters ``'F'`` and ``'D'`` respectively).
(Contributed by Sergey B Kirpichev in :gh:`146151` and :gh:`146238`.)


base64
------

Expand Down
75 changes: 72 additions & 3 deletions Lib/test/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ArraySubclassWithKwargs(array.array):
def __init__(self, typecode, newarg=None):
array.array.__init__(self)

typecodes = 'uwbBhHiIlLfdqQ'
typecodes = 'uwbBhHiIlLfdqQFDe'

class MiscTest(unittest.TestCase):

Expand Down Expand Up @@ -113,6 +113,10 @@ def __index__(self):
UTF16_BE = 19
UTF32_LE = 20
UTF32_BE = 21
IEEE_754_FLOAT_COMPLEX_LE = 22
IEEE_754_FLOAT_COMPLEX_BE = 23
IEEE_754_DOUBLE_COMPLEX_LE = 24
IEEE_754_DOUBLE_COMPLEX_BE = 25


class ArrayReconstructorTest(unittest.TestCase):
Expand All @@ -139,7 +143,7 @@ def test_error(self):
self.assertRaises(ValueError, array_reconstructor,
array.array, "b", UNKNOWN_FORMAT, b"")
self.assertRaises(ValueError, array_reconstructor,
array.array, "b", 22, b"")
array.array, "b", 28, b"")
self.assertRaises(ValueError, array_reconstructor,
array.array, "d", 16, b"a")

Expand Down Expand Up @@ -279,7 +283,7 @@ def test_byteswap(self):
example = self.example
a = array.array(self.typecode, example)
self.assertRaises(TypeError, a.byteswap, 42)
if a.itemsize in (1, 2, 4, 8):
if a.itemsize in (1, 2, 4, 8, 16):
b = array.array(self.typecode, example)
b.byteswap()
if a.itemsize==1:
Expand Down Expand Up @@ -1525,6 +1529,62 @@ def test_byteswap(self):
b.byteswap()
self.assertEqual(a, b)

class CFPTest(NumberTest):
example = [-42j, 0, 42+1j, 1e5j, -1e10]
outside = 23

def assertEntryEqual(self, entry1, entry2):
self.assertAlmostEqual(entry1, entry2)

def test_cmp(self):
a = array.array(self.typecode, self.example)
self.assertIs(a == 42, False)
self.assertIs(a != 42, True)

self.assertIs(a == a, True)
self.assertIs(a != a, False)
self.assertIs(a < a, False)
self.assertIs(a <= a, True)
self.assertIs(a > a, False)
self.assertIs(a >= a, True)

self.assertIs(a == 2*a, False)
self.assertIs(a != 2*a, True)
self.assertIs(a < 2*a, True)
self.assertIs(a <= 2*a, True)
self.assertIs(a > 2*a, False)
self.assertIs(a >= 2*a, False)

def test_nan(self):
a = array.array(self.typecode, [float('nan')])
b = array.array(self.typecode, [float('nan')])
self.assertIs(a != b, True)
self.assertIs(a == b, False)

def test_byteswap(self):
a = array.array(self.typecode, self.example)
self.assertRaises(TypeError, a.byteswap, 42)
if a.itemsize in (1, 2, 4, 8):
b = array.array(self.typecode, self.example)
b.byteswap()
if a.itemsize==1:
self.assertEqual(a, b)
else:
# On alphas treating the byte swapped bit patterns as
# floats/doubles results in floating-point exceptions
# => compare the 8bit string values instead
self.assertNotEqual(a.tobytes(), b.tobytes())
b.byteswap()
self.assertEqual(a, b)


class HalfFloatTest(FPTest, unittest.TestCase):
example = [-42.0, 0, 42, 1e3, -1e3]
smallerexample = [-42.0, 0, 42, 1e3, -2e3]
biggerexample = [-42.0, 0, 42, 1e3, 1e3]
typecode = 'e'
minitemsize = 2

class FloatTest(FPTest, unittest.TestCase):
typecode = 'f'
minitemsize = 4
Expand All @@ -1551,6 +1611,15 @@ def test_alloc_overflow(self):
self.fail("Array of size > maxsize created - MemoryError expected")


class ComplexFloatTest(CFPTest, unittest.TestCase):
typecode = 'F'
minitemsize = 8

class ComplexDoubleTest(CFPTest, unittest.TestCase):
typecode = 'D'
minitemsize = 16


class LargeArrayTest(unittest.TestCase):
typecode = 'b'

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Support the :c:expr:`float complex` and :c:expr:`double complex` C types in
the :mod:`array` module (formatting characters ``'F'`` and ``'D'``
respectively). Patch by Sergey B Kirpichev.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Support half-floats (type code ``'e'`` of the :mod:`struct` module) in the
:mod:`array` module. Patch by Sergey B Kirpichev.
Loading
Loading