Rev 2594: (Adeodato Simó) Merge annotate changes to make it behave in a non-ASCII world in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Mon Jul 9 06:57:37 BST 2007


At file:///home/pqm/archives/thelove/bzr/%2Btrunk/

------------------------------------------------------------
revno: 2594
revision-id: pqm at pqm.ubuntu.com-20070709055735-nechlzumkvkl2woi
parent: pqm at pqm.ubuntu.com-20070706141845-sije5bdx8pjw2fhk
parent: ian.clatworthy at internode.on.net-20070709052316-v812p7fielxa3th7
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Mon 2007-07-09 06:57:35 +0100
message:
  (Adeodato Simó) Merge annotate changes to make it behave in a non-ASCII world
modified:
  bzrlib/annotate.py             annotate.py-20050922133147-7c60541d2614f022
  bzrlib/builtins.py             builtins.py-20050830033751-fc01482b9ca23183
  bzrlib/tests/test_annotate.py  test_annotate.py-20061213215015-sttc9agsxomls7q0-1
    ------------------------------------------------------------
    revno: 2593.1.1
    merged: ian.clatworthy at internode.on.net-20070709052316-v812p7fielxa3th7
    parent: pqm at pqm.ubuntu.com-20070706141845-sije5bdx8pjw2fhk
    parent: dato at net.com.org.es-20070708135013-n0cfcwws6uulx4v6
    committer: Ian Clatworthy <ian.clatworthy at internode.on.net>
    branch nick: ianc-integration
    timestamp: Mon 2007-07-09 15:23:16 +1000
    message:
      (Adeodato Simó) Merge annotate changes to make it behave in a non-ASCII world
    ------------------------------------------------------------
    revno: 2593.2.5
    merged: dato at net.com.org.es-20070708135013-n0cfcwws6uulx4v6
    parent: dato at net.com.org.es-20070706202552-cecsuokhzhb7sqgn
    committer: Adeodato Simó <dato at net.com.org.es>
    branch nick: bzr.annotate_fixes
    timestamp: Sun 2007-07-08 15:50:13 +0200
    message:
      Fix copy&paste bug in test, catched by John.
    ------------------------------------------------------------
    revno: 2593.2.4
    merged: dato at net.com.org.es-20070706202552-cecsuokhzhb7sqgn
    parent: dato at net.com.org.es-20070706202443-1y35w9kw5g23yz8a
    committer: Adeodato Simó <dato at net.com.org.es>
    branch nick: bzr.annotate_fixes
    timestamp: Fri 2007-07-06 22:25:52 +0200
    message:
      Add comment from John to the try/except block.
    ------------------------------------------------------------
    revno: 2593.2.3
    merged: dato at net.com.org.es-20070706202443-1y35w9kw5g23yz8a
    parent: dato at net.com.org.es-20070706174552-1sqmi8ij1tus410q
    committer: Adeodato Simó <dato at net.com.org.es>
    branch nick: bzr.annotate_fixes
    timestamp: Fri 2007-07-06 22:24:43 +0200
    message:
      Cope with to_file.encoding being None or not present.
    ------------------------------------------------------------
    revno: 2593.2.2
    merged: dato at net.com.org.es-20070706174552-1sqmi8ij1tus410q
    parent: dato at net.com.org.es-20070706172157-11w04axiv4mtrabm
    committer: Adeodato Simó <dato at net.com.org.es>
    branch nick: bzr.annotate_fixes
    timestamp: Fri 2007-07-06 19:45:52 +0200
    message:
      Improve tests a bit, actually checking for the replace encoding.
    ------------------------------------------------------------
    revno: 2593.2.1
    merged: dato at net.com.org.es-20070706172157-11w04axiv4mtrabm
    parent: pqm at pqm.ubuntu.com-20070706141845-sije5bdx8pjw2fhk
    committer: Adeodato Simó <dato at net.com.org.es>
    branch nick: bzr.annotate_fixes
    timestamp: Fri 2007-07-06 19:21:57 +0200
    message:
      Improve annotate to prevent unicode exceptions in certain situations.
      
      The fixed bugs are:
      
        * when an annotation has the author part as unicode(), and line
          contents are 8-bit data, an UnicodeDecodeError is raised when
          formatting them together with '%s %s'
      
        * when the author part of an annotation contains characters not
          representable in the encoding of the user that runs annotate,
          an UnicodeEncodeError is raised
