diff options
author | Victor Stinner <vstinner@python.org> | 2022-11-08 16:40:27 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-08 16:40:27 (GMT) |
commit | 4d5fcca273b24a5566f1507758e5aae60cdf8a98 (patch) | |
tree | ac00d021776bd95cddd48ca272e07d8711d73922 /Lib/test/test_frame.py | |
parent | acf4d5d5bdecbc8756276731e09bae245d88518d (diff) | |
download | cpython-4d5fcca273b24a5566f1507758e5aae60cdf8a98.zip cpython-4d5fcca273b24a5566f1507758e5aae60cdf8a98.tar.gz cpython-4d5fcca273b24a5566f1507758e5aae60cdf8a98.tar.bz2 |
gh-91248: Add PyFrame_GetVar() function (#95712)
Add PyFrame_GetVar() and PyFrame_GetVarString() functions to get a
frame variable by its name.
Move PyFrameObject C API tests from test_capi to test_frame.
Diffstat (limited to 'Lib/test/test_frame.py')
-rw-r--r-- | Lib/test/test_frame.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/Lib/test/test_frame.py b/Lib/test/test_frame.py index 4b5bb7f..ada9666 100644 --- a/Lib/test/test_frame.py +++ b/Lib/test/test_frame.py @@ -5,6 +5,10 @@ import textwrap import types import unittest import weakref +try: + import _testcapi +except ImportError: + _testcapi = None from test import support from test.support.script_helper import assert_python_ok @@ -326,5 +330,36 @@ class TestIncompleteFrameAreInvisible(unittest.TestCase): gc.enable() +@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") + + 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)) + + if __name__ == "__main__": unittest.main() |