util: do not corrupt multi-byte characters in wrap
authorMartin Geisler <mg@lazybytes.net>
Sun, 27 Sep 2009 01:44:46 +0200
changeset 9480 44758742ad2e
parent 9479 f3569d95c2ab
child 9482 ca3390c19f88
child 9495 b2d65ee49a72
util: do not corrupt multi-byte characters in wrap
mercurial/util.py
--- a/mercurial/util.py	Wed Sep 23 18:56:09 2009 +0200
+++ b/mercurial/util.py	Sun Sep 27 01:44:46 2009 +0200
@@ -14,7 +14,7 @@
 """
 
 from i18n import _
-import error, osutil
+import error, osutil, encoding
 import cStringIO, errno, re, shutil, sys, tempfile, traceback
 import os, stat, time, calendar, random, textwrap
 import imp
@@ -1276,7 +1276,11 @@
 
 def wrap(line, hangindent, width=78):
     padding = '\n' + ' ' * hangindent
-    return padding.join(textwrap.wrap(line, width=width - hangindent))
+    # To avoid corrupting multi-byte characters in line, we must wrap
+    # a Unicode string instead of a bytestring.
+    u = line.decode(encoding.encoding)
+    w = padding.join(textwrap.wrap(u, width=width - hangindent))
+    return w.encode(encoding.encoding)
 
 def iterlines(iterator):
     for chunk in iterator: