Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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
6 changes: 6 additions & 0 deletions 6 Include/unicodeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,12 @@ PyAPI_FUNC(wchar_t*) PyUnicode_AsWideCharString(
);

#ifndef Py_LIMITED_API
/* Similar to PyUnicode_AsWideCharString(unicode, NULL), but check if
the string contains null characters. */
PyAPI_FUNC(wchar_t*) _PyUnicode_AsWideCharString(
PyObject *unicode /* Unicode object */
);

PyAPI_FUNC(void*) _PyUnicode_AsKind(PyObject *s, unsigned int kind);
#endif

Expand Down
4 changes: 2 additions & 2 deletions 4 Lib/ctypes/test/test_slicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def test_wchar_ptr(self):
dll.my_wcsdup.restype = POINTER(c_wchar)
dll.my_wcsdup.argtypes = POINTER(c_wchar),
dll.my_free.restype = None
res = dll.my_wcsdup(s)
res = dll.my_wcsdup(s[:-1])
self.assertEqual(res[:len(s)], s)
self.assertEqual(res[:len(s):], s)
self.assertEqual(res[len(s)-1:-1:-1], s[::-1])
Expand All @@ -153,7 +153,7 @@ def test_wchar_ptr(self):
dll.my_wcsdup.restype = POINTER(c_long)
else:
self.skipTest('Pointers to c_wchar are not supported')
res = dll.my_wcsdup(s)
res = dll.my_wcsdup(s[:-1])
tmpl = list(range(ord("a"), ord("z")+1))
self.assertEqual(res[:len(s)-1], tmpl)
self.assertEqual(res[:len(s)-1:], tmpl)
Expand Down
2 changes: 1 addition & 1 deletion 2 Modules/_ctypes/callproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ static int ConvParam(PyObject *obj, Py_ssize_t index, struct argument *pa)
#ifdef CTYPES_UNICODE
if (PyUnicode_Check(obj)) {
pa->ffi_type = &ffi_type_pointer;
pa->value.p = PyUnicode_AsWideCharString(obj, NULL);
pa->value.p = _PyUnicode_AsWideCharString(obj);
if (pa->value.p == NULL)
return -1;
pa->keep = PyCapsule_New(pa->value.p, CTYPES_CAPSULE_NAME_PYMEM, pymem_destructor);
Expand Down
2 changes: 1 addition & 1 deletion 2 Modules/_ctypes/cfield.c
Original file line number Diff line number Diff line change
Expand Up @@ -1386,7 +1386,7 @@ Z_set(void *ptr, PyObject *value, Py_ssize_t size)

/* We must create a wchar_t* buffer from the unicode object,
and keep it alive */
buffer = PyUnicode_AsWideCharString(value, NULL);
buffer = _PyUnicode_AsWideCharString(value);
if (!buffer)
return NULL;
keep = PyCapsule_New(buffer, CTYPES_CFIELD_CAPSULE_NAME_PYMEM, pymem_destructor);
Expand Down
2 changes: 1 addition & 1 deletion 2 Modules/_cursesmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ PyCurses_ConvertToString(PyCursesWindowObject *win, PyObject *obj,
if (PyUnicode_Check(obj)) {
#ifdef HAVE_NCURSESW
assert (wstr != NULL);
*wstr = PyUnicode_AsWideCharString(obj, NULL);
*wstr = _PyUnicode_AsWideCharString(obj);
if (*wstr == NULL)
return 0;
return 2;
Expand Down
4 changes: 2 additions & 2 deletions 4 Modules/_localemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,10 @@ PyLocale_strcoll(PyObject* self, PyObject* args)
if (!PyArg_ParseTuple(args, "UU:strcoll", &os1, &os2))
return NULL;
/* Convert the unicode strings to wchar[]. */
ws1 = PyUnicode_AsWideCharString(os1, NULL);
ws1 = _PyUnicode_AsWideCharString(os1);
if (ws1 == NULL)
goto done;
ws2 = PyUnicode_AsWideCharString(os2, NULL);
ws2 = _PyUnicode_AsWideCharString(os2);
if (ws2 == NULL)
goto done;
/* Collate the strings. */
Expand Down
2 changes: 1 addition & 1 deletion 2 Modules/_tkinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -3620,7 +3620,7 @@ PyInit__tkinter(void)
return NULL;
}
if (str_path != NULL) {
wcs_path = PyUnicode_AsWideCharString(str_path, NULL);
wcs_path = _PyUnicode_AsWideCharString(str_path);
if (wcs_path == NULL) {
return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion 2 Modules/overlapped.c
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,7 @@ ConnectPipe(OverlappedObject *self, PyObject *args)
if (!PyArg_ParseTuple(args, "U", &AddressObj))
return NULL;

Address = PyUnicode_AsWideCharString(AddressObj, NULL);
Address = _PyUnicode_AsWideCharString(AddressObj);
if (Address == NULL)
return NULL;

Expand Down
2 changes: 1 addition & 1 deletion 2 Modules/timemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ time_strftime(PyObject *self, PyObject *args)
buf.tm_isdst = 1;

#ifdef HAVE_WCSFTIME
format = PyUnicode_AsWideCharString(format_arg, NULL);
format = _PyUnicode_AsWideCharString(format_arg);
if (format == NULL)
return NULL;
fmt = format;
Expand Down
31 changes: 31 additions & 0 deletions 31 Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.