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.
This commit is contained in:
Victor Stinner 2020-11-12 15:38:13 +01:00 committed by GitHub
parent ef75a625cd
commit b5cc05bbe6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 7 additions and 31 deletions

View File

@ -0,0 +1,3 @@
It is no longer possible to build the ``_ctypes`` extension module without
:c:type:`wchar_t` type: remove ``CTYPES_UNICODE`` macro. Anyway, the
:c:type:`wchar_t` type is required to build Python. Patch by Victor Stinner.

View File

@ -1358,7 +1358,6 @@ static PyGetSetDef CharArray_getsets[] = {
{ NULL, NULL } { NULL, NULL }
}; };
#ifdef CTYPES_UNICODE
static PyObject * static PyObject *
WCharArray_get_value(CDataObject *self, void *Py_UNUSED(ignored)) WCharArray_get_value(CDataObject *self, void *Py_UNUSED(ignored))
{ {
@ -1408,7 +1407,6 @@ static PyGetSetDef WCharArray_getsets[] = {
"string value"}, "string value"},
{ NULL, NULL } { NULL, NULL }
}; };
#endif
/* /*
The next three functions copied from Python's typeobject.c. The next three functions copied from Python's typeobject.c.
@ -1615,11 +1613,10 @@ PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
if (itemdict->getfunc == _ctypes_get_fielddesc("c")->getfunc) { if (itemdict->getfunc == _ctypes_get_fielddesc("c")->getfunc) {
if (-1 == add_getset(result, CharArray_getsets)) if (-1 == add_getset(result, CharArray_getsets))
goto error; goto error;
#ifdef CTYPES_UNICODE }
} else if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) { else if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
if (-1 == add_getset(result, WCharArray_getsets)) if (-1 == add_getset(result, WCharArray_getsets))
goto error; goto error;
#endif
} }
return (PyObject *)result; return (PyObject *)result;
@ -4654,7 +4651,6 @@ Array_subscript(PyObject *myself, PyObject *item)
PyMem_Free(dest); PyMem_Free(dest);
return np; return np;
} }
#ifdef CTYPES_UNICODE
if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) { if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
wchar_t *ptr = (wchar_t *)self->b_ptr; wchar_t *ptr = (wchar_t *)self->b_ptr;
wchar_t *dest; wchar_t *dest;
@ -4681,7 +4677,6 @@ Array_subscript(PyObject *myself, PyObject *item)
PyMem_Free(dest); PyMem_Free(dest);
return np; return np;
} }
#endif
np = PyList_New(slicelen); np = PyList_New(slicelen);
if (np == NULL) if (np == NULL)
@ -5350,7 +5345,6 @@ Pointer_subscript(PyObject *myself, PyObject *item)
PyMem_Free(dest); PyMem_Free(dest);
return np; return np;
} }
#ifdef CTYPES_UNICODE
if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) { if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
wchar_t *ptr = *(wchar_t **)self->b_ptr; wchar_t *ptr = *(wchar_t **)self->b_ptr;
wchar_t *dest; wchar_t *dest;
@ -5371,7 +5365,6 @@ Pointer_subscript(PyObject *myself, PyObject *item)
PyMem_Free(dest); PyMem_Free(dest);
return np; return np;
} }
#endif
np = PyList_New(len); np = PyList_New(len);
if (np == NULL) if (np == NULL)
@ -5653,7 +5646,7 @@ cast(void *ptr, PyObject *src, PyObject *ctype)
return NULL; return NULL;
} }
#ifdef CTYPES_UNICODE
static PyObject * static PyObject *
wstring_at(const wchar_t *ptr, int size) wstring_at(const wchar_t *ptr, int size)
{ {
@ -5665,7 +5658,6 @@ wstring_at(const wchar_t *ptr, int size)
ssize = wcslen(ptr); ssize = wcslen(ptr);
return PyUnicode_FromWideChar(ptr, ssize); return PyUnicode_FromWideChar(ptr, ssize);
} }
#endif
static struct PyModuleDef _ctypesmodule = { static struct PyModuleDef _ctypesmodule = {
@ -5796,9 +5788,7 @@ _ctypes_add_objects(PyObject *mod)
MOD_ADD("_memset_addr", PyLong_FromVoidPtr(memset)); MOD_ADD("_memset_addr", PyLong_FromVoidPtr(memset));
MOD_ADD("_string_at_addr", PyLong_FromVoidPtr(string_at)); MOD_ADD("_string_at_addr", PyLong_FromVoidPtr(string_at));
MOD_ADD("_cast_addr", PyLong_FromVoidPtr(cast)); MOD_ADD("_cast_addr", PyLong_FromVoidPtr(cast));
#ifdef CTYPES_UNICODE
MOD_ADD("_wstring_at_addr", PyLong_FromVoidPtr(wstring_at)); MOD_ADD("_wstring_at_addr", PyLong_FromVoidPtr(wstring_at));
#endif
/* If RTLD_LOCAL is not defined (Windows!), set it to zero. */ /* If RTLD_LOCAL is not defined (Windows!), set it to zero. */
#if !HAVE_DECL_RTLD_LOCAL #if !HAVE_DECL_RTLD_LOCAL

