| 1 |
import inspect |
| 2 |
|
| 3 |
import friendly_traceback as ft |
| 4 |
|
| 5 |
global_a = 1 |
| 6 |
global_b = 2 |
| 7 |
global_and_nonlocal_different = 1 |
| 8 |
|
| 9 |
|
| 10 |
def test_get_variables_in_frame_by_scope(): |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
get = ft.info_variables.get_variables_in_frame_by_scope |
| 15 |
current_frame = None |
| 16 |
|
| 17 |
b = 2 |
| 18 |
global_and_nonlocal_different = 2 |
| 19 |
|
| 20 |
def outer(): |
| 21 |
c = 3 |
| 22 |
d = 4 |
| 23 |
|
| 24 |
def inner(): |
| 25 |
global global_a |
| 26 |
global global_b |
| 27 |
nonlocal current_frame |
| 28 |
nonlocal c |
| 29 |
e = 5 |
| 30 |
global_b += 1 |
| 31 |
current_frame = inspect.currentframe() |
| 32 |
|
| 33 |
inner() |
| 34 |
|
| 35 |
outer() |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
assert "global_a" in get(current_frame, "global") |
| 40 |
assert "global_a" not in get(current_frame, "local") |
| 41 |
assert "global_a" not in get(current_frame, "nonlocal") |
| 42 |
assert "global_b" not in get(current_frame, "local") |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
assert "b" in get(current_frame, "nonlocal") |
| 47 |
assert "b" not in get(current_frame, "local") |
| 48 |
assert "b" not in get(current_frame, "global") |
| 49 |
|
| 50 |
assert "d" in get(current_frame, "nonlocal") |
| 51 |
assert "d" not in get(current_frame, "local") |
| 52 |
assert "d" not in get(current_frame, "global") |
| 53 |
|
| 54 |
|
| 55 |
assert "c" in get(current_frame, "local") |
| 56 |
assert "c" in get(current_frame, "nonlocal") |
| 57 |
assert "c" not in get(current_frame, "global") |
| 58 |
|
| 59 |
assert "e" in get(current_frame, "local") |
| 60 |
|
| 61 |
assert "global_and_nonlocal_different" in get(current_frame, "nonlocal") |
| 62 |
assert "global_and_nonlocal_different" in get(current_frame, "global") |
| 63 |
|
| 64 |
|
| 65 |
get_scopes = ft.info_variables.get_definition_scope |
| 66 |
assert "nonlocal" in get_scopes("global_and_nonlocal_different", current_frame) |
| 67 |
assert "global" in get_scopes("global_and_nonlocal_different", current_frame) |
| 68 |
assert "nonlocal" not in get_scopes("global_a", current_frame) |
| 69 |
assert "global" in get_scopes("global_a", current_frame) |
| 70 |
assert "nonlocal" in get_scopes("b", current_frame) |
| 71 |
assert "global" not in get_scopes("b", current_frame) |
| 72 |
|
| 73 |
|
| 74 |
def test_simplify_repr(): |
| 75 |
import math |
| 76 |
import collections |
| 77 |
|
| 78 |
simplify = ft.info_variables.simplify_repr |
| 79 |
INDENT = ft.info_variables.INDENT |
| 80 |
|
| 81 |
simplified_math = simplify(repr(math)) |
| 82 |
assert simplified_math == "<module math (builtin)>" or ( |
| 83 |
"<module math>" in simplified_math and "PYTHON_LIB" in simplified_math |
| 84 |
) |
| 85 |
|
| 86 |
assert ( |
| 87 |
simplify(repr(collections)).replace("\\", "/") |
| 88 |
== "<module collections> from PYTHON_LIB:/collections/__init__.py" |
| 89 |
) |
| 90 |
assert simplify(repr(open)) == "<builtin function open>" |
| 91 |
assert simplify("<class 'AssertionError'>") == "<class AssertionError>" |
| 92 |
assert ( |
| 93 |
simplify("<bound method ChainMap.pop of ChainMap({(0, 0): 'origin'}, {})>") |
| 94 |
== "<bound method ChainMap.pop> of ChainMap({(0, 0): 'origin'}, {})" |
| 95 |
) |
| 96 |
assert ( |
| 97 |
simplify("<built-in method popitem of dict object at 0x00000267D1C96180>") |
| 98 |
== "<builtin method popitem of dict object>" |
| 99 |
) |
| 100 |
assert ( |
| 101 |
simplify("<function test_Generic.<locals>.a at 0x00000267D28B0F70>") |
| 102 |
== "<function a> defined in <function test_Generic>" |
| 103 |
) |
| 104 |
assert ( |
| 105 |
simplify( |
| 106 |
"<bound method test_method_got_multiple_argument.<locals>.T.some_method" |
| 107 |
" of <tests.runtime.test_type_error.test_method_got_multiple_argument." |
| 108 |
"<locals>.T object at 0x00000179EE9CD7F0>>" |
| 109 |
) |
| 110 |
== "<bound method T.some_method>" |
| 111 |
+ f"\n{INDENT}of <T object>" |
| 112 |
+ f"\n{INDENT}defined in <function " |
| 113 |
+ "tests.runtime.test_type_error.test_method_got_multiple_argument>" |
| 114 |
) |
| 115 |
|
| 116 |
|
| 117 |
def test_get_dotted_name_from_frame(): |
| 118 |
import itertools |
| 119 |
frame = inspect.currentframe() |
| 120 |
assert ft.info_variables.get_object_from_name('itertools.count', frame) != None |
| 121 |
|
| 122 |
|
| 123 |
def test_safe_repr_can_handle_errors_on_attribute_access(): |
| 124 |
|
| 125 |
class Proxy: |
| 126 |
def __getattr__(self, name): |
| 127 |
raise NotImplementedError |
| 128 |
|
| 129 |
assert ft.info_variables.safe_repr(Proxy()) |
| 130 |
|
| 131 |
|
| 132 |
if __name__ == "__main__": |
| 133 |
test_get_variables_in_frame_by_scope() |