Skip to content
Closed
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
9 changes: 9 additions & 0 deletions Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,15 @@ contextlib
(Contributed by Alex Grönholm & Gregory P. Smith in :gh:`125862`.)


ctypes
------

* Change the ``format`` field of exported buffers from ``F``, ``D`` and ``G``
to ``Zf``, ``Zd`` and ``Zg``, respectively, for compatibility with
the :pep:`3118` and NumPy.
(Contributed by Sergey B Kirpichev in :gh:`149344`.)


dataclasses
-----------

Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_ctypes/test_pep3118.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ctypes
import re
import sys
import unittest
Expand Down Expand Up @@ -231,6 +232,14 @@ class Complete(Structure):

]

if hasattr(ctypes, 'c_float_complex'):
c_float_complex = ctypes.c_float_complex
c_double_complex = ctypes.c_double_complex
c_longdouble_complex = ctypes.c_longdouble_complex
native_types.extend([(c_float_complex * 4, "<Zf", (4,), c_float_complex),
(c_double_complex * 4, "<Zd", (4,), c_double_complex),
(c_longdouble_complex * 4, "<Zg", (4,), c_longdouble_complex)])


class BEPoint(BigEndianStructure):
_fields_ = [("x", c_long), ("y", c_long)]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Use :pep:`3118` type codes for the buffer protocol support of complex types
in the :mod:`ctypes`. Patch by Sergey B Kirpichev.
28 changes: 28 additions & 0 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,34 @@ _ctypes_alloc_format_string_for_type(char code, int big_endian)
#else
# error SIZEOF__BOOL has an unexpected value
#endif /* SIZEOF__BOOL */
#if defined(_Py_FFI_SUPPORT_C_COMPLEX)
/* complex types */
case 'F':
case 'D':
case 'G':
{
result = PyMem_Malloc(4);
if (result == NULL) {
PyErr_NoMemory();
return NULL;
}

result[0] = big_endian ? '>' : '<';
result[1] = 'Z';
switch (code) {
case 'F':
result[2] = 'f';
break;
case 'D':
result[2] = 'd';
break;
default:
result[2] = 'g';
}
result[3] = '\0';
return result;
}
#endif
default:
/* The standard-size code is the same as the ctypes one */
pep_code = code;
Expand Down
Loading