Rev 4734: Fix python-2.4 compatibility. in file:///home/vila/src/bzr/bugs/405745-http-hangs/

Vincent Ladeuil v.ladeuil+lp at free.fr
Thu Oct 8 09:04:57 BST 2009


At file:///home/vila/src/bzr/bugs/405745-http-hangs/

------------------------------------------------------------
revno: 4734
revision-id: v.ladeuil+lp at free.fr-20091008080456-gzpvefk1170kxxtb
parent: v.ladeuil+lp at free.fr-20091007133103-bagf11imwtcenmzy
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: 405745-http-hangs
timestamp: Thu 2009-10-08 10:04:56 +0200
message:
  Fix python-2.4 compatibility.
  
  * bzrlib/tests/http_server.py:
  (TestingHTTPServer.server_bind,
  TestingThreadingHTTPServer.server_bind): Band aid to work around
  python-2.4 not updating server_address after bind.
  
  * bzrlib/osutils.py:
  (connect_socket): Band aid to work around python-2.4 missing
  socket.create_connection().
-------------- next part --------------
=== modified file 'bzrlib/osutils.py'
--- a/bzrlib/osutils.py	2009-09-19 01:33:10 +0000
+++ b/bzrlib/osutils.py	2009-10-08 08:04:56 +0000
@@ -1952,3 +1952,34 @@
     if use_cache:
         _cached_concurrency = concurrency
     return concurrency
+
+if sys.version_info < (2, 6):
+    def connect_socket(address):
+        import socket
+        """Connect to *address* and return the socket object.
+
+        Convenience function.  Connect to *address* (a 2-tuple ``(host,
+        port)``) and return the socket object.
+
+        Heavily inspired from the python 2.6 socket.create_connection()
+        function.
+        """
+
+        msg = "getaddrinfo returns an empty list"
+        host, port = address
+        for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
+            af, socktype, proto, canonname, sa = res
+            sock = None
+            try:
+                sock = socket.socket(af, socktype, proto)
+                sock.connect(sa)
+                return sock
+
+            except socket.error, msg:
+                if sock is not None:
+                    sock.close()
+
+        raise socket.error, msg
+else:
+    import socket
+    connect_socket = socket.create_connection

=== modified file 'bzrlib/tests/http_server.py'
--- a/bzrlib/tests/http_server.py	2009-10-07 13:31:03 +0000
+++ b/bzrlib/tests/http_server.py	2009-10-08 08:04:56 +0000
@@ -30,7 +30,10 @@
 import urllib
 import urlparse
 
-from bzrlib import transport
+from bzrlib import (
+    transport,
+    osutils,
+    )
 from bzrlib.transport import local
 
 
@@ -353,7 +356,7 @@
         self.serving = False
         # The server is listening for a last connection, let's give it:
         try:
-            fake_conn = socket.create_connection(self.server_address)
+            fake_conn = osutils.connect_socket(self.server_address)
             fake_conn.close()
         except socket.error, e:
             # But ignore connection errors as the point is to unblock the
@@ -373,6 +376,11 @@
         SocketServer.TCPServer.__init__(self, server_address,
                                         request_handler_class)
 
+    def server_bind(self):
+        SocketServer.TCPServer.server_bind(self)
+        if sys.version < (2, 5):
+            self.server_address = self.socket.getsockname()
+
 
 class TestingThreadingHTTPServer(TestingHTTPServerMixin,
                                  SocketServer.ThreadingTCPServer,
@@ -411,6 +419,11 @@
             else:
                 raise
 
+    def server_bind(self):
+        SocketServer.ThreadingTCPServer.server_bind(self)
+        if sys.version < (2, 5):
+            self.server_address = self.socket.getsockname()
+
 
 class HttpServer(transport.Server):
     """A test server for http transports.



More information about the bazaar-commits mailing list