[Python-checkins] cpython: asyncio, Tulip issue 143: UNIX domain methods, fix ResourceWarning and

victor.stinner python-checkins at python.org
Wed Feb 19 01:46:09 CET 2014


http://hg.python.org/cpython/rev/089ce5ddc147
changeset:   89265:089ce5ddc147
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Wed Feb 19 01:45:59 2014 +0100
summary:
  asyncio, Tulip issue 143: UNIX domain methods, fix ResourceWarning and
DeprecationWarning warnings. create_unix_server() closes the socket on any
error, not only on OSError.

files:
  Lib/asyncio/unix_events.py                |   8 +-
  Lib/test/test_asyncio/test_unix_events.py |  28 +++++-----
  2 files changed, 19 insertions(+), 17 deletions(-)


diff --git a/Lib/asyncio/unix_events.py b/Lib/asyncio/unix_events.py
--- a/Lib/asyncio/unix_events.py
+++ b/Lib/asyncio/unix_events.py
@@ -183,13 +183,12 @@
                 raise ValueError(
                     'path and sock can not be specified at the same time')
 
+            sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM, 0)
             try:
-                sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM, 0)
                 sock.setblocking(False)
                 yield from self.sock_connect(sock, path)
-            except OSError:
-                if sock is not None:
-                    sock.close()
+            except:
+                sock.close()
                 raise
 
         else:
@@ -213,6 +212,7 @@
             try:
                 sock.bind(path)
             except OSError as exc:
+                sock.close()
                 if exc.errno == errno.EADDRINUSE:
                     # Let's improve the error message by adding
                     # with what exact address it occurs.
diff --git a/Lib/test/test_asyncio/test_unix_events.py b/Lib/test/test_asyncio/test_unix_events.py
--- a/Lib/test/test_asyncio/test_unix_events.py
+++ b/Lib/test/test_asyncio/test_unix_events.py
@@ -221,17 +221,17 @@
         with test_utils.unix_socket_path() as path:
             sock = socket.socket(socket.AF_UNIX)
             sock.bind(path)
-
-            coro = self.loop.create_unix_server(lambda: None, path)
-            with self.assertRaisesRegexp(OSError,
-                                         'Address.*is already in use'):
-                self.loop.run_until_complete(coro)
+            with sock:
+                coro = self.loop.create_unix_server(lambda: None, path)
+                with self.assertRaisesRegex(OSError,
+                                            'Address.*is already in use'):
+                    self.loop.run_until_complete(coro)
 
     def test_create_unix_server_existing_path_nonsock(self):
         with tempfile.NamedTemporaryFile() as file:
             coro = self.loop.create_unix_server(lambda: None, file.name)
-            with self.assertRaisesRegexp(OSError,
-                                         'Address.*is already in use'):
+            with self.assertRaisesRegex(OSError,
+                                        'Address.*is already in use'):
                 self.loop.run_until_complete(coro)
 
     def test_create_unix_server_ssl_bool(self):
@@ -248,11 +248,13 @@
             self.loop.run_until_complete(coro)
 
     def test_create_unix_server_path_inetsock(self):
-        coro = self.loop.create_unix_server(lambda: None, path=None,
-                                            sock=socket.socket())
-        with self.assertRaisesRegex(ValueError,
-                                    'A UNIX Domain Socket was expected'):
-            self.loop.run_until_complete(coro)
+        sock = socket.socket()
+        with sock:
+            coro = self.loop.create_unix_server(lambda: None, path=None,
+                                                sock=sock)
+            with self.assertRaisesRegex(ValueError,
+                                        'A UNIX Domain Socket was expected'):
+                self.loop.run_until_complete(coro)
 
     def test_create_unix_connection_path_sock(self):
         coro = self.loop.create_unix_connection(
@@ -278,7 +280,7 @@
         coro = self.loop.create_unix_connection(
             lambda: None, '/dev/null', ssl=True)
 
-        with self.assertRaisesRegexp(
+        with self.assertRaisesRegex(
             ValueError, 'you have to pass server_hostname when using ssl'):
 
             self.loop.run_until_complete(coro)

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list