summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2011-12-16 22:56:01 (GMT)
committerVictor Stinner <victor.stinner@haypocalc.com>2011-12-16 22:56:01 (GMT)
commitaf02e1c85a66009cdc645a64de7d7ee1335c8301 (patch)
tree5bc78c3a8628589cf5a4c246afc0076871d51c62 /Modules
parent3607e3de278c89660f773064a94385066eebda1b (diff)
downloadcpython-af02e1c85a66009cdc645a64de7d7ee1335c8301.zip
cpython-af02e1c85a66009cdc645a64de7d7ee1335c8301.tar.gz
cpython-af02e1c85a66009cdc645a64de7d7ee1335c8301.tar.bz2
Add PyUnicode_DecodeLocaleAndSize() and PyUnicode_DecodeLocale()
* PyUnicode_DecodeLocaleAndSize() and PyUnicode_DecodeLocale() decode a string from the current locale encoding * _Py_char2wchar() writes an "error code" in the size argument to indicate if the function failed because of memory allocation failure or because of a decoding error. The function doesn't write the error message directly to stderr. * Fix time.strftime() (if wcsftime() is missing): decode strftime() result from the current locale encoding, not from the filesystem encoding.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_localemodule.c57
-rw-r--r--Modules/main.c13
-rw-r--r--Modules/timemodule.c6
3 files changed, 18 insertions, 58 deletions
diff --git a/Modules/_localemodule.c b/Modules/_localemodule.c
index 9bba1b3..1cab7c0 100644
--- a/Modules/_localemodule.c
+++ b/Modules/_localemodule.c
@@ -42,43 +42,6 @@ PyDoc_STRVAR(locale__doc__, "Support for POSIX locales.");
static PyObject *Error;
-/* Convert a char* to a Unicode object according to the current locale */
-static PyObject*
-str2uni(const char* s)
-{
-#ifdef HAVE_BROKEN_MBSTOWCS
- size_t needed = strlen(s);
-#else
- size_t needed = mbstowcs(NULL, s, 0);
-#endif
- size_t res1;
- wchar_t smallbuf[30];
- wchar_t *dest;
- PyObject *res2;
- if (needed == (size_t)-1) {
- PyErr_SetString(PyExc_ValueError, "Cannot convert byte to string");
- return NULL;
- }
- if (needed*sizeof(wchar_t) < sizeof(smallbuf))
- dest = smallbuf;
- else {
- dest = PyMem_Malloc((needed+1)*sizeof(wchar_t));
- if (!dest)
- return PyErr_NoMemory();
- }
- /* This shouldn't fail now */
- res1 = mbstowcs(dest, s, needed+1);
-#ifdef HAVE_BROKEN_MBSTOWCS
- assert(res1 != (size_t)-1);
-#else
- assert(res1 == needed);
-#endif
- res2 = PyUnicode_FromWideChar(dest, res1);
- if (dest != smallbuf)
- PyMem_Free(dest);
- return res2;
-}
-
/* support functions for formatting floating point numbers */
PyDoc_STRVAR(setlocale__doc__,
@@ -149,7 +112,7 @@ PyLocale_setlocale(PyObject* self, PyObject* args)
PyErr_SetString(Error, "unsupported locale setting");
return NULL;
}
- result_object = str2uni(result);
+ result_object = PyUnicode_DecodeLocale(result, 0);
if (!result_object)
return NULL;
} else {
@@ -159,7 +122,7 @@ PyLocale_setlocale(PyObject* self, PyObject* args)
PyErr_SetString(Error, "locale query failed");
return NULL;
}
- result_object = str2uni(result);
+ result_object = PyUnicode_DecodeLocale(result, 0);
}
return result_object;
}
@@ -185,7 +148,7 @@ PyLocale_localeconv(PyObject* self)
involved herein */
#define RESULT_STRING(s)\
- x = str2uni(l->s); \
+ x = PyUnicode_DecodeLocale(l->s, 0); \
if (!x) goto failed;\
PyDict_SetItemString(result, #s, x);\
Py_XDECREF(x)
@@ -476,7 +439,7 @@ PyLocale_nl_langinfo(PyObject* self, PyObject* args)
instead of an empty string for nl_langinfo(ERA). */
const char *result = nl_langinfo(item);
result = result != NULL ? result : "";
- return str2uni(result);
+ return PyUnicode_DecodeLocale(result, 0);
}
PyErr_SetString(PyExc_ValueError, "unsupported langinfo constant");
return NULL;
@@ -495,7 +458,7 @@ PyIntl_gettext(PyObject* self, PyObject *args)
char *in;
if (!PyArg_ParseTuple(args, "s", &in))
return 0;
- return str2uni(gettext(in));
+ return PyUnicode_DecodeLocale(gettext(in), 0);
}
PyDoc_STRVAR(dgettext__doc__,
@@ -508,7 +471,7 @@ PyIntl_dgettext(PyObject* self, PyObject *args)
char *domain, *in;
if (!PyArg_ParseTuple(args, "zs", &domain, &in))
return 0;
- return str2uni(dgettext(domain, in));
+ return PyUnicode_DecodeLocale(dgettext(domain, in), 0);
}
PyDoc_STRVAR(dcgettext__doc__,
@@ -522,7 +485,7 @@ PyIntl_dcgettext(PyObject *self, PyObject *args)
int category;
if (!PyArg_ParseTuple(args, "zsi", &domain, &msgid, &category))
return 0;
- return str2uni(dcgettext(domain,msgid,category));
+ return PyUnicode_DecodeLocale(dcgettext(domain,msgid,category), 0);
}
PyDoc_STRVAR(textdomain__doc__,
@@ -540,7 +503,7 @@ PyIntl_textdomain(PyObject* self, PyObject* args)
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}
- return str2uni(domain);
+ return PyUnicode_DecodeLocale(domain, 0);
}
PyDoc_STRVAR(bindtextdomain__doc__,
@@ -572,7 +535,7 @@ PyIntl_bindtextdomain(PyObject* self,PyObject*args)
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}
- result = str2uni(current_dirname);
+ result = PyUnicode_DecodeLocale(current_dirname, 0);
Py_XDECREF(dirname_bytes);
return result;
}
@@ -590,7 +553,7 @@ PyIntl_bind_textdomain_codeset(PyObject* self,PyObject*args)
return NULL;
codeset = bind_textdomain_codeset(domain, codeset);
if (codeset)
- return str2uni(codeset);
+ return PyUnicode_DecodeLocale(codeset, 0);
Py_RETURN_NONE;
}
#endif
diff --git a/Modules/main.c b/Modules/main.c
index d4c3314..4899378 100644
--- a/Modules/main.c
+++ b/Modules/main.c
@@ -495,16 +495,13 @@ Py_Main(int argc, wchar_t **argv)
/* Use utf-8 on Mac OS X */
unicode = PyUnicode_FromString(p);
#else
- wchar_t *wchar;
- size_t len;
- wchar = _Py_char2wchar(p, &len);
- if (wchar == NULL)
- continue;
- unicode = PyUnicode_FromWideChar(wchar, len);
- PyMem_Free(wchar);
+ unicode = PyUnicode_DecodeLocale(p, 1);
#endif
- if (unicode == NULL)
+ if (unicode == NULL) {
+ /* ignore errors */
+ PyErr_Clear();
continue;
+ }
PySys_AddWarnOptionUnicode(unicode);
Py_DECREF(unicode);
}
diff --git a/Modules/timemodule.c b/Modules/timemodule.c
index 001b311..a46c4f1 100644
--- a/Modules/timemodule.c
+++ b/Modules/timemodule.c
@@ -532,7 +532,7 @@ time_strftime(PyObject *self, PyObject *args)
#ifdef HAVE_WCSFTIME
ret = PyUnicode_FromWideChar(outbuf, buflen);
#else
- ret = PyUnicode_DecodeFSDefaultAndSize(outbuf, buflen);
+ ret = PyUnicode_DecodeLocaleAndSize(outbuf, buflen, 1);
#endif
PyMem_Free(outbuf);
break;
@@ -764,8 +764,8 @@ PyInit_timezone(PyObject *m) {
#endif /* PYOS_OS2 */
#endif
PyModule_AddIntConstant(m, "daylight", daylight);
- otz0 = PyUnicode_DecodeFSDefaultAndSize(tzname[0], strlen(tzname[0]));
- otz1 = PyUnicode_DecodeFSDefaultAndSize(tzname[1], strlen(tzname[1]));
+ otz0 = PyUnicode_DecodeLocale(tzname[0], 1);
+ otz1 = PyUnicode_DecodeLocale(tzname[1], 1);
PyModule_AddObject(m, "tzname", Py_BuildValue("(NN)", otz0, otz1));
#else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
#ifdef HAVE_STRUCT_TM_TM_ZONE