Skip to content

Commit 82f74eb

Browse files
gh-132535: Fix resource warnings in test_timeout (GH-132572)
They were emitted if internet connection was not available.
1 parent 4b15d10 commit 82f74eb

File tree

1 file changed

+16
-27
lines changed

1 file changed

+16
-27
lines changed

Lib/test/test_timeout.py

+16-27
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ class CreationTestCase(unittest.TestCase):
2626
"""Test case for socket.gettimeout() and socket.settimeout()"""
2727

2828
def setUp(self):
29-
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
30-
31-
def tearDown(self):
32-
self.sock.close()
29+
self.sock = self.enterContext(
30+
socket.socket(socket.AF_INET, socket.SOCK_STREAM))
3331

3432
def testObjectCreation(self):
3533
# Test Socket creation
@@ -113,8 +111,6 @@ class TimeoutTestCase(unittest.TestCase):
113111
def setUp(self):
114112
raise NotImplementedError()
115113

116-
tearDown = setUp
117-
118114
def _sock_operation(self, count, timeout, method, *args):
119115
"""
120116
Test the specified socket method.
@@ -142,12 +138,10 @@ class TCPTimeoutTestCase(TimeoutTestCase):
142138
"""TCP test case for socket.socket() timeout functions"""
143139

144140
def setUp(self):
145-
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
141+
self.sock = self.enterContext(
142+
socket.socket(socket.AF_INET, socket.SOCK_STREAM))
146143
self.addr_remote = resolve_address('www.python.org.', 80)
147144

148-
def tearDown(self):
149-
self.sock.close()
150-
151145
def testConnectTimeout(self):
152146
# Testing connect timeout is tricky: we need to have IP connectivity
153147
# to a host that silently drops our packets. We can't simulate this
@@ -190,19 +184,16 @@ def testConnectTimeout(self):
190184
# for the current configuration.
191185

192186
skip = True
193-
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
194-
timeout = support.LOOPBACK_TIMEOUT
195-
sock.settimeout(timeout)
196-
try:
197-
sock.connect((whitehole))
198-
except TimeoutError:
199-
pass
200-
except OSError as err:
201-
if err.errno == errno.ECONNREFUSED:
202-
skip = False
203-
finally:
204-
sock.close()
205-
del sock
187+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
188+
try:
189+
timeout = support.LOOPBACK_TIMEOUT
190+
sock.settimeout(timeout)
191+
sock.connect((whitehole))
192+
except TimeoutError:
193+
pass
194+
except OSError as err:
195+
if err.errno == errno.ECONNREFUSED:
196+
skip = False
206197

207198
if skip:
208199
self.skipTest(
@@ -269,10 +260,8 @@ class UDPTimeoutTestCase(TimeoutTestCase):
269260
"""UDP test case for socket.socket() timeout functions"""
270261

271262
def setUp(self):
272-
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
273-
274-
def tearDown(self):
275-
self.sock.close()
263+
self.sock = self.enterContext(
264+
socket.socket(socket.AF_INET, socket.SOCK_DGRAM))
276265

277266
def testRecvfromTimeout(self):
278267
# Test recvfrom() timeout

0 commit comments

Comments
 (0)