summaryrefslogtreecommitdiff
path: root/Lib/test/test_capi/test_eval_code_ex.py
blob: 2d28e5289eff94d23d2e4f3d421d88395e10710e (plain)
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
import unittest

from test.support import import_helper


# Skip this test if the _testcapi module isn't available.
_testcapi = import_helper.import_module('_testcapi')


class PyEval_EvalCodeExTests(unittest.TestCase):

    def test_simple(self):
        def f():
            return a

        self.assertEqual(_testcapi.eval_code_ex(f.__code__, dict(a=1)), 1)

    # Need to force the compiler to use LOAD_NAME
    # def test_custom_locals(self):
    #     def f():
    #         return

    def test_with_args(self):
        def f(a, b, c):
            return a

        self.assertEqual(_testcapi.eval_code_ex(f.__code__, {}, {}, (1, 2, 3)), 1)

    def test_with_kwargs(self):
        def f(a, b, c):
            return a

        self.assertEqual(_testcapi.eval_code_ex(f.__code__, {}, {}, (), dict(a=1, b=2, c=3)), 1)

    def test_with_default(self):
        def f(a):
            return a

        self.assertEqual(_testcapi.eval_code_ex(f.__code__, {}, {}, (), {}, (1,)), 1)

    def test_with_kwarg_default(self):
        def f(*, a):
            return a

        self.assertEqual(_testcapi.eval_code_ex(f.__code__, {}, {}, (), {}, (), dict(a=1)), 1)

    def test_with_closure(self):
        a = 1
        def f():
            return a

        self.assertEqual(_testcapi.eval_code_ex(f.__code__, {}, {}, (), {}, (), {}, f.__closure__), 1)


if __name__ == "__main__":
    unittest.main()