Skip to content
Open
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
21 changes: 3 additions & 18 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, complex numbers. Arrays are mutable :term:`sequence`
basic values: characters, integers, floating-point 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 @@ -48,11 +48,6 @@ defined:
+-----------+--------------------+-------------------+-----------------------+-------+
| ``'d'`` | double | float | 8 | |
+-----------+--------------------+-------------------+-----------------------+-------+
| ``'F'`` | float complex | complex | 8 | \(4) |
+-----------+--------------------+-------------------+-----------------------+-------+
| ``'D'`` | double complex | complex | 16 | \(4) |
+-----------+--------------------+-------------------+-----------------------+-------+


Notes:

Expand All @@ -79,15 +74,6 @@ Notes:

.. 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

.. seealso::

The :ref:`ctypes <ctypes-fundamental-data-types>` and
Expand Down Expand Up @@ -171,10 +157,9 @@ 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, 8 or 16 bytes in size; for other types of values, :exc:`RuntimeError` is
1, 2, 4, or 8 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. Note, that for complex types the order of
components (the real part, followed by imaginary part) is preserved.
different byte order.


.. method:: count(value, /)
Expand Down
9 changes: 0 additions & 9 deletions Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -636,11 +636,6 @@ Other language changes
making it a :term:`generic type`.
(Contributed by James Hilton-Balfe in :gh:`128335`.)

* The class :class:`memoryview` now supports 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`.)

* Allow the *count* argument of :meth:`bytes.replace` to be a keyword.
(Contributed by Stan Ulbrych in :gh:`147856`.)

Expand Down Expand Up @@ -678,10 +673,6 @@ argparse
array
-----

* Support 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`.)

* Support half-floats (16-bit IEEE 754 binary interchange format): formatting
character ``'e'``.
(Contributed by Sergey B Kirpichev in :gh:`146238`.)
Expand Down
81 changes: 6 additions & 75 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 = 'uwbBhHiIlLfdqQFDe'
typecodes = 'uwbBhHiIlLfdqQe'

class MiscTest(unittest.TestCase):

Expand Down Expand Up @@ -113,14 +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
IEEE_754_FLOAT16_LE = 26
IEEE_754_FLOAT16_BE = 27
IEEE_754_FLOAT16_LE = 22
IEEE_754_FLOAT16_BE = 23

MACHINE_FORMAT_CODE_MAX = 27
MACHINE_FORMAT_CODE_MAX = 23


class ArrayReconstructorTest(unittest.TestCase):
Expand Down Expand Up @@ -199,15 +195,7 @@ def test_numbers(self):
(['d'], IEEE_754_DOUBLE_LE, '<dddd',
[9006104071832581.0, float('inf'), float('-inf'), -0.0]),
(['d'], IEEE_754_DOUBLE_BE, '>dddd',
[9006104071832581.0, float('inf'), float('-inf'), -0.0]),
(['F'], IEEE_754_FLOAT_COMPLEX_LE, '<FFFF',
[16711938.0j, float('inf'), complex('1-infj'), -0.0]),
(['F'], IEEE_754_FLOAT_COMPLEX_BE, '>FFFF',
[16711938.0j, float('inf'), complex('1-infj'), -0.0]),
(['D'], IEEE_754_DOUBLE_COMPLEX_LE, '<DDDD',
[9006104071832581.0j, float('inf'), complex('1-infj'), -0.0]),
(['D'], IEEE_754_DOUBLE_COMPLEX_BE, '>DDDD',
[9006104071832581.0j, float('inf'), complex('1-infj'), -0.0]),
[9006104071832581.0, float('inf'), float('-inf'), -0.0])
)
for testcase in testcases:
valid_typecodes, mformat_code, struct_fmt, values = testcase
Expand Down Expand Up @@ -295,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, 16):
if a.itemsize in (1, 2, 4, 8):
b = array.array(self.typecode, example)
b.byteswap()
if a.itemsize==1:
Expand Down Expand Up @@ -1541,54 +1529,6 @@ 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, 1e2, -1e4]
Expand Down Expand Up @@ -1623,15 +1563,6 @@ 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
20 changes: 4 additions & 16 deletions Lib/test/test_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@
'?':0, 'c':0, 'b':0, 'B':0,
'h':0, 'H':0, 'i':0, 'I':0,
'l':0, 'L':0, 'n':0, 'N':0,
'e':0, 'f':0, 'd':0, 'P':0,
'F':0, 'D':0
'e':0, 'f':0, 'd':0, 'P':0
}

# NumPy does not have 'n' or 'N':
Expand All @@ -93,9 +92,7 @@
'l':(-(1<<31), 1<<31), 'L':(0, 1<<32),
'q':(-(1<<63), 1<<63), 'Q':(0, 1<<64),
'e':(-65519, 65520), 'f':(-(1<<63), 1<<63),
'd':(-(1<<1023), 1<<1023),
'F':(-(1<<63), 1<<63),
'D':(-(1<<1023), 1<<1023)
'd':(-(1<<1023), 1<<1023)
}

