Skip to content

Commit 760e2f0

Browse files
committed
Revert "gh-146151: memoryview supports 'F' and 'D' format types (complex) (#146241)"
This reverts commit c68a194.
1 parent 5c606ee commit 760e2f0

File tree

5 files changed

+11
-103
lines changed

5 files changed

+11
-103
lines changed

Doc/whatsnew/3.15.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -636,11 +636,6 @@ Other language changes
636636
making it a :term:`generic type`.
637637
(Contributed by James Hilton-Balfe in :gh:`128335`.)
638638

639-
* The class :class:`memoryview` now supports the :c:expr:`float complex` and
640-
:c:expr:`double complex` C types: formatting characters ``'F'`` and ``'D'``
641-
respectively.
642-
(Contributed by Sergey B Kirpichev in :gh:`146151`.)
643-
644639
* Allow the *count* argument of :meth:`bytes.replace` to be a keyword.
645640
(Contributed by Stan Ulbrych in :gh:`147856`.)
646641

Lib/test/test_buffer.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@
6666
'?':0, 'c':0, 'b':0, 'B':0,
6767
'h':0, 'H':0, 'i':0, 'I':0,
6868
'l':0, 'L':0, 'n':0, 'N':0,
69-
'e':0, 'f':0, 'd':0, 'P':0,
70-
'F':0, 'D':0
69+
'e':0, 'f':0, 'd':0, 'P':0
7170
}
7271

7372
# NumPy does not have 'n' or 'N':
@@ -93,9 +92,7 @@
9392
'l':(-(1<<31), 1<<31), 'L':(0, 1<<32),
9493
'q':(-(1<<63), 1<<63), 'Q':(0, 1<<64),
9594
'e':(-65519, 65520), 'f':(-(1<<63), 1<<63),
96-
'd':(-(1<<1023), 1<<1023),
97-
'F':(-(1<<63), 1<<63),
98-
'D':(-(1<<1023), 1<<1023)
95+
'd':(-(1<<1023), 1<<1023)
9996
}
10097

10198
def native_type_range(fmt):
@@ -110,10 +107,6 @@ def native_type_range(fmt):
110107
lh = (-(1<<63), 1<<63)
111108
elif fmt == 'd':
112109
lh = (-(1<<1023), 1<<1023)
113-
elif fmt == 'F':
114-
lh = (-(1<<63), 1<<63)
115-
elif fmt == 'D':
116-
lh = (-(1<<1023), 1<<1023)
117110
else:
118111
for exp in (128, 127, 64, 63, 32, 31, 16, 15, 8, 7):
119112
try:
@@ -182,11 +175,6 @@ def randrange_fmt(mode, char, obj):
182175
if char in 'efd':
183176
x = struct.pack(char, x)
184177
x = struct.unpack(char, x)[0]
185-
if char in 'FD':
186-
y = randrange(*fmtdict[mode][char])
187-
x = complex(x, y)
188-
x = struct.pack(char, x)
189-
x = struct.unpack(char, x)[0]
190178
return x
191179

192180
def gen_item(fmt, obj):
@@ -3027,7 +3015,7 @@ def test_memoryview_assign(self):
30273015
m = memoryview(nd)
30283016
self.assertRaises(TypeError, m.__setitem__, 0, 100)
30293017

3030-
ex = ndarray(list(range(144)), shape=[1,2,3,4,6], flags=ND_WRITABLE)
3018+
ex = ndarray(list(range(120)), shape=[1,2,3,4,5], flags=ND_WRITABLE)
30313019
m1 = memoryview(ex)
30323020

30333021
for fmt, _range in fmtdict['@'].items():
@@ -3037,7 +3025,7 @@ def test_memoryview_assign(self):
30373025
continue
30383026
m2 = m1.cast(fmt)
30393027
lo, hi = _range
3040-
if fmt in "dfDF":
3028+
if fmt == 'd' or fmt == 'f':
30413029
lo, hi = -2**1024, 2**1024
30423030
if fmt != 'P': # PyLong_AsVoidPtr() accepts negative numbers
30433031
self.assertRaises(ValueError, m2.__setitem__, 0, lo-1)

