Aurelien Larcher
2021-01-08 b5a8497ec9558d0061024ac4b49f445bbe7d1094
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
This patch fixes limitation in python which expects strxfrm function to return
string with characters with values lower than 10ffff. This is know issue:
 
https://bugs.python.org/issue16258
 
This is not for upstream as the idea is from the bug itself and was rejected
for use on all platforms.
 
--- Python-3.9.1/Modules/_localemodule.c
+++ Python-3.9.1/Modules/_localemodule.c
@@ -363,9 +363,10 @@ Return a string that can be used as a ke
 static PyObject*
 PyLocale_strxfrm(PyObject* self, PyObject* args)
 {
+    unsigned int i, j;
     PyObject *str;
     Py_ssize_t n1;
-    wchar_t *s = NULL, *buf = NULL;
+    wchar_t *s = NULL, *buf = NULL, *solbuf = NULL;
     size_t n2;
     PyObject *result = NULL;
 
@@ -409,8 +410,23 @@ PyLocale_strxfrm(PyObject* self, PyObjec
             goto exit;
         }
     }
-    result = PyUnicode_FromWideChar(buf, n2);
+
+    /* Split each character in resulting wide string in two
+       parts in order to prevent Python ValueErrors on Solaris. */
+    solbuf = PyMem_New(wchar_t, (n2*2) + 1);
+    if (!solbuf) {
+        PyErr_NoMemory();
+        goto exit;
+    }
+    for (i = 0, j = 0; i < n2; i ++, j+= 2) {
+        solbuf[j] = 0x10000 + (buf[i] >> 16);
+        solbuf[j+1] = buf[i] & 0xffff;
+    }
+    solbuf[j] = 0;
+
+    result = PyUnicode_FromWideChar(solbuf, n2*2);
 exit:
+    PyMem_Free(solbuf);
     PyMem_Free(buf);
     PyMem_Free(s);
     return result;