Skip to content

Commit

Permalink
Handle os.path.devnull access issues (#2579)
Browse files Browse the repository at this point in the history
Our release checking can fail because os.path.devnull is not there/is not properly accessible on some setups.
  • Loading branch information
sentrivana authored Dec 11, 2023
1 parent 4108662 commit ddf37a3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
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

0 comments on commit ddf37a3

Please sign in to comment.