Commit cb3ae558 authored by Victor Stinner's avatar Victor Stinner Committed by GitHub

bpo-29240: Ignore UTF-8 Mode in time module (#5148)

time.strftime() must use the current LC_CTYPE encoding, not UTF-8
if the UTF-8 mode is enabled.

Add _PyUnicode_DecodeCurrentLocale() function.
parent 3948207c
...@@ -1811,6 +1811,10 @@ PyAPI_FUNC(PyObject*) PyUnicode_EncodeLocale( ...@@ -1811,6 +1811,10 @@ PyAPI_FUNC(PyObject*) PyUnicode_EncodeLocale(
const char *errors const char *errors
); );
PyAPI_FUNC(PyObject*) _PyUnicode_DecodeCurrentLocale(
const char *str,
const char *errors);
PyAPI_FUNC(PyObject*) _PyUnicode_DecodeCurrentLocaleAndSize( PyAPI_FUNC(PyObject*) _PyUnicode_DecodeCurrentLocaleAndSize(
const char *str, const char *str,
Py_ssize_t len, Py_ssize_t len,
......
...@@ -138,8 +138,7 @@ encode(PyObject *b) ...@@ -138,8 +138,7 @@ encode(PyObject *b)
static PyObject * static PyObject *
decode(const char *s) decode(const char *s)
{ {
return _PyUnicode_DecodeCurrentLocaleAndSize(s, strlen(s), return _PyUnicode_DecodeCurrentLocale(s, "surrogateescape");
"surrogateescape");
} }
......
...@@ -418,11 +418,11 @@ tmtotuple(struct tm *p ...@@ -418,11 +418,11 @@ tmtotuple(struct tm *p
SET(8, p->tm_isdst); SET(8, p->tm_isdst);
#ifdef HAVE_STRUCT_TM_TM_ZONE #ifdef HAVE_STRUCT_TM_TM_ZONE
PyStructSequence_SET_ITEM(v, 9, PyStructSequence_SET_ITEM(v, 9,
PyUnicode_DecodeLocale(p->tm_zone, "surrogateescape")); _PyUnicode_DecodeCurrentLocale(p->tm_zone, "surrogateescape"));
SET(10, p->tm_gmtoff); SET(10, p->tm_gmtoff);
#else #else
PyStructSequence_SET_ITEM(v, 9, PyStructSequence_SET_ITEM(v, 9,
PyUnicode_DecodeLocale(zone, "surrogateescape")); _PyUnicode_DecodeCurrentLocale(zone, "surrogateescape"));
PyStructSequence_SET_ITEM(v, 10, _PyLong_FromTime_t(gmtoff)); PyStructSequence_SET_ITEM(v, 10, _PyLong_FromTime_t(gmtoff));
#endif /* HAVE_STRUCT_TM_TM_ZONE */ #endif /* HAVE_STRUCT_TM_TM_ZONE */
#undef SET #undef SET
...@@ -809,7 +809,7 @@ time_strftime(PyObject *self, PyObject *args) ...@@ -809,7 +809,7 @@ time_strftime(PyObject *self, PyObject *args)
#ifdef HAVE_WCSFTIME #ifdef HAVE_WCSFTIME
ret = PyUnicode_FromWideChar(outbuf, buflen); ret = PyUnicode_FromWideChar(outbuf, buflen);
#else #else
ret = PyUnicode_DecodeLocaleAndSize(outbuf, buflen, ret = _PyUnicode_DecodeCurrentLocaleAndSize(outbuf, buflen,
"surrogateescape"); "surrogateescape");
#endif #endif
PyMem_Free(outbuf); PyMem_Free(outbuf);
...@@ -1541,8 +1541,8 @@ PyInit_timezone(PyObject *m) { ...@@ -1541,8 +1541,8 @@ PyInit_timezone(PyObject *m) {
PyModule_AddIntConstant(m, "altzone", timezone-3600); PyModule_AddIntConstant(m, "altzone", timezone-3600);
#endif #endif
PyModule_AddIntConstant(m, "daylight", daylight); PyModule_AddIntConstant(m, "daylight", daylight);
otz0 = PyUnicode_DecodeLocale(tzname[0], "surrogateescape"); otz0 = _PyUnicode_DecodeCurrentLocale(tzname[0], "surrogateescape");
otz1 = PyUnicode_DecodeLocale(tzname[1], "surrogateescape"); otz1 = _PyUnicode_DecodeCurrentLocale(tzname[1], "surrogateescape");
PyModule_AddObject(m, "tzname", Py_BuildValue("(NN)", otz0, otz1)); PyModule_AddObject(m, "tzname", Py_BuildValue("(NN)", otz0, otz1));
#else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/ #else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
{ {
......
...@@ -3836,6 +3836,12 @@ _PyUnicode_DecodeCurrentLocaleAndSize(const char *str, Py_ssize_t len, ...@@ -3836,6 +3836,12 @@ _PyUnicode_DecodeCurrentLocaleAndSize(const char *str, Py_ssize_t len,
return unicode_decode_locale(str, len, errors, 1); return unicode_decode_locale(str, len, errors, 1);
} }
PyObject*
_PyUnicode_DecodeCurrentLocale(const char *str, const char *errors)
{
return unicode_decode_locale(str, (Py_ssize_t)strlen(str), errors, 1);
}
PyObject* PyObject*
PyUnicode_DecodeLocale(const char *str, const char *errors) PyUnicode_DecodeLocale(const char *str, const char *errors)
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment