diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2020-04-25 07:06:29 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-25 07:06:29 (GMT) |
commit | 16994912c93e8e5db7365d48b75d67d3f70dd7b2 (patch) | |
tree | 248f177a93676406af6d6ae977bed868aa2d1a34 /Lib/test/test_socket.py | |
parent | 3c8a5b459d68b4337776ada1e04f5b33f90a2275 (diff) | |
download | cpython-16994912c93e8e5db7365d48b75d67d3f70dd7b2.zip cpython-16994912c93e8e5db7365d48b75d67d3f70dd7b2.tar.gz cpython-16994912c93e8e5db7365d48b75d67d3f70dd7b2.tar.bz2 |
bpo-40275: Avoid importing socket in test.support (GH-19603)
* Move socket related functions from test.support to socket_helper.
* Import socket, nntplib and urllib.error lazily in transient_internet().
* Remove importing multiprocess.
Diffstat (limited to 'Lib/test/test_socket.py')
-rwxr-xr-x | Lib/test/test_socket.py | 115 |
1 files changed, 58 insertions, 57 deletions
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index 6e4e4fe..a70e282 100755 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -1,5 +1,6 @@ import unittest from test import support +from test.support import socket_helper import errno import io @@ -34,7 +35,7 @@ try: except ImportError: fcntl = None -HOST = support.HOST +HOST = socket_helper.HOST # test unicode string and carriage return MSG = 'Michael Gilfix was here\u1234\r\n'.encode('utf-8') @@ -161,7 +162,7 @@ class SocketTCPTest(unittest.TestCase): def setUp(self): self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - self.port = support.bind_port(self.serv) + self.port = socket_helper.bind_port(self.serv) self.serv.listen() def tearDown(self): @@ -172,7 +173,7 @@ class SocketUDPTest(unittest.TestCase): def setUp(self): self.serv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - self.port = support.bind_port(self.serv) + self.port = socket_helper.bind_port(self.serv) def tearDown(self): self.serv.close() @@ -182,7 +183,7 @@ class SocketUDPLITETest(SocketUDPTest): def setUp(self): self.serv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDPLITE) - self.port = support.bind_port(self.serv) + self.port = socket_helper.bind_port(self.serv) class ThreadSafeCleanupTestCase(unittest.TestCase): """Subclass of unittest.TestCase with thread-safe cleanup methods. @@ -265,7 +266,7 @@ class SocketRDSTest(unittest.TestCase): self.serv = socket.socket(socket.PF_RDS, socket.SOCK_SEQPACKET, 0) self.addCleanup(self.serv.close) try: - self.port = support.bind_port(self.serv) + self.port = socket_helper.bind_port(self.serv) except OSError: self.skipTest('unable to bind RDS socket') @@ -682,7 +683,7 @@ class UnixSocketTestBase(SocketTestBase): def bindSock(self, sock): path = tempfile.mktemp(dir=self.dir_path) - support.bind_unix_socket(sock, path) + socket_helper.bind_unix_socket(sock, path) self.addCleanup(support.unlink, path) class UnixStreamBase(UnixSocketTestBase): @@ -702,7 +703,7 @@ class InetTestBase(SocketTestBase): self.port = self.serv_addr[1] def bindSock(self, sock): - support.bind_port(sock, host=self.host) + socket_helper.bind_port(sock, host=self.host) class TCPTestBase(InetTestBase): """Base class for TCP-over-IPv4 tests.""" @@ -733,7 +734,7 @@ class SCTPStreamBase(InetTestBase): class Inet6TestBase(InetTestBase): """Base class for IPv6 socket tests.""" - host = support.HOSTv6 + host = socket_helper.HOSTv6 class UDP6TestBase(Inet6TestBase): """Base class for UDP-over-IPv6 tests.""" @@ -966,12 +967,12 @@ class GeneralModuleTests(unittest.TestCase): self.fail("Error testing host resolution mechanisms. (fqdn: %s, all: %s)" % (fqhn, repr(all_host_names))) def test_host_resolution(self): - for addr in [support.HOSTv4, '10.0.0.1', '255.255.255.255']: + for addr in [socket_helper.HOSTv4, '10.0.0.1', '255.255.255.255']: self.assertEqual(socket.gethostbyname(addr), addr) - # we don't test support.HOSTv6 because there's a chance it doesn't have + # we don't test socket_helper.HOSTv6 because there's a chance it doesn't have # a matching name entry (e.g. 'ip6-localhost') - for host in [support.HOSTv4]: + for host in [socket_helper.HOSTv4]: self.assertIn(host, socket.gethostbyaddr(host)[2]) def test_host_resolution_bad_address(self): @@ -1334,7 +1335,7 @@ class GeneralModuleTests(unittest.TestCase): def testSockName(self): # Testing getsockname() - port = support.find_unused_port() + port = socket_helper.find_unused_port() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.addCleanup(sock.close) sock.bind(("0.0.0.0", port)) @@ -1400,7 +1401,7 @@ class GeneralModuleTests(unittest.TestCase): def test_getsockaddrarg(self): sock = socket.socket() self.addCleanup(sock.close) - port = support.find_unused_port() + port = socket_helper.find_unused_port() big_port = port + 65536 neg_port = port - 65536 self.assertRaises(OverflowError, sock.bind, (HOST, big_port)) @@ -1408,7 +1409,7 @@ class GeneralModuleTests(unittest.TestCase): # Since find_unused_port() is inherently subject to race conditions, we # call it a couple times if necessary. for i in itertools.count(): - port = support.find_unused_port() + port = socket_helper.find_unused_port() try: sock.bind((HOST, port)) except OSError as e: @@ -1461,7 +1462,7 @@ class GeneralModuleTests(unittest.TestCase): socket.getaddrinfo('localhost', 80) socket.getaddrinfo('127.0.0.1', 80) socket.getaddrinfo(None, 80) - if support.IPV6_ENABLED: + if socket_helper.IPV6_ENABLED: socket.getaddrinfo('::1', 80) # port can be a string service name such as "http", a numeric # port number or None @@ -1674,14 +1675,14 @@ class GeneralModuleTests(unittest.TestCase): srv.bind((HOST, 0)) self.assertRaises(OverflowError, srv.listen, _testcapi.INT_MAX + 1) - @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') + @unittest.skipUnless(socket_helper.IPV6_ENABLED, 'IPv6 required for this test.') def test_flowinfo(self): self.assertRaises(OverflowError, socket.getnameinfo, - (support.HOSTv6, 0, 0xffffffff), 0) + (socket_helper.HOSTv6, 0, 0xffffffff), 0) with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as s: - self.assertRaises(OverflowError, s.bind, (support.HOSTv6, 0, -10)) + self.assertRaises(OverflowError, s.bind, (socket_helper.HOSTv6, 0, -10)) - @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') + @unittest.skipUnless(socket_helper.IPV6_ENABLED, 'IPv6 required for this test.') def test_getaddrinfo_ipv6_basic(self): ((*_, sockaddr),) = socket.getaddrinfo( 'ff02::1de:c0:face:8D', # Note capital letter `D`. @@ -1691,7 +1692,7 @@ class GeneralModuleTests(unittest.TestCase): ) self.assertEqual(sockaddr, ('ff02::1de:c0:face:8d', 1234, 0, 0)) - @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') + @unittest.skipUnless(socket_helper.IPV6_ENABLED, 'IPv6 required for this test.') @unittest.skipIf(sys.platform == 'win32', 'does not work on Windows') @unittest.skipIf(AIX, 'Symbolic scope id does not work') def test_getaddrinfo_ipv6_scopeid_symbolic(self): @@ -1706,7 +1707,7 @@ class GeneralModuleTests(unittest.TestCase): # Note missing interface name part in IPv6 address self.assertEqual(sockaddr, ('ff02::1de:c0:face:8d', 1234, 0, ifindex)) - @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') + @unittest.skipUnless(socket_helper.IPV6_ENABLED, 'IPv6 required for this test.') @unittest.skipUnless( sys.platform == 'win32', 'Numeric scope id does not work or undocumented') @@ -1723,7 +1724,7 @@ class GeneralModuleTests(unittest.TestCase): # Note missing interface name part in IPv6 address self.assertEqual(sockaddr, ('ff02::1de:c0:face:8d', 1234, 0, ifindex)) - @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') + @unittest.skipUnless(socket_helper.IPV6_ENABLED, 'IPv6 required for this test.') @unittest.skipIf(sys.platform == 'win32', 'does not work on Windows') @unittest.skipIf(AIX, 'Symbolic scope id does not work') def test_getnameinfo_ipv6_scopeid_symbolic(self): @@ -1733,7 +1734,7 @@ class GeneralModuleTests(unittest.TestCase): nameinfo = socket.getnameinfo(sockaddr, socket.NI_NUMERICHOST | socket.NI_NUMERICSERV) self.assertEqual(nameinfo, ('ff02::1de:c0:face:8d%' + test_interface, '1234')) - @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') + @unittest.skipUnless(socket_helper.IPV6_ENABLED, 'IPv6 required for this test.') @unittest.skipUnless( sys.platform == 'win32', 'Numeric scope id does not work or undocumented') def test_getnameinfo_ipv6_scopeid_numeric(self): @@ -1826,19 +1827,19 @@ class GeneralModuleTests(unittest.TestCase): def test_socket_fileno(self): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.addCleanup(s.close) - s.bind((support.HOST, 0)) + s.bind((socket_helper.HOST, 0)) self._test_socket_fileno(s, socket.AF_INET, socket.SOCK_STREAM) if hasattr(socket, "SOCK_DGRAM"): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.addCleanup(s.close) - s.bind((support.HOST, 0)) + s.bind((socket_helper.HOST, 0)) self._test_socket_fileno(s, socket.AF_INET, socket.SOCK_DGRAM) - if support.IPV6_ENABLED: + if socket_helper.IPV6_ENABLED: s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) self.addCleanup(s.close) - s.bind((support.HOSTv6, 0, 0, 0)) + s.bind((socket_helper.HOSTv6, 0, 0, 0)) self._test_socket_fileno(s, socket.AF_INET6, socket.SOCK_STREAM) if hasattr(socket, "AF_UNIX"): @@ -2214,12 +2215,12 @@ class BasicQIPCRTRTest(unittest.TestCase): def testBindSock(self): with socket.socket(socket.AF_QIPCRTR, socket.SOCK_DGRAM) as s: - support.bind_port(s, host=s.getsockname()[0]) + socket_helper.bind_port(s, host=s.getsockname()[0]) self.assertNotEqual(s.getsockname()[1], 0) def testInvalidBindSock(self): with socket.socket(socket.AF_QIPCRTR, socket.SOCK_DGRAM) as s: - self.assertRaises(OSError, support.bind_port, s, host=-2) + self.assertRaises(OSError, socket_helper.bind_port, s, host=-2) def testAutoBindSock(self): with socket.socket(socket.AF_QIPCRTR, socket.SOCK_DGRAM) as s: @@ -4094,25 +4095,25 @@ class SendrecvmsgUDP6TestBase(SendrecvmsgDgramFlagsBase, self.assertEqual(addr1[:-1], addr2[:-1]) @requireAttrs(socket.socket, "sendmsg") -@unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') +@unittest.skipUnless(socket_helper.IPV6_ENABLED, 'IPv6 required for this test.') @requireSocket("AF_INET6", "SOCK_DGRAM") class SendmsgUDP6Test(SendmsgConnectionlessTests, SendrecvmsgUDP6TestBase): pass @requireAttrs(socket.socket, "recvmsg") -@unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') +@unittest.skipUnless(socket_helper.IPV6_ENABLED, 'IPv6 required for this test.') @requireSocket("AF_INET6", "SOCK_DGRAM") class RecvmsgUDP6Test(RecvmsgTests, SendrecvmsgUDP6TestBase): pass @requireAttrs(socket.socket, "recvmsg_into") -@unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') +@unittest.skipUnless(socket_helper.IPV6_ENABLED, 'IPv6 required for this test.') @requireSocket("AF_INET6", "SOCK_DGRAM") class RecvmsgIntoUDP6Test(RecvmsgIntoTests, SendrecvmsgUDP6TestBase): pass @requireAttrs(socket.socket, "recvmsg") -@unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') +@unittest.skipUnless(socket_helper.IPV6_ENABLED, 'IPv6 required for this test.') @requireAttrs(socket, "IPPROTO_IPV6") @requireSocket("AF_INET6", "SOCK_DGRAM") class RecvmsgRFC3542AncillaryUDP6Test(RFC3542AncillaryTest, @@ -4120,7 +4121,7 @@ class RecvmsgRFC3542AncillaryUDP6Test(RFC3542AncillaryTest, pass @requireAttrs(socket.socket, "recvmsg_into") -@unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') +@unittest.skipUnless(socket_helper.IPV6_ENABLED, 'IPv6 required for this test.') @requireAttrs(socket, "IPPROTO_IPV6") @requireSocket("AF_INET6", "SOCK_DGRAM") class RecvmsgIntoRFC3542AncillaryUDP6Test(RecvmsgIntoMixin, @@ -4167,7 +4168,7 @@ class SendrecvmsgUDPLITE6TestBase(SendrecvmsgDgramFlagsBase, self.assertEqual(addr1[:-1], addr2[:-1]) @requireAttrs(socket.socket, "sendmsg") -@unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') +@unittest.skipUnless(socket_helper.IPV6_ENABLED, 'IPv6 required for this test.') @unittest.skipUnless(HAVE_SOCKET_UDPLITE, 'UDPLITE sockets required for this test.') @requireSocket("AF_INET6", "SOCK_DGRAM") @@ -4175,7 +4176,7 @@ class SendmsgUDPLITE6Test(SendmsgConnectionlessTests, SendrecvmsgUDPLITE6TestBas pass @requireAttrs(socket.socket, "recvmsg") -@unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') +@unittest.skipUnless(socket_helper.IPV6_ENABLED, 'IPv6 required for this test.') @unittest.skipUnless(HAVE_SOCKET_UDPLITE, 'UDPLITE sockets required for this test.') @requireSocket("AF_INET6", "SOCK_DGRAM") @@ -4183,7 +4184,7 @@ class RecvmsgUDPLITE6Test(RecvmsgTests, SendrecvmsgUDPLITE6TestBase): pass @requireAttrs(socket.socket, "recvmsg_into") -@unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') +@unittest.skipUnless(socket_helper.IPV6_ENABLED, 'IPv6 required for this test.') @unittest.skipUnless(HAVE_SOCKET_UDPLITE, 'UDPLITE sockets required for this test.') @requireSocket("AF_INET6", "SOCK_DGRAM") @@ -4191,7 +4192,7 @@ class RecvmsgIntoUDPLITE6Test(RecvmsgIntoTests, SendrecvmsgUDPLITE6TestBase): pass @requireAttrs(socket.socket, "recvmsg") -@unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') +@unittest.skipUnless(socket_helper.IPV6_ENABLED, 'IPv6 required for this test.') @unittest.skipUnless(HAVE_SOCKET_UDPLITE, 'UDPLITE sockets required for this test.') @requireAttrs(socket, "IPPROTO_IPV6") @@ -4201,7 +4202,7 @@ class RecvmsgRFC3542AncillaryUDPLITE6Test(RFC3542AncillaryTest, pass @requireAttrs(socket.socket, "recvmsg_into") -@unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') +@unittest.skipUnless(socket_helper.IPV6_ENABLED, 'IPv6 required for this test.') @unittest.skipUnless(HAVE_SOCKET_UDPLITE, 'UDPLITE sockets required for this test.') @requireAttrs(socket, "IPPROTO_IPV6") @@ -4999,7 +5000,7 @@ class NetworkConnectionNoServer(unittest.TestCase): socket.socket = old_socket def test_connect(self): - port = support.find_unused_port() + port = socket_helper.find_unused_port() cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.addCleanup(cli.close) with self.assertRaises(OSError) as cm: @@ -5009,7 +5010,7 @@ class NetworkConnectionNoServer(unittest.TestCase): def test_create_connection(self): # Issue #9792: errors raised by create_connection() should have # a proper errno attribute. - port = support.find_unused_port() + port = socket_helper.find_unused_port() with self.assertRaises(OSError) as cm: socket.create_connection((HOST, port)) @@ -5027,7 +5028,7 @@ class NetworkConnectionNoServer(unittest.TestCase): # On Solaris, ENETUNREACH is returned in this circumstance instead # of ECONNREFUSED. So, if that errno exists, add it to our list of # expected errnos. - expected_errnos = support.get_socket_conn_refused_errs() + expected_errnos = socket_helper.get_socket_conn_refused_errs() self.assertIn(cm.exception.errno, expected_errnos) def test_create_connection_timeout(self): @@ -5039,7 +5040,7 @@ class NetworkConnectionNoServer(unittest.TestCase): except socket.timeout: pass except OSError as exc: - if support.IPV6_ENABLED or exc.errno != errno.EAFNOSUPPORT: + if socket_helper.IPV6_ENABLED or exc.errno != errno.EAFNOSUPPORT: raise else: self.fail('socket.timeout not raised') @@ -5052,7 +5053,7 @@ class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest): ThreadableTest.__init__(self) def clientSetUp(self): - self.source_port = support.find_unused_port() + self.source_port = socket_helper.find_unused_port() def clientTearDown(self): self.cli.close() @@ -5338,7 +5339,7 @@ class TestUnixDomain(unittest.TestCase): def bind(self, sock, path): # Bind the socket try: - support.bind_unix_socket(sock, path) + socket_helper.bind_unix_socket(sock, path) except OSError as e: if str(e) == "AF_UNIX path too long": self.skipTest( @@ -6328,11 +6329,11 @@ class TestMSWindowsTCPFlags(unittest.TestCase): class CreateServerTest(unittest.TestCase): def test_address(self): - port = support.find_unused_port() + port = socket_helper.find_unused_port() with socket.create_server(("127.0.0.1", port)) as sock: self.assertEqual(sock.getsockname()[0], "127.0.0.1") self.assertEqual(sock.getsockname()[1], port) - if support.IPV6_ENABLED: + if socket_helper.IPV6_ENABLED: with socket.create_server(("::1", port), family=socket.AF_INET6) as sock: self.assertEqual(sock.getsockname()[0], "::1") @@ -6342,7 +6343,7 @@ class CreateServerTest(unittest.TestCase): with socket.create_server(("127.0.0.1", 0)) as sock: self.assertEqual(sock.family, socket.AF_INET) self.assertEqual(sock.type, socket.SOCK_STREAM) - if support.IPV6_ENABLED: + if socket_helper.IPV6_ENABLED: with socket.create_server(("::1", 0), family=socket.AF_INET6) as s: self.assertEqual(s.family, socket.AF_INET6) self.assertEqual(sock.type, socket.SOCK_STREAM) @@ -6362,14 +6363,14 @@ class CreateServerTest(unittest.TestCase): @unittest.skipIf(not hasattr(_socket, 'IPPROTO_IPV6') or not hasattr(_socket, 'IPV6_V6ONLY'), "IPV6_V6ONLY option not supported") - @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test') + @unittest.skipUnless(socket_helper.IPV6_ENABLED, 'IPv6 required for this test') def test_ipv6_only_default(self): with socket.create_server(("::1", 0), family=socket.AF_INET6) as sock: assert sock.getsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY) @unittest.skipIf(not socket.has_dualstack_ipv6(), "dualstack_ipv6 not supported") - @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test') + @unittest.skipUnless(socket_helper.IPV6_ENABLED, 'IPv6 required for this test') def test_dualstack_ipv6_family(self): with socket.create_server(("::1", 0), family=socket.AF_INET6, dualstack_ipv6=True) as sock: @@ -6411,14 +6412,14 @@ class CreateServerFunctionalTest(unittest.TestCase): self.assertEqual(sock.recv(1024), b'foo') def test_tcp4(self): - port = support.find_unused_port() + port = socket_helper.find_unused_port() with socket.create_server(("", port)) as sock: self.echo_server(sock) self.echo_client(("127.0.0.1", port), socket.AF_INET) - @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test') + @unittest.skipUnless(socket_helper.IPV6_ENABLED, 'IPv6 required for this test') def test_tcp6(self): - port = support.find_unused_port() + port = socket_helper.find_unused_port() with socket.create_server(("", port), family=socket.AF_INET6) as sock: self.echo_server(sock) @@ -6428,9 +6429,9 @@ class CreateServerFunctionalTest(unittest.TestCase): @unittest.skipIf(not socket.has_dualstack_ipv6(), "dualstack_ipv6 not supported") - @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test') + @unittest.skipUnless(socket_helper.IPV6_ENABLED, 'IPv6 required for this test') def test_dual_stack_client_v4(self): - port = support.find_unused_port() + port = socket_helper.find_unused_port() with socket.create_server(("", port), family=socket.AF_INET6, dualstack_ipv6=True) as sock: self.echo_server(sock) @@ -6438,9 +6439,9 @@ class CreateServerFunctionalTest(unittest.TestCase): @unittest.skipIf(not socket.has_dualstack_ipv6(), "dualstack_ipv6 not supported") - @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test') + @unittest.skipUnless(socket_helper.IPV6_ENABLED, 'IPv6 required for this test') def test_dual_stack_client_v6(self): - port = support.find_unused_port() + port = socket_helper.find_unused_port() with socket.create_server(("", port), family=socket.AF_INET6, dualstack_ipv6=True) as sock: self.echo_server(sock) |