Skip to content

bpo-38823: Always build _ctypes with wchar_t #23248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 12, 2020
Merged
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
Original file line number Diff line number Diff line change
@@ -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.
16 changes: 3 additions & 13 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1358,7 +1358,6 @@ static PyGetSetDef CharArray_getsets[] = {
{ NULL, NULL }
};

#ifdef CTYPES_UNICODE
static PyObject *
WCharArray_get_value(CDataObject *self, void *Py_UNUSED(ignored))
{
Expand Down Expand Up @@ -1408,7 +1407,6 @@ static PyGetSetDef WCharArray_getsets[] = {
"string value"},
{ NULL, NULL }
};
#endif

/*
The next three functions copied from Python's typeobject.c.
Expand Down Expand Up @@ -1615,11 +1613,10 @@ PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
if (itemdict->getfunc == _ctypes_get_fielddesc("c")->getfunc) {
if (-1 == add_getset(result, CharArray_getsets))
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))
goto error;
#endif
}

return (PyObject *)result;
Expand Down Expand Up @@ -4654,7 +4651,6 @@ Array_subscript(PyObject *myself, PyObject *item)
PyMem_Free(dest);
return np;
}
#ifdef CTYPES_UNICODE
if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
wchar_t *ptr = (wchar_t *)self->b_ptr;
wchar_t *dest;
Expand All @@ -4681,7 +4677,6 @@ Array_subscript(PyObject *myself, PyObject *item)
PyMem_Free(dest);
return np;
}
#endif

np = PyList_New(slicelen);
if (np == NULL)
Expand Down Expand Up @@ -5350,7 +5345,6 @@ Pointer_subscript(PyObject *myself, PyObject *item)
PyMem_Free(dest);
return np;
}
#ifdef CTYPES_UNICODE
if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
wchar_t *ptr = *(wchar_t **)self->b_ptr;
wchar_t *dest;
Expand All @@ -5371,7 +5365,6 @@ Pointer_subscript(PyObject *myself, PyObject *item)
PyMem_Free(dest);
return np;
}
#endif

np = PyList_New(len);
if (np == NULL)
Expand Down Expand Up @@ -5653,7 +5646,7 @@ cast(void *ptr, PyObject *src, PyObject *ctype)
return NULL;
}

#ifdef CTYPES_UNICODE

static PyObject *
wstring_at(const wchar_t *ptr, int size)
{
Expand All @@ -5665,7 +5658,6 @@ wstring_at(const wchar_t *ptr, int size)
ssize = wcslen(ptr);
return PyUnicode_FromWideChar(ptr, ssize);
}
#endif


static struct PyModuleDef _ctypesmodule = {
Expand Down Expand Up @@ -5796,9 +5788,7 @@ _ctypes_add_objects(PyObject *mod)
MOD_ADD("_memset_addr", PyLong_FromVoidPtr(memset));
MOD_ADD("_string_at_addr", PyLong_FromVoidPtr(string_at));
MOD_ADD("_cast_addr", PyLong_FromVoidPtr(cast));
#ifdef CTYPES_UNICODE
MOD_ADD("_wstring_at_addr", PyLong_FromVoidPtr(wstring_at));
#endif

/* If RTLD_LOCAL is not defined (Windows!), set it to zero. */
#if !HAVE_DECL_RTLD_LOCAL
Expand Down
2 changes: 0 additions & 2 deletions Modules/_ctypes/callproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,6 @@ static int ConvParam(PyObject *obj, Py_ssize_t index, struct argument *pa)
return 0;
}

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

{
_Py_IDENTIFIER(_as_parameter_);
Expand Down
11 changes: 1 addition & 10 deletions Modules/_ctypes/cfield.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,11 @@ PyCField_FromDesc(PyObject *desc, Py_ssize_t index,
getfunc = fd->getfunc;
setfunc = fd->setfunc;
}
#ifdef CTYPES_UNICODE
if (idict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
struct fielddesc *fd = _ctypes_get_fielddesc("U");
getfunc = fd->getfunc;
setfunc = fd->setfunc;
}
#endif
}
}

Expand Down Expand Up @@ -1137,7 +1135,6 @@ c_get(void *ptr, Py_ssize_t size)
return PyBytes_FromStringAndSize((char *)ptr, 1);
}

#ifdef CTYPES_UNICODE
/* u - a single wchar_t character */
static PyObject *
u_set(void *ptr, PyObject *value, Py_ssize_t size)
Expand Down Expand Up @@ -1232,7 +1229,6 @@ U_set(void *ptr, PyObject *value, Py_ssize_t length)
return value;
}

#endif

static PyObject *
s_get(void *ptr, Py_ssize_t size)
Expand Down Expand Up @@ -1321,7 +1317,6 @@ z_get(void *ptr, Py_ssize_t size)
}
}

#ifdef CTYPES_UNICODE
static PyObject *
Z_set(void *ptr, PyObject *value, Py_ssize_t size)
{
Expand Down Expand Up @@ -1373,7 +1368,7 @@ Z_get(void *ptr, Py_ssize_t size)
Py_RETURN_NONE;
}
}
#endif


#ifdef MS_WIN32
static PyObject *
Expand Down Expand Up @@ -1507,11 +1502,9 @@ static struct fielddesc formattable[] = {
#endif
{ 'P', P_set, P_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, &ffi_type_pointer},
{ 'Z', Z_set, Z_get, &ffi_type_pointer},
#endif
#ifdef MS_WIN32
{ 'X', BSTR_set, BSTR_get, &ffi_type_pointer},
#endif
Expand Down Expand Up @@ -1544,14 +1537,12 @@ _ctypes_get_fielddesc(const char *fmt)

if (!initialized) {
initialized = 1;
#ifdef CTYPES_UNICODE
if (sizeof(wchar_t) == sizeof(short))
_ctypes_get_fielddesc("u")->pffi_type = &ffi_type_sshort;
else if (sizeof(wchar_t) == sizeof(int))
_ctypes_get_fielddesc("u")->pffi_type = &ffi_type_sint;
else if (sizeof(wchar_t) == sizeof(long))
_ctypes_get_fielddesc("u")->pffi_type = &ffi_type_slong;
#endif
}

for (; table->code; ++table) {
Expand Down
4 changes: 0 additions & 4 deletions Modules/_ctypes/ctypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,6 @@ extern PyObject *PyExc_ArgError;
extern char *_ctypes_conversion_encoding;
extern char *_ctypes_conversion_errors;

#if defined(HAVE_WCHAR_H)
# define CTYPES_UNICODE
#endif


extern void _ctypes_free_closure(void *);
extern void *_ctypes_alloc_closure(void);
Expand Down
2 changes: 0 additions & 2 deletions Modules/_ctypes/stgdict.c
Original file line number Diff line number Diff line change
Expand Up @@ -538,9 +538,7 @@ PyCStructUnionType_update_stgdict(PyObject *type, PyObject *fields, int isStruct
case FFI_TYPE_SINT16:
case FFI_TYPE_SINT32:
if (dict->getfunc != _ctypes_get_fielddesc("c")->getfunc
#ifdef CTYPES_UNICODE
&& dict->getfunc != _ctypes_get_fielddesc("u")->getfunc
#endif
)
break;
/* else fall through */
Expand Down