Debian Codegraph @

lib-python/3/test/test_capi/test_eval_code_ex.py

raw symbols SCIP
1 import unittest
2
3 from test.support import import_helper
4
5
6 # Skip this test if the _testcapi module isn't available.
7 _testcapi = import_helper.import_module('_testcapi')
8
9
10 @unittest.skipUnless(hasattr(_testcapi, 'eval_code_ex'),
11 'need _testcapi.eval_code_ex')
12 class PyEval_EvalCodeExTests(unittest.TestCase):
13
14 def test_simple(self):
15 def f():
16 return a
17
18 self.assertEqual(_testcapi.eval_code_ex(f.__code__, dict(a=1)), 1)
19
20 # Need to force the compiler to use LOAD_NAME
21 # def test_custom_locals(self):
22 # def f():
23 # return
24
25 def test_with_args(self):
26 def f(a, b, c):
27 return a
28
29 self.assertEqual(_testcapi.eval_code_ex(f.__code__, {}, {}, (1, 2, 3)), 1)
30
31 def test_with_kwargs(self):
32 def f(a, b, c):
33 return a
34
35 self.assertEqual(_testcapi.eval_code_ex(f.__code__, {}, {}, (), dict(a=1, b=2, c=3)), 1)
36
37 def test_with_default(self):
38 def f(a):
39 return a
40
41 self.assertEqual(_testcapi.eval_code_ex(f.__code__, {}, {}, (), {}, (1,)), 1)
42
43 def test_with_kwarg_default(self):
44 def f(*, a):
45 return a
46
47 self.assertEqual(_testcapi.eval_code_ex(f.__code__, {}, {}, (), {}, (), dict(a=1)), 1)
48
49 def test_with_closure(self):
50 a = 1
51 def f():
52 return a
53
54 self.assertEqual(_testcapi.eval_code_ex(f.__code__, {}, {}, (), {}, (), {}, f.__closure__), 1)
55
56
57 if __name__ == "__main__":
58 unittest.main()