mirror of
https://github.com/python/cpython.git
synced 2025-07-23 11:15:24 +00:00
gh-93649: Move PyFrame C API tests to test_capi (#129512)
* Add Lib/test/test_capi/test_frame.py file. * Move C API tests from test_frame to test_capi.test_frame. * Add Modules/_testcapi/frame.c file. * Move C API tests from _testcapimodule.c to frame.c
This commit is contained in:
parent
22b2d37f42
commit
8b70ff5872
8 changed files with 199 additions and 157 deletions
|
@ -773,51 +773,6 @@ class TestIncompleteFrameAreInvisible(unittest.TestCase):
|
|||
self.assertIs(catcher.unraisable.exc_type, TypeError)
|
||||
self.assertIsNone(weak())
|
||||
|
||||
@unittest.skipIf(_testcapi is None, 'need _testcapi')
|
||||
class TestCAPI(unittest.TestCase):
|
||||
def getframe(self):
|
||||
return sys._getframe()
|
||||
|
||||
def test_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))
|
||||
|
||||
def test_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)
|
||||
with self.assertRaises(NameError):
|
||||
_testcapi.frame_getvar(current_frame, "y")
|
||||
with self.assertRaises(NameError):
|
||||
_testcapi.frame_getvarstring(current_frame, b"y")
|
||||
|
||||
# wrong name type
|
||||
with self.assertRaises(TypeError):
|
||||
_testcapi.frame_getvar(current_frame, b'x')
|
||||
with self.assertRaises(TypeError):
|
||||
_testcapi.frame_getvar(current_frame, 123)
|
||||
|
||||
def getgenframe(self):
|
||||
yield sys._getframe()
|
||||
|
||||
def test_frame_get_generator(self):
|
||||
gen = self.getgenframe()
|
||||
frame = next(gen)
|
||||
self.assertIs(gen, _testcapi.frame_getgenerator(frame))
|
||||
|
||||
def test_frame_fback_api(self):
|
||||
"""Test that accessing `f_back` does not cause a segmentation fault on
|
||||
a frame created with `PyFrame_New` (GH-99110)."""
|
||||
def dummy():
|
||||
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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue