|
204 | 204 | import weakref |
205 | 205 | import dis |
206 | 206 |
|
| 207 | +from test.support.constants_helper import iter_global_strings |
| 208 | + |
207 | 209 | try: |
208 | 210 | import ctypes |
209 | 211 | except ImportError: |
@@ -1168,34 +1170,6 @@ def test_stateless(self): |
1168 | 1170 | def isinterned(s): |
1169 | 1171 | return s is sys.intern(('_' + s + '_')[1:-1]) |
1170 | 1172 |
|
1171 | | -# copypaste from 'Tools/build/generate_global_objects.py' |
1172 | | -import os |
1173 | | -import re |
1174 | | -from pathlib import Path |
1175 | | -ROOT = Path(__file__).resolve().parents[2] |
1176 | | -def iter_files(): |
1177 | | - for name in ('Modules', 'Objects', 'Parser', 'PC', 'Programs', 'Python'): |
1178 | | - root = os.path.join(ROOT, name) |
1179 | | - for dirname, _, files in os.walk(root): |
1180 | | - for name in files: |
1181 | | - if not name.endswith(('.c', '.h')): |
1182 | | - continue |
1183 | | - yield os.path.join(dirname, name) |
1184 | | - |
1185 | | -def iter_global_strings(): |
1186 | | - str_regex = re.compile(r'\b_Py_DECLARE_STR\((\w+), "(.*?)"\)') |
1187 | | - for filename in iter_files(): |
1188 | | - try: |
1189 | | - infile = open(filename, encoding='utf-8') |
1190 | | - except FileNotFoundError: |
1191 | | - # The file must have been a temporary file. |
1192 | | - continue |
1193 | | - with infile: |
1194 | | - for lno, line in enumerate(infile, 1): |
1195 | | - for m in str_regex.finditer(line): |
1196 | | - varname, string = m.groups() |
1197 | | - yield string |
1198 | | - |
1199 | 1173 | class CodeConstsTest(unittest.TestCase): |
1200 | 1174 |
|
1201 | 1175 | def find_const(self, consts, value): |
@@ -1283,15 +1257,21 @@ class MyInt(int): |
1283 | 1257 | @unittest.skipIf(Py_GIL_DISABLED, "free-threaded build interns all string constants") |
1284 | 1258 | def test__Py_DECLARE_STR_is_interned(self): |
1285 | 1259 | for global_string in iter_global_strings(): |
1286 | | - # compile given string to a codeobject |
1287 | | - global_string = eval(f"'{global_string}'") |
1288 | | - self.assertIsInterned(global_string) |
| 1260 | + with self.subTest(global_string=global_string): |
| 1261 | + self.assertIsInterned(eval(f"'{global_string}'")) |
1289 | 1262 |
|
1290 | 1263 | @cpython_only |
1291 | 1264 | @unittest.skipIf(Py_GIL_DISABLED, "free-threaded build interns all string constants") |
1292 | 1265 | def test_non_internable_strings_not_interned(self): |
1293 | | - self.assertIsNotInterned("not-internable") |
1294 | | - self.assertIsNotInterned("not.internable") |
| 1266 | + noninternable_strings = ( |
| 1267 | + "not-internable", |
| 1268 | + "not.internable", |
| 1269 | + "не_интернируемый", |
| 1270 | + "", |
| 1271 | + ) |
| 1272 | + for noninternable in noninternable_strings: |
| 1273 | + with self.subTest(noninternable=noninternable): |
| 1274 | + self.assertIsNotInterned(eval(f"'{noninternable}'")) |
1295 | 1275 |
|
1296 | 1276 | class CodeWeakRefTest(unittest.TestCase): |
1297 | 1277 |
|
|
0 commit comments