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

Commit150fd4f

Browse files
serhiy-storchakamiss-islington
authored andcommitted
pythongh-137273: Fix debug assertion failure in locale.setlocale() on Windows (pythonGH-137300)
It happened when there were at least 16 characters after dot in thelocale name.(cherry picked from commit718e0c8)Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent42b58cf commit150fd4f

File tree

3 files changed

+97
-21
lines changed

3 files changed

+97
-21
lines changed

‎Lib/test/test_locale.py‎

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
fromunittestimportmock
66
importunittest
77
importlocale
8+
importos
89
importsys
910
importcodecs
1011

@@ -486,6 +487,54 @@ def test_japanese(self):
486487
self.check('jp_jp','ja_JP.eucJP')
487488

488489

490+
classTestRealLocales(unittest.TestCase):
491+
defsetUp(self):
492+
oldlocale=locale.setlocale(locale.LC_CTYPE)
493+
self.addCleanup(locale.setlocale,locale.LC_CTYPE,oldlocale)
494+
495+
deftest_getsetlocale_issue1813(self):
496+
# Issue #1813: setting and getting the locale under a Turkish locale
497+
try:
498+
locale.setlocale(locale.LC_CTYPE,'tr_TR')
499+
exceptlocale.Error:
500+
# Unsupported locale on this system
501+
self.skipTest('test needs Turkish locale')
502+
loc=locale.getlocale(locale.LC_CTYPE)
503+
ifverbose:
504+
print('testing with %a'% (loc,),end=' ',flush=True)
505+
try:
506+
locale.setlocale(locale.LC_CTYPE,loc)
507+
exceptlocale.Errorasexc:
508+
# bpo-37945: setlocale(LC_CTYPE) fails with getlocale(LC_CTYPE)
509+
# and the tr_TR locale on Windows. getlocale() builds a locale
510+
# which is not recognize by setlocale().
511+
self.skipTest(f"setlocale(LC_CTYPE,{loc!r}) failed:{exc!r}")
512+
self.assertEqual(loc,locale.getlocale(locale.LC_CTYPE))
513+
514+
@unittest.skipUnless(os.name=='nt','requires Windows')
515+
deftest_setlocale_long_encoding(self):
516+
withself.assertRaises(locale.Error):
517+
locale.setlocale(locale.LC_CTYPE,'English.%016d'%1252)
518+
locale.setlocale(locale.LC_CTYPE,'English.%015d'%1252)
519+
loc=locale.setlocale(locale.LC_ALL)
520+
self.assertIn('.1252',loc)
521+
loc2=loc.replace('.1252','.%016d'%1252,1)
522+
withself.assertRaises(locale.Error):
523+
locale.setlocale(locale.LC_ALL,loc2)
524+
loc2=loc.replace('.1252','.%015d'%1252,1)
525+
locale.setlocale(locale.LC_ALL,loc2)
526+
527+
# gh-137273: Debug assertion failure on Windows for long encoding.
528+
withself.assertRaises(locale.Error):
529+
locale.setlocale(locale.LC_CTYPE,'en_US.'+'x'*16)
530+
locale.setlocale(locale.LC_CTYPE,'en_US.UTF-8')
531+
loc=locale.setlocale(locale.LC_ALL)
532+
self.assertIn('.UTF-8',loc)
533+
loc2=loc.replace('.UTF-8','.'+'x'*16,1)
534+
withself.assertRaises(locale.Error):
535+
locale.setlocale(locale.LC_ALL,loc2)
536+
537+
489538
classTestMiscellaneous(unittest.TestCase):
490539
deftest_defaults_UTF8(self):
491540
# Issue #18378: on (at least) macOS setting LC_CTYPE to "UTF-8" is
@@ -552,27 +601,6 @@ def test_setlocale_category(self):
552601
# crasher from bug #7419
553602
self.assertRaises(locale.Error,locale.setlocale,12345)
554603

555-
deftest_getsetlocale_issue1813(self):
556-
# Issue #1813: setting and getting the locale under a Turkish locale
557-
oldlocale=locale.setlocale(locale.LC_CTYPE)
558-
self.addCleanup(locale.setlocale,locale.LC_CTYPE,oldlocale)
559-
try:
560-
locale.setlocale(locale.LC_CTYPE,'tr_TR')
561-
exceptlocale.Error:
562-
# Unsupported locale on this system
563-
self.skipTest('test needs Turkish locale')
564-
loc=locale.getlocale(locale.LC_CTYPE)
565-
ifverbose:
566-
print('testing with %a'% (loc,),end=' ',flush=True)
567-
try:
568-
locale.setlocale(locale.LC_CTYPE,loc)
569-
exceptlocale.Errorasexc:
570-
# bpo-37945: setlocale(LC_CTYPE) fails with getlocale(LC_CTYPE)
571-
# and the tr_TR locale on Windows. getlocale() builds a locale
572-
# which is not recognize by setlocale().
573-
self.skipTest(f"setlocale(LC_CTYPE,{loc!r}) failed:{exc!r}")
574-
self.assertEqual(loc,locale.getlocale(locale.LC_CTYPE))
575-
576604
deftest_invalid_locale_format_in_localetuple(self):
577605
withself.assertRaises(TypeError):
578606
locale.setlocale(locale.LC_ALL,b'fi_FI')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix debug assertion failure in:func:`locale.setlocale` on Windows.

‎Modules/_localemodule.c‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,41 @@ copy_grouping(const char* s)
8787
returnresult;
8888
}
8989

90+
#if defined(MS_WINDOWS)
91+
92+
// 16 is the number of elements in the szCodePage field
93+
// of the __crt_locale_strings structure.
94+
#defineMAX_CP_LEN 15
95+
96+
staticint
97+
check_locale_name(constchar*locale,constchar*end)
98+
{
99+
size_tlen=end ? (size_t)(end-locale) :strlen(locale);
100+
constchar*dot=memchr(locale,'.',len);
101+
if (dot&&locale+len-dot-1>MAX_CP_LEN) {
102+
return-1;
103+
}
104+
return0;
105+
}
106+
107+
staticint
108+
check_locale_name_all(constchar*locale)
109+
{
110+
constchar*start=locale;
111+
while (1) {
112+
constchar*end=strchr(start,';');
113+
if (check_locale_name(start,end)<0) {
114+
return-1;
115+
}
116+
if (end==NULL) {
117+
break;
118+
}
119+
start=end+1;
120+
}
121+
return0;
122+
}
123+
#endif
124+
90125
/*[clinic input]
91126
_locale.setlocale
92127
@@ -111,6 +146,18 @@ _locale_setlocale_impl(PyObject *module, int category, const char *locale)
111146
"invalid locale category");
112147
returnNULL;
113148
}
149+
if (locale) {
150+
if ((category==LC_ALL
151+
?check_locale_name_all(locale)
152+
:check_locale_name(locale,NULL))<0)
153+
{
154+
/* Debug assertion failure on Windows.
155+
* _Py_BEGIN_SUPPRESS_IPH/_Py_END_SUPPRESS_IPH do not help. */
156+
PyErr_SetString(get_locale_state(module)->Error,
157+
"unsupported locale setting");
158+
returnNULL;
159+
}
160+
}
114161
#endif
115162

116163
if (locale) {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp