Skip to content

Commit e208cbd

Browse files
ericapisaniclaude
andcommitted
ref: Remove more residual Python 2 compatibility code
Follow-up to 25baa75. Removes dead-code guards that only existed to tolerate Python 2 stdlib differences: - try/except AttributeError around nanosecond_time() in Span and StreamedSpan. time.perf_counter has existed since Python 3.3 and time.perf_counter_ns since 3.7, so the exception can never fire on any supported Python 3 version. - try/except ImportError around asyncio.iscoroutinefunction in the Django integration. asyncio.iscoroutinefunction has existed since Python 3.4. - Redundant IOError in exception tuples. IOError is an alias for OSError in Python 3. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent c54e374 commit e208cbd

4 files changed

Lines changed: 21 additions & 35 deletions

File tree

sentry_sdk/integrations/django/views.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99
from typing import Any
1010

1111

12-
try:
13-
from asyncio import iscoroutinefunction
14-
except ImportError:
15-
iscoroutinefunction = None # type: ignore
12+
from asyncio import iscoroutinefunction
1613

1714

1815
try:
@@ -48,10 +45,8 @@ def sentry_patched_make_view_atomic(
4845

4946
integration = sentry_sdk.get_client().get_integration(DjangoIntegration)
5047
if integration is not None:
51-
is_async_view = (
52-
iscoroutinefunction is not None
53-
and wrap_async_view is not None
54-
and iscoroutinefunction(callback)
48+
is_async_view = wrap_async_view is not None and iscoroutinefunction(
49+
callback
5550
)
5651
if is_async_view:
5752
sentry_wrapped_callback = wrap_async_view(callback)

sentry_sdk/traces.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,9 @@ def __init__(
278278
self._start_timestamp = datetime.now(timezone.utc)
279279
self._timestamp: "Optional[datetime]" = None
280280

281-
try:
282-
# profiling depends on this value and requires that
283-
# it is measured in nanoseconds
284-
self._start_timestamp_monotonic_ns = nanosecond_time()
285-
except AttributeError:
286-
pass
281+
# profiling depends on this value and requires that
282+
# it is measured in nanoseconds
283+
self._start_timestamp_monotonic_ns = nanosecond_time()
287284

288285
self._span_id: "Optional[str]" = None
289286

sentry_sdk/tracing.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -327,12 +327,9 @@ def __init__(
327327
elif isinstance(start_timestamp, float):
328328
start_timestamp = datetime.fromtimestamp(start_timestamp, timezone.utc)
329329
self.start_timestamp = start_timestamp
330-
try:
331-
# profiling depends on this value and requires that
332-
# it is measured in nanoseconds
333-
self._start_timestamp_monotonic_ns = nanosecond_time()
334-
except AttributeError:
335-
pass
330+
# profiling depends on this value and requires that
331+
# it is measured in nanoseconds
332+
self._start_timestamp_monotonic_ns = nanosecond_time()
336333

337334
#: End timestamp of span
338335
self.timestamp: "Optional[datetime]" = None
@@ -661,18 +658,15 @@ def finish(
661658
# This span is already finished, ignore.
662659
return None
663660

664-
try:
665-
if end_timestamp:
666-
if isinstance(end_timestamp, float):
667-
end_timestamp = datetime.fromtimestamp(end_timestamp, timezone.utc)
668-
self.timestamp = end_timestamp
669-
else:
670-
elapsed = nanosecond_time() - self._start_timestamp_monotonic_ns
671-
self.timestamp = self.start_timestamp + timedelta(
672-
microseconds=elapsed / 1000
673-
)
674-
except AttributeError:
675-
self.timestamp = datetime.now(timezone.utc)
661+
if end_timestamp:
662+
if isinstance(end_timestamp, float):
663+
end_timestamp = datetime.fromtimestamp(end_timestamp, timezone.utc)
664+
self.timestamp = end_timestamp
665+
else:
666+
elapsed = nanosecond_time() - self._start_timestamp_monotonic_ns
667+
self.timestamp = self.start_timestamp + timedelta(
668+
microseconds=elapsed / 1000
669+
)
676670

677671
scope = scope or sentry_sdk.get_current_scope()
678672

sentry_sdk/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def get_git_revision() -> "Optional[str]":
159159
.strip()
160160
.decode("utf-8")
161161
)
162-
except (OSError, IOError, FileNotFoundError):
162+
except OSError:
163163
return None
164164

165165
return revision
@@ -492,15 +492,15 @@ def get_lines_from_file(
492492
if loader is not None and hasattr(loader, "get_source"):
493493
try:
494494
source_str: "Optional[str]" = loader.get_source(module)
495-
except (ImportError, IOError):
495+
except (ImportError, OSError):
496496
source_str = None
497497
if source_str is not None:
498498
source = source_str.splitlines()
499499

500500
if source is None:
501501
try:
502502
source = linecache.getlines(filename)
503-
except (OSError, IOError):
503+
except OSError:
504504
return [], None, []
505505

506506
if not source:

0 commit comments

Comments
 (0)