Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6a448d4

Browse files
committed
Issue #7080: locale.strxfrm() raises a MemoryError on 64-bit non-Windows
platforms, and assorted locale fixes by Derk Drukker.
1 parent 86906f7 commit 6a448d4

5 files changed

Lines changed: 52 additions & 13 deletions

File tree

Lib/locale.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,14 +578,13 @@ def getpreferredencoding(do_setlocale = True):
578578
# returning nothing will crash the
579579
# interpreter.
580580
result = 'UTF-8'
581-
582581
setlocale(LC_CTYPE, oldloc)
583-
return result
584582
else:
585583
result = nl_langinfo(CODESET)
586584
if not result and sys.platform == 'darwin':
587585
# See above for explanation
588586
result = 'UTF-8'
587+
return result
589588

590589

591590
### Database

Lib/test/test_locale.py

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88

99
def get_enUS_locale():
1010
global enUS_locale
11-
if sys.platform == 'darwin':
12-
raise unittest.SkipTest("Locale support on MacOSX is minimal")
1311
if sys.platform.startswith("win"):
1412
tlocs = ("En", "English")
1513
else:
16-
tlocs = ("en_US.UTF-8", "en_US.US-ASCII", "en_US")
14+
tlocs = ("en_US.UTF-8", "en_US.ISO8859-1", "en_US.US-ASCII", "en_US")
1715
oldlocale = locale.setlocale(locale.LC_NUMERIC)
1816
for tloc in tlocs:
1917
try:
@@ -309,6 +307,39 @@ def test_currency(self):
309307
grouping=True, international=True)
310308

311309

310+
class TestCollation(unittest.TestCase):
311+
# Test string collation functions
312+
313+
def test_strcoll(self):
314+
self.assertLess(locale.strcoll('a', 'b'), 0)
315+
self.assertEqual(locale.strcoll('a', 'a'), 0)
316+
self.assertGreater(locale.strcoll('b', 'a'), 0)
317+
318+
def test_strxfrm(self):
319+
self.assertLess(locale.strxfrm('a'), locale.strxfrm('b'))
320+
321+
322+
class TestEnUSCollation(BaseLocalizedTest, TestCollation):
323+
# Test string collation functions with a real English locale
324+
325+
locale_type = locale.LC_ALL
326+
327+
def setUp(self):
328+
BaseLocalizedTest.setUp(self)
329+
enc = codecs.lookup(locale.getpreferredencoding(False) or 'ascii').name
330+
if enc not in ('utf-8', 'iso8859-1', 'cp1252'):
331+
raise unittest.SkipTest('encoding not suitable')
332+
if enc != 'iso8859-1' and (sys.platform == 'darwin' or
333+
sys.platform.startswith('freebsd')):
334+
raise unittest.SkipTest('wcscoll/wcsxfrm have known bugs')
335+
336+
def test_strcoll_with_diacritic(self):
337+
self.assertLess(locale.strcoll('à', 'b'), 0)
338+
339+
def test_strxfrm_with_diacritic(self):
340+
self.assertLess(locale.strxfrm('à'), locale.strxfrm('b'))
341+
342+
312343
class TestMiscellaneous(unittest.TestCase):
313344
def test_getpreferredencoding(self):
314345
# Invoke getpreferredencoding to make sure it does not cause exceptions.
@@ -317,11 +348,10 @@ def test_getpreferredencoding(self):
317348
# If encoding non-empty, make sure it is valid
318349
codecs.lookup(enc)
319350

320-
if hasattr(locale, "strcoll"):
321-
def test_strcoll_3303(self):
322-
# test crasher from bug #3303
323-
self.assertRaises(TypeError, locale.strcoll, "a", None)
324-
self.assertRaises(TypeError, locale.strcoll, b"a", None)
351+
def test_strcoll_3303(self):
352+
# test crasher from bug #3303
353+
self.assertRaises(TypeError, locale.strcoll, "a", None)
354+
self.assertRaises(TypeError, locale.strcoll, b"a", None)
325355

326356

327357
def test_main():
@@ -331,6 +361,7 @@ def test_main():
331361
TestEnUSNumberFormatting,
332362
TestCNumberFormatting,
333363
TestFrFRNumberFormatting,
364+
TestCollation
334365
]
335366
# SkipTest can't be raised inside unittests, handle it manually instead
336367
try:
@@ -339,7 +370,7 @@ def test_main():
339370
if verbose:
340371
print("Some tests will be disabled: %s" % e)
341372
else:
342-
tests += [TestNumberFormatting]
373+
tests += [TestNumberFormatting, TestEnUSCollation]
343374
run_unittest(*tests)
344375

345376
if __name__ == '__main__':

Misc/NEWS

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ C-API
100100
Library
101101
-------
102102

103+
- Issue #7080: locale.strxfrm() raises a MemoryError on 64-bit non-Windows
104+
platforms, and assorted locale fixes by Derk Drukker.
105+
103106
- Issue #5833: Fix extra space character in readline completion with the
104107
GNU readline library version 6.0.
105108

@@ -118,7 +121,7 @@ Library
118121
is too large to fit in the current precision.
119122

120123
- Issue #6236, #6348: Fix various failures in the I/O library under AIX
121-
and other platforms, when using a non-gcc compiler. Patch by egreen.
124+
and other platforms, when using a non-gcc compiler. Patch by Derk Drukker.
122125

123126
- Issue #4606: Passing 'None' if ctypes argtype is set to POINTER(...)
124127
does now always result in NULL.

Modules/_localemodule.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This software comes with no warranty. Use at your own risk.
99
1010
******************************************************************/
1111

12+
#define PY_SSIZE_T_CLEAN
1213
#include "Python.h"
1314

1415
#include <stdio.h>
@@ -315,7 +316,7 @@ PyLocale_strxfrm(PyObject* self, PyObject* args)
315316
result = PyUnicode_FromWideChar(buf, n2);
316317
exit:
317318
if (buf) PyMem_Free(buf);
318-
#ifdef HAVE_USABLE_WCHAR_T
319+
#ifndef HAVE_USABLE_WCHAR_T
319320
PyMem_Free(s);
320321
#endif
321322
return result;

PC/pyconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,11 @@ Py_NO_ENABLE_SHARED to find out. Also support MS_NO_COREDLL for b/w compat */
641641
#define HAVE_WCSCOLL 1
642642
#endif
643643

644+
/* Define to 1 if you have the `wcsxfrm' function. */
645+
#ifndef MS_WINCE
646+
#define HAVE_WCSXFRM 1
647+
#endif
648+
644649
/* Define if you have the <dlfcn.h> header file. */
645650
/* #undef HAVE_DLFCN_H */
646651

0 commit comments

Comments
 (0)