Skip to content

Commit 38f331d

Browse files
authored
bpo-45898: Remove duplicate symbols from _ctypes/cfield.c (GH-29791)
1 parent 2a6ece5 commit 38f331d

File tree

3 files changed

+106
-125
lines changed

3 files changed

+106
-125
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:mod:`ctypes` no longer defines ``ffi_type_*`` symbols in ``cfield.c``. The
2+
symbols have been provided by libffi for over a decade.

Modules/_ctypes/cfield.c

Lines changed: 103 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,55 +1481,40 @@ P_get(void *ptr, Py_ssize_t size)
14811481
}
14821482

14831483
static struct fielddesc formattable[] = {
1484-
{ 's', s_set, s_get, &ffi_type_pointer},
1485-
{ 'b', b_set, b_get, &ffi_type_schar},
1486-
{ 'B', B_set, B_get, &ffi_type_uchar},
1487-
{ 'c', c_set, c_get, &ffi_type_schar},
1488-
{ 'd', d_set, d_get, &ffi_type_double, d_set_sw, d_get_sw},
1489-
{ 'g', g_set, g_get, &ffi_type_longdouble},
1490-
{ 'f', f_set, f_get, &ffi_type_float, f_set_sw, f_get_sw},
1491-
{ 'h', h_set, h_get, &ffi_type_sshort, h_set_sw, h_get_sw},
1492-
{ 'H', H_set, H_get, &ffi_type_ushort, H_set_sw, H_get_sw},
1493-
{ 'i', i_set, i_get, &ffi_type_sint, i_set_sw, i_get_sw},
1494-
{ 'I', I_set, I_get, &ffi_type_uint, I_set_sw, I_get_sw},
1495-
/* XXX Hm, sizeof(int) == sizeof(long) doesn't hold on every platform */
1496-
/* As soon as we can get rid of the type codes, this is no longer a problem */
1497-
#if SIZEOF_LONG == 4
1498-
{ 'l', l_set, l_get, &ffi_type_sint32, l_set_sw, l_get_sw},
1499-
{ 'L', L_set, L_get, &ffi_type_uint32, L_set_sw, L_get_sw},
1500-
#elif SIZEOF_LONG == 8
1501-
{ 'l', l_set, l_get, &ffi_type_sint64, l_set_sw, l_get_sw},
1502-
{ 'L', L_set, L_get, &ffi_type_uint64, L_set_sw, L_get_sw},
1503-
#else
1504-
# error
1505-
#endif
1506-
#if SIZEOF_LONG_LONG == 8
1507-
{ 'q', q_set, q_get, &ffi_type_sint64, q_set_sw, q_get_sw},
1508-
{ 'Q', Q_set, Q_get, &ffi_type_uint64, Q_set_sw, Q_get_sw},
1509-
#else
1510-
# error
1511-
#endif
1512-
{ 'P', P_set, P_get, &ffi_type_pointer},
1513-
{ 'z', z_set, z_get, &ffi_type_pointer},
1514-
{ 'u', u_set, u_get, NULL}, /* ffi_type set later */
1515-
{ 'U', U_set, U_get, &ffi_type_pointer},
1516-
{ 'Z', Z_set, Z_get, &ffi_type_pointer},
1484+
{ 's', s_set, s_get, NULL},
1485+
{ 'b', b_set, b_get, NULL},
1486+
{ 'B', B_set, B_get, NULL},
1487+
{ 'c', c_set, c_get, NULL},
1488+
{ 'd', d_set, d_get, NULL, d_set_sw, d_get_sw},
1489+
{ 'g', g_set, g_get, NULL},
1490+
{ 'f', f_set, f_get, NULL, f_set_sw, f_get_sw},
1491+
{ 'h', h_set, h_get, NULL, h_set_sw, h_get_sw},
1492+
{ 'H', H_set, H_get, NULL, H_set_sw, H_get_sw},
1493+
{ 'i', i_set, i_get, NULL, i_set_sw, i_get_sw},
1494+
{ 'I', I_set, I_get, NULL, I_set_sw, I_get_sw},
1495+
{ 'l', l_set, l_get, NULL, l_set_sw, l_get_sw},
1496+
{ 'L', L_set, L_get, NULL, L_set_sw, L_get_sw},
1497+
{ 'q', q_set, q_get, NULL, q_set_sw, q_get_sw},
1498+
{ 'Q', Q_set, Q_get, NULL, Q_set_sw, Q_get_sw},
1499+
{ 'P', P_set, P_get, NULL},
1500+
{ 'z', z_set, z_get, NULL},
1501+
{ 'u', u_set, u_get, NULL},
1502+
{ 'U', U_set, U_get, NULL},
1503+
{ 'Z', Z_set, Z_get, NULL},
15171504
#ifdef MS_WIN32
1518-
{ 'X', BSTR_set, BSTR_get, &ffi_type_pointer},
1505+
{ 'X', BSTR_set, BSTR_get, NULL},
15191506
#endif
1520-
{ 'v', vBOOL_set, vBOOL_get, &ffi_type_sshort},
1521-
#if SIZEOF__BOOL == 1
1522-
{ '?', bool_set, bool_get, &ffi_type_uchar}, /* Also fallback for no native _Bool support */
1523-
#elif SIZEOF__BOOL == SIZEOF_SHORT
1524-
{ '?', bool_set, bool_get, &ffi_type_ushort},
1525-
#elif SIZEOF__BOOL == SIZEOF_INT
1526-
{ '?', bool_set, bool_get, &ffi_type_uint, I_set_sw, I_get_sw},
1507+
{ 'v', vBOOL_set, vBOOL_get, NULL},
1508+
#if SIZEOF__BOOL == SIZEOF_INT
1509+
{ '?', bool_set, bool_get, NULL, I_set_sw, I_get_sw},
15271510
#elif SIZEOF__BOOL == SIZEOF_LONG
1528-
{ '?', bool_set, bool_get, &ffi_type_ulong, L_set_sw, L_get_sw},
1511+
{ '?', bool_set, bool_get, NULL, L_set_sw, L_get_sw},
15291512
#elif SIZEOF__BOOL == SIZEOF_LONG_LONG
1530-
{ '?', bool_set, bool_get, &ffi_type_ulong, Q_set_sw, Q_get_sw},
1513+
{ '?', bool_set, bool_get, NULL, Q_set_sw, Q_get_sw},
1514+
#else
1515+
{ '?', bool_set, bool_get, NULL},
15311516
#endif /* SIZEOF__BOOL */
1532-
{ 'O', O_set, O_get, &ffi_type_pointer},
1517+
{ 'O', O_set, O_get, NULL},
15331518
{ 0, NULL, NULL, NULL},
15341519
};
15351520

