Skip to content

Commit 5dd5c8b

Browse files
authored
gh-148675: Remove F and D formats from array and memoryview (GH-149368)
1 parent b8ebd07 commit 5dd5c8b

8 files changed

Lines changed: 17 additions & 104 deletions

File tree

Doc/library/array.rst

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ defined:
4848
+-----------+--------------------+-------------------+-----------------------+-------+
4949
| ``'d'`` | double | float | 8 | |
5050
+-----------+--------------------+-------------------+-----------------------+-------+
51-
| ``'F'`` | float complex | complex | 8 | \(4) |
52-
+-----------+--------------------+-------------------+-----------------------+-------+
53-
| ``'D'`` | double complex | complex | 16 | \(4) |
54-
+-----------+--------------------+-------------------+-----------------------+-------+
5551
| ``'Zf'`` | float complex | complex | 8 | \(4) |
5652
+-----------+--------------------+-------------------+-----------------------+-------+
5753
| ``'Zd'`` | double complex | complex | 16 | \(4) |
@@ -84,7 +80,7 @@ Notes:
8480
.. versionadded:: 3.15
8581

8682
(4)
87-
Complex types (``F``, ``D``, ``Zf`` and ``Zd``) are available unconditionally,
83+
Complex types (``Zf`` and ``Zd``) are available unconditionally,
8884
regardless on support for complex types (the Annex G of the C11 standard)
8985
by the C compiler.
9086
As specified in the C11 standard, each complex type is represented by a

Doc/whatsnew/3.15.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -671,8 +671,8 @@ Other language changes
671671
(Contributed by James Hilton-Balfe in :gh:`128335`.)
672672

673673
* The class :class:`memoryview` now supports the :c:expr:`float complex` and
674-
:c:expr:`double complex` C types: formatting characters ``'F'``/``'Zf'``
675-
and ``'D'``/``'Zd'`` respectively.
674+
:c:expr:`double complex` C types: formatting characters ``'Zf'`` and ``'Zd'``
675+
respectively.
676676
(Contributed by Victor Stinner in :gh:`146151` and :gh:`148675`.)
677677

678678
* Allow the *count* argument of :meth:`bytes.replace` to be a keyword.
@@ -724,7 +724,7 @@ array
724724
-----
725725

726726
* Support the :c:expr:`float complex` and :c:expr:`double complex` C types:
727-
formatting characters ``'F'``/``'Zf'`` and ``'D'``/``'Zd'`` respectively.
727+
formatting characters ``'Zf'`` and ``'Zd'`` respectively.
728728
(Contributed by Victor Stinner in :gh:`146151` and :gh:`148675`.)
729729

730730
* Support half-floats (16-bit IEEE 754 binary interchange format): formatting

Lib/test/test_array.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def __init__(self, typecode, newarg=None):
3333

3434
typecodes = (
3535
'u', 'w', 'b', 'B', 'h', 'H', 'i', 'I', 'l', 'L',
36-
'f', 'd', 'q', 'Q', 'F', 'D', 'e', 'Zf', 'Zd')
36+
'f', 'd', 'q', 'Q', 'e', 'Zf', 'Zd')
3737

3838

