- Notifications
You must be signed in to change notification settings - Fork 31.8k
/
Copy pathtest_frame.py
56 lines (43 loc) · 1.86 KB
/
test_frame.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
importsys
importunittest
fromtest.supportimportimport_helper
_testcapi=import_helper.import_module('_testcapi')
classFrameTest(unittest.TestCase):
defgetframe(self):
returnsys._getframe()
deftest_frame_getters(self):
frame=self.getframe()
self.assertEqual(frame.f_locals, _testcapi.frame_getlocals(frame))
self.assertIs(frame.f_globals, _testcapi.frame_getglobals(frame))
self.assertIs(frame.f_builtins, _testcapi.frame_getbuiltins(frame))
self.assertEqual(frame.f_lasti, _testcapi.frame_getlasti(frame))
deftest_getvar(self):
current_frame=sys._getframe()
x=1
self.assertEqual(_testcapi.frame_getvar(current_frame, "x"), 1)
self.assertEqual(_testcapi.frame_getvarstring(current_frame, b"x"), 1)
withself.assertRaises(NameError):
_testcapi.frame_getvar(current_frame, "y")
withself.assertRaises(NameError):
_testcapi.frame_getvarstring(current_frame, b"y")
# wrong name type
withself.assertRaises(TypeError):
_testcapi.frame_getvar(current_frame, b'x')
withself.assertRaises(TypeError):
_testcapi.frame_getvar(current_frame, 123)
defgetgenframe(self):
yieldsys._getframe()
deftest_frame_get_generator(self):
gen=self.getgenframe()
frame=next(gen)
self.assertIs(gen, _testcapi.frame_getgenerator(frame))
deftest_frame_fback_api(self):
"""Test that accessing `f_back` does not cause a segmentation fault on
a frame created with `PyFrame_New` (GH-99110)."""
defdummy():
pass
frame=_testcapi.frame_new(dummy.__code__, globals(), locals())
# The following line should not cause a segmentation fault.
self.assertIsNone(frame.f_back)
if__name__=="__main__":
unittest.main()