@@ -2953,23 +2953,26 @@ PyUnicode_FromFormat(const char *format, ...)
29532953
29542954#ifdef HAVE_WCHAR_H
29552955
2956- /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString():
2957- convert a Unicode object to a wide character string.
2956+ /* Convert a Unicode object to a wide character string.
29582957
29592958 - If w is NULL: return the number of wide characters (including the null
29602959 character) required to convert the unicode object. Ignore size argument.
29612960
29622961 - Otherwise: return the number of wide characters (excluding the null
29632962 character) written into w. Write at most size wide characters (including
29642963 the null character). */
2965- static Py_ssize_t
2966- unicode_aswidechar (PyObject * unicode ,
2967- wchar_t * w ,
2968- Py_ssize_t size )
2964+ Py_ssize_t
2965+ PyUnicode_AsWideChar (PyObject * unicode ,
2966+ wchar_t * w ,
2967+ Py_ssize_t size )
29692968{
29702969 Py_ssize_t res ;
29712970 const wchar_t * wstr ;
29722971
2972+ if (unicode == NULL ) {
2973+ PyErr_BadInternalCall ();
2974+ return -1 ;
2975+ }
29732976 wstr = PyUnicode_AsUnicodeAndSize (unicode , & res );
29742977 if (wstr == NULL )
29752978 return -1 ;
@@ -2986,43 +2989,35 @@ unicode_aswidechar(PyObject *unicode,
29862989 return res + 1 ;
29872990}
29882991
2989- Py_ssize_t
2990- PyUnicode_AsWideChar (PyObject * unicode ,
2991- wchar_t * w ,
2992- Py_ssize_t size )
2993- {
2994- if (unicode == NULL ) {
2995- PyErr_BadInternalCall ();
2996- return -1 ;
2997- }
2998- return unicode_aswidechar (unicode , w , size );
2999- }
3000-
30012992wchar_t *
30022993PyUnicode_AsWideCharString (PyObject * unicode ,
30032994 Py_ssize_t * size )
30042995{
3005- wchar_t * buffer ;
2996+ const wchar_t * wstr ;
2997+ wchar_t * buffer ;
30062998 Py_ssize_t buflen ;
30072999
30083000 if (unicode == NULL ) {
30093001 PyErr_BadInternalCall ();
30103002 return NULL ;
30113003 }
30123004
3013- buflen = unicode_aswidechar (unicode , NULL , 0 );
3014- if (buflen == -1 )
3005+ wstr = PyUnicode_AsUnicodeAndSize (unicode , & buflen );
3006+ if (wstr == NULL ) {
30153007 return NULL ;
3016- buffer = PyMem_NEW (wchar_t , buflen );
3017- if (buffer == NULL ) {
3018- PyErr_NoMemory ();
3008+ }
3009+ if (size == NULL && wcslen (wstr ) != (size_t )buflen ) {
3010+ PyErr_SetString (PyExc_ValueError ,
3011+ "embedded null character" );
30193012 return NULL ;
30203013 }
3021- buflen = unicode_aswidechar (unicode , buffer , buflen );
3022- if (buflen == -1 ) {
3023- PyMem_FREE (buffer );
3014+
3015+ buffer = PyMem_NEW (wchar_t , buflen + 1 );
3016+ if (buffer == NULL ) {
3017+ PyErr_NoMemory ();
30243018 return NULL ;
30253019 }
3020+ memcpy (buffer , wstr , (buflen + 1 ) * sizeof (wchar_t ));
30263021 if (size != NULL )
30273022 * size = buflen ;
30283023 return buffer ;
0 commit comments