diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2010-09-07 21:05:49 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2010-09-07 21:05:49 (GMT) |
commit | 4b92b5fad3baaa22a3ab198556e1adf5a2df7d9c (patch) | |
tree | da28d16bf8ee3c8a13dda8a1c3bdf696669d7f8a /Lib/test | |
parent | a88c83cbabb925d55714ffcfb67bd4e82dcf3547 (diff) | |
download | cpython-4b92b5fad3baaa22a3ab198556e1adf5a2df7d9c.zip cpython-4b92b5fad3baaa22a3ab198556e1adf5a2df7d9c.tar.gz cpython-4b92b5fad3baaa22a3ab198556e1adf5a2df7d9c.tar.bz2 |
Issue #9792: In case of connection failure, socket.create_connection()
would swallow the exception and raise a new one, making it impossible
to fetch the original errno, or to filter timeout errors. Now the
original error is re-raised.
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_socket.py | 41 |
1 files changed, 36 insertions, 5 deletions
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index 2f6d007..19c494b 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -13,6 +13,7 @@ import queue import sys import os import array +import contextlib from weakref import proxy import signal @@ -1203,12 +1204,42 @@ class BasicTCPTest2(NetworkConnectionTest, BasicTCPTest): class NetworkConnectionNoServer(unittest.TestCase): - def testWithoutServer(self): + class MockSocket(socket.socket): + def connect(self, *args): + raise socket.timeout('timed out') + + @contextlib.contextmanager + def mocked_socket_module(self): + """Return a socket which times out on connect""" + old_socket = socket.socket + socket.socket = self.MockSocket + try: + yield + finally: + socket.socket = old_socket + + def test_connect(self): port = support.find_unused_port() - self.assertRaises( - socket.error, - lambda: socket.create_connection((HOST, port)) - ) + cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + with self.assertRaises(socket.error) as cm: + cli.connect((HOST, port)) + self.assertEqual(cm.exception.errno, errno.ECONNREFUSED) + + def test_create_connection(self): + # Issue #9792: errors raised by create_connection() should have + # a proper errno attribute. + port = support.find_unused_port() + with self.assertRaises(socket.error) as cm: + socket.create_connection((HOST, port)) + self.assertEqual(cm.exception.errno, errno.ECONNREFUSED) + + def test_create_connection_timeout(self): + # Issue #9792: create_connection() should not recast timeout errors + # as generic socket errors. + with self.mocked_socket_module(): + with self.assertRaises(socket.timeout): + socket.create_connection((HOST, 1234)) + @unittest.skipUnless(thread, 'Threading required for this test.') class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest): |