3939
class MiscTest(unittest.TestCase):
@@ -210,14 +210,6 @@ def test_numbers(self):
210210
[9006104071832581.0, float('inf'), float('-inf'), -0.0]),
211211
(['d'], IEEE_754_DOUBLE_BE, '>dddd',
212212
[9006104071832581.0, float('inf'), float('-inf'), -0.0]),
213-
(['F'], IEEE_754_FLOAT_COMPLEX_LE, '<FFFF',
214-
[16711938.0j, float('inf'), complex('1-infj'), -0.0]),
215-
(['F'], IEEE_754_FLOAT_COMPLEX_BE, '>FFFF',
216-
[16711938.0j, float('inf'), complex('1-infj'), -0.0]),
217-
(['D'], IEEE_754_DOUBLE_COMPLEX_LE, '<DDDD',
218-
[9006104071832581.0j, float('inf'), complex('1-infj'), -0.0]),
219-
(['D'], IEEE_754_DOUBLE_COMPLEX_BE, '>DDDD',
220-
[9006104071832581.0j, float('inf'), complex('1-infj'), -0.0]),
221213
(['Zf'], IEEE_754_FLOAT_COMPLEX_LE, '<ZfZfZfZf',
222214
[16711938.0j, float('inf'), complex('1-infj'), -0.0]),
223215
(['Zf'], IEEE_754_FLOAT_COMPLEX_BE, '>ZfZfZfZf',
@@ -1645,18 +1637,10 @@ def test_alloc_overflow(self):
16451637

16461638

16471639
class ComplexFloatTest(CFPTest, unittest.TestCase):
1648-
typecode = 'F'
1649-
minitemsize = 8
1650-
1651-
class ComplexDoubleTest(CFPTest, unittest.TestCase):
1652-
typecode = 'D'
1653-
minitemsize = 16
1654-
1655-
class ComplexZfFloatTest(CFPTest, unittest.TestCase):
16561640
typecode = 'Zf'
16571641
minitemsize = 8
16581642

1659-
class ComplexZdDoubleTest(CFPTest, unittest.TestCase):
1643+
class ComplexDoubleTest(CFPTest, unittest.TestCase):
16601644
typecode = 'Zd'
16611645
minitemsize = 16
16621646

Lib/test/test_buffer.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
'h':0, 'H':0, 'i':0, 'I':0,
6868
'l':0, 'L':0, 'n':0, 'N':0,
6969
'e':0, 'f':0, 'd':0, 'P':0,
70-
'F':0, 'D':0, 'Zf':0, 'Zd':0,
70+
'Zf':0, 'Zd':0,
7171
}
7272

7373
# NumPy does not have 'n' or 'N':
@@ -94,8 +94,6 @@
9494
'q':(-(1<<63), 1<<63), 'Q':(0, 1<<64),
9595
'e':(-65519, 65520), 'f':(-(1<<63), 1<<63),
9696
'd':(-(1<<1023), 1<<1023),
97-
'F':(-(1<<63), 1<<63),
98-
'D':(-(1<<1023), 1<<1023),
9997
'Zf':(-(1<<63), 1<<63),
10098
'Zd':(-(1<<1023), 1<<1023),
10199
}
@@ -112,9 +110,9 @@ def native_type_range(fmt):
112110
lh = (-(1<<63), 1<<63)
113111
elif fmt == 'd':
114112
lh = (-(1<<1023), 1<<1023)
115-
elif fmt in ('F', 'Zf'):
113+
elif fmt == 'Zf':
116114
lh = (-(1<<63), 1<<63)
117-
elif fmt in ('D', 'Zd'):
115+
elif fmt == 'Zd':
118116
lh = (-(1<<1023), 1<<1023)
119117
else:
120118
for exp in (128, 127, 64, 63, 32, 31, 16, 15, 8, 7):
@@ -184,7 +182,7 @@ def randrange_fmt(mode, char, obj):
184182
if char in 'efd':
185183
x = struct.pack(char, x)
186184
x = struct.unpack(char, x)[0]
187-
if char in ('F', 'D', 'Zf', 'Zd'):
185+
if char in ('Zf', 'Zd'):
188186
y = randrange(*fmtdict[mode][char])
189187
x = complex(x, y)
190188
x = struct.pack(char, x)
@@ -3055,7 +3053,7 @@ def test_memoryview_assign(self):
30553053
continue
30563054
m2 = m1.cast(fmt)
30573055
lo, hi = _range
3058-
if fmt in ("d", "f", "D", "F", "Zd", "Zf"):
3056+
if fmt in ("d", "f", "Zd", "Zf"):
30593057
lo, hi = -2**1024, 2**1024
30603058
if fmt != 'P': # PyLong_AsVoidPtr() accepts negative numbers
30613059
self.assertRaises(ValueError, m2.__setitem__, 0, lo-1)

Lib/test/test_memoryview.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ def check_equal(view, is_equal):
637637
check_equal(m, True)
638638

