Skip to content

Commit b5cc05b

Browse files
authored
bpo-38823: Always build _ctypes with wchar_t (GH-23248)
It is no longer possible to build the _ctypes extension module without wchar_t type: remove CTYPES_UNICODE macro. Anyway, the wchar_t type is required to build Python.
1 parent ef75a62 commit b5cc05b

File tree

6 files changed

+7
-31
lines changed

6 files changed

+7
-31
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
It is no longer possible to build the ``_ctypes`` extension module without
2+
:c:type:`wchar_t` type: remove ``CTYPES_UNICODE`` macro. Anyway, the
3+
:c:type:`wchar_t` type is required to build Python. Patch by Victor Stinner.

Modules/_ctypes/_ctypes.c

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,7 +1358,6 @@ static PyGetSetDef CharArray_getsets[] = {
13581358
{ NULL, NULL }
13591359
};
13601360

1361-
#ifdef CTYPES_UNICODE
13621361
static PyObject *
13631362
WCharArray_get_value(CDataObject *self, void *Py_UNUSED(ignored))
13641363
{
@@ -1408,7 +1407,6 @@ static PyGetSetDef WCharArray_getsets[] = {
14081407
"string value"},
14091408
{ NULL, NULL }
14101409
};
1411-
#endif
14121410

14131411
/*
14141412
The next three functions copied from Python's typeobject.c.
@@ -1615,11 +1613,10 @@ PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
16151613
if (itemdict->getfunc == _ctypes_get_fielddesc("c")->getfunc) {
16161614
if (-1 == add_getset(result, CharArray_getsets))
16171615
goto error;
1618-
#ifdef CTYPES_UNICODE
1619-
} else if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
1616+
}
1617+
else if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
16201618
if (-1 == add_getset(result, WCharArray_getsets))
16211619
goto error;
1622-
#endif
16231620
}
16241621

16251622
return (PyObject *)result;
@@ -4654,7 +4651,6 @@ Array_subscript(PyObject *myself, PyObject *item)
46544651
PyMem_Free(dest);
46554652
return np;
46564653
}
4657-
#ifdef CTYPES_UNICODE
46584654
if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
46594655
wchar_t *ptr = (wchar_t *)self->b_ptr;
46604656
wchar_t *dest;
@@ -4681,7 +4677,6 @@ Array_subscript(PyObject *myself, PyObject *item)
46814677
PyMem_Free(dest);
46824678
return np;
46834679
}
4684-
#endif
46854680

46864681
np = PyList_New(slicelen);
46874682
if (np == NULL)
@@ -5350,7 +5345,6 @@ Pointer_subscript(PyObject *myself, PyObject *item)
53505345
PyMem_Free(dest);
53515346
return np;
53525347
}
5353-
#ifdef CTYPES_UNICODE
53545348
if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
53555349
wchar_t *ptr = *(wchar_t **)self->b_ptr;
53565350
wchar_t *dest;
@@ -5371,7 +5365,6 @@ Pointer_subscript(PyObject *myself, PyObject *item)
53715365
PyMem_Free(dest);
53725366
return np;
53735367
}
5374-
#endif
53755368

53765369
np = PyList_New(len);
53775370
if (np == NULL)
@@ -5653,7 +5646,7 @@ cast(void *ptr, PyObject *src, PyObject *ctype)
56535646
return NULL;
56545647
}
56555648

5656-
#ifdef CTYPES_UNICODE
5649+
56575650
static PyObject *
56585651
wstring_at(const wchar_t *ptr, int size)
56595652
{
@@ -5665,7 +5658,6 @@ wstring_at(const wchar_t *ptr, int size)
56655658
ssize = wcslen(ptr);
56665659
return PyUnicode_FromWideChar(ptr, ssize);
56675660
}
5668-
#endif
56695661

56705662

56715663
static struct PyModuleDef _ctypesmodule = {
@@ -5796,9 +5788,7 @@ _ctypes_add_objects(PyObject *mod)
57965788
MOD_ADD("_memset_addr", PyLong_FromVoidPtr(memset));
57975789
MOD_ADD("_string_at_addr", PyLong_FromVoidPtr(string_at));
57985790
MOD_ADD("_cast_addr", PyLong_FromVoidPtr(cast));
5799-
#ifdef CTYPES_UNICODE
58005791
MOD_ADD("_wstring_at_addr", PyLong_FromVoidPtr(wstring_at));
5801-
#endif
58025792

58035793
/* If RTLD_LOCAL is not defined (Windows!), set it to zero. */
58045794
#if !HAVE_DECL_RTLD_LOCAL

Modules/_ctypes/callproc.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,6 @@ static int ConvParam(PyObject *obj, Py_ssize_t index, struct argument *pa)
713713
return 0;
714714
}
715715

