diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2017-06-27 18:52:10 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-27 18:52:10 (GMT) |
commit | 94b169fe48bc7ea76b926823885d1b12c2c381fa (patch) | |
tree | 254a685184c3fab7c2f1311986a82b03cc028371 /Objects | |
parent | eb3c52a0d273491e745e0cbff2b73900bb96aa45 (diff) | |
download | cpython-94b169fe48bc7ea76b926823885d1b12c2c381fa.zip cpython-94b169fe48bc7ea76b926823885d1b12c2c381fa.tar.gz cpython-94b169fe48bc7ea76b926823885d1b12c2c381fa.tar.bz2 |
[3.5] bpo-30708: Add private C API function _PyUnicode_AsWideCharString(). (GH-2285) (GH-2443) (#2448)
And use it instead of PyUnicode_AsWideCharString() if appropriate.
_PyUnicode_AsWideCharString(unicode) is like PyUnicode_AsWideCharString(unicode, NULL), but
raises a ValueError if the wchar_t* string contains null characters.
(cherry picked from commit e613e6add5f07ff6aad5802924596b631b707d2a).
(cherry picked from commit 0edffa3073b551ffeca34952529e7b292f1bd350)
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/unicodeobject.c | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index d037d80..64375da 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -2839,6 +2839,37 @@ PyUnicode_AsWideCharString(PyObject *unicode, return buffer; } +wchar_t* +_PyUnicode_AsWideCharString(PyObject *unicode) +{ + const wchar_t *wstr; + wchar_t *buffer; + Py_ssize_t buflen; + + if (unicode == NULL) { + PyErr_BadInternalCall(); + return NULL; + } + + wstr = PyUnicode_AsUnicodeAndSize(unicode, &buflen); + if (wstr == NULL) { + return NULL; + } + if (wcslen(wstr) != (size_t)buflen) { + PyErr_SetString(PyExc_ValueError, + "embedded null character"); + return NULL; + } + + buffer = PyMem_NEW(wchar_t, buflen + 1); + if (buffer == NULL) { + PyErr_NoMemory(); + return NULL; + } + memcpy(buffer, wstr, (buflen + 1) * sizeof(wchar_t)); + return buffer; +} + #endif /* HAVE_WCHAR_H */ PyObject * |