639639
# Test complex formats
640-
for complex_format in ('F', 'D', 'Zf', 'Zd'):
640+
for complex_format in ('Zf', 'Zd'):
641641
with self.subTest(format=complex_format):
642642
data = struct.pack(complex_format * 3, 1.0, 2.0, float('nan'))
643643
m = memoryview(data).cast(complex_format)
@@ -719,10 +719,6 @@ def test_half_float(self):
719719
def test_complex_types(self):
720720
float_complex_data = struct.pack('FFF', 0.0, -1.5j, 1+2j)
721721
double_complex_data = struct.pack('DDD', 0.0, -1.5j, 1+2j)
722-
float_complex_view = memoryview(float_complex_data).cast('F')
723-
double_complex_view = memoryview(double_complex_data).cast('D')
724-
self.assertEqual(float_complex_view.nbytes * 2, double_complex_view.nbytes)
725-
self.assertListEqual(float_complex_view.tolist(), double_complex_view.tolist())
726722
float_complex_view = memoryview(float_complex_data).cast('Zf')
727723
double_complex_view = memoryview(double_complex_data).cast('Zd')
728724
self.assertEqual(float_complex_view.nbytes * 2, double_complex_view.nbytes)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Remove ``F`` and ``D`` formats from :mod:`array` and :class:`memoryview`.
2+
Patch by Victor Stinner.

