Index: Include/unicodeobject.h =================================================================== --- Include/unicodeobject.h (revision 66358) +++ Include/unicodeobject.h (working copy) @@ -1209,6 +1209,12 @@ #endif /* MS_WIN32 */ +/* --- Locale Codec ------------------------------------------------------- */ + +PyAPI_FUNC(PyObject*) PyUnicode_DecodeLocale( + const char *string /* Multibyte string */ + ); + /* --- Decimal Encoder ---------------------------------------------------- */ /* Takes a Unicode string holding a decimal value and writes it into Index: Objects/unicodeobject.c =================================================================== --- Objects/unicodeobject.c (revision 66358) +++ Objects/unicodeobject.c (working copy) @@ -4162,6 +4162,45 @@ #endif /* MS_WINDOWS */ +/* --- Locale Codec ------------------------------------------------------- */ + +PyObject *PyUnicode_DecodeLocale(const char *s) +{ + wchar_t *wbuf; + PyObject *res; + +#ifdef HAVE_BROKEN_MBSTOWCS + /* Some platforms have a broken implementation of + * mbstowcs which does not count the characters that + * would result from conversion. Use an upper bound. + */ + size_t wlen = strlen(s); +#else + size_t wlen = mbstowcs(NULL, s, 0); +#endif + if (wlen == (size_t)-1) { + PyErr_SetString(PyExc_UnicodeError, "Could not convert to string"); + return NULL; + } + + wbuf = PyMem_Malloc((wlen+1)*sizeof(wchar_t)); + if (!wbuf) { + PyErr_NoMemory(); + return NULL; + } + + wlen = mbstowcs(wbuf, s, wlen+1); + if (wlen == (size_t)-1) { + PyErr_SetString(PyExc_UnicodeError, "Could not convert to string"); + PyMem_Free(wbuf); + return NULL; + } + + res = PyUnicode_FromWideChar(wbuf, wlen); + PyMem_Free(wbuf); + return res; +} + /* --- Character Mapping Codec -------------------------------------------- */ PyObject *PyUnicode_DecodeCharmap(const char *s, Index: Modules/grpmodule.c =================================================================== --- Modules/grpmodule.c (revision 66358) +++ Modules/grpmodule.c (working copy) @@ -47,7 +47,7 @@ return NULL; } for (member = p->gr_mem; *member != NULL; member++) { - PyObject *x = PyUnicode_FromString(*member); + PyObject *x = PyUnicode_DecodeLocale(*member); if (x == NULL || PyList_Append(w, x) != 0) { Py_XDECREF(x); Py_DECREF(w); @@ -58,13 +58,13 @@ } #define SET(i,val) PyStructSequence_SET_ITEM(v, i, val) - SET(setIndex++, PyUnicode_FromString(p->gr_name)); + SET(setIndex++, PyUnicode_DecodeLocale(p->gr_name)); #ifdef __VMS SET(setIndex++, Py_None); Py_INCREF(Py_None); #else if (p->gr_passwd) - SET(setIndex++, PyUnicode_FromString(p->gr_passwd)); + SET(setIndex++, PyUnicode_DecodeLocale(p->gr_passwd)); else { SET(setIndex++, Py_None); Py_INCREF(Py_None);