| 1 |
import inspect |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from pyinstrument.low_level import stat_profile as stat_profile_c |
| 6 |
from pyinstrument.low_level import stat_profile_python |
| 7 |
|
| 8 |
|
| 9 |
class AClass: |
| 10 |
def get_frame_info_for_a_method(self, getter_function, del_local): |
| 11 |
if del_local: |
| 12 |
del self |
| 13 |
frame = inspect.currentframe() |
| 14 |
assert frame |
| 15 |
return getter_function(frame) |
| 16 |
|
| 17 |
def get_frame_info_with_cell_variable(self, getter_function, del_local): |
| 18 |
def an_inner_function(): |
| 19 |
|
| 20 |
if self: |
| 21 |
pass |
| 22 |
|
| 23 |
if del_local: |
| 24 |
del self |
| 25 |
frame = inspect.currentframe() |
| 26 |
assert frame |
| 27 |
|
| 28 |
return getter_function(frame) |
| 29 |
|
| 30 |
@classmethod |
| 31 |
def get_frame_info_for_a_class_method(cls, getter_function, del_local): |
| 32 |
if del_local: |
| 33 |
del cls |
| 34 |
frame = inspect.currentframe() |
| 35 |
assert frame |
| 36 |
return getter_function(frame) |
| 37 |
|
| 38 |
@classmethod |
| 39 |
def get_frame_info_for_a_class_method_where_cls_is_reassigned(cls, getter_function, del_local): |
| 40 |
cls = 1 |
| 41 |
if del_local: |
| 42 |
del cls |
| 43 |
frame = inspect.currentframe() |
| 44 |
assert frame |
| 45 |
return getter_function(frame) |
| 46 |
|
| 47 |
|
| 48 |
def test_frame_info(): |
| 49 |
frame = inspect.currentframe() |
| 50 |
|
| 51 |
assert frame |
| 52 |
assert stat_profile_c.get_frame_info(frame) == stat_profile_python.get_frame_info(frame) |
| 53 |
|
| 54 |
|
| 55 |
def test_frame_info_hide_true(): |
| 56 |
__tracebackhide__ = True |
| 57 |
|
| 58 |
frame = inspect.currentframe() |
| 59 |
|
| 60 |
assert frame |
| 61 |
assert stat_profile_c.get_frame_info(frame) == stat_profile_python.get_frame_info(frame) |
| 62 |
|
| 63 |
|
| 64 |
def test_frame_info_hide_false(): |
| 65 |
"""to avoid calling FastToLocals on the c side, |
| 66 |
__tracebackhide__ = True |
| 67 |
and |
| 68 |
__tracebackhide__ = False |
| 69 |
are treated the same. All that matters is that the var is defined |
| 70 |
""" |
| 71 |
__tracebackhide__ = False |
| 72 |
|
| 73 |
frame = inspect.currentframe() |
| 74 |
|
| 75 |
assert frame |
| 76 |
assert stat_profile_c.get_frame_info(frame) == stat_profile_python.get_frame_info(frame) |
| 77 |
|
| 78 |
|
| 79 |
instance = AClass() |
| 80 |
|
| 81 |
|
| 82 |
@pytest.mark.parametrize( |
| 83 |
"test_function", |
| 84 |
[ |
| 85 |
instance.get_frame_info_for_a_method, |
| 86 |
AClass.get_frame_info_for_a_class_method, |
| 87 |
instance.get_frame_info_with_cell_variable, |
| 88 |
AClass.get_frame_info_for_a_class_method_where_cls_is_reassigned, |
| 89 |
], |
| 90 |
) |
| 91 |
@pytest.mark.parametrize("del_local", [True, False]) |
| 92 |
def test_frame_info_with_classes(test_function, del_local): |
| 93 |
c_frame_info = test_function(stat_profile_c.get_frame_info, del_local=del_local) |
| 94 |
py_frame_info = test_function(stat_profile_python.get_frame_info, del_local=del_local) |
| 95 |
|
| 96 |
assert c_frame_info == py_frame_info |