|
24 | 24 | from contextlib import contextmanager
|
25 | 25 |
|
26 | 26 | import sentry_sdk
|
27 |
| -from sentry_sdk._compat import PY33 |
| 27 | +from sentry_sdk._compat import PY33, PY311 |
28 | 28 | from sentry_sdk._types import MYPY
|
29 | 29 | from sentry_sdk.utils import (
|
30 | 30 | filename_for_module,
|
@@ -269,55 +269,60 @@ def extract_frame(frame, cwd):
|
269 | 269 | )
|
270 | 270 |
|
271 | 271 |
|
272 |
| -def get_frame_name(frame): |
273 |
| - # type: (FrameType) -> str |
| 272 | +if PY311: |
274 | 273 |
|
275 |
| - # in 3.11+, there is a frame.f_code.co_qualname that |
276 |
| - # we should consider using instead where possible |
| 274 | + def get_frame_name(frame): |
| 275 | + # type: (FrameType) -> str |
| 276 | + return frame.f_code.co_qualname # type: ignore |
277 | 277 |
|
278 |
| - f_code = frame.f_code |
279 |
| - co_varnames = f_code.co_varnames |
| 278 | +else: |
280 | 279 |
|
281 |
| - # co_name only contains the frame name. If the frame was a method, |
282 |
| - # the class name will NOT be included. |
283 |
| - name = f_code.co_name |
| 280 | + def get_frame_name(frame): |
| 281 | + # type: (FrameType) -> str |
284 | 282 |
|
285 |
| - # if it was a method, we can get the class name by inspecting |
286 |
| - # the f_locals for the `self` argument |
287 |
| - try: |
288 |
| - if ( |
289 |
| - # the co_varnames start with the frame's positional arguments |
290 |
| - # and we expect the first to be `self` if its an instance method |
291 |
| - co_varnames |
292 |
| - and co_varnames[0] == "self" |
293 |
| - and "self" in frame.f_locals |
294 |
| - ): |
295 |
| - for cls in frame.f_locals["self"].__class__.__mro__: |
296 |
| - if name in cls.__dict__: |
297 |
| - return "{}.{}".format(cls.__name__, name) |
298 |
| - except AttributeError: |
299 |
| - pass |
300 |
| - |
301 |
| - # if it was a class method, (decorated with `@classmethod`) |
302 |
| - # we can get the class name by inspecting the f_locals for the `cls` argument |
303 |
| - try: |
304 |
| - if ( |
305 |
| - # the co_varnames start with the frame's positional arguments |
306 |
| - # and we expect the first to be `cls` if its a class method |
307 |
| - co_varnames |
308 |
| - and co_varnames[0] == "cls" |
309 |
| - and "cls" in frame.f_locals |
310 |
| - ): |
311 |
| - for cls in frame.f_locals["cls"].__mro__: |
312 |
| - if name in cls.__dict__: |
313 |
| - return "{}.{}".format(cls.__name__, name) |
314 |
| - except AttributeError: |
315 |
| - pass |
316 |
| - |
317 |
| - # nothing we can do if it is a staticmethod (decorated with @staticmethod) |
318 |
| - |
319 |
| - # we've done all we can, time to give up and return what we have |
320 |
| - return name |
| 283 | + f_code = frame.f_code |
| 284 | + co_varnames = f_code.co_varnames |
| 285 | + |
| 286 | + # co_name only contains the frame name. If the frame was a method, |
| 287 | + # the class name will NOT be included. |
| 288 | + name = f_code.co_name |
| 289 | + |
| 290 | + # if it was a method, we can get the class name by inspecting |
| 291 | + # the f_locals for the `self` argument |
| 292 | + try: |
| 293 | + if ( |
| 294 | + # the co_varnames start with the frame's positional arguments |
| 295 | + # and we expect the first to be `self` if its an instance method |
| 296 | + co_varnames |
| 297 | + and co_varnames[0] == "self" |
| 298 | + and "self" in frame.f_locals |
| 299 | + ): |
| 300 | + for cls in frame.f_locals["self"].__class__.__mro__: |
| 301 | + if name in cls.__dict__: |
| 302 | + return "{}.{}".format(cls.__name__, name) |
| 303 | + except AttributeError: |
| 304 | + pass |
| 305 | + |
| 306 | + # if it was a class method, (decorated with `@classmethod`) |
| 307 | + # we can get the class name by inspecting the f_locals for the `cls` argument |
| 308 | + try: |
| 309 | + if ( |
| 310 | + # the co_varnames start with the frame's positional arguments |
| 311 | + # and we expect the first to be `cls` if its a class method |
| 312 | + co_varnames |
| 313 | + and co_varnames[0] == "cls" |
| 314 | + and "cls" in frame.f_locals |
| 315 | + ): |
| 316 | + for cls in frame.f_locals["cls"].__mro__: |
| 317 | + if name in cls.__dict__: |
| 318 | + return "{}.{}".format(cls.__name__, name) |
| 319 | + except AttributeError: |
| 320 | + pass |
| 321 | + |
| 322 | + # nothing we can do if it is a staticmethod (decorated with @staticmethod) |
| 323 | + |
| 324 | + # we've done all we can, time to give up and return what we have |
| 325 | + return name |
321 | 326 |
|
322 | 327 |
|
323 | 328 | MAX_PROFILE_DURATION_NS = int(3e10) # 30 seconds
|
|
0 commit comments