7140680 "pkg history" does not work in some localized locales
authorabhinandan.ekande@oracle.com
Thu, 17 May 2012 14:04:02 +0530
changeset 2675 49931dc5f9b6
parent 2674 d342fbfbc2fd
child 2676 4ea2dbd3337e
7140680 "pkg history" does not work in some localized locales
src/client.py
src/tests/cli/t_pkg_history.py
src/tests/pkg5unittest.py
--- a/src/client.py	Thu May 17 10:53:56 2012 -0700
+++ b/src/client.py	Thu May 17 14:04:02 2012 +0530
@@ -5652,9 +5652,17 @@
                 if not output["new_be_uuid"]:
                         output["new_be_uuid"] = _("(None)")
 
+                enc = locale.getlocale(locale.LC_CTYPE)[1]
+                if not enc:
+                        enc = locale.getpreferredencoding()
+
                 if long_format:
                         data = __get_long_history_data(he, output)
                         for field, value in data:
+                                if isinstance(field, unicode):
+                                        field = field.encode(enc)
+                                if isinstance(value, unicode):
+                                        value = value.encode(enc)
                                 msg("%18s: %s" % (field, value))
 
                         # Separate log entries with a blank line.
@@ -5662,7 +5670,10 @@
                 else:
                         items = []
                         for col in columns:
-                                items.append(output[col])
+                                item = output[col]
+                                if isinstance(item, unicode):
+                                        item = item.encode(enc)
+                                items.append(item)
                         msg(history_fmt % tuple(items))
         return EXIT_OK
 
--- a/src/tests/cli/t_pkg_history.py	Thu May 17 10:53:56 2012 -0700
+++ b/src/tests/cli/t_pkg_history.py	Thu May 17 14:04:02 2012 +0530
@@ -34,6 +34,7 @@
 import random
 import re
 import shutil
+import subprocess
 import time
 import unittest
 import xml.etree.ElementTree
@@ -567,5 +568,22 @@
                 self.pkg("history -n 1 -o time")
                 self.assert_("369576:0:0" in self.output)
 
+        def test_14_history_unicode_locale(self):
+                """Verify we can get history when unicode locale is set"""
+
+                # If pkg history run when below locales set, it fails.
+                unicode_locales = ["fr_FR.UTF-8", "zh_TW.UTF-8", "zh_CN.UTF-8",
+                    "ko_KR.UTF-8", "ja_JP.UTF-8"]
+                p = subprocess.Popen(["/usr/bin/locale", "-a"],
+                    stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+                lines = p.stdout.readlines()
+                locale_list = [i.rstrip() for i in lines]
+                unicode_list = list(set(locale_list) & set(unicode_locales))
+                self.assert_(unicode_list, "You must have one of the "
+                    " following locales installed for this test to succeed: "
+                    + ", ".join(unicode_locales))
+                env = { "LC_ALL": unicode_list[0] }
+                self.pkg("history", env_arg=env)
+
 if __name__ == "__main__":
         unittest.main()
--- a/src/tests/pkg5unittest.py	Thu May 17 10:53:56 2012 -0700
+++ b/src/tests/pkg5unittest.py	Thu May 17 14:04:02 2012 +0530
@@ -2211,7 +2211,7 @@
 
         def pkg(self, command, exit=0, comment="", prefix="", su_wrap=None,
             out=False, stderr=False, cmd_path=None, use_img_root=True,
-            debug_smf=True):
+            debug_smf=True, env_arg=None):
                 if debug_smf and "smf_cmds_dir" not in command:
                         command = "--debug smf_cmds_dir=%s %s" % \
                             (DebugValues["smf_cmds_dir"], command)
@@ -2222,7 +2222,8 @@
                         cmd_path = self.pkg_cmdpath
                 cmdline = "%s %s" % (cmd_path, command)
                 return self.cmdline_run(cmdline, exit=exit, comment=comment,
-                    prefix=prefix, su_wrap=su_wrap, out=out, stderr=stderr)
+                    prefix=prefix, su_wrap=su_wrap, out=out, stderr=stderr,
+                    env_arg=env_arg)
 
         def pkgdepend_resolve(self, args, exit=0, comment="", su_wrap=False):
                 ops = ""