|
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,7 +672,6 @@ 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 | 677 | defs.spam_with_builtins: {
|
@@ -897,7 +897,6 @@ def new_var_counts(*,
|
897 | 897 | },
|
898 | 898 | }
|
899 | 899 |
|
900 |
| - import test._code_definitions as defs |
901 | 900 | funcs = {
|
902 | 901 | defs.spam_minimal: new_var_counts(),
|
903 | 902 | defs.spam_with_builtins: new_var_counts(
|
@@ -1025,55 +1024,70 @@ def new_var_counts(*,
|
1025 | 1024 | counts = _testinternalcapi.get_code_var_counts(func.__code__)
|
1026 | 1025 | self.assertEqual(counts, expected)
|
1027 | 1026 |
|
1028 |
| - def func_with_globals_and_builtins(): |
1029 |
| - mod1 = _testinternalcapi |
1030 |
| - mod2 = dis |
1031 |
| - mods = (mod1, mod2) |
1032 |
| - checks = tuple(callable(m) for m in mods) |
1033 |
| - return callable(mod2), tuple(mods), list(mods), checks |
1034 |
| - |
1035 |
| - func = func_with_globals_and_builtins |
| 1027 | + func = defs.spam_with_globals_and_builtins |
1036 | 1028 | with self.subTest(f'{func} code'):
|
1037 | 1029 | expected = new_var_counts(
|
1038 |
| - purelocals=4, |
1039 |
| - globalvars=5, |
| 1030 | + purelocals=5, |
| 1031 | + globalvars=6, |
1040 | 1032 | )
|
1041 | 1033 | counts = _testinternalcapi.get_code_var_counts(func.__code__)
|
1042 | 1034 | self.assertEqual(counts, expected)
|
1043 | 1035 |
|
1044 | 1036 | with self.subTest(f'{func} with own globals and builtins'):
|
1045 | 1037 | expected = new_var_counts(
|
1046 |
| - purelocals=4, |
1047 |
| - globalvars=(2, 3), |
| 1038 | + purelocals=5, |
| 1039 | + globalvars=(2, 4), |
1048 | 1040 | )
|
1049 | 1041 | counts = _testinternalcapi.get_code_var_counts(func)
|
1050 | 1042 | self.assertEqual(counts, expected)
|
1051 | 1043 |
|
1052 | 1044 | with self.subTest(f'{func} without globals'):
|
1053 | 1045 | expected = new_var_counts(
|
1054 |
| - purelocals=4, |
1055 |
| - globalvars=(0, 3, 2), |
| 1046 | + purelocals=5, |
| 1047 | + globalvars=(0, 4, 2), |
1056 | 1048 | )
|
1057 | 1049 | counts = _testinternalcapi.get_code_var_counts(func, globalsns={})
|
1058 | 1050 | self.assertEqual(counts, expected)
|
1059 | 1051 |
|
1060 | 1052 | with self.subTest(f'{func} without both'):
|
1061 | 1053 | expected = new_var_counts(
|
1062 |
| - purelocals=4, |
1063 |
| - globalvars=5, |
| 1054 | + purelocals=5, |
| 1055 | + globalvars=6, |
1064 | 1056 | )
|
1065 | 1057 | counts = _testinternalcapi.get_code_var_counts(func, globalsns={},
|
1066 | 1058 | builtinsns={})
|
1067 | 1059 | self.assertEqual(counts, expected)
|
1068 | 1060 |
|
1069 | 1061 | with self.subTest(f'{func} without builtins'):
|
1070 | 1062 | expected = new_var_counts(
|
1071 |
| - purelocals=4, |
1072 |
| - globalvars=(2, 0, 3), |
| 1063 | + purelocals=5, |
| 1064 | + globalvars=(2, 0, 4), |
1073 | 1065 | )
|
1074 | 1066 | counts = _testinternalcapi.get_code_var_counts(func, builtinsns={})
|
1075 | 1067 | self.assertEqual(counts, expected)
|
1076 | 1068 |
|
| 1069 | + @unittest.skipIf(_testinternalcapi is None, "missing _testinternalcapi") |
| 1070 | + def test_stateless(self): |
| 1071 | + self.maxDiff = None |
| 1072 | + |
| 1073 | + for func in defs.STATELESS_CODE: |
| 1074 | + with self.subTest((func, '(code)')): |
| 1075 | + _testinternalcapi.verify_stateless_code(func.__code__) |
| 1076 | + for func in defs.STATELESS_FUNCTIONS: |
| 1077 | + with self.subTest((func, '(func)')): |
| 1078 | + _testinternalcapi.verify_stateless_code(func) |
| 1079 | + |
| 1080 | + for func in defs.FUNCTIONS: |
| 1081 | + if func not in defs.STATELESS_CODE: |
| 1082 | + with self.subTest((func, '(code)')): |
| 1083 | + with self.assertRaises(Exception): |
| 1084 | + _testinternalcapi.verify_stateless_code(func.__code__) |
| 1085 | + |
| 1086 | + if func not in defs.STATELESS_FUNCTIONS: |
| 1087 | + with self.subTest((func, '(func)')): |
| 1088 | + with self.assertRaises(Exception): |
| 1089 | + _testinternalcapi.verify_stateless_code(func) |
| 1090 | + |
1077 | 1091 |
|
1078 | 1092 | def isinterned(s):
|
1079 | 1093 | return s is sys.intern(('_' + s + '_')[1:-1])
|
|
0 commit comments