View File

@ -713,7 +713,6 @@ static int ConvParam(PyObject *obj, Py_ssize_t index, struct argument *pa)
return 0; return 0;
} }
#ifdef CTYPES_UNICODE
if (PyUnicode_Check(obj)) { if (PyUnicode_Check(obj)) {
pa->ffi_type = &ffi_type_pointer; pa->ffi_type = &ffi_type_pointer;
pa->value.p = PyUnicode_AsWideCharString(obj, NULL); pa->value.p = PyUnicode_AsWideCharString(obj, NULL);
@ -726,7 +725,6 @@ static int ConvParam(PyObject *obj, Py_ssize_t index, struct argument *pa)
} }
return 0; return 0;
} }
#endif
{ {
_Py_IDENTIFIER(_as_parameter_); _Py_IDENTIFIER(_as_parameter_);

View File

@ -125,13 +125,11 @@ PyCField_FromDesc(PyObject *desc, Py_ssize_t index,
getfunc = fd->getfunc; getfunc = fd->getfunc;
setfunc = fd->setfunc; setfunc = fd->setfunc;
} }
#ifdef CTYPES_UNICODE
if (idict->getfunc == _ctypes_get_fielddesc("u")->getfunc) { if (idict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
struct fielddesc *fd = _ctypes_get_fielddesc("U"); struct fielddesc *fd = _ctypes_get_fielddesc("U");
getfunc = fd->getfunc; getfunc = fd->getfunc;
setfunc = fd->setfunc; setfunc = fd->setfunc;
} }
#endif
} }
} }
@ -1137,7 +1135,6 @@ c_get(void *ptr, Py_ssize_t size)
return PyBytes_FromStringAndSize((char *)ptr, 1); return PyBytes_FromStringAndSize((char *)ptr, 1);
} }
#ifdef CTYPES_UNICODE
/* u - a single wchar_t character */ /* u - a single wchar_t character */
static PyObject * static PyObject *
u_set(void *ptr, PyObject *value, Py_ssize_t size) u_set(void *ptr, PyObject *value, Py_ssize_t size)
@ -1232,7 +1229,6 @@ U_set(void *ptr, PyObject *value, Py_ssize_t length)
return value; return value;
} }
#endif
static PyObject * static PyObject *
s_get(void *ptr, Py_ssize_t size) s_get(void *ptr, Py_ssize_t size)
@ -1321,7 +1317,6 @@ z_get(void *ptr, Py_ssize_t size)
} }
} }
#ifdef CTYPES_UNICODE
static PyObject * static PyObject *
Z_set(void *ptr, PyObject *value, Py_ssize_t size) Z_set(void *ptr, PyObject *value, Py_ssize_t size)
{ {
@ -1373,7 +1368,7 @@ Z_get(void *ptr, Py_ssize_t size)
Py_RETURN_NONE; Py_RETURN_NONE;
} }
} }
#endif
#ifdef MS_WIN32 #ifdef MS_WIN32
static PyObject * static PyObject *
@ -1507,11 +1502,9 @@ static struct fielddesc formattable[] = {
#endif #endif
{ 'P', P_set, P_get, &ffi_type_pointer}, { 'P', P_set, P_get, &ffi_type_pointer},
{ 'z', z_set, z_get, &ffi_type_pointer}, { 'z', z_set, z_get, &ffi_type_pointer},
#ifdef CTYPES_UNICODE
{ 'u', u_set, u_get, NULL}, /* ffi_type set later */ { 'u', u_set, u_get, NULL}, /* ffi_type set later */
{ 'U', U_set, U_get, &ffi_type_pointer}, { 'U', U_set, U_get, &ffi_type_pointer},
{ 'Z', Z_set, Z_get, &ffi_type_pointer}, { 'Z', Z_set, Z_get, &ffi_type_pointer},
#endif
#ifdef MS_WIN32 #ifdef MS_WIN32
{ 'X', BSTR_set, BSTR_get, &ffi_type_pointer}, { 'X', BSTR_set, BSTR_get, &ffi_type_pointer},
#endif #endif
@ -1544,14 +1537,12 @@ _ctypes_get_fielddesc(const char *fmt)
if (!initialized) { if (!initialized) {
initialized = 1; initialized = 1;
#ifdef CTYPES_UNICODE
if (sizeof(wchar_t) == sizeof(short)) if (sizeof(wchar_t) == sizeof(short))
_ctypes_get_fielddesc("u")->pffi_type = &ffi_type_sshort; _ctypes_get_fielddesc("u")->pffi_type = &ffi_type_sshort;
else if (sizeof(wchar_t) == sizeof(int)) else if (sizeof(wchar_t) == sizeof(int))
_ctypes_get_fielddesc("u")->pffi_type = &ffi_type_sint; _ctypes_get_fielddesc("u")->pffi_type = &ffi_type_sint;
else if (sizeof(wchar_t) == sizeof(long)) else if (sizeof(wchar_t) == sizeof(long))
_ctypes_get_fielddesc("u")->pffi_type = &ffi_type_slong; _ctypes_get_fielddesc("u")->pffi_type = &ffi_type_slong;
#endif
} }
for (; table->code; ++table) { for (; table->code; ++table) {

View File

@ -343,10 +343,6 @@ extern PyObject *PyExc_ArgError;
extern char *_ctypes_conversion_encoding; extern char *_ctypes_conversion_encoding;
extern char *_ctypes_conversion_errors; extern char *_ctypes_conversion_errors;
#if defined(HAVE_WCHAR_H)
# define CTYPES_UNICODE
#endif
extern void _ctypes_free_closure(void *); extern void _ctypes_free_closure(void *);
extern void *_ctypes_alloc_closure(void); extern void *_ctypes_alloc_closure(void);

View File

@ -538,9 +538,7 @@ PyCStructUnionType_update_stgdict(PyObject *type, PyObject *fields, int isStruct
case FFI_TYPE_SINT16: case FFI_TYPE_SINT16:
case FFI_TYPE_SINT32: case FFI_TYPE_SINT32:
if (dict->getfunc != _ctypes_get_fielddesc("c")->getfunc if (dict->getfunc != _ctypes_get_fielddesc("c")->getfunc
#ifdef CTYPES_UNICODE
&& dict->getfunc != _ctypes_get_fielddesc("u")->getfunc && dict->getfunc != _ctypes_get_fielddesc("u")->getfunc
#endif
) )
break; break;
/* else fall through */ /* else fall through */