|
4 | 4 | from test.support import socket_helper |
5 | 5 | from test.support import threading_helper |
6 | 6 |
|
| 7 | +import _thread as thread |
| 8 | +import array |
| 9 | +import contextlib |
7 | 10 | import errno |
8 | 11 | import io |
9 | 12 | import itertools |
10 | | -import socket |
11 | | -import select |
12 | | -import tempfile |
13 | | -import time |
14 | | -import traceback |
15 | | -import queue |
16 | | -import sys |
17 | | -import os |
18 | | -import platform |
19 | | -import array |
20 | | -import contextlib |
21 | | -from weakref import proxy |
22 | | -import signal |
23 | 13 | import math |
| 14 | +import os |
24 | 15 | import pickle |
25 | | -import re |
26 | | -import struct |
| 16 | +import platform |
| 17 | +import queue |
27 | 18 | import random |
28 | | -import shutil |
| 19 | +import re |
| 20 | +import select |
| 21 | +import signal |
| 22 | +import socket |
29 | 23 | import string |
30 | | -import _thread as thread |
| 24 | +import struct |
| 25 | +import sys |
| 26 | +import tempfile |
31 | 27 | import threading |
| 28 | +import time |
| 29 | +import traceback |
| 30 | +from weakref import proxy |
32 | 31 | try: |
33 | 32 | import multiprocessing |
34 | 33 | except ImportError: |
@@ -605,17 +604,18 @@ class SocketTestBase(unittest.TestCase): |
605 | 604 |
|
606 | 605 | def setUp(self): |
607 | 606 | self.serv = self.newSocket() |
| 607 | + self.addCleanup(self.close_server) |
608 | 608 | self.bindServer() |
609 | 609 |
|
| 610 | + def close_server(self): |
| 611 | + self.serv.close() |
| 612 | + self.serv = None |
| 613 | + |
610 | 614 | def bindServer(self): |
611 | 615 | """Bind server socket and set self.serv_addr to its address.""" |
612 | 616 | self.bindSock(self.serv) |
613 | 617 | self.serv_addr = self.serv.getsockname() |
614 | 618 |
|
615 | | - def tearDown(self): |
616 | | - self.serv.close() |
617 | | - self.serv = None |
618 | | - |
619 | 619 |
|
620 | 620 | class SocketListeningTestMixin(SocketTestBase): |
621 | 621 | """Mixin to listen on the server socket.""" |
@@ -700,15 +700,10 @@ class UnixSocketTestBase(SocketTestBase): |
700 | 700 | # can't send anything that might be problematic for a privileged |
701 | 701 | # user running the tests. |
702 | 702 |
|
703 | | - def setUp(self): |
704 | | - self.dir_path = tempfile.mkdtemp() |
705 | | - self.addCleanup(os.rmdir, self.dir_path) |
706 | | - super().setUp() |
707 | | - |
708 | 703 | def bindSock(self, sock): |
709 | | - path = tempfile.mktemp(dir=self.dir_path) |
710 | | - socket_helper.bind_unix_socket(sock, path) |
| 704 | + path = socket_helper.create_unix_domain_name() |
711 | 705 | self.addCleanup(os_helper.unlink, path) |
| 706 | + socket_helper.bind_unix_socket(sock, path) |
712 | 707 |
|
713 | 708 | class UnixStreamBase(UnixSocketTestBase): |
714 | 709 | """Base class for Unix-domain SOCK_STREAM tests.""" |
@@ -1905,17 +1900,18 @@ def test_socket_fileno(self): |
1905 | 1900 | self._test_socket_fileno(s, socket.AF_INET6, socket.SOCK_STREAM) |
1906 | 1901 |
|
1907 | 1902 | if hasattr(socket, "AF_UNIX"): |
1908 | | - tmpdir = tempfile.mkdtemp() |
1909 | | - self.addCleanup(shutil.rmtree, tmpdir) |
| 1903 | + unix_name = socket_helper.create_unix_domain_name() |
| 1904 | + self.addCleanup(os_helper.unlink, unix_name) |
| 1905 | + |
1910 | 1906 | s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
1911 | | - self.addCleanup(s.close) |
1912 | | - try: |
1913 | | - s.bind(os.path.join(tmpdir, 'socket')) |
1914 | | - except PermissionError: |
1915 | | - pass |
1916 | | - else: |
1917 | | - self._test_socket_fileno(s, socket.AF_UNIX, |
1918 | | - socket.SOCK_STREAM) |
| 1907 | + with s: |
| 1908 | + try: |
| 1909 | + s.bind(unix_name) |
| 1910 | + except PermissionError: |
| 1911 | + pass |
| 1912 | + else: |
| 1913 | + self._test_socket_fileno(s, socket.AF_UNIX, |
| 1914 | + socket.SOCK_STREAM) |
1919 | 1915 |
|
1920 | 1916 | def test_socket_fileno_rejects_float(self): |
1921 | 1917 | with self.assertRaises(TypeError): |
|
0 commit comments