Skip to content

Commit

Permalink
Performance optimizations (#1725)
Browse files Browse the repository at this point in the history
* Made function faster
  • Loading branch information
antonpirker authored Nov 4, 2022
1 parent fa1b964 commit 76b413a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
1 change: 1 addition & 0 deletions sentry_sdk/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
PY2 = sys.version_info[0] == 2
PY33 = sys.version_info[0] == 3 and sys.version_info[1] >= 3
PY37 = sys.version_info[0] == 3 and sys.version_info[1] >= 7
PY310 = sys.version_info[0] == 3 and sys.version_info[1] >= 10

if PY2:
import urlparse
Expand Down
10 changes: 7 additions & 3 deletions sentry_sdk/integrations/django/signals_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ def _get_receiver_name(receiver):
name = ""

if hasattr(receiver, "__qualname__"):
name += receiver.__qualname__
name = receiver.__qualname__
elif hasattr(receiver, "__name__"): # Python 2.7 has no __qualname__
name += receiver.__name__
name = receiver.__name__
elif hasattr(
receiver, "func"
): # certain functions (like partials) dont have a name
name = "partial(<function " + receiver.func.__name__ + ">)" # type: ignore

if (
name == ""
): # certain functions (like partials) dont have a name so return the string representation
): # In case nothing was found, return the string representation (this is the slowest case)
return str(receiver)

if hasattr(receiver, "__module__"): # prepend with module, if there is one
Expand Down
3 changes: 2 additions & 1 deletion test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ Werkzeug<2.1.0
jsonschema==3.2.0
pyrsistent==0.16.0 # TODO(py3): 0.17.0 requires python3, see https://github.com/tobgu/pyrsistent/issues/205
executing
asttokens
asttokens
ipdb
7 changes: 5 additions & 2 deletions tests/integrations/django/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
except ImportError:
from django.core.urlresolvers import reverse

from sentry_sdk._compat import PY2
from sentry_sdk._compat import PY2, PY310
from sentry_sdk import capture_message, capture_exception, configure_scope
from sentry_sdk.integrations.django import DjangoIntegration
from sentry_sdk.integrations.django.signals_handlers import _get_receiver_name
Expand Down Expand Up @@ -834,4 +834,7 @@ def dummy(a, b):

a_partial = partial(dummy)
name = _get_receiver_name(a_partial)
assert name == str(a_partial)
if PY310:
assert name == "functools.partial(<function " + a_partial.func.__name__ + ">)"
else:
assert name == "partial(<function " + a_partial.func.__name__ + ">)"

0 comments on commit 76b413a

Please sign in to comment.