Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use contextlib.suppress instead of empty except #6185

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions requests/__init__.py
Expand Up @@ -38,6 +38,7 @@
:license: Apache 2.0, see LICENSE for more details.
"""

import contextlib
import warnings

import urllib3
Expand Down Expand Up @@ -117,7 +118,7 @@ def _check_cryptography(cryptography_version):
# Attempt to enable urllib3's fallback for SNI support
# if the standard library doesn't support SNI or the
# 'ssl' library isn't available.
try:
with contextlib.suppress(ImportError):
try:
import ssl
except ImportError:
Expand All @@ -132,8 +133,6 @@ def _check_cryptography(cryptography_version):
from cryptography import __version__ as cryptography_version

_check_cryptography(cryptography_version)
except ImportError:
pass

# urllib3's DependencyWarnings should be silenced.
from urllib3.exceptions import DependencyWarning
Expand Down
5 changes: 2 additions & 3 deletions requests/sessions.py
Expand Up @@ -5,6 +5,7 @@
This module provides a Session object to manage and persist settings across
requests (cookies, auth, proxies).
"""
import contextlib
import os
import sys
import time
Expand Down Expand Up @@ -734,12 +735,10 @@ def send(self, request, **kwargs):

# If redirects aren't being followed, store the response on the Request for Response.next().
if not allow_redirects:
try:
with contextlib.suppress(StopIteration):
r._next = next(
self.resolve_redirects(r, request, yield_requests=True, **kwargs)
)
except StopIteration:
pass

if not stream:
r.content
Expand Down
7 changes: 2 additions & 5 deletions requests/utils.py
Expand Up @@ -200,7 +200,8 @@ def get_netrc_auth(url, raise_errors=False):
else:
netrc_locations = (f"~/{f}" for f in NETRC_FILES)

try:
# App Engine hack requires suppressing these errors
with contextlib.suppress(ImportError, AttributeError):
from netrc import NetrcParseError, netrc

netrc_path = None
Expand Down Expand Up @@ -243,10 +244,6 @@ def get_netrc_auth(url, raise_errors=False):
if raise_errors:
raise

# App Engine hackiness.
except (ImportError, AttributeError):
pass


def guess_filename(obj):
"""Tries to guess the filename of the given object."""
Expand Down