Skip to content

Handle os.path.devnull access issues #2579

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

Merged
merged 4 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from urllib.parse import urlencode
from urllib.parse import urlsplit
from urllib.parse import urlunsplit

except ImportError:
# Python 2
from cgi import parse_qs # type: ignore
Expand All @@ -30,6 +29,13 @@
from urlparse import urlsplit # type: ignore
from urlparse import urlunsplit # type: ignore

try:
# Python 3
FileNotFoundError
except NameError:
# Python 2
FileNotFoundError = IOError

try:
# Python 3.11
from builtins import BaseExceptionGroup
Expand Down Expand Up @@ -97,8 +103,8 @@ def _get_debug_hub():

def get_git_revision():
# type: () -> Optional[str]
with open(os.path.devnull, "w+") as null:
try:
try:
with open(os.path.devnull, "w+") as null:
revision = (
subprocess.Popen(
["git", "rev-parse", "HEAD"],
Expand All @@ -110,8 +116,8 @@ def get_git_revision():
.strip()
.decode("utf-8")
)
except (OSError, IOError):
return None
except (OSError, IOError, FileNotFoundError):
return None

return revision

Expand Down
22 changes: 22 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Components,
Dsn,
get_error_message,
get_git_revision,
is_valid_sample_rate,
logger,
match_regex_list,
Expand All @@ -25,6 +26,13 @@
except ImportError:
import mock # python < 3.3

try:
# Python 3
FileNotFoundError
except NameError:
# Python 2
FileNotFoundError = IOError


def _normalize_distribution_name(name):
# type: (str) -> str
Expand Down Expand Up @@ -557,3 +565,17 @@ def test_installed_modules_caching():

_get_installed_modules()
mock_generate_installed_modules.assert_not_called()


def test_devnull_inaccessible():
with mock.patch("sentry_sdk.utils.open", side_effect=OSError("oh no")):
revision = get_git_revision()

assert revision is None


def test_devnull_not_found():
with mock.patch("sentry_sdk.utils.open", side_effect=FileNotFoundError("oh no")):
revision = get_git_revision()

assert revision is None