8000 bpo-40275: Avoid importing socket in test.support (GH-19603) · python/cpython@1699491 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1699491

Browse files
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.
1 parent 3c8a5b4 commit 1699491

37 files changed

+472
-429
lines changed

Doc/library/test.rst

Lines changed: 71 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -348,11 +348,6 @@ The :mod:`test.support` module defines the following constants:
348348
:data:`SHORT_TIMEOUT`.
349349

350350

351-
.. data:: IPV6_ENABLED
352-
353-
Set to ``True`` if IPV6 is enabled on this host, ``False`` otherwise.
354-
355-
356351
.. data:: SAVEDCWD
357352

358353
Set to :func:`os.getcwd`.
@@ -901,12 +896,6 @@ The :mod:`test.support` module defines the following functions:
901896
A decorator for running tests that require support for xattr.
902897

903898

904-
.. decorator:: skip_unless_bind_unix_socket
905-
906-
A decorator for running tests that require a functional bind() for Unix
907-
sockets.
908-
909-
910899
.. decorator:: anticipate_failure(condition)
911900

912901
A decorator to conditionally mark tests with
@@ -1157,31 +1146,6 @@ The :mod:`test.support` module defines the following functions:
11571146
is raised.
11581147

11591148

1160-
.. function:: bind_port(sock, host=HOST)
1161-
1162-
Bind the socket to a free port and return the port number. Relies on
1163-
ephemeral ports in order to ensure we are using an unbound port. This is
1164-
important as many tests may be running simultaneously, especially in a
1165-
buildbot environment. This method raises an exception if the
1166-
``sock.family`` is :const:`~socket.AF_INET` and ``sock.type`` is
1167-
:const:`~socket.SOCK_STREAM`, and the socket has
1168-
:const:`~socket.SO_REUSEADDR` or :const:`~socket.SO_REUSEPORT` set on it.
1169-
Tests should never set these socket options for TCP/IP sockets.
1170-
The only case for setting these options is testing multicasting via
1171-
multiple UDP sockets.
1172-
1173-
Additionally, if the :const:`~socket.SO_EXCLUSIVEADDRUSE` socket option is
1174-
available (i.e. on Windows), it will be set on the socket. This will
1175-
prevent anyone else from binding to our host/port for the duration of the
1176-
test.
1177-
1178-
1179-
.. function:: bind_unix_socket(sock, addr)
1180-
1181-
Bind a unix socket, raising :exc:`unittest.SkipTest` if
1182-
:exc:`PermissionError` is raised.
1183-
1184-
11851149
.. function:: catch_threading_exception()
11861150

11871151
Context manager catching :class:`threading.Thread` exception using
@@ -1243,29 +1207,6 @@ The :mod:`test.support` module defines the following functions:
12431207
.. versionadded:: 3.8
12441208

12451209

