(root)/
Python-3.11.7/
Lib/
test/
test_capi/
test_eval_code_ex.py
       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  class ESC[4;38;5;81mPyEval_EvalCodeExTests(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
      11  
      12      def test_simple(self):
      13          def f():
      14              return a
      15  
      16          self.assertEqual(_testcapi.eval_code_ex(f.__code__, dict(a=1)), 1)
      17  
      18      # Need to force the compiler to use LOAD_NAME
      19      # def test_custom_locals(self):
      20      #     def f():
      21      #         return
      22  
      23      def test_with_args(self):
      24          def f(a, b, c):
      25              return a
      26  
      27          self.assertEqual(_testcapi.eval_code_ex(f.__code__, {}, {}, (1, 2, 3)), 1)
      28  
      29      def test_with_kwargs(self):
      30          def f(a, b, c):
      31              return a
      32  
      33          self.assertEqual(_testcapi.eval_code_ex(f.__code__, {}, {}, (), dict(a=1, b=2, c=3)), 1)
      34  
      35      def test_with_default(self):
      36          def f(a):
      37              return a
      38  
      39          self.assertEqual(_testcapi.eval_code_ex(f.__code__, {}, {}, (), {}, (1,)), 1)
      40  
      41      def test_with_kwarg_default(self):
      42          def f(*, a):
      43              return a
      44  
      45          self.assertEqual(_testcapi.eval_code_ex(f.__code__, {}, {}, (), {}, (), dict(a=1)), 1)
      46  
      47      def test_with_closure(self):
      48          a = 1
      49          def f():
      50              return a
      51  
      52          self.assertEqual(_testcapi.eval_code_ex(f.__code__, {}, {}, (), {}, (), {}, f.__closure__), 1)
      53  
      54  
      55  if __name__ == "__main__":
      56      unittest.main()