Merge lp:~gz/launchpad/py27_xmlrpc_transport_timeout into lp:launchpad

Proposed by Martin Packman on 2012-06-28
Status: Merged
Approved by: Jelmer Vernooij on 2012-06-28
Approved revision: no longer in the source branch.
Merged at revision: 15514
Proposed branch: lp:~gz/launchpad/py27_xmlrpc_transport_timeout
Merge into: lp:launchpad
Diff against target: 85 lines (+12/-31)
2 files modified
lib/lp/services/tests/test_xmlrpc.py (+5/-20)
lib/lp/services/xmlrpc.py (+7/-11)
To merge this branch: bzr merge lp:~gz/launchpad/py27_xmlrpc_transport_timeout
Reviewer Review Type Date Requested Status
Jelmer Vernooij (community) 2012-06-28 Approve on 2012-06-28
Review via email: mp+112528@code.launchpad.net

Commit Message

Cleanup lp.services.xmlrpc for Python 2.7 compatibility

Description of the Change

In Python 2.7 xmlrpclib started using HTTPConnection directly rather than the legacy compat HTTP wrapper around it. The custom Transport subclass that passes down a socket timeout therefore needed fixing to return the right thing from make_connection which varies by version. Using the existing make_connection method and poking the timeout attribute onto the correct class reduces the complexity and avoids needing to care (as much) about which version of xmlrpclib is used.

To post a comment you must log in.
Jelmer Vernooij (jelmer) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/lp/services/tests/test_xmlrpc.py'
2--- lib/lp/services/tests/test_xmlrpc.py 2012-01-01 02:58:52 +0000
3+++ lib/lp/services/tests/test_xmlrpc.py 2012-06-28 09:59:24 +0000
4@@ -8,22 +8,12 @@
5 import httplib
6
7 from lp.services.xmlrpc import (
8- HTTP,
9 Transport,
10 )
11 from lp.testing import TestCase
12 from lp.testing.layers import BaseLayer
13
14
15-class DummyConnectionClass:
16- def __init__(self, *args, **kwargs):
17- self.args = args
18- self.kwargs = kwargs
19-
20- def __getattr__(self, name):
21- return name
22-
23-
24 class TestTransport(TestCase):
25 """Test code that allows xmlrpclib.ServerProxy to have a socket timeout"""
26
27@@ -39,13 +29,8 @@
28 self.assertEqual(25, transport.timeout)
29
30 def test_timeout_passed_to_connection(self):
31- # The _connection_class is actually set on a parent class. We verify
32- # this, so we can just delete it from the class at the end.
33- self.assertEqual(self, HTTP.__dict__.get('_connection_class', self))
34- HTTP._connection_class = DummyConnectionClass
35- try:
36- transport = Transport(timeout=25)
37- http = transport.make_connection('localhost')
38- self.assertEqual(25, http._conn.kwargs['timeout'])
39- finally:
40- del HTTP.__dict__['_connection_class']
41+ transport = Transport(timeout=25)
42+ http = transport.make_connection('localhost')
43+ # See logic in lp.services.xmlrpc.Transport.make_connection
44+ http = getattr(http, "_conn", http)
45+ self.assertEqual(25, http.timeout)
46
47=== modified file 'lib/lp/services/xmlrpc.py'
48--- lib/lp/services/xmlrpc.py 2011-07-27 14:01:17 +0000
49+++ lib/lp/services/xmlrpc.py 2012-06-28 09:59:24 +0000
50@@ -9,7 +9,6 @@
51 'Transport',
52 ]
53
54-import httplib
55 import socket
56 import xmlrpclib
57
58@@ -43,15 +42,6 @@
59 return not (self == other)
60
61
62-class HTTP(httplib.HTTP):
63- """A version of httplib.HTTP with a timeout argument."""
64-
65- def __init__(self, host='', port=None, strict=None,
66- timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
67- self._setup(
68- self._connection_class(host, port, strict, timeout=timeout))
69-
70-
71 class Transport(xmlrpclib.Transport):
72 """An xmlrpclib transport that supports a timeout argument.
73
74@@ -65,4 +55,10 @@
75 self.timeout = timeout
76
77 def make_connection(self, host):
78- return HTTP(host, timeout=self.timeout)
79+ conn = xmlrpclib.Transport.make_connection(self, host)
80+ # In Python 2.6 make_connection returns a legacy HTTP wrapper object
81+ # around the HTTPConnection, 2.7 returns the HTTPConnection directly.
82+ # No connection is yet opened so it's okay to set timeout after init.
83+ real_conn = getattr(conn, "_conn", conn)
84+ real_conn.timeout = self.timeout
85+ return conn