8000 bpo-42413: socket.timeout is now an alias of TimeoutError (GH-23413) · python/cpython@03c8ddd · GitHub
[go: up one dir, main page]

Skip to content

Commit 03c8ddd

Browse files
authored
bpo-42413: socket.timeout is now an alias of TimeoutError (GH-23413)
Signed-off-by: Christian Heimes <christian@python.org>
1 parent 7ddbaa7 commit 03c8ddd

24 files changed

+72
-62
lines changed

Doc/library/smtplib.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Protocol) and :rfc:`1869` (SMTP Service Extensions).
3232
than a success code, an :exc:`SMTPConnectError` is raised. The optional
3333
*timeout* parameter specifies a timeout in seconds for blocking operations
3434
like the connection attempt (if not specified, the global default timeout
35-
setting will be used). If the timeout expires, :exc:`socket.timeout` is
35+
setting will be used). If the timeout expires, :exc:`TimeoutError` is
3636
raised. The optional source_address parameter allows binding
3737
to some specific source address in a machine with multiple network
3838
interfaces, and/or to some specific source TCP port. It takes a 2-tuple

Doc/library/socket.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ Exceptions
283283

284284
.. exception:: timeout
285285

286+
A deprecated alias of :exc:`TimeoutError`.
287+
286288
A subclass of :exc:`OSError`, this exception is raised when a timeout
287289
occurs on a socket which has had timeouts enabled via a prior call to
288290
:meth:`~socket.settimeout` (or implicitly through
@@ -292,6 +294,9 @@ Exceptions
292294
.. versionchanged:: 3.3
293295
This class was made a subclass of :exc:`OSError`.
294296

297+
.. versionchanged:: 3.10
298+
This class was made an alias of :exc:`TimeoutError`.
299+
295300

296301
Constants
297302
^^^^^^^^^
@@ -1208,7 +1213,7 @@ to sockets.
12081213
address family --- see above.)
12091214

12101215
If the connection is interrupted by a signal, the method waits until the
1211-
connection completes, or raise a :exc:`socket.timeout` on timeout, if the
1216+
connection completes, or raise a :exc:`TimeoutError` on timeout, if the
12121217
signal handler doesn't raise an exception and the socket is blocking or has
12131218
a timeout. For non-blocking sockets, the method raises an
12141219
:exc:`InterruptedError` exception if the connection is interrupted by a

Doc/whatsnew/3.10.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,12 @@ site
263263
When a module does not define ``__loader__``, fall back to ``__spec__.loader``.
264264
(Contributed by Brett Cannon in :issue:`42133`.)
265265

266+
socket
267+
------
268+
269+
The exception :exc:`socket.timeout` is now an alias of :exc:`TimeoutError`.
270+
(Contributed by Christian Heimes in :issue:`42413`.)
271+
266272
sys
267273
---
268274

Lib/http/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ def handle_one_request(self):
414414
method = getattr(self, mname)
415415
method()
416416
self.wfile.flush() #actually send the response if not already done.
417-
except socket.timeout as e:
417+
except TimeoutError as e:
418418
#a read or a write timed out. Discard this connection
419419
self.log_error("Request timed out: %r", e)
420420
self.close_connection = True

Lib/idlelib/pyshell.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ def start_subprocess(self):
463463
self.rpcclt.listening_sock.settimeout(10)
464464
try:
465465
self.rpcclt.accept()
466-
except socket.timeout:
466+
except TimeoutError:
467467
self.display_no_subprocess_error()
468468
return None
469469
self.rpcclt.register("console", self.tkconsole)
@@ -498,7 +498,7 @@ def restart_subprocess(self, with_cwd=False, filename=''):
498498
self.spawn_subprocess()
499499
try:
500500
self.rpcclt.accept()
501-
except socket.timeout:
501+
except TimeoutError:
502502
self.display_no_subprocess_error()
503503
return None
504504
self.transfer_path(with_cwd=with_cwd)

Lib/socket.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ def _sendfile_use_sendfile(self, file, offset=0, count=None):
377377
try:
378378
while True:
379379
if timeout and not selector_select(timeout):
380-
raise _socket.timeout('timed out')
380+
raise TimeoutError('timed out')
381381
if count:
382382
blocksize = count - total_sent
383383
if blocksize <= 0:

