Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitea8283a

Browse files
Fix locale.strxfrm()
It should interpret the result of wcsxfrm() as a sequence of abstractintegers, not a sequence of Unicode code points or using other encodingscheme that does not preserve ordering.
1 parentc779f23 commitea8283a

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

‎Modules/_localemodule.c‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,54 @@ _locale_strxfrm_impl(PyObject *module, PyObject *str)
483483
gotoexit;
484484
}
485485
}
486-
result=PyUnicode_FromWideChar(buf,n2);
486+
/* The result is just a sequence of integers, they are not necessary
487+
Unicode code points, so PyUnicode_FromWideChar() cannot be used
488+
here. For example, 0xD83D 0xDC0D should not be larger than 0xFF41.
489+
*/
490+
#ifSIZEOF_WCHAR_T==4
491+
{
492+
/* Some codes can exceed the range of Unicode code points
493+
(0 - 0x10FFFF), so they cannot be directly used in
494+
PyUnicode_FromKindAndData(). They should be first encoded in
495+
a way that preserves the lexicographical order.
496+
497+
Codes in the range 0-0xFFFF represent themself.
498+
Codes larger than 0xFFFF are encoded as a pair:
499+
* 0x1xxxx -- the highest 16 bits
500+
* 0x0xxxx -- the lowest 16 bits
501+
*/
502+
size_tn3=0;
503+
for (size_ti=0;i<n2;i++) {
504+
if (buf[i]>0x10000) {
505+
n3++;
506+
}
507+
}
508+
if (n3) {
509+
n3+=n2;// no integer overflow
510+
Py_UCS4*buf2=PyMem_New(Py_UCS4,n3);
511+
if (buf2==NULL) {
512+
PyErr_NoMemory();
513+
gotoexit;
514+
}
515+
size_tj=0;
516+
for (size_ti=0,j=0;i<n2;i++) {
517+
wchar_tc=buf[i];
518+
if (c>0x10000) {
519+
buf2[j++]= (((Py_UCS4)c) >>16) |0x10000u;
520+
buf2[j++]= ((Py_UCS4)c)&0xffffu;
521+
}
522+
else {
523+
buf2[j++]=c;
524+
}
525+
}
526+
assert(j==n3);
527+
result=PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND,buf2,n3);
528+
PyMem_Free(buf2);
529+
gotoexit;
530+
}
531+
}
532+
#endif
533+
result=PyUnicode_FromKindAndData(sizeof(wchar_t),buf,n2);
487534
exit:
488535
PyMem_Free(buf);
489536
PyMem_Free(s);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp