Skip to content

Commit 6afb285

Browse files
bpo-45020: Add tests for the -X "frozen_modules" option. (gh-28997)
We hadn't explicitly added any tests for this, so here they are. https://bugs.python.org/issue45020
1 parent 2b8677a commit 6afb285

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

Lib/test/test_cmd_line.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,21 @@ def run_python(*args):
123123
else:
124124
self.assertEqual(err, b'')
125125

126+
def test_xoption_frozen_modules(self):
127+
tests = {
128+
('=on', 'FrozenImporter'),
129+
('=off', 'SourceFileLoader'),
130+
('=', 'FrozenImporter'),
131+
('', 'FrozenImporter'),
132+
}
133+
for raw, expected in tests:
134+
cmd = ['-X', f'frozen_modules{raw}',
135+
#'-c', 'import os; print(os.__spec__.loader.__name__, end="")']
136+
'-c', 'import os; print(os.__spec__.loader, end="")']
137+
with self.subTest(raw):
138+
res = assert_python_ok(*cmd)
139+
self.assertRegex(res.out.decode('utf-8'), expected)
140+
126141
def test_run_module(self):
127142
# Test expected operation of the '-m' switch
128143
# Switch needs an argument

Lib/test/test_embed.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,6 +1466,30 @@ def test_get_argc_argv(self):
14661466
self.run_embedded_interpreter("test_get_argc_argv")
14671467
# ignore output
14681468

1469+
def test_init_use_frozen_modules(self):
1470+
tests = {
1471+
('=on', 1),
1472+
('=off', 0),
1473+
('=', 1),
1474+
('', 1),
1475+
}
1476+
for raw, expected in tests:
1477+
optval = f'frozen_modules{raw}'
1478+
config = {
1479+
'parse_argv': 2,
1480+
'argv': ['-c'],
1481+
'orig_argv': ['./argv0', '-X', optval, '-c', 'pass'],
1482+
'program_name': './argv0',
1483+
'run_command': 'pass\n',
1484+
'use_environment': 1,
1485+
'xoptions': [optval],
1486+
'use_frozen_modules': expected,
1487+
}
1488+
env = {'TESTFROZEN': raw[1:]} if raw else None
1489+
with self.subTest(repr(raw)):
1490+
self.check_all_configs("test_init_use_frozen_modules", config,
1491+
api=API_PYTHON, env=env)
1492+
14691493

14701494
class SetConfigTests(unittest.TestCase):
14711495
def test_set_config(self):

Programs/_testembed.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,6 +1726,44 @@ static int test_get_argc_argv(void)
17261726
}
17271727

17281728

1729+
static int check_use_frozen_modules(const char *rawval)
1730+
{
1731+
wchar_t optval[100];
1732+
if (rawval == NULL) {
1733+
wcscpy(optval, L"frozen_modules");
1734+
}
1735+
else if (swprintf(optval, 100, L"frozen_modules=%s", rawval) < 0) {
1736+
error("rawval is too long");
1737+
return -1;
1738+
}
1739+
1740+
PyConfig config;
1741+
PyConfig_InitPythonConfig(&config);
1742+
1743+
config.parse_argv = 1;
1744+
1745+
wchar_t* argv[] = {
1746+
L"./argv0",
1747+
L"-X",
1748+
optval,
1749+
L"-c",
1750+
L"pass",
1751+
};
1752+
config_set_argv(&config, Py_ARRAY_LENGTH(argv), argv);
1753+
init_from_config_clear(&config);
1754+
1755+
dump_config();
1756+
Py_Finalize();
1757+
return 0;
1758+
}
1759+
1760+
static int test_init_use_frozen_modules(void)
1761+
{
1762+
const char *envvar = getenv("TESTFROZEN");
1763+
return check_use_frozen_modules(envvar);
1764+
}
1765+
1766+
17291767
static int test_unicode_id_init(void)
17301768
{
17311769
// bpo-42882: Test that _PyUnicode_FromId() works
@@ -1912,6 +1950,7 @@ static struct TestCase TestCases[] = {
19121950
{"test_run_main", test_run_main},
19131951
{"test_run_main_loop", test_run_main_loop},
19141952
{"test_get_argc_argv", test_get_argc_argv},
1953+
{"test_init_use_frozen_modules", test_init_use_frozen_modules},
19151954

19161955
// Audit
19171956
{"test_open_code_hook", test_open_code_hook},

0 commit comments

Comments
 (0)