Skip to content
Closed
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
1 change: 0 additions & 1 deletion sentry_sdk/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
T = TypeVar("T")


PY37 = sys.version_info[0] == 3 and sys.version_info[1] >= 7
PY38 = sys.version_info[0] == 3 and sys.version_info[1] >= 8
PY310 = sys.version_info[0] == 3 and sys.version_info[1] >= 10
PY311 = sys.version_info[0] == 3 and sys.version_info[1] >= 11
Expand Down
6 changes: 1 addition & 5 deletions sentry_sdk/_types.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
try:
from re import Pattern
except ImportError:
# 3.6
from typing import Pattern
from re import Pattern

from typing import TYPE_CHECKING, TypeVar, Union

Expand Down
7 changes: 1 addition & 6 deletions sentry_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,7 @@ def _get_options(*args: "Optional[str]", **kwargs: "Any") -> "Dict[str, Any]":
return rv


try:
# Python 3.6+
module_not_found_error = ModuleNotFoundError
except Exception:
# Older Python versions
module_not_found_error = ImportError # type: ignore
module_not_found_error = ModuleNotFoundError


class BaseClient:
Expand Down
7 changes: 1 addition & 6 deletions sentry_sdk/integrations/_wsgi_common.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from contextlib import contextmanager
from contextlib import nullcontext
import json
from copy import deepcopy

Expand Down Expand Up @@ -52,11 +52,6 @@
)


# This noop context manager can be replaced with "from contextlib import nullcontext" when we drop Python 3.6 support
@contextmanager
def nullcontext() -> "Iterator[None]":
yield


def request_body_within_bounds(
client: "Optional[sentry_sdk.client.BaseClient]", content_length: int
Expand Down
86 changes: 26 additions & 60 deletions sentry_sdk/integrations/aws_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,76 +214,42 @@ def setup_once() -> None:
)
return

pre_37 = hasattr(lambda_bootstrap, "handle_http_request") # Python 3.6
lambda_bootstrap.LambdaRuntimeClient.post_init_error = _wrap_init_error(
lambda_bootstrap.LambdaRuntimeClient.post_init_error
)

if pre_37:
old_handle_event_request = lambda_bootstrap.handle_event_request
old_handle_event_request = lambda_bootstrap.handle_event_request

def sentry_handle_event_request(
request_handler: "Any", *args: "Any", **kwargs: "Any"
) -> "Any":
request_handler = _wrap_handler(request_handler)
return old_handle_event_request(request_handler, *args, **kwargs)

lambda_bootstrap.handle_event_request = sentry_handle_event_request

old_handle_http_request = lambda_bootstrap.handle_http_request

def sentry_handle_http_request(
request_handler: "Any", *args: "Any", **kwargs: "Any"
) -> "Any":
request_handler = _wrap_handler(request_handler)
return old_handle_http_request(request_handler, *args, **kwargs)

lambda_bootstrap.handle_http_request = sentry_handle_http_request

# Patch to_json to drain the queue. This should work even when the
# SDK is initialized inside of the handler

old_to_json = lambda_bootstrap.to_json

def sentry_to_json(*args: "Any", **kwargs: "Any") -> "Any":
_drain_queue()
return old_to_json(*args, **kwargs)

lambda_bootstrap.to_json = sentry_to_json
else:
lambda_bootstrap.LambdaRuntimeClient.post_init_error = _wrap_init_error(
lambda_bootstrap.LambdaRuntimeClient.post_init_error
)

old_handle_event_request = lambda_bootstrap.handle_event_request

def sentry_handle_event_request( # type: ignore
def sentry_handle_event_request( # type: ignore
lambda_runtime_client, request_handler, *args, **kwargs
):
request_handler = _wrap_handler(request_handler)
return old_handle_event_request(
lambda_runtime_client, request_handler, *args, **kwargs
):
request_handler = _wrap_handler(request_handler)
return old_handle_event_request(
lambda_runtime_client, request_handler, *args, **kwargs
)
)

lambda_bootstrap.handle_event_request = sentry_handle_event_request
lambda_bootstrap.handle_event_request = sentry_handle_event_request

# Patch the runtime client to drain the queue. This should work
# even when the SDK is initialized inside of the handler
# Patch the runtime client to drain the queue. This should work
# even when the SDK is initialized inside of the handler

