Replace deprecated socket.error with OSError
MODIFY
src/urllib3/connection.py
src/urllib3/connection.py
@@ -6,7 +6,6 @@ import socket
import warnings
from http.client import HTTPConnection as _HTTPConnection
from http.client import HTTPException # noqa: F401
-from socket import error as SocketError
from socket import timeout as SocketTimeout
from .util.proxy import create_proxy_ssl_context
@@ -156,7 +155,7 @@ class HTTPConnection(_HTTPConnection):
f"Connection to {self.host} timed out. (connect timeout={self.timeout})",
)
- except SocketError as e:
+ except OSError as e:
raise NewConnectionError(self, f"Failed to establish a new connection: {e}")
return conn

MODIFY
src/urllib3/connectionpool.py
src/urllib3/connectionpool.py
@@ -4,7 +4,6 @@ import queue
import socket
import sys
import warnings
-from socket import error as SocketError
from socket import timeout as SocketTimeout
from .connection import (
@@ -398,7 +397,7 @@ class HTTPConnectionPool(ConnectionPool, RequestMethods):
# Receive the response from the server
try:
httplib_response = conn.getresponse()
- except (SocketTimeout, BaseSSLError, SocketError) as e:
+ except (BaseSSLError, OSError) as e:
self._raise_timeout(err=e, url=url, timeout_value=read_timeout)
raise
@@ -690,7 +689,7 @@ class HTTPConnectionPool(ConnectionPool, RequestMethods):
except (
TimeoutError,
HTTPException,
- SocketError,
+ OSError,
ProtocolError,
BaseSSLError,
SSLError,
@@ -701,9 +700,9 @@ class HTTPConnectionPool(ConnectionPool, RequestMethods):
clean_exit = False
if isinstance(e, (BaseSSLError, CertificateError)):
e = SSLError(e)
- elif isinstance(e, (SocketError, NewConnectionError)) and self.proxy:
+ elif isinstance(e, (OSError, NewConnectionError)) and self.proxy:
e = ProxyError("Cannot connect to proxy.", e)
- elif isinstance(e, (SocketError, HTTPException)):
+ elif isinstance(e, (OSError, HTTPException)):
e = ProtocolError("Connection aborted.", e)
retries = retries.increment(

MODIFY
src/urllib3/contrib/pyopenssl.py
src/urllib3/contrib/pyopenssl.py
@@ -62,7 +62,6 @@ except ImportError:
import logging
import ssl
from io import BytesIO
-from socket import error as SocketError
from socket import socket as socket_cls
from socket import timeout
@@ -279,7 +278,7 @@ class WrappedSocket:
if self.suppress_ragged_eofs and e.args == (-1, "Unexpected EOF"):
return b""
else:
- raise SocketError(e.args[0], str(e))
+ raise OSError(e.args[0], str(e))
except OpenSSL.SSL.ZeroReturnError:
if self.connection.get_shutdown() == OpenSSL.SSL.RECEIVED_SHUTDOWN:
return b""
@@ -304,7 +303,7 @@ class WrappedSocket:
if self.suppress_ragged_eofs and e.args == (-1, "Unexpected EOF"):
return 0
else:
- raise SocketError(e.args[0], str(e))
+ raise OSError(e.args[0], str(e))
except OpenSSL.SSL.ZeroReturnError:
if self.connection.get_shutdown() == OpenSSL.SSL.RECEIVED_SHUTDOWN:
return 0
@@ -332,7 +331,7 @@ class WrappedSocket:
raise timeout()
continue
except OpenSSL.SSL.SysCallError as e:
- raise SocketError(e.args[0], str(e))
+ raise OSError(e.args[0], str(e))
def sendall(self, data):
total_sent = 0

MODIFY
src/urllib3/contrib/socks.py
src/urllib3/contrib/socks.py
@@ -55,7 +55,6 @@ except ImportError:
)
raise
-from socket import error as SocketError
from socket import timeout as SocketTimeout
from ..connection import HTTPConnection, HTTPSConnection
@@ -128,7 +127,7 @@ class SOCKSConnection(HTTPConnection):
self, f"Failed to establish a new connection: {e}"
)
- except SocketError as e: # Defensive: PySocks should catch all these.
+ except OSError as e: # Defensive: PySocks should catch all these.
raise NewConnectionError(self, f"Failed to establish a new connection: {e}")
return conn

MODIFY
src/urllib3/response.py
src/urllib3/response.py
@@ -3,7 +3,6 @@ import logging
import typing
import zlib
from contextlib import contextmanager
-from socket import error as SocketError
from socket import timeout as SocketTimeout
try:
@@ -441,7 +440,7 @@ class HTTPResponse(BaseHTTPResponse):
"""
try:
self.read()
- except (HTTPError, SocketError, BaseSSLError, HTTPException):
+ except (HTTPError, OSError, BaseSSLError, HTTPException):
pass
@property
@@ -548,7 +547,7 @@ class HTTPResponse(BaseHTTPResponse):
raise ReadTimeoutError(self._pool, None, "Read timed out.")
- except (HTTPException, SocketError) as e:
+ except (HTTPException, OSError) as e:
# This includes IncompleteRead.
raise ProtocolError(f"Connection broken: {e!r}", e)

Replace deprecated socket.error with OSError