Skip to content

Commit a1655f1

Browse files
reorginize tests
1 parent 9ec2e09 commit a1655f1

File tree

3 files changed

+56
-33
lines changed

3 files changed

+56
-33
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import re
2+
from pathlib import Path
3+
4+
from typing import Iterable
5+
6+
from test.support.project_files_helper import iter_all_c_files
7+
8+
9+
def iter_global_strings() -> Iterable[str]:
10+
id_regex = re.compile(r"\b_Py_ID\((\w+)\)")
11+
str_regex = re.compile(r'\b_Py_DECLARE_STR\((?:\w+), "(.*?)"\)')
12+
for filename in iter_all_c_files():
13+
infile = Path(filename)
14+
if not infile.exists():
15+
# The file must have been a temporary file.
16+
continue
17+
with infile.open(encoding="utf-8") as infile_open:
18+
for line in infile_open:
19+
for m in id_regex.finditer(line):
20+
yield m.group(1)
21+
for m in str_regex.finditer(line):
22+
yield m.group(1)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from pathlib import Path
2+
3+
from typing import Iterable
4+
5+
python_root = Path(__file__).resolve().parents[3]
6+
7+
8+
def iter_all_c_files() -> Iterable[Path]:
9+
for top_directory_name in (
10+
"Modules",
11+
"Objects",
12+
"Parser",
13+
"PC",
14+
"Programs",
15+
"Python",
16+
):
17+
for dirname, _, files in (python_root / top_directory_name).walk():
18+
for name in files:
19+
if not name.endswith((".c", ".h")):
20+
continue
21+
yield dirname / name

Lib/test/test_code.py

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@
204204
import weakref
205205
import dis
206206

207+
from test.support.constants_helper import iter_global_strings
208+
207209
try:
208210
import ctypes
209211
except ImportError:
@@ -1168,34 +1170,6 @@ def test_stateless(self):
11681170
def isinterned(s):
11691171
return s is sys.intern(('_' + s + '_')[1:-1])
11701172

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-
11991173
class CodeConstsTest(unittest.TestCase):
12001174

12011175
def find_const(self, consts, value):
@@ -1283,15 +1257,21 @@ class MyInt(int):
12831257
@unittest.skipIf(Py_GIL_DISABLED, "free-threaded build interns all string constants")
12841258
def test__Py_DECLARE_STR_is_interned(self):
12851259
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}'"))
12891262

12901263
@cpython_only
12911264
@unittest.skipIf(Py_GIL_DISABLED, "free-threaded build interns all string constants")
12921265
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}'"))
12951275

12961276
class CodeWeakRefTest(unittest.TestCase):
12971277

0 commit comments

Comments
 (0)