1246-
.. function:: find_unused_port(family=socket.AF_INET, socktype=socket.SOCK_STREAM)
1247-
1248-
Returns an unused port that should be suitable for binding. This is
1249-
achieved by creating a temporary socket with the same family and type as
1250-
the ``sock`` parameter (default is :const:`~socket.AF_INET`,
1251-
:const:`~socket.SOCK_STREAM`),
1252-
and binding it to the specified host address (defaults to ``0.0.0.0``)
1253-
with the port set to 0, eliciting an unused ephemeral port from the OS.
1254-
The temporary socket is then closed and deleted, and the ephemeral port is
1255-
returned.
1256-
1257-
Either this method or :func:`bind_port` should be used for any tests
1258-
where a server socket needs to be bound to a particular port for the
1259-
duration of the test.
1260-
Which one to use depends on whether the calling code is creating a Python
1261-
socket, or if an unused port needs to be provided in a constructor
1262-
or passed to an external program (i.e. the ``-accept`` argument to
1263-
openssl's s_server mode). Always prefer :func:`bind_port` over
1264-
:func:`find_unused_port` where possible. Using a hard coded port is
1265-
discouraged since it can make multiple instances of the test impossible to
1266-
run simultaneously, which is a problem for buildbots.
1267-
1268-
12691210
.. function:: load_package_tests(pkg_dir, loader, standard_tests, pattern)
12701211

12711212
Generic implementation of the :mod:`unittest` ``load_tests`` protocol for
@@ -1481,6 +1422,77 @@ The :mod:`test.support` module defines the following classes:
14811422
it will be raised in :meth:`!__fspath__`.
14821423

14831424

1425+
:mod:`test.support.socket_helper` --- Utilities for socket tests
1426+
================================================================
1427+
1428+
.. module:: test.support.socket_helper
1429+
:synopsis: Support for socket tests.
1430+
1431+
1432+
The :mod:`test.support.socket_helper` module provides support for socket tests.
1433+
1434+
.. versionadded:: 3.9
1435+
1436+
1437+
.. data:: IPV6_ENABLED
1438+
1439+
Set to ``True`` if IPv6 is enabled on this host, ``False`` otherwise.
1440+
1441+
1442+
.. function:: find_unused_port(family=socket.AF_INET, socktype=socket.SOCK_STREAM)
1443+
1444+
Returns an unused port that should be suitable for binding. This is
1445+
achieved by creating a temporary socket with the same family and type as
1446+
the ``sock`` parameter (default is :const:`~socket.AF_INET`,
1447+
:const:`~socket.SOCK_STREAM`),
1448+
and binding it to the specified host address (defaults to ``0.0.0.0``)
1449+
with the port set to 0, eliciting an unused ephemeral port from the OS.
1450+
The temporary socket is then closed and deleted, and the ephemeral port is
1451+
returned.
1452+
1453+
Either this method or :func:`bind_port` should be used for any tests
1454+
where a server socket needs to be bound to a particular port for the
1455+
duration of the test.
1456+
Which one to use depends on whether the calling code is creating a Python
1457+
socket, or if an unused port needs to be provided in a constructor
1458+
or passed to an external program (i.e. the ``-accept`` argument to
1459+
openssl's s_server mode). Always prefer :func:`bind_port` over
1460+
:func:`find_unused_port` where possible. Using a hard coded port is
1461+
discouraged since it can make multiple instances of the test impossible to
1462+
run simultaneously, which is a problem for buildbots.
1463+
1464+
1465+
.. function:: bind_port(sock, host=HOST)
1466+
1467+
Bind the socket to a free port and return the port number. Relies on
1468+
ephemeral ports in order to ensure we are using an unbound port. This is
1469+
important as many tests may be running simultaneously, especially in a
1470+
buildbot environment. This method raises an exception if the
1471+
``sock.family`` is :const:`~socket.AF_INET` and ``sock.type`` is
1472+
:const:`~socket.SOCK_STREAM`, and the socket has
1473+
:const:`~socket.SO_REUSEADDR` or :const:`~socket.SO_REUSEPORT` set on it.
1474+
Tests should never set these socket options for TCP/IP sockets.
1475+
The only case for setting these options is testing multicasting via
1476+
multiple UDP sockets.
1477+
1478+
Additionally, if the :const:`~socket.SO_EXCLUSIVEADDRUSE` socket option is
1479+
available (i.e. on Windows), it will be set on the socket. This will
1480+
prevent anyone else from binding to our host/port for the duration of the
1481+
test.
1482+
1483+
1484+
.. function:: bind_unix_socket(sock, addr)
1485+
1486+
Bind a unix socket, raising :exc:`unittest.SkipTest` if
1487+
:exc:`PermissionError` is raised.
1488+
1489+
1490+
.. decorator:: skip_unless_bind_unix_socket
1491+
1492+
A decorator for running tests that require a functional ``bind()`` for Unix
1493+
sockets.
1494+
1495+
14841496
:mod:`test.support.script_helper` --- Utilities for the Python execution tests
14851497
==============================================================================
14861498

Lib/test/_test_multiprocessing.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import test.support
2727
import test.support.script_helper
2828
from test import support
29+
from test.support import socket_helper
2930

3031

3132
# Skip tests if _multiprocessing wasn't built.
@@ -2928,7 +2929,7 @@ def test_remote(self):
29282929
authkey = os.urandom(32)
29292930

29302931
manager = QueueManager(
2931-
address=(test.support.HOST, 0), authkey=authkey, serializer=SERIALIZER
2932+
address=(socket_helper.HOST, 0), authkey=authkey, serializer=SERIALIZER
29322933
)
29332934
manager.start()
29342935
self.addCleanup(manager.shutdown)
@@ -2965,7 +2966,7 @@ def _putter(cls, address, authkey):
29652966
def test_rapid_restart(self):
29662967
authkey = os.urandom(32)
29672968
manager = QueueManager(
2968-
address=(test.support.HOST, 0), authkey=authkey, serializer=SERIALIZER)
2969+
address=(socket_helper.HOST, 0), authkey=authkey, serializer=SERIALIZER)
29692970
try:
29702971
srvr = manager.get_server()
29712972
addr = srvr.address
@@ -3455,7 +3456,7 @@ def _listener(cls, conn, families):
34553456
new_conn.close()
34563457
l.close()
34573458

3458-
l = socket.create_server((test.support.HOST, 0))
3459+
l = socket.create_server((socket_helper.HOST, 0))
34593460
conn.send(l.getsockname())
34603461
new_conn, addr = l.accept()
34613462
conn.send(new_conn)
@@ -4593,7 +4594,7 @@ def _child_test_wait_socket(cls, address, slow):
45934594

45944595
def test_wait_socket(self, slow=False):
45954596
from multiprocessing.connection import wait
4596-
l = socket.create_server((test.support.HOST, 0))
4597+
l = socket.create_server((socket_helper.HOST, 0))
45974598
addr = l.getsockname()
45984599
readers = []
45994600
procs = []

Lib/test/eintrdata/eintr_tester.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import unittest
2323

2424
from test import support
25+
from test.support import socket_helper
2526

2627
@contextlib.contextmanager
2728
def kill_on_error(proc):
@@ -283,14 +284,14 @@ def test_sendmsg(self):
283284
self._test_send(lambda sock, data: sock.sendmsg([data]))
284285

285286
def test_accept(self):
286-
sock = socket.create_server((support.HOST, 0))
287+
sock = socket.create_server((socket_helper.HOST, 0))
287288
self.addCleanup(sock.close)
288289
port = sock.getsockname()[1]
289290

290291
code = '\n'.join((
291292
'import socket, time',
292293
'',
293-
'host = %r' % support.HOST,
294+
'host = %r' % socket_helper.HOST,
294295
'port = %s' % port,
295296
'sleep_time = %r' % self.sleep_time,
296297
'',

Lib/test/ssl_servers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
SimpleHTTPRequestHandler, BaseHTTPRequestHandler)
1010

1111
from test import support
12+
from test.support import socket_helper
1213

1314
here = os.path.dirname(__file__)
1415

15-
HOST = support.HOST
16+
HOST = socket_helper.HOST
1617
CERTFILE = os.path.join(here, 'keycert.pem')
1718

1819
# This one's based on HTTPServer, which is based on socketserver

0 commit comments

Comments
 (0)
0