def _wrap_post_function(f: "F") -> "F":
def inner(*args: "Any", **kwargs: "Any") -> "Any":
_drain_queue()
return f(*args, **kwargs)
def _wrap_post_function(f: "F") -> "F":
def inner(*args: "Any", **kwargs: "Any") -> "Any":
_drain_queue()
return f(*args, **kwargs)

return inner # type: ignore
return inner # type: ignore

lambda_bootstrap.LambdaRuntimeClient.post_invocation_result = (
_wrap_post_function(
lambda_bootstrap.LambdaRuntimeClient.post_invocation_result
)
lambda_bootstrap.LambdaRuntimeClient.post_invocation_result = (
_wrap_post_function(
lambda_bootstrap.LambdaRuntimeClient.post_invocation_result
)
lambda_bootstrap.LambdaRuntimeClient.post_invocation_error = (
_wrap_post_function(
lambda_bootstrap.LambdaRuntimeClient.post_invocation_error
)
)
lambda_bootstrap.LambdaRuntimeClient.post_invocation_error = (
_wrap_post_function(
lambda_bootstrap.LambdaRuntimeClient.post_invocation_error
)
)


def get_lambda_bootstrap() -> "Optional[Any]":
Expand Down
6 changes: 1 addition & 5 deletions sentry_sdk/tracing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@
from urllib.parse import quote, unquote
import uuid

try:
from re import Pattern
except ImportError:
# 3.6
from typing import Pattern
from re import Pattern

import sentry_sdk
from sentry_sdk.consts import OP, SPANDATA, SPANSTATUS, SPANTEMPLATE
Expand Down
16 changes: 4 additions & 12 deletions sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
from typing import TYPE_CHECKING

import sentry_sdk
from sentry_sdk._compat import PY37
from sentry_sdk._types import SENSITIVE_DATA_SUBSTITUTE, Annotated, AnnotatedValue
from sentry_sdk.consts import (
DEFAULT_ADD_FULL_STACK,
Expand Down Expand Up @@ -1302,8 +1301,8 @@ def _is_contextvars_broken() -> bool:
# that case, check if contextvars are effectively patched.
if (
# Gevent 20.9.0+
(sys.version_info >= (3, 7) and version_tuple >= (20, 9))
# Gevent 20.5.0+ or Python < 3.7
version_tuple >= (20, 9)
# Gevent 20.5.0+ with patched contextvars
or (is_object_patched("contextvars", "ContextVar"))
):
return False
Expand Down Expand Up @@ -1848,15 +1847,8 @@ def runner(*args: "P.args", **kwargs: "P.kwargs") -> "R":
return patcher


if PY37:

def nanosecond_time() -> int:
return time.perf_counter_ns()

else:

def nanosecond_time() -> int:
return int(time.perf_counter() * 1e9)
def nanosecond_time() -> int:
return time.perf_counter_ns()


def now() -> float:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_gevent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from datetime import datetime, timezone

import sentry_sdk
from sentry_sdk._compat import PY37, PY38
from sentry_sdk._compat import PY38

import pytest
from tests.conftest import CapturingServer
Expand Down Expand Up @@ -50,7 +50,7 @@ def inner(**kwargs):
@pytest.mark.parametrize("compression_level", (0, 9, None))
@pytest.mark.parametrize(
"compression_algo",
(("gzip", "br", "<invalid>", None) if PY37 else ("gzip", "<invalid>", None)),
("gzip", "br", "<invalid>", None),
)
@pytest.mark.parametrize("http2", [True, False] if PY38 else [False])
def test_transport_works_gevent(
Expand Down
4 changes: 2 additions & 2 deletions tests/test_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
get_isolation_scope,
Hub,
)
from sentry_sdk._compat import PY37, PY38
from sentry_sdk._compat import PY38
from sentry_sdk.envelope import Envelope, Item, parse_json, PayloadRef
from sentry_sdk.transport import (
KEEP_ALIVE_SOCKET_OPTIONS,
Expand Down Expand Up @@ -82,7 +82,7 @@ def mock_transaction_envelope(span_count: int) -> "Envelope":
@pytest.mark.parametrize("compression_level", (0, 9, None))
@pytest.mark.parametrize(
"compression_algo",
(("gzip", "br", "<invalid>", None) if PY37 else ("gzip", "<invalid>", None)),
("gzip", "br", "<invalid>", None),
)
@pytest.mark.parametrize("http2", [True, False] if PY38 else [False])
def test_transport_works(
Expand Down
Loading