mirror of
https://github.com/python/cpython.git
synced 2025-10-27 11:44:39 +00:00
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.
This commit is contained in:
parent
065ca25aae
commit
884e40b982
2 changed files with 19 additions and 17 deletions
|
|
@ -183,13 +183,12 @@ def create_unix_connection(self, protocol_factory, path, *,
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
'path and sock can not be specified at the same time')
|
'path and sock can not be specified at the same time')
|
||||||
|
|
||||||
|
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM, 0)
|
||||||
try:
|
try:
|
||||||
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM, 0)
|
|
||||||
sock.setblocking(False)
|
sock.setblocking(False)
|
||||||
yield from self.sock_connect(sock, path)
|
yield from self.sock_connect(sock, path)
|
||||||
except OSError:
|
except:
|
||||||
if sock is not None:
|
sock.close()
|
||||||
sock.close()
|
|
||||||
raise
|
raise
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
@ -213,6 +212,7 @@ def create_unix_server(self, protocol_factory, path=None, *,
|
||||||
try:
|
try:
|
||||||
sock.bind(path)
|
sock.bind(path)
|
||||||
except OSError as exc:
|
except OSError as exc:
|
||||||
|
sock.close()
|
||||||
if exc.errno == errno.EADDRINUSE:
|
if exc.errno == errno.EADDRINUSE:
|
||||||
# Let's improve the error message by adding
|
# Let's improve the error message by adding
|
||||||
# with what exact address it occurs.
|
# with what exact address it occurs.
|
||||||
|
|
|
||||||
|
|
@ -221,17 +221,17 @@ def test_create_unix_server_existing_path_sock(self):
|
||||||
with test_utils.unix_socket_path() as path:
|
with test_utils.unix_socket_path() as path:
|
||||||
sock = socket.socket(socket.AF_UNIX)
|
sock = socket.socket(socket.AF_UNIX)
|
||||||
sock.bind(path)
|
sock.bind(path)
|
||||||
|
with sock:
|
||||||
coro = self.loop.create_unix_server(lambda: None, path)
|
coro = self.loop.create_unix_server(lambda: None, path)
|
||||||
with self.assertRaisesRegexp(OSError,
|
with self.assertRaisesRegex(OSError,
|
||||||
'Address.*is already in use'):
|
'Address.*is already in use'):
|
||||||
self.loop.run_until_complete(coro)
|
self.loop.run_until_complete(coro)
|
||||||
|
|
||||||
def test_create_unix_server_existing_path_nonsock(self):
|
def test_create_unix_server_existing_path_nonsock(self):
|
||||||
with tempfile.NamedTemporaryFile() as file:
|
with tempfile.NamedTemporaryFile() as file:
|
||||||
coro = self.loop.create_unix_server(lambda: None, file.name)
|
coro = self.loop.create_unix_server(lambda: None, file.name)
|
||||||
with self.assertRaisesRegexp(OSError,
|
with self.assertRaisesRegex(OSError,
|
||||||
'Address.*is already in use'):
|
'Address.*is already in use'):
|
||||||
self.loop.run_until_complete(coro)
|
self.loop.run_until_complete(coro)
|
||||||
|
|
||||||
def test_create_unix_server_ssl_bool(self):
|
def test_create_unix_server_ssl_bool(self):
|
||||||
|
|
@ -248,11 +248,13 @@ def test_create_unix_server_nopath_nosock(self):
|
||||||
self.loop.run_until_complete(coro)
|
self.loop.run_until_complete(coro)
|
||||||
|
|
||||||
def test_create_unix_server_path_inetsock(self):
|
def test_create_unix_server_path_inetsock(self):
|
||||||
coro = self.loop.create_unix_server(lambda: None, path=None,
|
sock = socket.socket()
|
||||||
sock=socket.socket())
|
with sock:
|
||||||
with self.assertRaisesRegex(ValueError,
|
coro = self.loop.create_unix_server(lambda: None, path=None,
|
||||||
'A UNIX Domain Socket was expected'):
|
sock=sock)
|
||||||
self.loop.run_until_complete(coro)
|
with self.assertRaisesRegex(ValueError,
|
||||||
|
'A UNIX Domain Socket was expected'):
|
||||||
|
self.loop.run_until_complete(coro)
|
||||||
|
|
||||||
def test_create_unix_connection_path_sock(self):
|
def test_create_unix_connection_path_sock(self):
|
||||||
coro = self.loop.create_unix_connection(
|
coro = self.loop.create_unix_connection(
|
||||||
|
|
@ -278,7 +280,7 @@ def test_create_unix_connection_ssl_noserverhost(self):
|
||||||
coro = self.loop.create_unix_connection(
|
coro = self.loop.create_unix_connection(
|
||||||
lambda: None, '/dev/null', ssl=True)
|
lambda: None, '/dev/null', ssl=True)
|
||||||
|
|
||||||
with self.assertRaisesRegexp(
|
with self.assertRaisesRegex(
|
||||||
ValueError, 'you have to pass server_hostname when using ssl'):
|
ValueError, 'you have to pass server_hostname when using ssl'):
|
||||||
|
|
||||||
self.loop.run_until_complete(coro)
|
self.loop.run_until_complete(coro)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue