summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2024-10-21 18:54:12 (GMT)
committerGitHub <noreply@github.com>2024-10-21 18:54:12 (GMT)
commitdcc4fb2c9068f60353f0c0978948b7681f7745e6 (patch)
treeab6fb1486cbab8ba8ba304d75084572f215540bf /Modules
parent5ca4e34bc1aab8321911aac6d5b2b9e75ff764d8 (diff)
downloadcpython-dcc4fb2c9068f60353f0c0978948b7681f7745e6.zip
cpython-dcc4fb2c9068f60353f0c0978948b7681f7745e6.tar.gz
cpython-dcc4fb2c9068f60353f0c0978948b7681f7745e6.tar.bz2
gh-124969: Make locale.nl_langinfo(locale.ALT_DIGITS) returning a string again (GH-125774)
This is a follow up of GH-124974. Only Glibc needed a fix. Now the returned value is a string consisting of semicolon-separated symbols on all Posix platforms.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_localemodule.c38
1 files changed, 23 insertions, 15 deletions
diff --git a/Modules/_localemodule.c b/Modules/_localemodule.c
index 0daec64..2a789ea 100644
--- a/Modules/_localemodule.c
+++ b/Modules/_localemodule.c
@@ -667,29 +667,37 @@ _locale_nl_langinfo_impl(PyObject *module, int item)
return NULL;
}
PyObject *pyresult;
+#ifdef __GLIBC__
#ifdef ALT_DIGITS
- if (item == ALT_DIGITS) {
- /* The result is a sequence of up to 100 NUL-separated strings. */
- const char *s = result;
+ if (item == ALT_DIGITS && *result) {
+ /* According to the POSIX specification the result must be
+ * a sequence of up to 100 semicolon-separated strings.
+ * But in Glibc they are NUL-separated. */
+ Py_ssize_t i = 0;
int count = 0;
- for (; count < 100 && *s; count++) {
- s += strlen(s) + 1;
+ for (; count < 100 && result[i]; count++) {
+ i += strlen(result + i) + 1;
}
- pyresult = PyTuple_New(count);
- if (pyresult != NULL) {
- for (int i = 0; i < count; i++) {
- PyObject *unicode = PyUnicode_DecodeLocale(result, NULL);
- if (unicode == NULL) {
- Py_CLEAR(pyresult);
- break;
- }
- PyTuple_SET_ITEM(pyresult, i, unicode);
- result += strlen(result) + 1;
+ char *buf = PyMem_Malloc(i);
+ if (buf == NULL) {
+ PyErr_NoMemory();
+ pyresult = NULL;
+ }
+ else {
+ memcpy(buf, result, i);
+ /* Replace all NULs with semicolons. */
+ i = 0;
+ while (--count) {
+ i += strlen(buf + i);
+ buf[i++] = ';';
}
+ pyresult = PyUnicode_DecodeLocale(buf, NULL);
+ PyMem_Free(buf);
}
}
else
#endif
+#endif
{
pyresult = PyUnicode_DecodeLocale(result, NULL);
}