def native_type_range(fmt):
Expand All @@ -110,10 +107,6 @@ def native_type_range(fmt):
lh = (-(1<<63), 1<<63)
elif fmt == 'd':
lh = (-(1<<1023), 1<<1023)
elif fmt == 'F':
lh = (-(1<<63), 1<<63)
elif fmt == 'D':
lh = (-(1<<1023), 1<<1023)
else:
for exp in (128, 127, 64, 63, 32, 31, 16, 15, 8, 7):
try:
Expand Down Expand Up @@ -182,11 +175,6 @@ def randrange_fmt(mode, char, obj):
if char in 'efd':
x = struct.pack(char, x)
x = struct.unpack(char, x)[0]
if char in 'FD':
y = randrange(*fmtdict[mode][char])
x = complex(x, y)
x = struct.pack(char, x)
x = struct.unpack(char, x)[0]
return x

def gen_item(fmt, obj):
Expand Down Expand Up @@ -3027,7 +3015,7 @@ def test_memoryview_assign(self):
m = memoryview(nd)
self.assertRaises(TypeError, m.__setitem__, 0, 100)

ex = ndarray(list(range(144)), shape=[1,2,3,4,6], flags=ND_WRITABLE)
ex = ndarray(list(range(120)), shape=[1,2,3,4,5], flags=ND_WRITABLE)
m1 = memoryview(ex)

for fmt, _range in fmtdict['@'].items():
Expand All @@ -3037,7 +3025,7 @@ def test_memoryview_assign(self):
continue
m2 = m1.cast(fmt)
lo, hi = _range
if fmt in "dfDF":
if fmt == 'd' or fmt == 'f':
lo, hi = -2**1024, 2**1024
if fmt != 'P': # PyLong_AsVoidPtr() accepts negative numbers
self.assertRaises(ValueError, m2.__setitem__, 0, lo-1)
Expand Down
20 changes: 0 additions & 20 deletions Lib/test/test_memoryview.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,18 +636,6 @@ def check_equal(view, is_equal):
m = memoryview(a)
check_equal(m, True)

# Test complex formats
for complex_format in 'FD':
with self.subTest(format=complex_format):
data = struct.pack(complex_format * 3, 1.0, 2.0, float('nan'))
m = memoryview(data).cast(complex_format)
# nan is not equal to nan
check_equal(m, False)

data = struct.pack(complex_format * 3, 1.0, 2.0, 3.0)
m = memoryview(data).cast(complex_format)
check_equal(m, True)

def test_boolean_format(self):
# Test '?' format (keep all the checks below for UBSan)
# See github.com/python/cpython/issues/148390.
Expand Down Expand Up @@ -716,14 +704,6 @@ def test_half_float(self):
self.assertEqual(half_view.nbytes * 2, float_view.nbytes)
self.assertListEqual(half_view.tolist(), float_view.tolist())

def test_complex_types(self):
float_complex_data = struct.pack('FFF', 0.0, -1.5j, 1+2j)
double_complex_data = struct.pack('DDD', 0.0, -1.5j, 1+2j)
float_complex_view = memoryview(float_complex_data).cast('F')
double_complex_view = memoryview(double_complex_data).cast('D')
self.assertEqual(float_complex_view.nbytes * 2, double_complex_view.nbytes)
self.assertListEqual(float_complex_view.tolist(), double_complex_view.tolist())

def test_memoryview_hex(self):
# Issue #9951: memoryview.hex() segfaults with non-contiguous buffers.
x = b'0' * 200000
Expand Down
4 changes: 2 additions & 2 deletions Modules/_testbuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ pack_from_list(PyObject *obj, PyObject *items, PyObject *format,

item = PySequence_Fast_GET_ITEM(items, i);
if ((PyBytes_Check(item) || PyLong_Check(item) ||
PyFloat_Check(item) || PyComplex_Check(item)) && nmemb == 1) {
PyFloat_Check(item)) && nmemb == 1) {
PyTuple_SET_ITEM(args, 2, item);
}
else if ((PyList_Check(item) || PyTuple_Check(item)) &&
Expand Down Expand Up @@ -433,7 +433,7 @@ pack_single(char *ptr, PyObject *item, const char *fmt, Py_ssize_t itemsize)
PyTuple_SET_ITEM(args, 1, zero);

if ((PyBytes_Check(item) || PyLong_Check(item) ||
PyFloat_Check(item) || PyComplex_Check(item)) && nmemb == 1) {
PyFloat_Check(item)) && nmemb == 1) {
PyTuple_SET_ITEM(args, 2, item);
}
else if ((PyList_Check(item) || PyTuple_Check(item)) &&
Expand Down
Loading
Loading