← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~cjwatson/launchpad:py3-linkify-substitution-with-target into launchpad:master

 

Colin Watson has proposed merging ~cjwatson/launchpad:py3-linkify-substitution-with-target into launchpad:master.

Commit message:
Fix FormattersAPI._linkify_substitution_with_target for Python 3

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/399070

`lxml.html.tostring` returns bytes (at least without `encoding='unicode'`, which has other effects), but we need text here.
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:py3-linkify-substitution-with-target into launchpad:master.
diff --git a/lib/lp/app/browser/stringformatter.py b/lib/lp/app/browser/stringformatter.py
index b816ae7..8fa5a2b 100644
--- a/lib/lp/app/browser/stringformatter.py
+++ b/lib/lp/app/browser/stringformatter.py
@@ -25,6 +25,7 @@ import sys
 from breezy.patches import hunk_from_header
 from lxml import html
 import markdown
+import six
 from six.moves import zip_longest as izip_longest
 from zope.component import getUtility
 from zope.error.interfaces import IErrorReportingUtility
@@ -490,7 +491,11 @@ class FormattersAPI:
         element_tree = html.fromstring(linkified_text)
         for link in element_tree.xpath('//a'):
             link.set('target', '_new')
-        return html.tostring(element_tree)
+        # html.tostring returns bytes; we want text.  (Passing
+        # encoding='unicode' would cause it to return text, but that would
+        # also disable "&#...;" character encoding of non-ASCII characters,
+        # which we probably want to keep.)
+        return six.ensure_text(html.tostring(element_tree))
 
     # match whitespace at the beginning of a line
     _re_leadingspace = re.compile(r'^(\s+)')