Skip to content

Commit

Permalink
Convert IOError/OSError handling for Python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
nateprewitt committed Mar 29, 2022
1 parent 35c9e70 commit edd6c3c
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
8 changes: 4 additions & 4 deletions requests/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def cert_verify(self, conn, url, verify, cert):
cert_loc = extract_zipped_paths(DEFAULT_CA_BUNDLE_PATH)

if not cert_loc or not os.path.exists(cert_loc):
raise IOError(
raise OSError(
f"Could not find a suitable TLS CA certificate bundle, "
f"invalid path: {cert_loc}"
)
Expand All @@ -284,12 +284,12 @@ def cert_verify(self, conn, url, verify, cert):
conn.cert_file = cert
conn.key_file = None
if conn.cert_file and not os.path.exists(conn.cert_file):
raise IOError(
raise OSError(
f"Could not find the TLS certificate file, "
f"invalid path: {conn.cert_file}"
)
if conn.key_file and not os.path.exists(conn.key_file):
raise IOError(
raise OSError(
f"Could not find the TLS key file, invalid path: {conn.key_file}"
)

Expand Down Expand Up @@ -543,7 +543,7 @@ def send(
low_conn.close()
raise

except (ProtocolError, socket.error) as err:
except (ProtocolError, OSError) as err:
raise ConnectionError(err, request=request)

except MaxRetryError as e:
Expand Down
2 changes: 1 addition & 1 deletion requests/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def info():
"system": platform.system(),
"release": platform.release(),
}
except IOError:
except OSError:
platform_info = {
"system": "Unknown",
"release": "Unknown",
Expand Down
2 changes: 1 addition & 1 deletion requests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ def prepare_body(self, data, files, json=None):
# of a redirect.
try:
self._body_position = body.tell()
except (IOError, OSError):
except OSError:
# This differentiates from None, allowing us to catch
# a failed `tell()` later when trying to rewind the body
self._body_position = object()
Expand Down
12 changes: 6 additions & 6 deletions requests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def super_len(o):
if hasattr(o, "tell"):
try:
current_position = o.tell()
except (OSError, IOError):
except OSError:
# This can happen in some weird situations, such as when the file
# is actually a special file descriptor like stdin. In this
# instance, we don't know what the length is, so set it to zero and
Expand All @@ -182,7 +182,7 @@ def super_len(o):
# seek back to current position to support
# partially read file-like objects
o.seek(current_position or 0)
except (OSError, IOError):
except OSError:
total_length = 0

if total_length is None:
Expand Down Expand Up @@ -237,7 +237,7 @@ def get_netrc_auth(url, raise_errors=False):
# Return with login / password
login_i = 0 if _netrc[0] else 1
return (_netrc[login_i], _netrc[2])
except (NetrcParseError, IOError):
except (NetrcParseError, OSError):
# If there was a parsing error or a permissions issue reading the file,
# we'll just skip netrc auth unless explicitly asked to raise errors.
if raise_errors:
Expand Down Expand Up @@ -705,7 +705,7 @@ def is_ipv4_address(string_ip):
"""
try:
socket.inet_aton(string_ip)
except socket.error:
except OSError:
return False
return True

Expand All @@ -727,7 +727,7 @@ def is_valid_cidr(string_network):

try:
socket.inet_aton(string_network.split("/")[0])
except socket.error:
except OSError:
return False
else:
return False
Expand Down Expand Up @@ -1080,7 +1080,7 @@ def rewind_body(prepared_request):
):
try:
body_seek(prepared_request._body_position)
except (IOError, OSError):
except OSError:
raise UnrewindableBodyError(
"An error occurred when rewinding request body for redirect."
)
Expand Down
4 changes: 2 additions & 2 deletions tests/testserver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def _create_socket_and_bind(self):
def _close_server_sock_ignore_errors(self):
try:
self.server_sock.close()
except IOError:
except OSError:
pass

def _handle_requests(self):
Expand All @@ -110,7 +110,7 @@ def _accept_connection(self):
return None

return self.server_sock.accept()[0]
except (select.error, socket.error):
except OSError:
return None

def __enter__(self):
Expand Down

0 comments on commit edd6c3c

Please sign in to comment.