[PATCH stable] encoding: ensure locale is reset to its default state on darwin

Brodie Rao brodie at bitheap.org
Fri Aug 13 12:58:47 CDT 2010


# HG changeset patch
# User Brodie Rao <brodie at bitheap.org>
# Date 1281721763 14400
# Branch stable
# Node ID 4e122809905b38c8a784fc8f069e70af0c09ab68
# Parent  00f8e78376685c4f2f22ee1a07e757618b30f94c
encoding: ensure locale is reset to its default state on darwin

This works around an issue in Python 2.6.5 and older where isspace()
would incorrectly report True for 0x85 and 0xa0 after the locale is
changed from C.

diff -r 00f8e7837668 -r 4e122809905b mercurial/encoding.py
--- a/mercurial/encoding.py	Fri Aug 13 18:02:02 2010 +0200
+++ b/mercurial/encoding.py	Fri Aug 13 13:49:23 2010 -0400
@@ -16,10 +16,17 @@ try:
         # On darwin, getpreferredencoding ignores the locale environment and
         # always returns mac-roman. We override this if the environment is
         # not C (has been customized by the user).
-        lc = locale.setlocale(locale.LC_CTYPE, '')
-        if lc == 'UTF-8':
-            locale.setlocale(locale.LC_CTYPE, 'en_US.UTF-8')
-        encoding = locale.getlocale()[1]
+        oldlc = locale.setlocale(locale.LC_CTYPE)
+        try:
+            lc = locale.setlocale(locale.LC_CTYPE, '')
+            if lc == 'UTF-8':
+                locale.setlocale(locale.LC_CTYPE, 'en_US.UTF-8')
+            encoding = locale.getlocale()[1]
+        finally:
+            # Ensure locale is reset to its default state. Otherwise, we
+            # could run into Python issue7072 where isspace() will
+            # incorrectly report True for 0x85 and 0xa0.
+            locale.setlocale(locale.LC_CTYPE, oldlc)
     if not encoding:
         encoding = locale.getpreferredencoding() or 'ascii'
         encoding = _encodingfixup.get(encoding, encoding)


More information about the Mercurial-devel mailing list