← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~jameinel/launchpad/py27-zope-response-692057 into lp:launchpad

 

John A Meinel has proposed merging lp:~jameinel/launchpad/py27-zope-response-692057 into lp:launchpad.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)
Related bugs:
  Bug #692057 in Launchpad itself: "canonical.functional.XMLRPCTestTransport broken in Python 2.7"
  https://bugs.launchpad.net/launchpad/+bug/692057

For more details, see:
https://code.launchpad.net/~jameinel/launchpad/py27-zope-response-692057/+merge/112743


-- 
https://code.launchpad.net/~jameinel/launchpad/py27-zope-response-692057/+merge/112743
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~jameinel/launchpad/py27-zope-response-692057 into lp:launchpad.
=== modified file 'lib/lp/testing/xmlrpc.py'
--- lib/lp/testing/xmlrpc.py	2011-12-24 17:49:30 +0000
+++ lib/lp/testing/xmlrpc.py	2012-06-29 10:27:23 +0000
@@ -23,6 +23,18 @@
     )
 
 
+class _FakeSocket(object):
+    """Pretend to be a socket that has a makefile method.
+
+    This is used because it is what httplib.HTTPResponse expects.
+    """
+    def __init__(self, output):
+        self._output = output
+
+    def makefile(self, mode='rb', bufsize=0):
+        return StringIO(self._output)
+
+
 class HTTPCallerHTTPConnection(httplib.HTTPConnection):
     """A HTTPConnection which talks to HTTPCaller instead of a real server.
 
@@ -46,7 +58,7 @@
         # everything at once when the client requests a response.
         self._data_to_send += data
 
-    def getresponse(self):
+    def _zope_response(self):
         """Get the response."""
         current_principal = None
         # End and save the current interaction, since HTTPCaller creates
@@ -60,9 +72,21 @@
         setupInteraction(current_principal)
         return self._response
 
+    def getresponse(self, buffering=False):
+        content = self._zope_response().getOutput()
+        sock = _FakeSocket(content)
+        response = httplib.HTTPResponse(sock)
+        response.begin()
+        return response
+
+    # py2.6 compatibility
+    # in Python 2.6, XMLRPC uses their 'compatibility' HTTP class, which
+    # expects getreply() and getfile() methods. Python 2.7 now uses the
+    # getresponse(buffering=True) api. Once we switch to python 2.6, the
+    # following two methods can be removed.
     def getreply(self):
         """Return a tuple of status code, reason string, and headers."""
-        response = self.getresponse()
+        response = self._zope_response()
         return (
             response.getStatus(),
             response.getStatusString(),
@@ -70,7 +94,7 @@
 
     def getfile(self):
         """Get the response body as a file like object."""
-        response = self.getresponse()
+        response = self._zope_response()
         return StringIO(response.consumeBody())
 
 


Follow ups