Rev 6469: (gz) Store the path to .bzr.log as unicode so it can be safely printed by in file:///srv/pqm.bazaar-vcs.org/archives/thelove/bzr/2.5/

Patch Queue Manager pqm at pqm.ubuntu.com
Wed Feb 1 17:41:56 UTC 2012


At file:///srv/pqm.bazaar-vcs.org/archives/thelove/bzr/2.5/

------------------------------------------------------------
revno: 6469 [merge]
revision-id: pqm at pqm.ubuntu.com-20120201174155-pjv6onozsfu709jg
parent: pqm at pqm.ubuntu.com-20120131182557-ywfu6m6vm89u525v
parent: martin.packman at canonical.com-20120201171204-ssanikmj5av0fghw
committer: Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: 2.5
timestamp: Wed 2012-02-01 17:41:55 +0000
message:
  (gz) Store the path to .bzr.log as unicode so it can be safely printed by
   `bzr version` (Martin Packman)
modified:
  bzrlib/osutils.py              osutils.py-20050309040759-eeaff12fbf77ac86
  bzrlib/tests/blackbox/test_version.py test_version.py-20070312060045-ol7th9z035r3im3d-1
  bzrlib/tests/test_osutils.py   test_osutils.py-20051201224856-e48ee24c12182989
  bzrlib/trace.py                trace.py-20050309040759-c8ed824bdcd4748a
  doc/en/release-notes/bzr-2.5.txt bzr2.5.txt-20110708125756-587p0hpw7oke4h05-1
=== modified file 'bzrlib/osutils.py'
--- a/bzrlib/osutils.py	2012-01-05 10:44:12 +0000
+++ b/bzrlib/osutils.py	2012-02-01 12:31:53 +0000
@@ -342,6 +342,15 @@
         raise errors.BadFilenameEncoding(val, _fs_enc)
 
 
+def _posix_get_home_dir():
+    """Get the home directory of the current user as a unicode path"""
+    path = posixpath.expanduser("~")
+    try:
+        return path.decode(_fs_enc)
+    except UnicodeDecodeError:
+        raise errors.BadFilenameEncoding(path, _fs_enc)
+
+
 def _posix_getuser_unicode():
     """Get username from environment or password database as unicode"""
     name = getpass.getuser()
@@ -448,6 +457,7 @@
 pathjoin = os.path.join
 normpath = _posix_normpath
 path_from_environ = _posix_path_from_environ
+_get_home_dir = _posix_get_home_dir
 getuser_unicode = _posix_getuser_unicode
 getcwd = os.getcwdu
 rename = os.rename
@@ -511,6 +521,7 @@
     if f is not None:
         get_unicode_argv = f
     path_from_environ = win32utils.get_environ_unicode
+    _get_home_dir = win32utils.get_home_location
     getuser_unicode = win32utils.get_user_name
 
 elif sys.platform == 'darwin':

=== modified file 'bzrlib/tests/blackbox/test_version.py'
--- a/bzrlib/tests/blackbox/test_version.py	2011-05-13 12:51:05 +0000
+++ b/bzrlib/tests/blackbox/test_version.py	2012-02-01 13:23:26 +0000
@@ -129,3 +129,20 @@
         self.assertTrue(len(out) > 0)
         self.assertContainsRe(out, r"(?m)^  Bazaar log file: " + bzr_log)
         self.assertPathDoesNotExist(default_log)
+
+    def test_unicode_bzr_log(self):
+        uni_val = u"\xa7"
+        enc = osutils.get_user_encoding()
+        try:
+            str_val = uni_val.encode(enc)
+        except UnicodeEncodeError:
+            self.skip("Test string %r unrepresentable in user encoding %s" % (
+                uni_val, enc))
+        self.overrideEnv('BZR_HOME', self.test_base_dir)
+        self.overrideEnv("BZR_LOG",
+            os.path.join(self.test_base_dir, uni_val).encode(enc))
+        out, err = self.run_bzr_subprocess("version")
+        uni_out = out.decode(enc)
+        self.assertContainsRe(uni_out, u"(?m)^  Bazaar log file: .*/\xa7$")
+
+

=== modified file 'bzrlib/tests/test_osutils.py'
--- a/bzrlib/tests/test_osutils.py	2012-01-04 11:35:56 +0000
+++ b/bzrlib/tests/test_osutils.py	2012-02-01 12:32:26 +0000
@@ -2095,6 +2095,63 @@
         self.assertEquals(self.gid, s.st_gid)
 
 
