Skip to content

Commit cc63fa2

Browse files
initial tests
1 parent 485414c commit cc63fa2

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Lib/test/test_code.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,34 @@ def test_stateless(self):
11681168
def isinterned(s):
11691169
return s is sys.intern(('_' + s + '_')[1:-1])
11701170

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+
11711199
class CodeConstsTest(unittest.TestCase):
11721200

11731201
def find_const(self, consts, value):
@@ -1251,6 +1279,10 @@ class MyInt(int):
12511279
self.assertIsInstance(code.co_consts[1], Unhashable)
12521280
self.assertEqual(code.co_consts[2], code.co_consts[3])
12531281

1282+
@cpython_only
1283+
def test__Py_DECLARE_STR_is_interned(self):
1284+
for global_string in iter_global_strings():
1285+
self.assertIsInterned(global_string)
12541286

12551287
class CodeWeakRefTest(unittest.TestCase):
12561288

0 commit comments

Comments
 (0)