@@ -1538,20 +1523,87 @@ static struct fielddesc formattable[] = {
15381523
Use '?' as code for BOOL.
15391524
*/
15401525

1526+
/* Delayed initialization. Windows cannot statically reference dynamically
1527+
loaded addresses from DLLs. */
1528+
void
1529+
_ctypes_init_fielddesc(void)
1530+
{
1531+
struct fielddesc *fd = formattable;
1532+
for (; fd->code; ++fd) {
1533+
switch (fd->code) {
1534+
case 's': fd->pffi_type = &ffi_type_pointer; break;
1535+
case 'b': fd->pffi_type = &ffi_type_schar; break;
1536+
case 'B': fd->pffi_type = &ffi_type_uchar; break;
1537+
case 'c': fd->pffi_type = &ffi_type_schar; break;
1538+
case 'd': fd->pffi_type = &ffi_type_double; break;
1539+
case 'g': fd->pffi_type = &ffi_type_longdouble; break;
1540+
case 'f': fd->pffi_type = &ffi_type_float; break;
1541+
case 'h': fd->pffi_type = &ffi_type_sshort; break;
1542+
case 'H': fd->pffi_type = &ffi_type_ushort; break;
1543+
case 'i': fd->pffi_type = &ffi_type_sint; break;
1544+
case 'I': fd->pffi_type = &ffi_type_uint; break;
1545+
/* XXX Hm, sizeof(int) == sizeof(long) doesn't hold on every platform */
1546+
/* As soon as we can get rid of the type codes, this is no longer a problem */
1547+
#if SIZEOF_LONG == 4
1548+
case 'l': fd->pffi_type = &ffi_type_sint32; break;
1549+
case 'L': fd->pffi_type = &ffi_type_uint32; break;
1550+
#elif SIZEOF_LONG == 8
1551+
case 'l': fd->pffi_type = &ffi_type_sint64; break;
1552+
case 'L': fd->pffi_type = &ffi_type_uint64; break;
1553+
#else
1554+
#error
1555+
#endif
1556+
#if SIZEOF_LONG_LONG == 8
1557+
case 'q': fd->pffi_type = &ffi_type_sint64; break;
1558+
case 'Q': fd->pffi_type = &ffi_type_uint64; break;
1559+
#else
1560+
#error
1561+
#endif
1562+
case 'P': fd->pffi_type = &ffi_type_pointer; break;
1563+
case 'z': fd->pffi_type = &ffi_type_pointer; break;
1564+
case 'u':
1565+
if (sizeof(wchar_t) == sizeof(short))
1566+
fd->pffi_type = &ffi_type_sshort;
1567+
else if (sizeof(wchar_t) == sizeof(int))
1568+
fd->pffi_type = &ffi_type_sint;
1569+
else if (sizeof(wchar_t) == sizeof(long))
1570+
fd->pffi_type = &ffi_type_slong;
1571+
else
1572+
Py_UNREACHABLE();
1573+
break;
1574+
case 'U': fd->pffi_type = &ffi_type_pointer; break;
1575+
case 'Z': fd->pffi_type = &ffi_type_pointer; break;
1576+
#ifdef MS_WIN32
1577+
case 'X': fd->pffi_type = &ffi_type_pointer; break;
1578+
#endif
1579+
case 'v': fd->pffi_type = &ffi_type_sshort; break;
1580+
#if SIZEOF__BOOL == 1
1581+
case '?': fd->pffi_type = &ffi_type_uchar; break; /* Also fallback for no native _Bool support */
1582+
#elif SIZEOF__BOOL == SIZEOF_SHORT
1583+
case '?': fd->pffi_type = &ffi_type_ushort; break;
1584+
#elif SIZEOF__BOOL == SIZEOF_INT
1585+
case '?': fd->pffi_type = &ffi_type_uint; break;
1586+
#elif SIZEOF__BOOL == SIZEOF_LONG
1587+
case '?': fd->pffi_type = &ffi_type_ulong; break;
1588+
#elif SIZEOF__BOOL == SIZEOF_LONG_LONG
1589+
case '?': fd->pffi_type = &ffi_type_ulong; break;
1590+
#endif /* SIZEOF__BOOL */
1591+
case 'O': fd->pffi_type = &ffi_type_pointer; break;
1592+
default:
1593+
Py_UNREACHABLE();
1594+
}
1595+
}
1596+
1597+
}
1598+
15411599
struct fielddesc *
15421600
_ctypes_get_fielddesc(const char *fmt)
15431601
{
15441602
static int initialized = 0;
15451603
struct fielddesc *table = formattable;
15461604

15471605
if (!initialized) {
1548-
initialized = 1;
1549-
if (sizeof(wchar_t) == sizeof(short))
1550-
_ctypes_get_fielddesc("u")->pffi_type = &ffi_type_sshort;
1551-
else if (sizeof(wchar_t) == sizeof(int))
1552-
_ctypes_get_fielddesc("u")->pffi_type = &ffi_type_sint;
1553-
else if (sizeof(wchar_t) == sizeof(long))
1554-
_ctypes_get_fielddesc("u")->pffi_type = &ffi_type_slong;
1606+
_ctypes_init_fielddesc();
15551607
}
15561608

15571609
for (; table->code; ++table) {
@@ -1561,77 +1613,4 @@ _ctypes_get_fielddesc(const char *fmt)
15611613
return NULL;
15621614
}
15631615

1564-
typedef struct { char c; char x; } s_char;
1565-
typedef struct { char c; short x; } s_short;
1566-
typedef struct { char c; int x; } s_int;
1567-
typedef struct { char c; long x; } s_long;
1568-
typedef struct { char c; float x; } s_float;
1569-
typedef struct { char c; double x; } s_double;
1570-
typedef struct { char c; long double x; } s_long_double;
1571-
typedef struct { char c; char *x; } s_char_p;
1572-
typedef struct { char c; void *x; } s_void_p;
1573-
1574-
/*
1575-
#define CHAR_ALIGN (sizeof(s_char) - sizeof(char))
1576-
#define SHORT_ALIGN (sizeof(s_short) - sizeof(short))
1577-
#define LONG_ALIGN (sizeof(s_long) - sizeof(long))
1578-
*/
1579-
#define INT_ALIGN (sizeof(s_int) - sizeof(int))
1580-
#define FLOAT_ALIGN (sizeof(s_float) - sizeof(float))
1581-
#define DOUBLE_ALIGN (sizeof(s_double) - sizeof(double))
1582-
#define LONGDOUBLE_ALIGN (sizeof(s_long_double) - sizeof(long double))
1583-
1584-
/* #define CHAR_P_ALIGN (sizeof(s_char_p) - sizeof(char*)) */
1585-
#define VOID_P_ALIGN (sizeof(s_void_p) - sizeof(void*))
1586-
1587-
/*
1588-
#ifdef HAVE_USABLE_WCHAR_T
1589-
typedef struct { char c; wchar_t x; } s_wchar;
1590-
typedef struct { char c; wchar_t *x; } s_wchar_p;
1591-
1592-
#define WCHAR_ALIGN (sizeof(s_wchar) - sizeof(wchar_t))
1593-
#define WCHAR_P_ALIGN (sizeof(s_wchar_p) - sizeof(wchar_t*))
1594-
#endif
1595-
*/
1596-
1597-
typedef struct { char c; long long x; } s_long_long;
1598-
#define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(long long))
1599-
1600-
/* from ffi.h:
1601-
typedef struct _ffi_type
1602-
{
1603-
size_t size;
1604-
unsigned short alignment;
1605-
unsigned short type;
1606-
struct _ffi_type **elements;
1607-
} ffi_type;
1608-
*/
1609-
1610-
/* align and size are bogus for void, but they must not be zero */
1611-
ffi_type ffi_type_void = { 1, 1, FFI_TYPE_VOID };
1612-
1613-
ffi_type ffi_type_uint8 = { 1, 1, FFI_TYPE_UINT8 };
1614-
ffi_type ffi_type_sint8 = { 1, 1, FFI_TYPE_SINT8 };
1615-
1616-
ffi_type ffi_type_uint16 = { 2, 2, FFI_TYPE_UINT16 };
1617-
ffi_type ffi_type_sint16 = { 2, 2, FFI_TYPE_SINT16 };
1618-
1619-
ffi_type ffi_type_uint32 = { 4, INT_ALIGN, FFI_TYPE_UINT32 };
1620-
ffi_type ffi_type_sint32 = { 4, INT_ALIGN, FFI_TYPE_SINT32 };
1621-
1622-
ffi_type ffi_type_uint64 = { 8, LONG_LONG_ALIGN, FFI_TYPE_UINT64 };
1623-
ffi_type ffi_type_sint64 = { 8, LONG_LONG_ALIGN, FFI_TYPE_SINT64 };
1624-
1625-
ffi_type ffi_type_float = { sizeof(float), FLOAT_ALIGN, FFI_TYPE_FLOAT };
1626-
ffi_type ffi_type_double = { sizeof(double), DOUBLE_ALIGN, FFI_TYPE_DOUBLE };
1627-
1628-
#ifdef ffi_type_longdouble
1629-
#undef ffi_type_longdouble
1630-
#endif
1631-
/* This is already defined on OSX */
1632-
ffi_type ffi_type_longdouble = { sizeof(long double), LONGDOUBLE_ALIGN,
1633-
FFI_TYPE_LONGDOUBLE };
1634-
1635-
ffi_type ffi_type_pointer = { sizeof(void *), VOID_P_ALIGN, FFI_TYPE_POINTER };
1636-
16371616
/*---------------- EOF ----------------*/

PCbuild/_ctypes.vcxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
</PropertyGroup>
9595
<ItemDefinitionGroup>
9696
<ClCompile>
97-
<PreprocessorDefinitions>FFI_BUILDING;%(PreprocessorDefinitions)</PreprocessorDefinitions>
97+
<PreprocessorDefinitions>USING_MALLOC_CLOSURE_DOT_C=1;%(PreprocessorDefinitions)</PreprocessorDefinitions>
9898
</ClCompile>
9999
<Link>
100100
<AdditionalOptions>/EXPORT:DllGetClassObject,PRIVATE /EXPORT:DllCanUnloadNow,PRIVATE %(AdditionalOptions)</AdditionalOptions>

0 commit comments

Comments
 (0)