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,
} 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");
+ }
+ // PyUnicode_AsWideChar() returns number of wchars including trailing null byte,
+ // when it is called with NULL.
+ if (((size_t)len-1) > self->b_size/sizeof(wchar_t)) {
+ PyErr_SetString(PyExc_ValueError, "string too long");
result = -1;
goto done;
}
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);
return NULL;
}
- wstr = PyUnicode_AsUnicodeAndSize(value, &size);
- if (wstr == NULL)
+ Py_ssize_t size = PyUnicode_AsWideChar(value, NULL, 0);
+ if (size < 0) {
return NULL;
+ }
+ // PyUnicode_AsWideChar() returns number of wchars including trailing null byte,
+ // when it is called with NULL.
+ size--;
+ assert(size >= 0);
if (size > length) {
PyErr_Format(PyExc_ValueError,
"string too long (%zd, maximum length %zd)",
/* create a BSTR from value */
if (value) {
- wchar_t* wvalue;
Py_ssize_t wsize;
- wvalue = PyUnicode_AsUnicodeAndSize(value, &wsize);
- if (wvalue == NULL)
+ wchar_t *wvalue = PyUnicode_AsWideCharString(value, &wsize);
+ if (wvalue == NULL) {
return NULL;
+ }
if ((unsigned) wsize != wsize) {
PyErr_SetString(PyExc_ValueError, "String too long for BSTR");
+ PyMem_Free(wvalue);
return NULL;
}
bstr = SysAllocStringLen(wvalue, (unsigned)wsize);
+ PyMem_Free(wvalue);
} else
bstr = NULL;