Lib/test/support/socket_helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def transient_internet(resource_name, *, timeout=_NOT_SET, errnos=()):
225225

226226
def filter_error(err):
227227
n = getattr(err, 'errno', None)
228-
if (isinstance(err, socket.timeout) or
228+
if (isinstance(err, TimeoutError) or
229229
(isinstance(err, socket.gaierror) and n in gai_errnos) or
230230
(isinstance(err, urllib.error.HTTPError) and
231231
500 <= err.code <= 599) or

Lib/test/test_asyncio/functional.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def _run(self):
248248
conn, addr = self._sock.accept()
249249
except BlockingIOError:
250250
continue
251-
except socket.timeout:
251+
except TimeoutError:
252252
if not self._active:
253253
return
254254
else:

Lib/test/test_asyncore.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def capture_server(evt, buf, serv):
6969
try:
7070
serv.listen()
7171
conn, addr = serv.accept()
72-
except socket.timeout:
72+
except TimeoutError:
7373
pass
7474
else:
7575
n = 200

Lib/test/test_exception_hierarchy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ def test_builtin_errors(self):
4040
self.assertIs(EnvironmentError, OSError)
4141

4242
def test_socket_errors(self):
43-
self.assertIs(socket.error, IOError)
43+
self.assertIs(socket.error, OSError)
4444
self.assertIs(socket.gaierror.__base__, OSError)
4545
self.assertIs(socket.herror.__base__, OSError)
46-
self.assertIs(socket.timeout.__base__, OSError)
46+
self.assertIs(socket.timeout, TimeoutError)
4747

4848
def test_select_error(self):
4949
self.assertIs(select.error, OSError)

Lib/test/test_ftplib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,7 @@ def server(self):
10361036
self.evt.set()
10371037
try:
10381038
conn, addr = self.sock.accept()
1039-
except socket.timeout:
1039+
except TimeoutError:
10401040
pass
10411041
else:
10421042
conn.sendall(b"1 Hola mundo\n")

Lib/test/test_imaplib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ def handle(self):
476476

477477
_, server = self._setup(TimeoutHandler)
478478
addr = server.server_address[1]
479-
with self.assertRaises(socket.timeout):
479+
with self.assertRaises(TimeoutError):
480480
client = self.imap_class("localhost", addr, timeout=0.001)
481481

482482
def test_with_statement(self):

Lib/test/test_poplib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ def server(self, evt, serv):
501501
conn, addr = serv.accept()
502502
conn.send(b"+ Hola mundo\n")
503503
conn.close()
504-
except socket.timeout:
504+
except TimeoutError:
505505
pass
506506
finally:
507507
serv.close()

Lib/test/test_signal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ def handler(signum, frame):
528528
while True:
529529
write.send(chunk)
530530
written += chunk_size
531-
except (BlockingIOError, socket.timeout):
531+
except (BlockingIOError, TimeoutError):
532532
pass
533533
534534
print(f"%s bytes written into the socketpair" % written, flush=True)

Lib/test/test_smtplib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def server(evt, buf, serv):
4040
evt.set()
4141
try:
4242
conn, addr = serv.accept()
43-
except socket.timeout:
43+
except TimeoutError:
4444
pass
4545
else:
4646
n = 500
@@ -193,7 +193,7 @@ def debugging_server(serv, serv_evt, client_evt):
193193

194194
n -= 1
195195

196-
except socket.timeout:
196+
except TimeoutError:
197197
pass
198198
finally:
199199
if not client_evt.is_set():

Lib/test/test_socket.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,7 +1611,7 @@ def raising_handler(*args):
16111611
if with_timeout:
16121612
signal.signal(signal.SIGALRM, ok_handler)
16131613
signal.alarm(1)
1614-
self.assertRaises(socket.timeout, c.sendall,
1614+
self.assertRaises(TimeoutError, c.sendall,
16151615
b"x" * support.SOCK_MAX_SIZE)
16161616
finally:
16171617
signal.alarm(0)
@@ -2966,15 +2966,15 @@ def _testSendmsgTimeout(self):
29662966
try:
29672967
while True:
29682968
self.sendmsgToServer([b"a"*512])
2969-
except socket.timeout:
2969+
except TimeoutError:
29702970
pass
29712971
except OSError as exc:
29722972
if exc.errno != errno.ENOMEM:
29732973
raise
29742974
# bpo-33937 the test randomly fails on Travis CI with
29752975
# "OSError: [Errno 12] Cannot allocate memory"
29762976
else:
2977-
self.fail("socket.timeout not raised")
2977+
self.fail("TimeoutError not raised")
29782978
finally:
29792979
self.misc_event.set()
29802980

@@ -3109,7 +3109,7 @@ def testRecvmsgTimeout(self):
31093109
# Check that timeout works.
31103110
try:
31113111
self.serv_sock.settimeout(0.03)
3112-
self.assertRaises(socket.timeout,
3112+
self.assertRaises(TimeoutError,
31133113
self.doRecvmsg, self.serv_sock, len(MSG))
31143114
finally:
31153115
self.misc_event.set()
@@ -4827,7 +4827,7 @@ def testReadAfterTimeout(self):
48274827
self.cli_conn.settimeout(1)
48284828
self.read_file.read(3)
48294829
# First read raises a timeout
4830-
self.assertRaises(socket.timeout, self.read_file.read, 1)
4830+
self.assertRaises(TimeoutError, self.read_file.read, 1)
48314831
# Second read is disallowed
48324832
with self.assertRaises(OSError) as ctx:
48334833
self.read_file.read(1)
@@ -5092,7 +5092,7 @@ class NetworkConnectionNoServer(unittest.TestCase):
50925092

50935093
class MockSocket(socket.socket):
50945094
def connect(self, *args):
5095-
raise socket.timeout('timed out')
5095+
raise TimeoutError('timed out')
50965096

50975097
@contextlib.contextmanager
50985098
def mocked_socket_module(self):
@@ -5142,13 +5142,13 @@ def test_create_connection_timeout(self):
51425142
with self.mocked_socket_module():
51435143
try:
51445144
socket.create_connection((HOST, 1234))
5145-
except socket.timeout:
5145+
except TimeoutError:
51465146
pass
51475147
except OSError as exc:
51485148
if socket_helper.IPV6_ENABLED or exc.errno != errno.EAFNOSUPPORT:
51495149
raise
51505150
else:
5151-
self.fail('socket.timeout not raised')
5151+
self.fail('TimeoutError not raised')
51525152

51535153

51545154
class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest):
@@ -5250,7 +5250,7 @@ def _testInsideTimeout(self):
52505250

52515251
def _testOutsideTimeout(self):
52525252
self.cli = sock = socket.create_connection((HOST, self.port), timeout=1)
5253-
self.assertRaises(socket.timeout, lambda: sock.recv(5))
5253+
self.assertRaises(TimeoutError, lambda: sock.recv(5))
52545254

52555255

52565256
class TCPTimeoutTest(SocketTCPTest):
@@ -5259,15 +5259,15 @@ def testTCPTimeout(self):
52595259
def raise_timeout(*args, **kwargs):
52605260
self.serv.settimeout(1.0)
52615261
self.serv.accept()
5262-
self.assertRaises(socket.timeout, raise_timeout,
5262+
self.assertRaises(TimeoutError, raise_timeout,
52635263
"Error generating a timeout exception (TCP)")
52645264

52655265
def testTimeoutZero(self):
52665266
ok = False
52675267
try:
52685268
self.serv.settimeout(0.0)
52695269
foo = self.serv.accept()
5270-
except socket.timeout:
5270+
except TimeoutError:
52715271
self.fail("caught timeout instead of error (TCP)")
52725272
except OSError:
52735273
ok = True
@@ -5292,7 +5292,7 @@ def alarm_handler(signal, frame):
52925292
try:
52935293
signal.alarm(2) # POSIX allows alarm to be up to 1 second early
52945294
foo = self.serv.accept()
5295-
except socket.timeout:
5295+
except TimeoutError:
52965296
self.fail("caught timeout instead of Alarm")
52975297
except Alarm:
52985298
pass
@@ -5316,15 +5316,15 @@ def testUDPTimeout(self):
53165316
def raise_timeout(*args, **kwargs):
53175317
self.serv.settimeout(1.0)
53185318
self.serv.recv(1024)
5319-
self.assertRaises(socket.timeout, raise_timeout,
5319+
self.assertRaises(TimeoutError, raise_timeout,
53205320
"Error generating a timeout exception (UDP)")
53215321

53225322
def testTimeoutZero(self):
53235323
ok = False
53245324
try:
53255325
self.serv.settimeout(0.0)
53265326
foo = self.serv.recv(1024)
5327-
except socket.timeout:
5327+
except TimeoutError:
53285328
self.fail("caught timeout instead of error (UDP)")
53295329
except OSError:
53305330
ok = True
@@ -5341,15 +5341,15 @@ def testUDPLITETimeout(self):
53415341
def raise_timeout(*args, **kwargs):
53425342
self.serv.settimeout(1.0)
53435343
self.serv.recv(1024)
5344-
self.assertRaises(socket.timeout, raise_timeout,
5344+
self.assertRaises(TimeoutError, raise_timeout,
53455345
"Error generating a timeout exception (UDPLITE)")
53465346

53475347
def testTimeoutZero(self):
53485348
ok = False
53495349
try:
53505350
self.serv.settimeout(0.0)
53515351
foo = self.serv.recv(1024)
5352-
except socket.timeout:
5352+
except TimeoutError:
53535353
self.fail("caught timeout instead of error (UDPLITE)")
53545354
except OSError:
53555355
ok = True
@@ -5365,6 +5365,8 @@ def testExceptionTree(self):
53655365
self.assertTrue(issubclass(socket.herror, OSError))
53665366
self.assertTrue(issubclass(socket.gaierror, OSError))
53675367
self.assertTrue(issubclass(socket.timeout, OSError))
5368+
self.assertIs(socket.error, OSError)
5369+
self.assertIs(socket.timeout, TimeoutError)
53685370

53695371
def test_setblocking_invalidfd(self):
53705372
# Regression test for issue #28471
@@ -6167,7 +6169,7 @@ def _testWithTimeoutTriggeredSend(self):
61676169
with socket.create_connection(address) as sock:
61686170
sock.settimeout(0.01)
61696171
meth = self.meth_from_sock(sock)
6170-
self.assertRaises(socket.timeout, meth, file)
6172+
self.assertRaises(TimeoutError, meth, file)
61716173

61726174
def testWithTimeoutTriggeredSend(self):
61736175
conn = self.accept_conn()

Lib/test/test_ssl.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2574,7 +2574,7 @@ def run(self):
25742574
handler = self.ConnectionHandler(self, newconn, connaddr)
25752575
handler.start()
25762576
handler.join()
2577-
except socket.timeout:
2577+
except TimeoutError:
25782578
pass
25792579
except KeyboardInterrupt:
25802580
self.stop()
@@ -3691,7 +3691,7 @@ def serve():
36913691
c.settimeout(0.2)
36923692
c.connect((host, port))
36933693
# Will attempt handshake and time out
3694-
self.assertRaisesRegex(socket.timeout, "timed out",
3694+
self.assertRaisesRegex(TimeoutError, "timed out",
36953695
test_wrap_socket, c)
36963696
finally:
36973697
c.close()
@@ -3700,7 +3700,7 @@ def serve():
37003700
c = test_wrap_socket(c)
37013701
c.settimeout(0.2)
37023702
# Will attempt handshake and time out
3703-
self.assertRaisesRegex(socket.timeout, "timed out",
3703+
self.assertRaisesRegex(TimeoutError, "timed out",
37043704
c.connect, (host, port))
37053705
finally:
37063706
c.close()

Lib/test/test_telnetlib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def server(evt, serv):
1616
try:
1717
conn, addr = serv.accept()
1818
conn.close()
19-
except socket.timeout:
19+
except TimeoutError:
2020
pass
2121
finally:
2222
serv.close()

0 commit comments

Comments
 (0)
0