Rev 6344: (gz) Add get_message_encoding() for reading encoding name from LC_MESSAGE in file:///srv/pqm.bazaar-vcs.org/archives/thelove/bzr/%2Btrunk/

Patch Queue Manager pqm at pqm.ubuntu.com
Mon Dec 5 12:23:58 UTC 2011


At file:///srv/pqm.bazaar-vcs.org/archives/thelove/bzr/%2Btrunk/

------------------------------------------------------------
revno: 6344 [merge]
revision-id: pqm at pqm.ubuntu.com-20111205122358-4vml1qhofl1ll84q
parent: pqm at pqm.ubuntu.com-20111205115857-eakx9wlixnx2wpwz
parent: martin.packman at canonical.com-20111205112659-3yemrq3o7fxg7y7c
committer: Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Mon 2011-12-05 12:23:58 +0000
message:
  (gz) Add get_message_encoding() for reading encoding name from LC_MESSAGE
   setting (Martin Packman)
modified:
  bzrlib/osutils.py              osutils.py-20050309040759-eeaff12fbf77ac86
  bzrlib/tests/test_osutils_encodings.py test_osutils_encodin-20061226013130-kkp732tpt3lm91vv-1
=== modified file 'bzrlib/osutils.py'
--- a/bzrlib/osutils.py	2011-12-02 12:49:48 +0000
+++ b/bzrlib/osutils.py	2011-12-05 12:23:58 +0000
@@ -2007,6 +2007,28 @@
     return get_terminal_encoding()
 
 
+_message_encoding = None
+
+
+def get_message_encoding():
+    """Return the encoding used for messages
+
+    While the message encoding is a general setting it should usually only be
+    needed for decoding system error strings such as from OSError instances.
+    """
+    global _message_encoding
+    if _message_encoding is None:
+        if os.name == "posix":
+            import locale
+            # This is a process-global setting that can change, but should in
+            # general just get set once at process startup then be constant.
+            _message_encoding = locale.getlocale(locale.LC_MESSAGES)[1]
+        else:
+            # On windows want the result of GetACP() which this boils down to.
+            _message_encoding = get_user_encoding()
+    return _message_encoding or "ascii"
+        
+
 def get_host_name():
     """Return the current unicode host name.
 

=== modified file 'bzrlib/tests/test_osutils_encodings.py'
--- a/bzrlib/tests/test_osutils_encodings.py	2011-01-12 01:01:53 +0000
+++ b/bzrlib/tests/test_osutils_encodings.py	2011-12-02 13:36:43 +0000
@@ -17,7 +17,9 @@
 """Tests for the osutils wrapper."""
 
 import codecs
+import errno
 import locale
+import os
 import sys
 
 from bzrlib import (
@@ -221,3 +223,17 @@
                           '  doesn\'t support the locale set by $LANG (BOGUS)\n'
                           '  Continuing with ascii encoding.\n',
                           sys.stderr.getvalue())
+
+
+class TestMessageEncoding(TestCase):
+    """Tests for getting the encoding used by system messages"""
+
+    def test_get_message_encoding(self):
+        encoding_name = osutils.get_message_encoding()
+        "".decode(encoding_name) # should be a valid encoding name
+
+    def test_get_message_encoding_decodes_strerror(self):
+        encoding_name = osutils.get_message_encoding()
+        for number, name in errno.errorcode.iteritems():
+            string = os.strerror(number)
+            string.decode(encoding_name)




More information about the bazaar-commits mailing list