Lib/test/test_memoryview.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -636,18 +636,6 @@ def check_equal(view, is_equal):
636636
m = memoryview(a)
637637
check_equal(m, True)
638638

639-
# Test complex formats
640-
for complex_format in 'FD':
641-
with self.subTest(format=complex_format):
642-
data = struct.pack(complex_format * 3, 1.0, 2.0, float('nan'))
643-
m = memoryview(data).cast(complex_format)
644-
# nan is not equal to nan
645-
check_equal(m, False)
646-
647-
data = struct.pack(complex_format * 3, 1.0, 2.0, 3.0)
648-
m = memoryview(data).cast(complex_format)
649-
check_equal(m, True)
650-
651639
def test_boolean_format(self):
652640
# Test '?' format (keep all the checks below for UBSan)
653641
# See github.com/python/cpython/issues/148390.
@@ -716,14 +704,6 @@ def test_half_float(self):
716704
self.assertEqual(half_view.nbytes * 2, float_view.nbytes)
717705
self.assertListEqual(half_view.tolist(), float_view.tolist())
718706

719-
def test_complex_types(self):
720-
float_complex_data = struct.pack('FFF', 0.0, -1.5j, 1+2j)
721-
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())
726-
727707
def test_memoryview_hex(self):
728708
# Issue #9951: memoryview.hex() segfaults with non-contiguous buffers.
729709
x = b'0' * 200000

Modules/_testbuffer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ pack_from_list(PyObject *obj, PyObject *items, PyObject *format,
351351

352352
item = PySequence_Fast_GET_ITEM(items, i);
353353
if ((PyBytes_Check(item) || PyLong_Check(item) ||
354-
PyFloat_Check(item) || PyComplex_Check(item)) && nmemb == 1) {
354+
PyFloat_Check(item)) && nmemb == 1) {
355355
PyTuple_SET_ITEM(args, 2, item);
356356
}
357357
else if ((PyList_Check(item) || PyTuple_Check(item)) &&
@@ -433,7 +433,7 @@ pack_single(char *ptr, PyObject *item, const char *fmt, Py_ssize_t itemsize)
433433
PyTuple_SET_ITEM(args, 1, zero);
434434

435435
if ((PyBytes_Check(item) || PyLong_Check(item) ||
436-
PyFloat_Check(item) || PyComplex_Check(item)) && nmemb == 1) {
436+
PyFloat_Check(item)) && nmemb == 1) {
437437
PyTuple_SET_ITEM(args, 2, item);
438438
}
439439
else if ((PyList_Check(item) || PyTuple_Check(item)) &&

Objects/memoryobject.c

Lines changed: 5 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,8 +1216,6 @@ get_native_fmtchar(char *result, const char *fmt)
12161216
case 'f': size = sizeof(float); break;
12171217
case 'd': size = sizeof(double); break;
12181218
case 'e': size = sizeof(float) / 2; break;
1219-
case 'F': size = 2*sizeof(float); break;
1220-
case 'D': size = 2*sizeof(double); break;
12211219
case '?': size = sizeof(_Bool); break;
12221220
case 'P': size = sizeof(void *); break;
12231221
}
@@ -1262,8 +1260,6 @@ get_native_fmtstr(const char *fmt)
12621260
case 'f': RETURN("f");
12631261
case 'd': RETURN("d");
12641262
case 'e': RETURN("e");
1265-
case 'F': RETURN("F");
1266-
case 'D': RETURN("D");
12671263
case '?': RETURN("?");
12681264
case 'P': RETURN("P");
12691265
}
@@ -1793,7 +1789,7 @@ unpack_single(PyMemoryViewObject *self, const char *ptr, const char *fmt)
17931789
long long lld;
17941790
long ld;
17951791
Py_ssize_t zd;
1796-
double d[2];
1792+
double d;
17971793
unsigned char uc;
17981794
void *p;
17991795

@@ -1831,20 +1827,9 @@ unpack_single(PyMemoryViewObject *self, const char *ptr, const char *fmt)
18311827
case 'N': UNPACK_SINGLE(zu, ptr, size_t); goto convert_zu;
18321828