=== modified file 'bzrlib/annotate.py'
--- a/bzrlib/annotate.py	2007-04-17 21:19:29 +0000
+++ b/bzrlib/annotate.py	2007-07-06 20:25:52 +0000
@@ -30,6 +30,7 @@
 
 from bzrlib import (
     errors,
+    osutils,
     patiencediff,
     tsort,
     )
@@ -79,7 +80,19 @@
             anno = "%-*s %-7s " % (max_revno_len, revno_str, author[:7])
 
         if anno.lstrip() == "" and full: anno = prevanno
-        print >>to_file, '%s| %s' % (anno, text)
+        try:
+            to_file.write(anno)
+        except UnicodeEncodeError:
+            # cmd_annotate should be passing in an 'exact' object, which means
+            # we have a direct handle to sys.stdout or equivalent. It may not
+            # be able to handle the exact Unicode characters, but 'annotate' is
+            # a user function (non-scripting), so shouldn't die because of
+            # unrepresentable annotation characters. So encode using 'replace',
+            # and write them again.
+            encoding = getattr(to_file, 'encoding', None) or \
+                    osutils.get_terminal_encoding()
+            to_file.write(anno.encode(encoding, 'replace'))
+        print >>to_file, '| %s' % (text,)
         prevanno=anno
 
 

=== modified file 'bzrlib/builtins.py'
--- a/bzrlib/builtins.py	2007-07-04 08:08:08 +0000
+++ b/bzrlib/builtins.py	2007-07-06 17:21:57 +0000
@@ -3100,6 +3100,7 @@
                      'revision',
                      'show-ids',
                      ]
+    encoding_type = 'exact'
 
     @display_command
     def run(self, filename, all=False, long=False, revision=None,
@@ -3120,7 +3121,7 @@
                 raise errors.NotVersionedError(filename)
             tree = branch.repository.revision_tree(revision_id)
             file_version = tree.inventory[file_id].revision
-            annotate_file(branch, file_version, file_id, long, all, sys.stdout,
+            annotate_file(branch, file_version, file_id, long, all, self.outf,
                           show_ids=show_ids)
         finally:
             branch.unlock()

=== modified file 'bzrlib/tests/test_annotate.py'
--- a/bzrlib/tests/test_annotate.py	2007-01-29 21:11:28 +0000
+++ b/bzrlib/tests/test_annotate.py	2007-07-08 13:50:13 +0000
@@ -16,6 +16,7 @@
 
 """Whitebox tests for annotate functionality."""
 
+import codecs
 from cStringIO import StringIO
 
 from bzrlib import (
@@ -301,6 +302,42 @@
                              'rev-1_1_1_1_1_1_1 | sixth\n',
                              sio.getvalue())
 
+    def test_annotate_unicode_author(self):
+        tree1 = self.make_branch_and_tree('tree1')
+
+        self.build_tree_contents([('tree1/a', 'adi\xc3\xb3s')])
+        tree1.add(['a'], ['a-id'])
+        tree1.commit('a', rev_id='rev-1',
+                     committer=u'Pepe P\xe9rez <pperez at ejemplo.com>',
+                     timestamp=1166046000.00, timezone=0)
+
+        self.build_tree_contents([('tree1/b', 'bye')])
+        tree1.add(['b'], ['b-id'])
+        tree1.commit('b', rev_id='rev-2',
+                     committer=u'p\xe9rez',
+                     timestamp=1166046000.00, timezone=0)
+
+        # this passes if no exception is raised
+        to_file = StringIO()
+        annotate.annotate_file(tree1.branch, 'rev-1', 'a-id', to_file=to_file)
+
+        sio = StringIO()
+        to_file = codecs.getwriter('ascii')(sio)
+        to_file.encoding = 'ascii' # codecs does not set it
+        annotate.annotate_file(tree1.branch, 'rev-2', 'b-id', to_file=to_file)
+        self.assertEqualDiff('2   p?rez   | bye\n', sio.getvalue())
+
+        # test now with to_file.encoding = None
+        to_file = tests.StringIOWrapper()
+        to_file.encoding = None
+        annotate.annotate_file(tree1.branch, 'rev-2', 'b-id', to_file=to_file)
+        self.assertContainsRe('2   p.rez   | bye\n', to_file.getvalue())
+
+        # and when it does not exist
+        to_file = StringIO()
+        annotate.annotate_file(tree1.branch, 'rev-2', 'b-id', to_file=to_file)
+        self.assertContainsRe('2   p.rez   | bye\n', to_file.getvalue())
+
 
 class TestReannotate(tests.TestCase):
 




More information about the bazaar-commits mailing list