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`.)
+
New modules
===========
'?':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
+ 'e':0, 'f':0, 'd':0, 'P':0,
+ 'F':0, 'D':0
}
# NumPy does not have 'n' or 'N':
'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)
+ 'd':(-(1<<1023), 1<<1023),
+ 'F':(-(1<<63), 1<<63),
+ 'D':(-(1<<1023), 1<<1023)
}
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:
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):
m = memoryview(nd)
self.assertRaises(TypeError, m.__setitem__, 0, 100)
- ex = ndarray(list(range(120)), shape=[1,2,3,4,5], flags=ND_WRITABLE)
+ ex = ndarray(list(range(144)), shape=[1,2,3,4,6], flags=ND_WRITABLE)
m1 = memoryview(ex)
for fmt, _range in fmtdict['@'].items():
continue
m2 = m1.cast(fmt)
lo, hi = _range
- if fmt == 'd' or fmt == 'f':
+ if fmt in "dfDF":
lo, hi = -2**1024, 2**1024
if fmt != 'P': # PyLong_AsVoidPtr() accepts negative numbers
self.assertRaises(ValueError, m2.__setitem__, 0, lo-1)
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)
+
class BytesMemorySliceTest(unittest.TestCase,
BaseMemorySliceTests, BaseBytesMemoryTests):
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
--- /dev/null
+:class:`memoryview` now supports the :c:expr:`float complex` and
+:c:expr:`double complex` C types: formatting characters ``'F'`` and ``'D'``
+respectively. Patch by Sergey B Kirpichev.
item = PySequence_Fast_GET_ITEM(items, i);
if ((PyBytes_Check(item) || PyLong_Check(item) ||
- PyFloat_Check(item)) && nmemb == 1) {
+ PyFloat_Check(item) || PyComplex_Check(item)) && nmemb == 1) {
PyTuple_SET_ITEM(args, 2, item);
}
else if ((PyList_Check(item) || PyTuple_Check(item)) &&
PyTuple_SET_ITEM(args, 1, zero);
if ((PyBytes_Check(item) || PyLong_Check(item) ||
- PyFloat_Check(item)) && nmemb == 1) {
+ PyFloat_Check(item) || PyComplex_Check(item)) && nmemb == 1) {
PyTuple_SET_ITEM(args, 2, item);
}
else if ((PyList_Check(item) || PyTuple_Check(item)) &&
case 'f': size = sizeof(float); break;
case 'd': size = sizeof(double); break;
case 'e': size = sizeof(float) / 2; break;
+ case 'F': size = 2*sizeof(float); break;
+ case 'D': size = 2*sizeof(double); break;
case '?': size = sizeof(_Bool); break;
case 'P': size = sizeof(void *); break;
}
case 'f': RETURN("f");
case 'd': RETURN("d");
case 'e': RETURN("e");
+ case 'F': RETURN("F");
+ case 'D': RETURN("D");
case '?': RETURN("?");
case 'P': RETURN("P");
}
long long lld;
long ld;
Py_ssize_t zd;
- double d;
+ double d[2];
unsigned char uc;
void *p;
case 'N': UNPACK_SINGLE(zu, ptr, size_t); goto convert_zu;
/* floats */
- case 'f': UNPACK_SINGLE(d, ptr, float); goto convert_double;
- case 'd': UNPACK_SINGLE(d, ptr, double); goto convert_double;
- case 'e': d = PyFloat_Unpack2(ptr, endian); goto convert_double;
+ case 'f': UNPACK_SINGLE(d[0], ptr, float); goto convert_double;
+ case 'd': UNPACK_SINGLE(d[0], ptr, double); goto convert_double;
+ case 'e': d[0] = PyFloat_Unpack2(ptr, endian); goto convert_double;
+
+ /* complexes */
+ case 'F':
+ d[0] = PyFloat_Unpack4(ptr, endian);
+ d[1] = PyFloat_Unpack4(ptr + sizeof(float), endian);
+ goto convert_double_complex;
+
+ case 'D':
+ d[0] = PyFloat_Unpack8(ptr, endian);
+ d[1] = PyFloat_Unpack8(ptr + sizeof(double), endian);
+ goto convert_double_complex;
/* bytes object */
case 'c': goto convert_bytes;
convert_zu:
return PyLong_FromSize_t(zu);
convert_double:
- return PyFloat_FromDouble(d);
+ return PyFloat_FromDouble(d[0]);
+convert_double_complex:
+ return PyComplex_FromDoubles(d[0], d[1]);
convert_bool:
return PyBool_FromLong(ld);
convert_bytes:
long ld;
Py_ssize_t zd;
double d;
+ Py_complex c;
void *p;
#if PY_LITTLE_ENDIAN
}
break;
+ /* complexes */
+ case 'F': case 'D':
+ c = PyComplex_AsCComplex(item);
+ if (c.real == -1.0 && PyErr_Occurred()) {
+ goto err_occurred;
+ }
+ CHECK_RELEASED_INT_AGAIN(self);
+ if (fmt[0] == 'D') {
+ double x[2] = {c.real, c.imag};
+
+ memcpy(ptr, &x, sizeof(x));
+ }
+ else {
+ float x[2] = {(float)c.real, (float)c.imag};
+
+ memcpy(ptr, &x, sizeof(x));
+ }
+ break;
+
/* bool */
case '?':
ld = PyObject_IsTrue(item);
return (u == v);
}
+ /* complexes */
+ case 'F':
+ {
+ float x[2], y[2];
+
+ memcpy(&x, p, sizeof(x));
+ memcpy(&y, q, sizeof(y));
+ return (x[0] == y[0]) && (x[1] == y[1]);
+ }
+ case 'D':
+ {
+ double x[2], y[2];
+
+ memcpy(&x, p, sizeof(x));
+ memcpy(&y, q, sizeof(y));
+ return (x[0] == y[0]) && (x[1] == y[1]);
+ }
+
/* bytes object */
case 'c': return *p == *q;