@@ -42,43 +42,6 @@ PyDoc_STRVAR(locale__doc__, "Support for POSIX locales.");
4242
4343static PyObject * Error ;
4444
45- /* Convert a char* to a Unicode object according to the current locale */
46- static PyObject *
47- str2uni (const char * s )
48- {
49- #ifdef HAVE_BROKEN_MBSTOWCS
50- size_t needed = strlen (s );
51- #else
52- size_t needed = mbstowcs (NULL , s , 0 );
53- #endif
54- size_t res1 ;
55- wchar_t smallbuf [30 ];
56- wchar_t * dest ;
57- PyObject * res2 ;
58- if (needed == (size_t )-1 ) {
59- PyErr_SetString (PyExc_ValueError , "Cannot convert byte to string" );
60- return NULL ;
61- }
62- if (needed * sizeof (wchar_t ) < sizeof (smallbuf ))
63- dest = smallbuf ;
64- else {
65- dest = PyMem_Malloc ((needed + 1 )* sizeof (wchar_t ));
66- if (!dest )
67- return PyErr_NoMemory ();
68- }
69- /* This shouldn't fail now */
70- res1 = mbstowcs (dest , s , needed + 1 );
71- #ifdef HAVE_BROKEN_MBSTOWCS
72- assert (res1 != (size_t )-1 );
73- #else
74- assert (res1 == needed );
75- #endif
76- res2 = PyUnicode_FromWideChar (dest , res1 );
77- if (dest != smallbuf )
78- PyMem_Free (dest );
79- return res2 ;
80- }
81-
8245/* support functions for formatting floating point numbers */
8346
8447PyDoc_STRVAR (setlocale__doc__ ,
@@ -149,7 +112,7 @@ PyLocale_setlocale(PyObject* self, PyObject* args)
149112 PyErr_SetString (Error , "unsupported locale setting" );
150113 return NULL ;
151114 }
152- result_object = str2uni (result );
115+ result_object = PyUnicode_DecodeLocale (result , 0 );
153116 if (!result_object )
154117 return NULL ;
155118 } else {
@@ -159,7 +122,7 @@ PyLocale_setlocale(PyObject* self, PyObject* args)
159122 PyErr_SetString (Error , "locale query failed" );
160123 return NULL ;
161124 }
162- result_object = str2uni (result );
125+ result_object = PyUnicode_DecodeLocale (result , 0 );
163126 }
164127 return result_object ;
165128}
@@ -185,7 +148,7 @@ PyLocale_localeconv(PyObject* self)
185148 involved herein */
186149
187150#define RESULT_STRING (s )\
188- x = str2uni (l->s); \
151+ x = PyUnicode_DecodeLocale (l->s, 0 ); \
189152 if (!x) goto failed;\
190153 PyDict_SetItemString(result, #s, x);\
191154 Py_XDECREF(x)
@@ -476,7 +439,7 @@ PyLocale_nl_langinfo(PyObject* self, PyObject* args)
476439 instead of an empty string for nl_langinfo(ERA). */
477440 const char * result = nl_langinfo (item );
478441 result = result != NULL ? result : "" ;
479- return str2uni (result );
442+ return PyUnicode_DecodeLocale (result , 0 );
480443 }
481444 PyErr_SetString (PyExc_ValueError , "unsupported langinfo constant" );
482445 return NULL ;
@@ -495,7 +458,7 @@ PyIntl_gettext(PyObject* self, PyObject *args)
495458 char * in ;
496459 if (!PyArg_ParseTuple (args , "s" , & in ))
497460 return 0 ;
498- return str2uni (gettext (in ));
461+ return PyUnicode_DecodeLocale (gettext (in ), 0 );
499462}
500463
501464PyDoc_STRVAR (dgettext__doc__ ,
@@ -508,7 +471,7 @@ PyIntl_dgettext(PyObject* self, PyObject *args)
508471 char * domain , * in ;
509472 if (!PyArg_ParseTuple (args , "zs" , & domain , & in ))
510473 return 0 ;
511- return str2uni (dgettext (domain , in ));
474+ return PyUnicode_DecodeLocale (dgettext (domain , in ), 0 );
512475}
513476
514477PyDoc_STRVAR (dcgettext__doc__ ,
@@ -522,7 +485,7 @@ PyIntl_dcgettext(PyObject *self, PyObject *args)
522485 int category ;
523486 if (!PyArg_ParseTuple (args , "zsi" , & domain , & msgid , & category ))
524487 return 0 ;
525- return str2uni (dcgettext (domain ,msgid ,category ));
488+ return PyUnicode_DecodeLocale (dcgettext (domain ,msgid ,category ), 0 );
526489}
527490
528491PyDoc_STRVAR (textdomain__doc__ ,
@@ -540,7 +503,7 @@ PyIntl_textdomain(PyObject* self, PyObject* args)
540503 PyErr_SetFromErrno (PyExc_OSError );
541504 return NULL ;
542505 }
543- return str2uni (domain );
506+ return PyUnicode_DecodeLocale (domain , 0 );
544507}
545508
546509PyDoc_STRVAR (bindtextdomain__doc__ ,
@@ -572,7 +535,7 @@ PyIntl_bindtextdomain(PyObject* self,PyObject*args)
572535 PyErr_SetFromErrno (PyExc_OSError );
573536 return NULL ;
574537 }
575- result = str2uni (current_dirname );
538+ result = PyUnicode_DecodeLocale (current_dirname , 0 );
576539 Py_XDECREF (dirname_bytes );
577540 return result ;
578541}
@@ -590,7 +553,7 @@ PyIntl_bind_textdomain_codeset(PyObject* self,PyObject*args)
590553 return NULL ;
591554 codeset = bind_textdomain_codeset (domain , codeset );
592555 if (codeset )
593- return str2uni (codeset );
556+ return PyUnicode_DecodeLocale (codeset , 0 );
594557 Py_RETURN_NONE ;
595558}
596559#endif
0 commit comments