|
| 1 | +import inspect |
1 | 2 | import json
|
2 | 3 | import os
|
3 | 4 | import re
|
| 5 | +import sys |
4 | 6 | import pytest
|
5 | 7 | from functools import partial
|
6 | 8 | from unittest.mock import patch
|
|
12 | 14 | from django.core.management import execute_from_command_line
|
13 | 15 | from django.db.utils import OperationalError, ProgrammingError, DataError
|
14 | 16 | from django.http.request import RawPostDataException
|
| 17 | +from django.utils.functional import SimpleLazyObject |
15 | 18 |
|
16 | 19 | try:
|
17 | 20 | from django.urls import reverse
|
|
29 | 32 | )
|
30 | 33 | from sentry_sdk.integrations.django.signals_handlers import _get_receiver_name
|
31 | 34 | from sentry_sdk.integrations.executing import ExecutingIntegration
|
| 35 | +from sentry_sdk.profiler.utils import get_frame_name |
32 | 36 | from sentry_sdk.tracing import Span
|
33 | 37 | from tests.conftest import unpack_werkzeug_response
|
34 | 38 | from tests.integrations.django.myapp.wsgi import application
|
@@ -1295,3 +1299,47 @@ def test_ensures_no_spotlight_middleware_when_no_spotlight(
|
1295 | 1299 | added = frozenset(settings.MIDDLEWARE) ^ original_middleware
|
1296 | 1300 |
|
1297 | 1301 | assert "sentry_sdk.spotlight.SpotlightMiddleware" not in added
|
| 1302 | + |
| 1303 | + |
| 1304 | +def test_get_frame_name_when_in_lazy_object(): |
| 1305 | + allowed_to_init = False |
| 1306 | + |
| 1307 | + class SimpleLazyObjectWrapper(SimpleLazyObject): |
| 1308 | + def unproxied_method(self): |
| 1309 | + """ |
| 1310 | + For testing purposes. We inject a method on the SimpleLazyObject |
| 1311 | + class so if python is executing this method, we should get |
| 1312 | + this class instead of the wrapped class and avoid evaluating |
| 1313 | + the wrapped object too early. |
| 1314 | + """ |
| 1315 | + return inspect.currentframe() |
| 1316 | + |
| 1317 | + class GetFrame: |
| 1318 | + def __init__(self): |
| 1319 | + assert allowed_to_init, "GetFrame not permitted to initialize yet" |
| 1320 | + |
| 1321 | + def proxied_method(self): |
| 1322 | + """ |
| 1323 | + For testing purposes. We add an proxied method on the instance |
| 1324 | + class so if python is executing this method, we should get |
| 1325 | + this class instead of the wrapper class. |
| 1326 | + """ |
| 1327 | + return inspect.currentframe() |
| 1328 | + |
| 1329 | + instance = SimpleLazyObjectWrapper(lambda: GetFrame()) |
| 1330 | + |
| 1331 | + assert get_frame_name(instance.unproxied_method()) == ( |
| 1332 | + "SimpleLazyObjectWrapper.unproxied_method" |
| 1333 | + if sys.version_info < (3, 11) |
| 1334 | + else "test_get_frame_name_when_in_lazy_object.<locals>.SimpleLazyObjectWrapper.unproxied_method" |
| 1335 | + ) |
| 1336 | + |
| 1337 | + # Now that we're about to access an instance method on the wrapped class, |
| 1338 | + # we should permit initializing it |
| 1339 | + allowed_to_init = True |
| 1340 | + |
| 1341 | + assert get_frame_name(instance.proxied_method()) == ( |
| 1342 | + "GetFrame.proxied_method" |
| 1343 | + if sys.version_info < (3, 11) |
| 1344 | + else "test_get_frame_name_when_in_lazy_object.<locals>.GetFrame.proxied_method" |
| 1345 | + ) |
0 commit comments