summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2022-11-08 16:40:27 (GMT)
committerGitHub <noreply@github.com>2022-11-08 16:40:27 (GMT)
commit4d5fcca273b24a5566f1507758e5aae60cdf8a98 (patch)
treeac00d021776bd95cddd48ca272e07d8711d73922 /Lib
parentacf4d5d5bdecbc8756276731e09bae245d88518d (diff)
downloadcpython-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')
-rw-r--r--Lib/test/test_capi.py21
-rw-r--r--Lib/test/test_frame.py35
2 files changed, 35 insertions, 21 deletions
diff --git a/Lib/test/test_capi.py b/Lib/test/test_capi.py
index 213b6d4..ea4c9de 100644
--- a/Lib/test/test_capi.py
+++ b/Lib/test/test_capi.py
@@ -1677,27 +1677,6 @@ class Test_ModuleStateAccess(unittest.TestCase):
self.assertIs(Subclass().get_defining_module(), self.module)
-class Test_FrameAPI(unittest.TestCase):
-
- def getframe(self):
- return sys._getframe()
-
- def getgenframe(self):
- yield 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_frame_get_generator(self):
- gen = self.getgenframe()
- frame = next(gen)
- self.assertIs(gen, _testcapi.frame_getgenerator(frame))
-
-
SUFFICIENT_TO_DEOPT_AND_SPECIALIZE = 100
class Test_Pep523API(unittest.TestCase):
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()