← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~cjwatson/launchpad:py3-testing-keyserver-web into launchpad:master

 

Colin Watson has proposed merging ~cjwatson/launchpad:py3-testing-keyserver-web into launchpad:master.

Commit message:
Fix lp.testing.keyserver.web and tests for Python 3

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/395948
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:py3-testing-keyserver-web into launchpad:master.
diff --git a/lib/lp/services/compat.py b/lib/lp/services/compat.py
index b985631..887651c 100644
--- a/lib/lp/services/compat.py
+++ b/lib/lp/services/compat.py
@@ -10,6 +10,7 @@ from __future__ import absolute_import, print_function, unicode_literals
 
 __metaclass__ = type
 __all__ = [
+    'escape',
     'lzma',
     'message_as_bytes',
     'message_from_bytes',
@@ -27,6 +28,11 @@ try:
 except ImportError:
     from email import message_from_string as message_from_bytes
 
+try:
+    from html import escape
+except ImportError:
+    from cgi import escape
+
 import io
 
 try:
diff --git a/lib/lp/testing/keyserver/tests/test_web.py b/lib/lp/testing/keyserver/tests/test_web.py
index 188c320..e11f86c 100644
--- a/lib/lp/testing/keyserver/tests/test_web.py
+++ b/lib/lp/testing/keyserver/tests/test_web.py
@@ -54,7 +54,7 @@ class TestWebResources(TestCase):
             path.lstrip('/'))
         client = self.useFixture(TReqFixture(reactor)).client
         return client.get(url).addCallback(check_status).addCallback(
-            treq.content)
+            treq.text_content)
 
     def getURL(self, path):
         """Start a test key server and get the content at 'path'."""
@@ -99,11 +99,11 @@ class TestWebResources(TestCase):
                 self.fail('Response was not an HTTP error response.')
             if not isinstance(failure, Failure):
                 raise failure
-            self.assertEqual('404', failure.value.status)
+            self.assertEqual(b'404', failure.value.status)
             self.assertEqual(
-                '<html><head><title>Error handling request</title></head>\n'
-                '<body><h1>Error handling request</h1>'
-                'No results found: No keys found</body></html>',
+                b'<html><head><title>Error handling request</title></head>\n'
+                b'<body><h1>Error handling request</h1>'
+                b'No results found: No keys found</body></html>',
                 failure.value.response)
 
         d.addCallback(regular_execution_callback)
diff --git a/lib/lp/testing/keyserver/web.py b/lib/lp/testing/keyserver/web.py
index 86db0e5..98f204d 100644
--- a/lib/lp/testing/keyserver/web.py
+++ b/lib/lp/testing/keyserver/web.py
@@ -32,7 +32,6 @@ __all__ = [
     'KeyServerResource',
     ]
 
-import cgi
 import glob
 import os
 from time import sleep
@@ -40,6 +39,7 @@ from time import sleep
 from twisted.web.resource import Resource
 from zope.component import getUtility
 
+from lp.services.compat import escape
 from lp.services.gpg.interfaces import (
     GPGKeyNotFoundError,
     IGPGHandler,
@@ -146,7 +146,7 @@ class LookUp(Resource):
         path = locate_key(self.root, filename)
         if path is not None:
             with open(path) as f:
-                content = cgi.escape(f.read())
+                content = escape(f.read(), quote=False)
             page = ('<html>\n<head>\n'
                     '<title>Results for Key %s</title>\n'
                     '</head>\n<body>'