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
11 changes: 11 additions & 0 deletions Include/cpython/unicodeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ typedef struct {
4 bytes (see issue #19537 on m68k). */
unsigned int :24;
} state;
#ifdef Py_WCHAR_CACHE
wchar_t *wstr; /* wchar_t representation (null-terminated) */
#endif
} PyASCIIObject;

/* Non-ASCII strings allocated through PyUnicode_New use the
Expand All @@ -229,8 +231,10 @@ typedef struct {
Py_ssize_t utf8_length; /* Number of bytes in utf8, excluding the
* terminating \0. */
char *utf8; /* UTF-8 representation (null-terminated) */
#ifdef Py_WCHAR_CACHE
Py_ssize_t wstr_length; /* Number of code points in wstr, possible
* surrogates count as two code points. */
#endif
} PyCompactUnicodeObject;

/* Strings allocated through PyUnicode_FromUnicode(NULL, len) use the
Expand All @@ -246,6 +250,9 @@ typedef struct {
} data; /* Canonical, smallest-form Unicode buffer */
} PyUnicodeObject;


#ifdef Py_WCHAR_CACHE

/* Fast access macros */
#define PyUnicode_WSTR_LENGTH(op) \
(PyUnicode_IS_COMPACT_ASCII(op) ? \
Expand Down Expand Up @@ -285,6 +292,8 @@ typedef struct {
((const char *)(PyUnicode_AS_UNICODE(op)))
/* Py_DEPRECATED(3.3) */

#endif


/* --- Flexible String Representation Helper Macros (PEP 393) -------------- */

Expand Down Expand Up @@ -568,6 +577,7 @@ PyAPI_FUNC(Py_UCS4) _PyUnicode_FindMaxChar (
Py_ssize_t start,
Py_ssize_t end);

#ifdef Py_WCHAR_CACHE
/* Return a read-only pointer to the Unicode object's internal
Py_UNICODE buffer.
If the wchar_t/Py_UNICODE representation is not yet available, this
Expand Down Expand Up @@ -595,6 +605,7 @@ PyAPI_FUNC(Py_UNICODE *) PyUnicode_AsUnicodeAndSize(
/* Get the maximum ordinal for a Unicode character. */
PyAPI_FUNC(Py_UNICODE) PyUnicode_GetMax(void) Py_DEPRECATED(3.3);

#endif

/* --- _PyUnicodeWriter API ----------------------------------------------- */

Expand Down
10 changes: 4 additions & 6 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1293,8 +1293,6 @@ static int
WCharArray_set_value(CDataObject *self, PyObject *value, void *Py_UNUSED(ignored))
{
Py_ssize_t result = 0;
Py_UNICODE *wstr;
Py_ssize_t len;

if (value == NULL) {
PyErr_SetString(PyExc_TypeError,
Expand All @@ -1309,12 +1307,12 @@ WCharArray_set_value(CDataObject *self, PyObject *value, void *Py_UNUSED(ignored
} else
Py_INCREF(value);

wstr = PyUnicode_AsUnicodeAndSize(value, &len);
if (wstr == NULL)
Py_ssize_t len = PyUnicode_AsWideChar(value, NULL, 0);
if (len < 0) {
return -1;
}
if ((size_t)len > self->b_size/sizeof(wchar_t)) {
PyErr_SetString(PyExc_ValueError,
"string too long");
PyErr_SetString(PyExc_ValueError, "string too long");
result = -1;
goto done;
}
Expand Down
15 changes: 6 additions & 9 deletions Modules/_ctypes/cfield.c
Original file line number Diff line number Diff line change
Expand Up @@ -1229,9 +1229,6 @@ U_get(void *ptr, Py_ssize_t size)
static PyObject *
U_set(void *ptr, PyObject *value, Py_ssize_t length)
{
Py_UNICODE *wstr;
Py_ssize_t size;

/* It's easier to calculate in characters than in bytes */
length /= sizeof(wchar_t);

Expand All @@ -1242,9 +1239,10 @@ U_set(void *ptr, PyObject *value, Py_ssize_t length)
return NULL;
}

wstr = PyUnicode_AsUnicodeAndSize(value, &size);
if (wstr == NULL)
Py_ssize_t size = PyUnicode_AsWideChar(value, NULL, 0);
if (size < 0) {
return NULL;
}
if (size > length) {
PyErr_Format(PyExc_ValueError,
"string too long (%zd, maximum length %zd)",
Expand Down Expand Up @@ -1421,11 +1419,10 @@ BSTR_set(void *ptr, PyObject *value, Py_ssize_t size)

/* create a BSTR from value */
if (value) {
wchar_t* wvalue;
Py_ssize_t wsize;
wvalue = PyUnicode_AsUnicodeAndSize(value, &wsize);
if (wvalue == NULL)
Py_ssize_t wsize = PyUnicode_AsWideChar(value, NULL, 0);
if (wsize < 0) {
return NULL;
}
if ((unsigned) wsize != wsize) {
PyErr_SetString(PyExc_ValueError, "String too long for BSTR");
return NULL;
Expand Down
10 changes: 10 additions & 0 deletions Modules/arraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,9 @@ d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)

DEFINE_COMPAREITEMS(b, signed char)
DEFINE_COMPAREITEMS(BB, unsigned char)
#ifdef Py_WCHAR_CACHE
DEFINE_COMPAREITEMS(u, Py_UNICODE)
#endif
DEFINE_COMPAREITEMS(h, short)
DEFINE_COMPAREITEMS(HH, unsigned short)
DEFINE_COMPAREITEMS(i, int)
Expand All @@ -550,7 +552,9 @@ DEFINE_COMPAREITEMS(QQ, unsigned long long)
static const struct arraydescr descriptors[] = {
{'b', 1, b_getitem, b_setitem, b_compareitems, "b", 1, 1},
{'B', 1, BB_getitem, BB_setitem, BB_compareitems, "B", 1, 0},
#ifdef Py_WCHAR_CACHE
{'u', sizeof(Py_UNICODE), u_getitem, u_setitem, u_compareitems, "u", 0, 0},
#endif
{'h', sizeof(short), h_getitem, h_setitem, h_compareitems, "h", 1, 1},
{'H', sizeof(short), HH_getitem, HH_setitem, HH_compareitems, "H", 1, 0},
{'i', sizeof(int), i_getitem, i_setitem, i_compareitems, "i", 1, 1},
Expand Down Expand Up @@ -2633,6 +2637,7 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
if (!PyArg_ParseTuple(args, "C|O:array", &c, &initial))
return NULL;

#ifdef Py_WCHAR_CACHE
if (initial && c != 'u') {
if (PyUnicode_Check(initial)) {
PyErr_Format(PyExc_TypeError, "cannot use a str to initialize "
Expand All @@ -2646,12 +2651,15 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return NULL;
}
}
#endif

if (!(initial == NULL || PyList_Check(initial)
|| PyByteArray_Check(initial)
|| PyBytes_Check(initial)
|| PyTuple_Check(initial)
#ifdef Py_WCHAR_CACHE
|| ((c=='u') && PyUnicode_Check(initial))
#endif
|| (array_Check(initial)
&& c == ((arrayobject*)initial)->ob_descr->typecode))) {
it = PyObject_GetIter(initial);
Expand Down Expand Up @@ -2710,6 +2718,7 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
}
Py_DECREF(v);
}
#ifdef Py_WCHAR_CACHE
else if (initial != NULL && PyUnicode_Check(initial)) {
Py_UNICODE *ustr;
Py_ssize_t n;
Expand Down Expand Up @@ -2737,6 +2746,7 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
self->allocated = Py_SIZE(self);
}
}
#endif
else if (initial != NULL && array_Check(initial) && len > 0) {
arrayobject *self = (arrayobject *)a;
arrayobject *other = (arrayobject *)initial;
Expand Down
Loading