Modules/arraymodule.c

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -782,8 +782,6 @@ static const struct arraydescr descriptors[] = {
782782
{"e", sizeof(short), e_getitem, e_setitem, NULL, 0, 0},
783783
{"f", sizeof(float), f_getitem, f_setitem, NULL, 0, 0},
784784
{"d", sizeof(double), d_getitem, d_setitem, NULL, 0, 0},
785-
{"F", 2*sizeof(float), cf_getitem, cf_setitem, NULL, 0, 0},
786-
{"D", 2*sizeof(double), cd_getitem, cd_setitem, NULL, 0, 0},
787785
{"Zf", 2*sizeof(float), cf_getitem, cf_setitem, NULL, 0, 0},
788786
{"Zd", 2*sizeof(double), cd_getitem, cd_setitem, NULL, 0, 0},
789787
{NULL, 0, 0, 0, 0, 0, 0} /* Sentinel */
@@ -1612,9 +1610,7 @@ array_array_byteswap_impl(arrayobject *self)
16121610
}
16131611
break;
16141612
case 8:
1615-
if (strcmp(self->ob_descr->typecode, "F") != 0
1616-
&& strcmp(self->ob_descr->typecode, "Zf") != 0)
1617-
{
1613+
if (strcmp(self->ob_descr->typecode, "Zf") != 0) {
16181614
for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 8) {
16191615
char p0 = p[0];
16201616
char p1 = p[1];
@@ -1648,8 +1644,7 @@ array_array_byteswap_impl(arrayobject *self)
16481644
}
16491645
break;
16501646
case 16:
1651-
assert(strcmp(self->ob_descr->typecode, "D") == 0
1652-
|| strcmp(self->ob_descr->typecode, "Zd") == 0);
1647+
assert(strcmp(self->ob_descr->typecode, "Zd") == 0);
16531648
for (p = self->ob_item, i = Py_SIZE(self); --i >= 0; p += 8) {
16541649
char t0 = p[0];
16551650
char t1 = p[1];
@@ -2159,14 +2154,6 @@ typecode_to_mformat_code(const char *typecode)
21592154
case 'd':
21602155
return _PY_FLOAT_BIG_ENDIAN ? IEEE_754_DOUBLE_BE : IEEE_754_DOUBLE_LE;
21612156

2162-
case 'F':
2163-
return _PY_FLOAT_BIG_ENDIAN ? \
2164-
IEEE_754_FLOAT_COMPLEX_BE : IEEE_754_FLOAT_COMPLEX_LE;
2165-
2166-
case 'D':
2167-
return _PY_FLOAT_BIG_ENDIAN ? \
2168-
IEEE_754_DOUBLE_COMPLEX_BE : IEEE_754_DOUBLE_COMPLEX_LE;
2169-
21702157
case 'Z': {
21712158
switch (typecode[1]) {
21722159
case 'f':
@@ -3167,7 +3154,7 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
31673154
}
31683155
Py_XDECREF(it);
31693156
PyErr_SetString(PyExc_ValueError,
3170-
"bad typecode (must be b, B, u, w, h, H, i, I, l, L, q, Q, e, f, d, F, D, Zf or Zd)");
3157+
"bad typecode (must be b, B, u, w, h, H, i, I, l, L, q, Q, e, f, d, Zf or Zd)");
31713158
return NULL;
31723159
}
31733160

@@ -3205,8 +3192,6 @@ The following type codes are defined:\n\
32053192
'e' 16-bit IEEE floats 2\n\
32063193
'f' floating-point 4\n\
32073194
'd' floating-point 8\n\
3208-
'F' float complex 8\n\
3209-
'D' double complex 16\n\
32103195
'Zf' float complex 8\n\
32113196
'Zd' double complex 16\n\
32123197
\n\

Objects/memoryobject.c

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,8 +1217,6 @@ get_native_fmtchar(const char **result, const char *fmt)
12171217
case 'f': size = sizeof(float); break;
12181218
case 'd': size = sizeof(double); break;
12191219
case 'e': size = sizeof(float) / 2; break;
1220-
case 'F': size = 2*sizeof(float); break;
1221-
case 'D': size = 2*sizeof(double); break;
12221220
case '?': size = sizeof(_Bool); break;
12231221
case 'P': size = sizeof(void *); break;
12241222
case 'Z': {
@@ -1284,8 +1282,6 @@ get_native_fmtstr(const char *fmt)
12841282
case 'f': RETURN("f");
12851283
case 'd': RETURN("d");
12861284
case 'e': RETURN("e");
1287-
case 'F': RETURN("F");
1288-
case 'D': RETURN("D");
12891285
case 'Z': {
12901286
switch (fmt[1]) {
12911287
case 'f': RETURN("Zf");
@@ -1865,16 +1861,6 @@ unpack_single(PyMemoryViewObject *self, const char *ptr, const char *fmt)
18651861
case 'e': d[0] = PyFloat_Unpack2(ptr, endian); goto convert_double;
18661862

18671863
/* complexes */
1868-
case 'F':
1869-
d[0] = PyFloat_Unpack4(ptr, endian);
1870-
d[1] = PyFloat_Unpack4(ptr + sizeof(float), endian);
1871-
goto convert_double_complex;
1872-
1873-
case 'D':
1874-
d[0] = PyFloat_Unpack8(ptr, endian);
1875-
d[1] = PyFloat_Unpack8(ptr + sizeof(double), endian);
1876-
goto convert_double_complex;
1877-
18781864
case 'Z': {
18791865
switch (fmt[1]) {
18801866
case 'f':
@@ -2055,24 +2041,6 @@ pack_single(PyMemoryViewObject *self, char *ptr, PyObject *item, const char *fmt
20552041
break;
20562042

20572043
/* complexes */
2058-
case 'F': case 'D':
2059-
c = PyComplex_AsCComplex(item);
2060-
if (c.real == -1.0 && PyErr_Occurred()) {
2061-
goto err_occurred;
2062-
}
2063-
CHECK_RELEASED_INT_AGAIN(self);
2064-
if (fmt[0] == 'D') {
2065-
double x[2] = {c.real, c.imag};
2066-
2067-
memcpy(ptr, &x, sizeof(x));
2068-
}
2069-
else {
2070-
float x[2] = {(float)c.real, (float)c.imag};
2071-
2072-
memcpy(ptr, &x, sizeof(x));
2073-
}
2074-
break;
2075-
20762044
case 'Z': {
20772045
switch (fmt[1]) {
20782046
case 'f': case 'd':
@@ -3138,22 +3106,6 @@ unpack_cmp(const char *p, const char *q, const char *fmt,
31383106
}
31393107

31403108
/* complexes */
3141-
case 'F':
3142-
{
3143-
float x[2], y[2];
3144-
3145-
memcpy(&x, p, sizeof(x));
3146-
memcpy(&y, q, sizeof(y));
3147-
return (x[0] == y[0]) && (x[1] == y[1]);
3148-
}
3149-
case 'D':
3150-
{
3151-
double x[2], y[2];
3152-
3153-
memcpy(&x, p, sizeof(x));
3154-
memcpy(&y, q, sizeof(y));
3155-
return (x[0] == y[0]) && (x[1] == y[1]);
3156-
}
31573109
case 'Z': {
31583110
switch (fmt[1]) {
31593111
case 'f':

0 commit comments

Comments
 (0)