Debian Codegraph @

tests/test_tblib.py

raw
1 import pickle
2 import traceback
3
4 from tblib import Traceback
5 from tblib import pickling_support
6
7 pickling_support.install()
8
9 pytest_plugins = ('pytester',)
10
11
12 def test_get_locals():
13 def get_locals(frame):
14 print(frame, frame.f_locals)
15 if 'my_variable' in frame.f_locals:
16 return {'my_variable': int(frame.f_locals['my_variable'])}
17 else:
18 return {}
19
20 def func(my_arg='2'):
21 my_variable = '1'
22 raise ValueError(my_variable)
23
24 try:
25 func()
26 except Exception as e:
27 exc = e
28 else:
29 raise AssertionError
30
31 f_locals = exc.__traceback__.tb_next.tb_frame.f_locals
32 assert 'my_variable' in f_locals
33 assert f_locals['my_variable'] == '1'
34
35 value = Traceback(exc.__traceback__, get_locals=get_locals).as_dict()
36 lineno = exc.__traceback__.tb_lineno
37 assert value == {
38 'tb_frame': {
39 'f_globals': {'__name__': 'test_tblib', '__file__': __file__},
40 'f_locals': {},
41 'f_code': {'co_filename': __file__, 'co_name': 'test_get_locals'},
42 'f_lineno': lineno + 10,
43 },
44 'tb_lineno': lineno,
45 'tb_next': {
46 'tb_frame': {
47 'f_globals': {'__name__': 'test_tblib', '__file__': __file__},
48 'f_locals': {'my_variable': 1},
49 'f_code': {'co_filename': __file__, 'co_name': 'func'},
50 'f_lineno': lineno - 3,
51 },
52 'tb_lineno': lineno - 3,
53 'tb_next': None,
54 },
55 }
56
57 assert Traceback.from_dict(value).tb_next.tb_frame.f_locals == {'my_variable': 1}
58
59
60 def test_parse_traceback():
61 tb1 = Traceback.from_string(
62 """
63 Traceback (most recent call last):
64 File "file1", line 123, in <module>
65 code1
66 File "file2", line 234, in ???
67 code2
68 File "file3", line 345, in function3
69 File "file4", line 456, in
70 code4
71 KeyboardInterrupt"""
72 )
73 pytb = tb1.as_traceback()
74 assert traceback.format_tb(pytb) == [
75 ' File "file1", line 123, in <module>\n',
76 ' File "file2", line 234, in ???\n',
77 ' File "file3", line 345, in function3\n',
78 ]
79 tb2 = Traceback(pytb)
80
81 expected_dict = {
82 'tb_frame': {
83 'f_code': {'co_filename': 'file1', 'co_name': '<module>'},
84 'f_globals': {'__file__': 'file1', '__name__': '?'},
85 'f_locals': {},
86 'f_lineno': 123,
87 },
88 'tb_lineno': 123,
89 'tb_next': {
90 'tb_frame': {
91 'f_code': {'co_filename': 'file2', 'co_name': '???'},
92 'f_globals': {'__file__': 'file2', '__name__': '?'},
93 'f_locals': {},
94 'f_lineno': 234,
95 },
96 'tb_lineno': 234,
97 'tb_next': {
98 'tb_frame': {
99 'f_code': {'co_filename': 'file3', 'co_name': 'function3'},
100 'f_globals': {'__file__': 'file3', '__name__': '?'},
101 'f_locals': {},
102 'f_lineno': 345,
103 },
104 'tb_lineno': 345,
105 'tb_next': None,
106 },
107 },
108 }
109 tb3 = Traceback.from_dict(expected_dict)
110 tb4 = pickle.loads(pickle.dumps(tb3))
111 assert tb4.as_dict() == tb3.as_dict() == tb2.as_dict() == tb1.as_dict() == expected_dict
112
113
114 def test_large_line_number():
115 line_number = 2**31 - 1
116 tb1 = Traceback.from_string(
117 f"""
118 Traceback (most recent call last):
119 File "file1", line {line_number}, in <module>
120 code1
121 """
122 ).as_traceback()
123 assert tb1.tb_lineno == line_number
124
125
126 def test_pytest_integration(testdir):
127 test = testdir.makepyfile(
128 """
129 from tblib import Traceback
130
131 def test_raise():
132 tb1 = Traceback.from_string('''
133 Traceback (most recent call last):
134 File "file1", line 123, in <module>
135 code1
136 File "file2", line 234, in ???
137 code2
138 File "file3", line 345, in function3
139 File "file4", line 456, in ""
140 ''')
141 pytb = tb1.as_traceback()
142 raise RuntimeError().with_traceback(pytb)
143 """
144 )
145
146 # mode(auto / long / short / line / native / no).
147
148 result = testdir.runpytest_subprocess('--tb=long', '-vv', test)
149 result.stdout.fnmatch_lines(
150 [
151 '_ _ _ _ _ _ _ _ *',
152 '',
153 '> [?][?][?]',
154 '',
155 'file1:123:*',
156 '_ _ _ _ _ _ _ _ *',
157 '',
158 '> [?][?][?]',
159 '',
160 'file2:234:*',
161 '_ _ _ _ _ _ _ _ *',
162 '',
163 '> [?][?][?]',
164 '',
165 'file3:345:*',
166 '_ _ _ _ _ _ _ _ *',
167 '',
168 '> [?][?][?]',
169 'E RuntimeError',
170 '',
171 'file4:456: RuntimeError',
172 '===*=== 1 failed in * ===*===',
173 ]
174 )
175
176 result = testdir.runpytest_subprocess('--tb=short', '-vv', test)
177 result.stdout.fnmatch_lines(
178 [
179 'test_pytest_integration.py:*: in test_raise',
180 ' raise RuntimeError().with_traceback(pytb)',
181 'file1:123: in <module>',
182 ' ???',
183 'file2:234: in ???',
184 ' ???',
185 'file3:345: in function3',
186 ' ???',
187 'file4:456: in ""',
188 ' ???',
189 'E RuntimeError',
190 ]
191 )
192
193 result = testdir.runpytest_subprocess('--tb=line', '-vv', test)
194 result.stdout.fnmatch_lines(
195 [
196 '===*=== FAILURES ===*===',
197 'file4:456: RuntimeError',
198 '===*=== 1 failed in * ===*===',
199 ]
200 )
201
202 result = testdir.runpytest_subprocess('--tb=native', '-vv', test)
203 result.stdout.fnmatch_lines(
204 [
205 'Traceback (most recent call last):',
206 ' File "*test_pytest_integration.py", line *, in test_raise',
207 ' raise RuntimeError().with_traceback(pytb)',
208 ' File "file1", line 123, in <module>',
209 ' File "file2", line 234, in ???',
210 ' File "file3", line 345, in function3',
211 ' File "file4", line 456, in ""',
212 'RuntimeError',
213 ]
214 )