Skip to content

Commit 76b413a

Browse files
authored
Performance optimizations (#1725)
* Made function faster
1 parent fa1b964 commit 76b413a

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

sentry_sdk/_compat.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
PY2 = sys.version_info[0] == 2
1616
PY33 = sys.version_info[0] == 3 and sys.version_info[1] >= 3
1717
PY37 = sys.version_info[0] == 3 and sys.version_info[1] >= 7
18+
PY310 = sys.version_info[0] == 3 and sys.version_info[1] >= 10
1819

1920
if PY2:
2021
import urlparse

sentry_sdk/integrations/django/signals_handlers.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@ def _get_receiver_name(receiver):
1919
name = ""
2020

2121
if hasattr(receiver, "__qualname__"):
22-
name += receiver.__qualname__
22+
name = receiver.__qualname__
2323
elif hasattr(receiver, "__name__"): # Python 2.7 has no __qualname__
24-
name += receiver.__name__
24+
name = receiver.__name__
25+
elif hasattr(
26+
receiver, "func"
27+
): # certain functions (like partials) dont have a name
28+
name = "partial(<function " + receiver.func.__name__ + ">)" # type: ignore
2529

2630
if (
2731
name == ""
28-
): # certain functions (like partials) dont have a name so return the string representation
32+
): # In case nothing was found, return the string representation (this is the slowest case)
2933
return str(receiver)
3034

3135
if hasattr(receiver, "__module__"): # prepend with module, if there is one

test-requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ Werkzeug<2.1.0
1010
jsonschema==3.2.0
1111
pyrsistent==0.16.0 # TODO(py3): 0.17.0 requires python3, see https://github.com/tobgu/pyrsistent/issues/205
1212
executing
13-
asttokens
13+
asttokens
14+
ipdb

tests/integrations/django/test_basic.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
except ImportError:
1717
from django.core.urlresolvers import reverse
1818

19-
from sentry_sdk._compat import PY2
19+
from sentry_sdk._compat import PY2, PY310
2020
from sentry_sdk import capture_message, capture_exception, configure_scope
2121
from sentry_sdk.integrations.django import DjangoIntegration
2222
from sentry_sdk.integrations.django.signals_handlers import _get_receiver_name
@@ -834,4 +834,7 @@ def dummy(a, b):
834834

835835
a_partial = partial(dummy)
836836
name = _get_receiver_name(a_partial)
837-
assert name == str(a_partial)
837+
if PY310:
838+
assert name == "functools.partial(<function " + a_partial.func.__name__ + ">)"
839+
else:
840+
assert name == "partial(<function " + a_partial.func.__name__ + ">)"

0 commit comments

Comments
 (0)