|
220 | 220 | import _testinternalcapi |
221 | 221 | except ModuleNotFoundError: |
222 | 222 | _testinternalcapi = None |
| 223 | +import test._code_definitions as defs |
223 | 224 |
|
224 | 225 | COPY_FREE_VARS = opmap['COPY_FREE_VARS'] |
225 | 226 |
|
@@ -671,9 +672,31 @@ def test_local_kinds(self): |
671 | 672 | VARARGS = CO_FAST_LOCAL | CO_FAST_ARG_VAR | CO_FAST_ARG_POS |
672 | 673 | VARKWARGS = CO_FAST_LOCAL | CO_FAST_ARG_VAR | CO_FAST_ARG_KW |
673 | 674 |
|
674 | | - import test._code_definitions as defs |
675 | 675 | funcs = { |
676 | 676 | defs.spam_minimal: {}, |
| 677 | + defs.spam_with_builtins: { |
| 678 | + 'x': CO_FAST_LOCAL, |
| 679 | + 'values': CO_FAST_LOCAL, |
| 680 | + 'checks': CO_FAST_LOCAL, |
| 681 | + 'res': CO_FAST_LOCAL, |
| 682 | + }, |
| 683 | + defs.spam_with_globals_and_builtins: { |
| 684 | + 'func1': CO_FAST_LOCAL, |
| 685 | + 'func2': CO_FAST_LOCAL, |
| 686 | + 'funcs': CO_FAST_LOCAL, |
| 687 | + 'checks': CO_FAST_LOCAL, |
| 688 | + 'res': CO_FAST_LOCAL, |
| 689 | + }, |
| 690 | + defs.spam_returns_arg: { |
| 691 | + 'x': POSORKW, |
| 692 | + }, |
| 693 | + defs.spam_with_inner_not_closure: { |
| 694 | + 'eggs': CO_FAST_LOCAL, |
| 695 | + }, |
| 696 | + defs.spam_with_inner_closure: { |
| 697 | + 'x': CO_FAST_CELL, |
| 698 | + 'eggs': CO_FAST_LOCAL, |
| 699 | + }, |
677 | 700 | defs.spam_full: { |
678 | 701 | 'a': POSONLY, |
679 | 702 | 'b': POSONLY, |
@@ -859,9 +882,26 @@ def new_var_counts(*, |
859 | 882 | }, |
860 | 883 | } |
861 | 884 |
|
862 | | - import test._code_definitions as defs |
863 | 885 | funcs = { |
864 | 886 | defs.spam_minimal: new_var_counts(), |
| 887 | + defs.spam_with_builtins: new_var_counts( |
| 888 | + purelocals=4, |
| 889 | + globalvars=4, |
| 890 | + ), |
| 891 | + defs.spam_with_globals_and_builtins: new_var_counts( |
| 892 | + purelocals=5, |
| 893 | + globalvars=6, |
| 894 | + ), |
| 895 | + defs.spam_returns_arg: new_var_counts( |
| 896 | + posorkw=1, |
| 897 | + ), |
| 898 | + defs.spam_with_inner_not_closure: new_var_counts( |
| 899 | + purelocals=1, |
| 900 | + ), |
| 901 | + defs.spam_with_inner_closure: new_var_counts( |
| 902 | + othercells=1, |
| 903 | + purelocals=1, |
| 904 | + ), |
865 | 905 | defs.spam_full: new_var_counts( |
866 | 906 | posonly=2, |
867 | 907 | posorkw=2, |
@@ -958,55 +998,70 @@ def new_var_counts(*, |
958 | 998 | counts = _testinternalcapi.get_code_var_counts(func.__code__) |
959 | 999 | self.assertEqual(counts, expected) |
960 | 1000 |
|
961 | | - def func_with_globals_and_builtins(): |
962 | | - mod1 = _testinternalcapi |
963 | | - mod2 = dis |
964 | | - mods = (mod1, mod2) |
965 | | - checks = tuple(callable(m) for m in mods) |
966 | | - return callable(mod2), tuple(mods), list(mods), checks |
967 | | - |
968 | | - func = func_with_globals_and_builtins |
| 1001 | + func = defs.spam_with_globals_and_builtins |
969 | 1002 | with self.subTest(f'{func} code'): |
970 | 1003 | expected = new_var_counts( |
971 | | - purelocals=4, |
972 | | - globalvars=5, |
| 1004 | + purelocals=5, |
| 1005 | + globalvars=6, |
973 | 1006 | ) |
974 | 1007 | counts = _testinternalcapi.get_code_var_counts(func.__code__) |
975 | 1008 | self.assertEqual(counts, expected) |
976 | 1009 |
|
977 | 1010 | with self.subTest(f'{func} with own globals and builtins'): |
978 | 1011 | expected = new_var_counts( |
979 | | - purelocals=4, |
980 | | - globalvars=(2, 3), |
| 1012 | + purelocals=5, |
| 1013 | + globalvars=(2, 4), |
981 | 1014 | ) |
982 | 1015 | counts = _testinternalcapi.get_code_var_counts(func) |
983 | 1016 | self.assertEqual(counts, expected) |
984 | 1017 |
|
985 | 1018 | with self.subTest(f'{func} without globals'): |
986 | 1019 | expected = new_var_counts( |
987 | | - purelocals=4, |
988 | | - globalvars=(0, 3, 2), |
| 1020 | + purelocals=5, |
| 1021 | + globalvars=(0, 4, 2), |
989 | 1022 | ) |
990 | 1023 | counts = _testinternalcapi.get_code_var_counts(func, globalsns={}) |
991 | 1024 | self.assertEqual(counts, expected) |
992 | 1025 |
|
993 | 1026 | with self.subTest(f'{func} without both'): |
994 | 1027 | expected = new_var_counts( |
995 | | - purelocals=4, |
996 | | - globalvars=5, |
| 1028 | + purelocals=5, |
| 1029 | + globalvars=6, |
997 | 1030 | ) |
998 | 1031 | counts = _testinternalcapi.get_code_var_counts(func, globalsns={}, |
999 | 1032 | builtinsns={}) |
1000 | 1033 | self.assertEqual(counts, expected) |
1001 | 1034 |
|
1002 | 1035 | with self.subTest(f'{func} without builtins'): |
1003 | 1036 | expected = new_var_counts( |
1004 | | - purelocals=4, |
1005 | | - globalvars=(2, 0, 3), |
| 1037 | + purelocals=5, |
| 1038 | + globalvars=(2, 0, 4), |
1006 | 1039 | ) |
1007 | 1040 | counts = _testinternalcapi.get_code_var_counts(func, builtinsns={}) |
1008 | 1041 | self.assertEqual(counts, expected) |
1009 | 1042 |
|
| 1043 | + @unittest.skipIf(_testinternalcapi is None, "missing _testinternalcapi") |
| 1044 | + def test_stateless(self): |
| 1045 | + self.maxDiff = None |
| 1046 |
4EB8
+ |
| 1047 | + for func in defs.STATELESS_CODE: |
| 1048 | + with self.subTest((func, '(code)')): |
| 1049 | + _testinternalcapi.verify_stateless_code(func.__code__) |
| 1050 | + for func in defs.STATELESS_FUNCTIONS: |
| 1051 | + with self.subTest((func, '(func)')): |
| 1052 | + _testinternalcapi.verify_stateless_code(func) |
| 1053 | + |
| 1054 | + for func in defs.FUNCTIONS: |
| 1055 | + if func not in defs.STATELESS_CODE: |
| 1056 | + with self.subTest((func, '(code)')): |
| 1057 | + with self.assertRaises(Exception): |
| 1058 | + _testinternalcapi.verify_stateless_code(func.__code__) |
| 1059 | + |
| 1060 | + if func not in defs.STATELESS_FUNCTIONS: |
| 1061 | + with self.subTest((func, '(func)')): |
| 1062 | + with self.assertRaises(Exception): |
| 1063 | + _testinternalcapi.verify_stateless_code(func) |
| 1064 | + |
1010 | 1065 |
|
1011 | 1066 | def isinterned(s): |
1012 | 1067 | return s is sys.intern(('_' + s + '_')[1:-1]) |
|
0 commit comments