18331829
/* floats */
1834-
case 'f': UNPACK_SINGLE(d[0], ptr, float); goto convert_double;
1835-
case 'd': UNPACK_SINGLE(d[0], ptr, double); goto convert_double;
1836-
case 'e': d[0] = PyFloat_Unpack2(ptr, endian); goto convert_double;
1837-
1838-
/* complexes */
1839-
case 'F':
1840-
d[0] = PyFloat_Unpack4(ptr, endian);
1841-
d[1] = PyFloat_Unpack4(ptr + sizeof(float), endian);
1842-
goto convert_double_complex;
1843-
1844-
case 'D':
1845-
d[0] = PyFloat_Unpack8(ptr, endian);
1846-
d[1] = PyFloat_Unpack8(ptr + sizeof(double), endian);
1847-
goto convert_double_complex;
1830+
case 'f': UNPACK_SINGLE(d, ptr, float); goto convert_double;
1831+
case 'd': UNPACK_SINGLE(d, ptr, double); goto convert_double;
1832+
case 'e': d = PyFloat_Unpack2(ptr, endian); goto convert_double;
18481833

18491834
/* bytes object */
18501835
case 'c': goto convert_bytes;
@@ -1872,9 +1857,7 @@ unpack_single(PyMemoryViewObject *self, const char *ptr, const char *fmt)
18721857
convert_zu:
18731858
return PyLong_FromSize_t(zu);
18741859
convert_double:
1875-
return PyFloat_FromDouble(d[0]);
1876-
convert_double_complex:
1877-
return PyComplex_FromDoubles(d[0], d[1]);
1860+
return PyFloat_FromDouble(d);
18781861
convert_bool:
18791862
return PyBool_FromLong(ld);
18801863
convert_bytes:
@@ -1906,7 +1889,6 @@ pack_single(PyMemoryViewObject *self, char *ptr, PyObject *item, const char *fmt
19061889
long ld;
19071890
Py_ssize_t zd;
19081891
double d;
1909-
Py_complex c;
19101892
void *p;
19111893

19121894
#if PY_LITTLE_ENDIAN
@@ -2008,25 +1990,6 @@ pack_single(PyMemoryViewObject *self, char *ptr, PyObject *item, const char *fmt
20081990
}
20091991
break;
20101992

2011-
/* complexes */
2012-
case 'F': case 'D':
2013-
c = PyComplex_AsCComplex(item);
2014-
if (c.real == -1.0 && PyErr_Occurred()) {
2015-
goto err_occurred;
2016-
}
2017-
CHECK_RELEASED_INT_AGAIN(self);
2018-
if (fmt[0] == 'D') {
2019-
double x[2] = {c.real, c.imag};
2020-
2021-
memcpy(ptr, &x, sizeof(x));
2022-
}
2023-
else {
2024-
float x[2] = {(float)c.real, (float)c.imag};
2025-
2026-
memcpy(ptr, &x, sizeof(x));
2027-
}
2028-
break;
2029-
20301993
/* bool */
20311994
case '?':
20321995
ld = PyObject_IsTrue(item);
@@ -3064,24 +3027,6 @@ unpack_cmp(const char *p, const char *q, char fmt,
30643027
return (u == v);
30653028
}
30663029

3067-
/* complexes */
3068-
case 'F':
3069-
{
3070-
float x[2], y[2];
3071-
3072-
memcpy(&x, p, sizeof(x));
3073-
memcpy(&y, q, sizeof(y));
3074-
return (x[0] == y[0]) && (x[1] == y[1]);
3075-
}
3076-
case 'D':
3077-
{
3078-
double x[2], y[2];
3079-
3080-
memcpy(&x, p, sizeof(x));
3081-
memcpy(&y, q, sizeof(y));
3082-
return (x[0] == y[0]) && (x[1] == y[1]);
3083-
}
3084-
30853030
/* bytes object */
30863031
case 'c': return *p == *q;
30873032

0 commit comments

Comments
 (0)