+class TestPathFromEnviron(tests.TestCase):
+
+    def test_is_unicode(self):
+        self.overrideEnv('BZR_TEST_PATH', './anywhere at all/')
+        path = osutils.path_from_environ('BZR_TEST_PATH')
+        self.assertIsInstance(path, unicode)
+        self.assertEqual(u'./anywhere at all/', path)
+
+    def test_posix_path_env_ascii(self):
+        self.overrideEnv('BZR_TEST_PATH', '/tmp')
+        home = osutils._posix_path_from_environ('BZR_TEST_PATH')
+        self.assertIsInstance(home, unicode)
+        self.assertEqual(u'/tmp', home)
+
+    def test_posix_path_env_unicode(self):
+        self.requireFeature(features.ByteStringNamedFilesystem)
+        self.overrideEnv('BZR_TEST_PATH', '/home/\xa7test')
+        self.overrideAttr(osutils, "_fs_enc", "iso8859-1")
+        self.assertEqual(u'/home/\xa7test',
+            osutils._posix_path_from_environ('BZR_TEST_PATH'))
+        osutils._fs_enc = "iso8859-5"
+        self.assertEqual(u'/home/\u0407test',
+            osutils._posix_path_from_environ('BZR_TEST_PATH'))
+        osutils._fs_enc = "utf-8"
+        self.assertRaises(errors.BadFilenameEncoding,
+            osutils._posix_path_from_environ, 'BZR_TEST_PATH')
+
+
+class TestGetHomeDir(tests.TestCase):
+
+    def test_is_unicode(self):
+        home = osutils._get_home_dir()
+        self.assertIsInstance(home, unicode)
+
+    def test_posix_homeless(self):
+        self.overrideEnv('HOME', None)
+        home = osutils._get_home_dir()
+        self.assertIsInstance(home, unicode)
+
+    def test_posix_home_ascii(self):
+        self.overrideEnv('HOME', '/home/test')
+        home = osutils._posix_get_home_dir()
+        self.assertIsInstance(home, unicode)
+        self.assertEqual(u'/home/test', home)
+
+    def test_posix_home_unicode(self):
+        self.requireFeature(features.ByteStringNamedFilesystem)
+        self.overrideEnv('HOME', '/home/\xa7test')
+        self.overrideAttr(osutils, "_fs_enc", "iso8859-1")
+        self.assertEqual(u'/home/\xa7test', osutils._posix_get_home_dir())
+        osutils._fs_enc = "iso8859-5"
+        self.assertEqual(u'/home/\u0407test', osutils._posix_get_home_dir())
+        osutils._fs_enc = "utf-8"
+        self.assertRaises(errors.BadFilenameEncoding,
+            osutils._posix_get_home_dir)
+
+
 class TestGetuserUnicode(tests.TestCase):
 
     def test_is_unicode(self):

=== modified file 'bzrlib/trace.py'
--- a/bzrlib/trace.py	2011-12-19 13:23:58 +0000
+++ b/bzrlib/trace.py	2012-02-01 13:24:42 +0000
@@ -194,16 +194,15 @@
 
 
 def _get_bzr_log_filename():
-    bzr_log = os.environ.get('BZR_LOG')
+    bzr_log = osutils.path_from_environ('BZR_LOG')
     if bzr_log:
         return bzr_log
-    home = os.environ.get('BZR_HOME')
+    home = osutils.path_from_environ('BZR_HOME')
     if home is None:
-        if sys.platform == 'win32':
-            from bzrlib import win32utils
-            home = win32utils.get_home_location()
-        else:
-            home = os.path.expanduser('~')
+        # GZ 2012-02-01: Logging to the home dir is bad, but XDG is unclear
+        #                over what would be better. On windows, bug 240550
+        #                suggests LOCALAPPDATA be used instead.
+        home = osutils._get_home_dir()
     return os.path.join(home, '.bzr.log')
 
 

=== modified file 'doc/en/release-notes/bzr-2.5.txt'
--- a/doc/en/release-notes/bzr-2.5.txt	2012-01-31 18:25:57 +0000
+++ b/doc/en/release-notes/bzr-2.5.txt	2012-02-01 17:41:55 +0000
@@ -66,6 +66,9 @@
 * ``bzr send`` works on treeless branches again.
   (Jelmer Vernooij, #921591)
 
+* ``bzr version`` no longer throws a UnicodeDecodeError if the .bzr.log path
+  contains non-ascii characters. (Martin Packman, #312841)
+
 * Support scripts that don't call bzrlib.initialize() but still call run_bzr().
   (Vincent Ladeuil, #917733)
 




More information about the bazaar-commits mailing list