Skip to content

Commit cb3ae55

Browse files
authored
time.strftime() must use the current LC_CTYPE encoding, not UTF-8 if the UTF-8 mode is enabled. Add _PyUnicode_DecodeCurrentLocale() function.
1 parent 3948207 commit cb3ae55

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

Include/unicodeobject.h

+4
Original file line numberDiff line numberDiff line change
@@ -1811,6 +1811,10 @@ PyAPI_FUNC(PyObject*) PyUnicode_EncodeLocale(
18111811
const char *errors
18121812
);
18131813

1814+
PyAPI_FUNC(PyObject*) _PyUnicode_DecodeCurrentLocale(
1815+
const char *str,
1816+
const char *errors);
1817+
18141818
PyAPI_FUNC(PyObject*) _PyUnicode_DecodeCurrentLocaleAndSize(
18151819
const char *str,
18161820
Py_ssize_t len,

Modules/readline.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ encode(PyObject *b)
138138
static PyObject *
139139
decode(const char *s)
140140
{
141-
return _PyUnicode_DecodeCurrentLocaleAndSize(s, strlen(s),
142-
"surrogateescape");
141+
return _PyUnicode_DecodeCurrentLocale(s, "surrogateescape");
143142
}
144143

145144

Modules/timemodule.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -418,11 +418,11 @@ tmtotuple(struct tm *p
418418
SET(8, p->tm_isdst);
419419
#ifdef HAVE_STRUCT_TM_TM_ZONE
420420
PyStructSequence_SET_ITEM(v, 9,
421-
PyUnicode_DecodeLocale(p->tm_zone, "surrogateescape"));
421+
_PyUnicode_DecodeCurrentLocale(p->tm_zone, "surrogateescape"));
422422
SET(10, p->tm_gmtoff);
423423
#else
424424
PyStructSequence_SET_ITEM(v, 9,
425-
PyUnicode_DecodeLocale(zone, "surrogateescape"));
425+
_PyUnicode_DecodeCurrentLocale(zone, "surrogateescape"));
426426
PyStructSequence_SET_ITEM(v, 10, _PyLong_FromTime_t(gmtoff));
427427
#endif /* HAVE_STRUCT_TM_TM_ZONE */
428428
#undef SET
@@ -809,8 +809,8 @@ time_strftime(PyObject *self, PyObject *args)
809809
#ifdef HAVE_WCSFTIME
810810
ret = PyUnicode_FromWideChar(outbuf, buflen);
811811
#else
812-
ret = PyUnicode_DecodeLocaleAndSize(outbuf, buflen,
813-
"surrogateescape");
812+
ret = _PyUnicode_DecodeCurrentLocaleAndSize(outbuf, buflen,
813+
"surrogateescape");
814814
#endif
815815
PyMem_Free(outbuf);
816816
break;
@@ -1541,8 +1541,8 @@ PyInit_timezone(PyObject *m) {
15411541
PyModule_AddIntConstant(m, "altzone", timezone-3600);
15421542
#endif
15431543
PyModule_AddIntConstant(m, "daylight", daylight);
1544-
otz0 = PyUnicode_DecodeLocale(tzname[0], "surrogateescape");
1545-
otz1 = PyUnicode_DecodeLocale(tzname[1], "surrogateescape");
1544+
otz0 = _PyUnicode_DecodeCurrentLocale(tzname[0], "surrogateescape");
1545+
otz1 = _PyUnicode_DecodeCurrentLocale(tzname[1], "surrogateescape");
15461546
PyModule_AddObject(m, "tzname", Py_BuildValue("(NN)", otz0, otz1));
15471547
#else /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
15481548
{

Objects/unicodeobject.c

+6
Original file line numberDiff line numberDiff line change
@@ -3836,6 +3836,12 @@ _PyUnicode_DecodeCurrentLocaleAndSize(const char *str, Py_ssize_t len,
38363836
return unicode_decode_locale(str, len, errors, 1);
38373837
}
38383838

3839+
PyObject*
3840+
_PyUnicode_DecodeCurrentLocale(const char *str, const char *errors)
3841+
{
3842+
return unicode_decode_locale(str, (Py_ssize_t)strlen(str), errors, 1);
3843+
}
3844+
38393845
PyObject*
38403846
PyUnicode_DecodeLocale(const char *str, const char *errors)
38413847
{

0 commit comments

Comments
 (0)

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant