Skip to content

Commit 85f89cb

Browse files
authored
gh-121249: adjust formatting codes for complex types in struct/ctypes (#132827)
* F - for float _Complex * D - for double _Complex * G - for long double _Complex (not supported by the struct module)
1 parent 41dec41 commit 85f89cb

File tree

8 files changed

+58
-58
lines changed

8 files changed

+58
-58
lines changed

Doc/library/struct.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,9 @@ C11 standard) is supported, the following format characters are available:
273273
+--------+--------------------------+--------------------+----------------+------------+
274274
| Format | C Type | Python type | Standard size | Notes |
275275
+========+==========================+====================+================+============+
276-
| ``E`` | :c:expr:`float complex` | complex | 8 | \(10) |
276+
| ``F`` | :c:expr:`float complex` | complex | 8 | \(10) |
277277
+--------+--------------------------+--------------------+----------------+------------+
278-
| ``C`` | :c:expr:`double complex` | complex | 16 | \(10) |
278+
| ``D`` | :c:expr:`double complex` | complex | 16 | \(10) |
279279
+--------+--------------------------+--------------------+----------------+------------+
280280

281281
.. versionchanged:: 3.3
@@ -285,7 +285,7 @@ C11 standard) is supported, the following format characters are available:
285285
Added support for the ``'e'`` format.
286286

287287
.. versionchanged:: 3.14
288-
Added support for the ``'E'`` and ``'C'`` formats.
288+
Added support for the ``'F'`` and ``'D'`` formats.
289289

290290

291291
Notes:

Doc/whatsnew/3.14.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1173,7 +1173,7 @@ struct
11731173
------
11741174

11751175
* Support the :c:expr:`float complex` and :c:expr:`double complex` C types in
1176-
the :mod:`struct` module (formatting characters ``'E'`` and ``'C'``,
1176+
the :mod:`struct` module (formatting characters ``'F'`` and ``'D'``,
11771177
respectively) if the compiler has C11 complex arithmetic.
11781178
(Contributed by Sergey B Kirpichev in :gh:`121249`.)
11791179

Lib/ctypes/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,13 @@ class c_longdouble(_SimpleCData):
209209

210210
try:
211211
class c_double_complex(_SimpleCData):
212-
_type_ = "C"
212+
_type_ = "D"
213213
_check_size(c_double_complex)
214214
class c_float_complex(_SimpleCData):
215-
_type_ = "E"
215+
_type_ = "F"
216216
_check_size(c_float_complex)
217217
class c_longdouble_complex(_SimpleCData):
218-
_type_ = "F"
218+
_type_ = "G"
219219
except AttributeError:
220220
pass
221221

Lib/test/test_ctypes/test_c_simple_type_meta.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,11 @@ def test_bad_type_message(self):
160160
class F(metaclass=PyCSimpleType):
161161
_type_ = "\0"
162162
message = str(cm.exception)
163-
expected_type_chars = list('cbBhHiIlLdCEFfuzZqQPXOv?g')
163+
expected_type_chars = list('cbBhHiIlLdDFGfuzZqQPXOv?g')
164164
if not hasattr(ctypes, 'c_float_complex'):
165-
expected_type_chars.remove('C')
166-
expected_type_chars.remove('E')
167165
expected_type_chars.remove('F')
166+
expected_type_chars.remove('D')
167+
expected_type_chars.remove('G')
168168
if not MS_WINDOWS:
169169
expected_type_chars.remove('X')
170170
self.assertIn("'" + ''.join(expected_type_chars) + "'", message)

Lib/test/test_struct.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
NAN = float('nan')
2424

2525
try:
26-
struct.pack('C', 1j)
26+
struct.pack('D', 1j)
2727
have_c_complex = True
2828
except struct.error:
2929
have_c_complex = False
@@ -801,23 +801,23 @@ def test_c_complex_round_trip(self):
801801
values = [complex(*_) for _ in combinations([1, -1, 0.0, -0.0, 2,
802802
-3, INF, -INF, NAN], 2)]
803803
for z in values:
804-
for f in ['E', 'C', '>E', '>C', '<E', '<C']:
804+
for f in ['F', 'D', '>F', '>D', '<F', '<D']:
805805
with self.subTest(z=z, format=f):
806806
round_trip = struct.unpack(f, struct.pack(f, z))[0]
807807
self.assertComplexesAreIdentical(z, round_trip)
808808

809809
@unittest.skipIf(have_c_complex, "requires no C11 complex type support")
810810
def test_c_complex_error(self):
811-
msg1 = "'E' format not supported on this system"
812-
msg2 = "'C' format not supported on this system"
811+
msg1 = "'F' format not supported on this system"
812+
msg2 = "'D' format not supported on this system"
813813
with self.assertRaisesRegex(struct.error, msg1):
814-
struct.pack('E', 1j)
814+
struct.pack('F', 1j)
815815
with self.assertRaisesRegex(struct.error, msg1):
816-
struct.unpack('E', b'1')
816+
struct.unpack('F', b'1')
817817
with self.assertRaisesRegex(struct.error, msg2):
818-
struct.pack('C', 1j)
818+
struct.pack('D', 1j)
819819
with self.assertRaisesRegex(struct.error, msg2):
820-
struct.unpack('C', b'1')
820+
struct.unpack('D', b'1')
821821

822822

823823
class UnpackIteratorTest(unittest.TestCase):

Modules/_ctypes/callproc.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -648,14 +648,14 @@ union result {
648648
int i;
649649
long l;
650650
long long q;
651-
long double D;
651+
long double g;
652652
double d;
653653
float f;
654654
void *p;
655655
#if defined(Py_HAVE_C_COMPLEX) && defined(Py_FFI_SUPPORT_C_COMPLEX)
656-
double complex C;
657-
float complex E;
658-
long double complex F;
656+
double complex D;
657+
float complex F;
658+
long double complex G;
659659
#endif
660660
};
661661

Modules/_ctypes/cfield.c

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -764,9 +764,9 @@ d_get(void *ptr, Py_ssize_t size)
764764
}
765765

