Skip to content

Commit 3031948

Browse files
Add more tests and comments.
1 parent c6dbeb8 commit 3031948

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

Lib/test/test_locale.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from unittest import mock
66
import unittest
77
import locale
8+
import os
89
import sys
910
import codecs
1011

@@ -510,16 +511,26 @@ def test_getsetlocale_issue1813(self):
510511
self.skipTest(f"setlocale(LC_CTYPE, {loc!r}) failed: {exc!r}")
511512
self.assertEqual(loc, locale.getlocale(locale.LC_CTYPE))
512513

514+
@unittest.skipUnless(os.name == 'nt', 'requires Windows')
513515
def test_setlocale_long_encoding(self):
516+
with self.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+
with self.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+
514527
# gh-137273: Debug assertion failure on Windows for long encoding.
515-
oldlocale = locale.setlocale(locale.LC_ALL)
516-
self.addCleanup(locale.setlocale, locale.LC_ALL, oldlocale)
517528
with self.assertRaises(locale.Error):
518529
locale.setlocale(locale.LC_CTYPE, 'en_US.' + 'x'*16)
519530
locale.setlocale(locale.LC_CTYPE, 'en_US.UTF-8')
520531
loc = locale.setlocale(locale.LC_ALL)
521532
self.assertIn('.UTF-8', loc)
522-
loc2 = loc.replace('UTF-8', 'x'*16, 1)
533+
loc2 = loc.replace('.UTF-8', '.' + 'x'*16, 1)
523534
with self.assertRaises(locale.Error):
524535
locale.setlocale(locale.LC_ALL, loc2)
525536

Modules/_localemodule.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,17 @@ copy_grouping(const char* s)
8888
}
8989

9090
#if defined(MS_WINDOWS)
91+
92+
// The number of elements in the szCodePage field
93+
// of the __crt_locale_strings structure.
94+
#define MAX_ENCODING_SIZE 16
95+
9196
static int
9297
check_locale_name(const char *locale, const char *end)
9398
{
9499
size_t len = end ? (size_t)(end - locale) : strlen(locale);
95100
const char *dot = memchr(locale, '.', len);
96-
if (dot && locale + len - dot > 16) {
101+
if (dot && locale + len - dot > MAX_ENCODING_SIZE) {
97102
return -1;
98103
}
99104
return 0;

0 commit comments

Comments
 (0)