716-
#ifdef CTYPES_UNICODE
717716
if (PyUnicode_Check(obj)) {
718717
pa->ffi_type = &ffi_type_pointer;
719718
pa->value.p = PyUnicode_AsWideCharString(obj, NULL);
@@ -726,7 +725,6 @@ static int ConvParam(PyObject *obj, Py_ssize_t index, struct argument *pa)
726725
}
727726
return 0;
728727
}
729-
#endif
730728

731729
{
732730
_Py_IDENTIFIER(_as_parameter_);

Modules/_ctypes/cfield.c

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,11 @@ PyCField_FromDesc(PyObject *desc, Py_ssize_t index,
125125
getfunc = fd->getfunc;
126126
setfunc = fd->setfunc;
127127
}
128-
#ifdef CTYPES_UNICODE
129128
if (idict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
130129
struct fielddesc *fd = _ctypes_get_fielddesc("U");
131130
getfunc = fd->getfunc;
132131
setfunc = fd->setfunc;
133132
}
134-
#endif
135133
}
136134
}
137135

@@ -1137,7 +1135,6 @@ c_get(void *ptr, Py_ssize_t size)
11371135
return PyBytes_FromStringAndSize((char *)ptr, 1);
11381136
}
11391137

1140-
#ifdef CTYPES_UNICODE
11411138
/* u - a single wchar_t character */
11421139
static PyObject *
11431140
u_set(void *ptr, PyObject *value, Py_ssize_t size)
@@ -1232,7 +1229,6 @@ U_set(void *ptr, PyObject *value, Py_ssize_t length)
12321229
return value;
12331230
}
12341231

1235-
#endif
12361232

12371233
static PyObject *
12381234
s_get(void *ptr, Py_ssize_t size)
@@ -1321,7 +1317,6 @@ z_get(void *ptr, Py_ssize_t size)
13211317
}
13221318
}
13231319

1324-
#ifdef CTYPES_UNICODE
13251320
static PyObject *
13261321
Z_set(void *ptr, PyObject *value, Py_ssize_t size)
13271322
{
@@ -1373,7 +1368,7 @@ Z_get(void *ptr, Py_ssize_t size)
13731368
Py_RETURN_NONE;
13741369
}
13751370
}
1376-
#endif
1371+
13771372

13781373
#ifdef MS_WIN32
13791374
static PyObject *
@@ -1507,11 +1502,9 @@ static struct fielddesc formattable[] = {
15071502
#endif
15081503
{ 'P', P_set, P_get, &ffi_type_pointer},
15091504
{ 'z', z_set, z_get, &ffi_type_pointer},
1510-
#ifdef CTYPES_UNICODE
15111505
{ 'u', u_set, u_get, NULL}, /* ffi_type set later */
15121506
{ 'U', U_set, U_get, &ffi_type_pointer},
15131507
{ 'Z', Z_set, Z_get, &ffi_type_pointer},
1514-
#endif
15151508
#ifdef MS_WIN32
15161509
{ 'X', BSTR_set, BSTR_get, &ffi_type_pointer},
15171510
#endif
@@ -1544,14 +1537,12 @@ _ctypes_get_fielddesc(const char *fmt)
15441537

15451538
if (!initialized) {
15461539
initialized = 1;
1547-
#ifdef CTYPES_UNICODE
15481540
if (sizeof(wchar_t) == sizeof(short))
15491541
_ctypes_get_fielddesc("u")->pffi_type = &ffi_type_sshort;
15501542
else if (sizeof(wchar_t) == sizeof(int))
15511543
_ctypes_get_fielddesc("u")->pffi_type = &ffi_type_sint;
15521544
else if (sizeof(wchar_t) == sizeof(long))
15531545
_ctypes_get_fielddesc("u")->pffi_type = &ffi_type_slong;
1554-
#endif
15551546
}
15561547

15571548
for (; table->code; ++table) {

Modules/_ctypes/ctypes.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,10 +343,6 @@ extern PyObject *PyExc_ArgError;
343343
extern char *_ctypes_conversion_encoding;
344344
extern char *_ctypes_conversion_errors;
345345

346-
#if defined(HAVE_WCHAR_H)
347-
# define CTYPES_UNICODE
348-
#endif
349-
350346

351347
extern void _ctypes_free_closure(void *);
352348
extern void *_ctypes_alloc_closure(void);

Modules/_ctypes/stgdict.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,9 +538,7 @@ PyCStructUnionType_update_stgdict(PyObject *type, PyObject *fields, int isStruct
538538
case FFI_TYPE_SINT16:
539539
case FFI_TYPE_SINT32:
540540
if (dict->getfunc != _ctypes_get_fielddesc("c")->getfunc
541-
#ifdef CTYPES_UNICODE
542541
&& dict->getfunc != _ctypes_get_fielddesc("u")->getfunc
543-
#endif
544542
)
545543
break;
546544
/* else fall through */

0 commit comments

Comments
 (0)