Skip to content

Commit ddf37a3

Browse files
authored
Handle os.path.devnull access issues (#2579)
Our release checking can fail because os.path.devnull is not there/is not properly accessible on some setups.
1 parent 4108662 commit ddf37a3

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

sentry_sdk/utils.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from urllib.parse import urlencode
2222
from urllib.parse import urlsplit
2323
from urllib.parse import urlunsplit
24-
2524
except ImportError:
2625
# Python 2
2726
from cgi import parse_qs # type: ignore
@@ -30,6 +29,13 @@
3029
from urlparse import urlsplit # type: ignore
3130
from urlparse import urlunsplit # type: ignore
3231

32+
try:
33+
# Python 3
34+
FileNotFoundError
35+
except NameError:
36+
# Python 2
37+
FileNotFoundError = IOError
38+
3339
try:
3440
# Python 3.11
3541
from builtins import BaseExceptionGroup
@@ -97,8 +103,8 @@ def _get_debug_hub():
97103

98104
def get_git_revision():
99105
# type: () -> Optional[str]
100-
with open(os.path.devnull, "w+") as null:
101-
try:
106+
try:
107+
with open(os.path.devnull, "w+") as null:
102108
revision = (
103109
subprocess.Popen(
104110
["git", "rev-parse", "HEAD"],
@@ -110,8 +116,8 @@ def get_git_revision():
110116
.strip()
111117
.decode("utf-8")
112118
)
113-
except (OSError, IOError):
114-
return None
119+
except (OSError, IOError, FileNotFoundError):
120+
return None
115121

116122
return revision
117123

tests/test_utils.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
Components,
77
Dsn,
88
get_error_message,
9+
get_git_revision,
910
is_valid_sample_rate,
1011
logger,
1112
match_regex_list,
@@ -25,6 +26,13 @@
2526
except ImportError:
2627
import mock # python < 3.3
2728

29+
try:
30+
# Python 3
31+
FileNotFoundError
32+
except NameError:
33+
# Python 2
34+
FileNotFoundError = IOError
35+
2836

2937
def _normalize_distribution_name(name):
3038
# type: (str) -> str
@@ -557,3 +565,17 @@ def test_installed_modules_caching():
557565

558566
_get_installed_modules()
559567
mock_generate_installed_modules.assert_not_called()
568+
569+
570+
def test_devnull_inaccessible():
571+
with mock.patch("sentry_sdk.utils.open", side_effect=OSError("oh no")):
572+
revision = get_git_revision()
573+
574+
assert revision is None
575+
576+
577+
def test_devnull_not_found():
578+
with mock.patch("sentry_sdk.utils.open", side_effect=FileNotFoundError("oh no")):
579+
revision = get_git_revision()
580+
581+
assert revision is None

0 commit comments

Comments
 (0)