[Python-checkins] r85952 - in python/branches/py3k/Lib/test: ssl_servers.py test_ssl.py

antoine.pitrou python-checkins at python.org
Sat Oct 30 01:41:37 CEST 2010


Author: antoine.pitrou
Date: Sat Oct 30 01:41:37 2010
New Revision: 85952

Log:
Clean up socket closing in test_ssl and test.ssl_servers



Modified:
   python/branches/py3k/Lib/test/ssl_servers.py
   python/branches/py3k/Lib/test/test_ssl.py

Modified: python/branches/py3k/Lib/test/ssl_servers.py
==============================================================================
--- python/branches/py3k/Lib/test/ssl_servers.py	(original)
+++ python/branches/py3k/Lib/test/ssl_servers.py	Sat Oct 30 01:41:37 2010
@@ -133,7 +133,10 @@
     def run(self):
         if self.flag:
             self.flag.set()
-        self.server.serve_forever(0.05)
+        try:
+            self.server.serve_forever(0.05)
+        finally:
+            self.server.server_close()
 
     def stop(self):
         self.server.shutdown()

Modified: python/branches/py3k/Lib/test/test_ssl.py
==============================================================================
--- python/branches/py3k/Lib/test/test_ssl.py	(original)
+++ python/branches/py3k/Lib/test/test_ssl.py	Sat Oct 30 01:41:37 2010
@@ -198,13 +198,16 @@
         self.assertRaisesRegexp(ValueError, "can't connect in server-side mode",
                                 s.connect, (HOST, 8080))
         with self.assertRaises(IOError) as cm:
-            ssl.wrap_socket(socket.socket(), certfile=WRONGCERT)
+            with socket.socket() as sock:
+                ssl.wrap_socket(sock, certfile=WRONGCERT)
         self.assertEqual(cm.exception.errno, errno.ENOENT)
         with self.assertRaises(IOError) as cm:
-            ssl.wrap_socket(socket.socket(), certfile=CERTFILE, keyfile=WRONGCERT)
+            with socket.socket() as sock:
+                ssl.wrap_socket(sock, certfile=CERTFILE, keyfile=WRONGCERT)
         self.assertEqual(cm.exception.errno, errno.ENOENT)
         with self.assertRaises(IOError) as cm:
-            ssl.wrap_socket(socket.socket(), certfile=WRONGCERT, keyfile=WRONGCERT)
+            with socket.socket() as sock:
+                ssl.wrap_socket(sock, certfile=WRONGCERT, keyfile=WRONGCERT)
         self.assertEqual(cm.exception.errno, errno.ENOENT)
 
     def test_match_hostname(self):
@@ -281,9 +284,9 @@
     def test_server_side(self):
         # server_hostname doesn't work for server sockets
         ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
-        sock = socket.socket()
-        self.assertRaises(ValueError, ctx.wrap_socket, sock, True,
-                          server_hostname="some.hostname")
+        with socket.socket() as sock:
+            self.assertRaises(ValueError, ctx.wrap_socket, sock, True,
+                              server_hostname="some.hostname")
 
 class ContextTests(unittest.TestCase):
 
@@ -579,9 +582,10 @@
             s.connect(remote)
             # Error checking can happen at instantiation or when connecting
             with self.assertRaisesRegexp(ssl.SSLError, "No cipher can be selected"):
-                s = ssl.wrap_socket(socket.socket(socket.AF_INET),
-                                    cert_reqs=ssl.CERT_NONE, ciphers="^$:,;?*'dorothyx")
-                s.connect(remote)
+                with socket.socket(socket.AF_INET) as sock:
+                    s = ssl.wrap_socket(sock,
+                                        cert_reqs=ssl.CERT_NONE, ciphers="^$:,;?*'dorothyx")
+                    s.connect(remote)
 
     def test_algorithms(self):
         # Issue #8484: all algorithms should be available when verifying a
@@ -903,10 +907,11 @@
         # try to connect
         try:
             try:
-                s = ssl.wrap_socket(socket.socket(),
-                                    certfile=certfile,
-                                    ssl_version=ssl.PROTOCOL_TLSv1)
-                s.connect((HOST, server.port))
+                with socket.socket() as sock:
+                    s = ssl.wrap_socket(sock,
+                                        certfile=certfile,
+                                        ssl_version=ssl.PROTOCOL_TLSv1)
+                    s.connect((HOST, server.port))
             except ssl.SSLError as x:
                 if support.verbose:
                     sys.stdout.write("\nSSLError is %s\n" % x.args[1])
@@ -1093,21 +1098,22 @@
             def listener():
                 s.listen(5)
                 listener_ready.set()
-                s.accept()
+                newsock, addr = s.accept()
+                newsock.close()
                 s.close()
                 listener_gone.set()
 
             def connector():
                 listener_ready.wait()
-                c = socket.socket()
-                c.connect((HOST, port))
-                listener_gone.wait()
-                try:
-                    ssl_sock = ssl.wrap_socket(c)
-                except IOError:
-                    pass
-                else:
-                    self.fail('connecting to closed SSL socket should have failed')
+                with socket.socket() as c:
+                    c.connect((HOST, port))
+                    listener_gone.wait()
+                    try:
+                        ssl_sock = ssl.wrap_socket(c)
+                    except IOError:
+                        pass
+                    else:
+                        self.fail('connecting to closed SSL socket should have failed')
 
             t = threading.Thread(target=listener)
             t.start()
@@ -1469,6 +1475,8 @@
                         # Let the socket hang around rather than having
                         # it closed by garbage collection.
                         conns.append(server.accept()[0])
+                for sock in conns:
+                    sock.close()
 
             t = threading.Thread(target=serve)
             t.start()


More information about the Python-checkins mailing list