766766
#if defined(Py_HAVE_C_COMPLEX) && defined(Py_FFI_SUPPORT_C_COMPLEX)
767-
/* C: double complex */
767+
/* D: double complex */
768768
static PyObject *
769-
C_set(void *ptr, PyObject *value, Py_ssize_t size)
769+
D_set(void *ptr, PyObject *value, Py_ssize_t size)
770770
{
771771
assert(NUM_BITS(size) || (size == sizeof(double complex)));
772772
Py_complex c = PyComplex_AsCComplex(value);
@@ -780,7 +780,7 @@ C_set(void *ptr, PyObject *value, Py_ssize_t size)
780780
}
781781

782782
static PyObject *
783-
C_get(void *ptr, Py_ssize_t size)
783+
D_get(void *ptr, Py_ssize_t size)
784784
{
785785
assert(NUM_BITS(size) || (size == sizeof(double complex)));
786786
double complex x;
@@ -789,9 +789,9 @@ C_get(void *ptr, Py_ssize_t size)
789789
return PyComplex_FromDoubles(creal(x), cimag(x));
790790
}
791791

792-
/* E: float complex */
792+
/* F: float complex */
793793
static PyObject *
794-
E_set(void *ptr, PyObject *value, Py_ssize_t size)
794+
F_set(void *ptr, PyObject *value, Py_ssize_t size)
795795
{
796796
assert(NUM_BITS(size) || (size == sizeof(float complex)));
797797
Py_complex c = PyComplex_AsCComplex(value);
@@ -805,7 +805,7 @@ E_set(void *ptr, PyObject *value, Py_ssize_t size)
805805
}
806806

807807
static PyObject *
808-
E_get(void *ptr, Py_ssize_t size)
808+
F_get(void *ptr, Py_ssize_t size)
809809
{
810810
assert(NUM_BITS(size) || (size == sizeof(float complex)));
811811
float complex x;
@@ -814,9 +814,9 @@ E_get(void *ptr, Py_ssize_t size)
814814
return PyComplex_FromDoubles(crealf(x), cimagf(x));
815815
}
816816

817-
/* F: long double complex */
817+
/* G: long double complex */
818818
static PyObject *
819-
F_set(void *ptr, PyObject *value, Py_ssize_t size)
819+
G_set(void *ptr, PyObject *value, Py_ssize_t size)
820820
{
821821
assert(NUM_BITS(size) || (size == sizeof(long double complex)));
822822
Py_complex c = PyComplex_AsCComplex(value);
@@ -830,7 +830,7 @@ F_set(void *ptr, PyObject *value, Py_ssize_t size)
830830
}
831831

832832
static PyObject *
833-
F_get(void *ptr, Py_ssize_t size)
833+
G_get(void *ptr, Py_ssize_t size)
834834
{
835835
assert(NUM_BITS(size) || (size == sizeof(long double complex)));
836836
long double complex x;
@@ -1372,7 +1372,7 @@ struct formattable {
13721372
for nbytes in 8, 16, 32, 64:
13731373
for sgn in 'i', 'u':
13741374
print(f' struct fielddesc fmt_{sgn}{nbytes};')
1375-
for code in 'sbBcdCEFgfhHiIlLqQPzuUZXvO':
1375+
for code in 'sbBcdFDGgfhHiIlLqQPzuUZXvO':
13761376
print(f' struct fielddesc fmt_{code};')
13771377
[python start generated code]*/
13781378
struct fielddesc fmt_i8;
@@ -1388,9 +1388,9 @@ for code in 'sbBcdCEFgfhHiIlLqQPzuUZXvO':
13881388
struct fielddesc fmt_B;
13891389
struct fielddesc fmt_c;
13901390
struct fielddesc fmt_d;
1391-
struct fielddesc fmt_C;
1392-
struct fielddesc fmt_E;
13931391
struct fielddesc fmt_F;
1392+
struct fielddesc fmt_D;
1393+
struct fielddesc fmt_G;
13941394
struct fielddesc fmt_g;
13951395
struct fielddesc fmt_f;
13961396
struct fielddesc fmt_h;
@@ -1409,7 +1409,7 @@ for code in 'sbBcdCEFgfhHiIlLqQPzuUZXvO':
14091409
struct fielddesc fmt_X;
14101410
struct fielddesc fmt_v;
14111411
struct fielddesc fmt_O;
1412-
/*[python end generated code: output=fa648744ec7f919d input=087d58357d4bf2c5]*/
1412+
/*[python end generated code: output=f5a07c066fedaca6 input=ffa5d46c29dfb07a]*/
14131413

14141414
// bool has code '?':
14151415
struct fielddesc fmt_bool;
@@ -1598,9 +1598,9 @@ for base_code, base_c_type in [
15981598
TABLE_ENTRY_SW(d, &ffi_type_double);
15991599
#if defined(Py_HAVE_C_COMPLEX) && defined(Py_FFI_SUPPORT_C_COMPLEX)
16001600
if (Py_FFI_COMPLEX_AVAILABLE) {
1601-
TABLE_ENTRY(C, &ffi_type_complex_double);
1602-
TABLE_ENTRY(E, &ffi_type_complex_float);
1603-
TABLE_ENTRY(F, &ffi_type_complex_longdouble);
1601+
TABLE_ENTRY(D, &ffi_type_complex_double);
1602+
TABLE_ENTRY(F, &ffi_type_complex_float);
1603+
TABLE_ENTRY(G, &ffi_type_complex_longdouble);
16041604
}
16051605
#endif
16061606
TABLE_ENTRY(g, &ffi_type_longdouble);
@@ -1635,7 +1635,7 @@ for base_code, base_c_type in [
16351635
formattable.fmt_bool.getfunc = bool_get;
16361636

16371637
/*[python input]
1638-
all_chars = "cbBhHiIlLdCEFfuzZqQPXOv?g"
1638+
all_chars = "cbBhHiIlLdDFGfuzZqQPXOv?g"
16391639
print(f' assert(sizeof(formattable.simple_type_chars) == {len(all_chars)+1});')
16401640
print(f' int i = 0;')
16411641
for char in all_chars:
@@ -1656,9 +1656,9 @@ print(f" formattable.simple_type_chars[i] = 0;")
16561656
if (formattable.fmt_l.code) formattable.simple_type_chars[i++] = 'l';
16571657
if (formattable.fmt_L.code) formattable.simple_type_chars[i++] = 'L';
16581658
if (formattable.fmt_d.code) formattable.simple_type_chars[i++] = 'd';
1659-
if (formattable.fmt_C.code) formattable.simple_type_chars[i++] = 'C';
1660-
if (formattable.fmt_E.code) formattable.simple_type_chars[i++] = 'E';
1659+
if (formattable.fmt_D.code) formattable.simple_type_chars[i++] = 'D';
16611660
if (formattable.fmt_F.code) formattable.simple_type_chars[i++] = 'F';
1661+
if (formattable.fmt_G.code) formattable.simple_type_chars[i++] = 'G';
16621662
if (formattable.fmt_f.code) formattable.simple_type_chars[i++] = 'f';
16631663
if (formattable.fmt_u.code) formattable.simple_type_chars[i++] = 'u';
16641664
if (formattable.fmt_z.code) formattable.simple_type_chars[i++] = 'z';
@@ -1672,7 +1672,7 @@ print(f" formattable.simple_type_chars[i] = 0;")
16721672
if (formattable.fmt_bool.code) formattable.simple_type_chars[i++] = '?';
16731673
if (formattable.fmt_g.code) formattable.simple_type_chars[i++] = 'g';
16741674
formattable.simple_type_chars[i] = 0;
1675-
/*[python end generated code: output=e6e5098a02f4b606 input=72031a625eac00c1]*/
1675+
/*[python end generated code: output=2aa52670d1570f18 input=cff3e7cb95adac61]*/
16761676

16771677
}
16781678
#undef FIXINT_FIELDDESC_FOR
@@ -1689,17 +1689,17 @@ _ctypes_get_fielddesc(const char *fmt)
16891689
struct fielddesc *result = NULL;
16901690
switch(fmt[0]) {
16911691
/*[python input]
1692-
for code in 'sbBcdCEFgfhHiIlLqQPzuUZXvO':
1692+
for code in 'sbBcdDFGgfhHiIlLqQPzuUZXvO':
16931693
print(f" case '{code}': result = &formattable.fmt_{code}; break;")
16941694
[python start generated code]*/
16951695
case 's': result = &formattable.fmt_s; break;
16961696
case 'b': result = &formattable.fmt_b; break;
16971697
case 'B': result = &formattable.fmt_B; break;
16981698
case 'c': result = &formattable.fmt_c; break;
16991699
case 'd': result = &formattable.fmt_d; break;
1700-
case 'C': result = &formattable.fmt_C; break;
1701-
case 'E': result = &formattable.fmt_E; break;
1700+
case 'D': result = &formattable.fmt_D; break;
17021701
case 'F': result = &formattable.fmt_F; break;
1702+
case 'G': result = &formattable.fmt_G; break;
17031703
case 'g': result = &formattable.fmt_g; break;
17041704
case 'f': result = &formattable.fmt_f; break;
17051705
case 'h': result = &formattable.fmt_h; break;
@@ -1718,7 +1718,7 @@ for code in 'sbBcdCEFgfhHiIlLqQPzuUZXvO':
17181718
case 'X': result = &formattable.fmt_X; break;
17191719
case 'v': result = &formattable.fmt_v; break;
17201720
case 'O': result = &formattable.fmt_O; break;
1721-
/*[python end generated code: output=81a8223dda9f81f7 input=2f59666d3c024edf]*/
1721+
/*[python end generated code: output=6e5c91940732fde9 input=902223feffc2fe38]*/
17221722
case '?': result = &formattable.fmt_bool; break;
17231723
}
17241724
if (!result || !result->code) {

Modules/_struct.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -913,11 +913,11 @@ static const formatdef native_table[] = {
913913
{'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float},
914914
{'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double},
915915
#ifdef Py_HAVE_C_COMPLEX
916-
{'E', sizeof(float complex), FLOAT_COMPLEX_ALIGN, nu_float_complex, np_float_complex},
917-
{'C', sizeof(double complex), DOUBLE_COMPLEX_ALIGN, nu_double_complex, np_double_complex},
916+
{'F', sizeof(float complex), FLOAT_COMPLEX_ALIGN, nu_float_complex, np_float_complex},
917+
{'D', sizeof(double complex), DOUBLE_COMPLEX_ALIGN, nu_double_complex, np_double_complex},
918918
#else
919-
{'E', 1, 0, nu_complex_stub, np_complex_stub},
920-
{'C', 1, 0, nu_complex_stub, np_complex_stub},
919+
{'F', 1, 0, nu_complex_stub, np_complex_stub},
920+
{'D', 1, 0, nu_complex_stub, np_complex_stub},
921921
#endif
922922
{'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
923923
{0}
@@ -1253,11 +1253,11 @@ static formatdef bigendian_table[] = {
12531253
{'f', 4, 0, bu_float, bp_float},
12541254
{'d', 8, 0, bu_double, bp_double},
12551255
#ifdef Py_HAVE_C_COMPLEX
1256-
{'E', 8, 0, bu_float_complex, bp_float_complex},
1257-
{'C', 16, 0, bu_double_complex, bp_double_complex},
1256+
{'F', 8, 0, bu_float_complex, bp_float_complex},
1257+
{'D', 16, 0, bu_double_complex, bp_double_complex},
12581258
#else
1259-
{'E', 1, 0, nu_complex_stub, np_complex_stub},
1260-
{'C', 1, 0, nu_complex_stub, np_complex_stub},
1259+
{'F', 1, 0, nu_complex_stub, np_complex_stub},
1260+
{'D', 1, 0, nu_complex_stub, np_complex_stub},
12611261
#endif
12621262
{0}
12631263
};
@@ -1577,11 +1577,11 @@ static formatdef lilendian_table[] = {
15771577
{'f', 4, 0, lu_float, lp_float},
15781578
{'d', 8, 0, lu_double, lp_double},
15791579
#ifdef Py_HAVE_C_COMPLEX
1580-
{'E', 8, 0, lu_float_complex, lp_float_complex},
1581-
{'C', 16, 0, lu_double_complex, lp_double_complex},
1580+
{'F', 8, 0, lu_float_complex, lp_float_complex},
1581+
{'D', 16, 0, lu_double_complex, lp_double_complex},
15821582
#else
1583-
{'E', 1, 0, nu_complex_stub, np_complex_stub},
1584-
{'C', 1, 0, nu_complex_stub, np_complex_stub},
1583+
{'F', 1, 0, nu_complex_stub, np_complex_stub},
1584+
{'D', 1, 0, nu_complex_stub, np_complex_stub},
15851585
#endif
15861586
{0}
15871587
};

0 commit comments

Comments
 (0)