]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-45020: Add tests for the -X "frozen_modules" option. (gh-28997)
authorEric Snow <ericsnowcurrently@gmail.com>
Mon, 25 Oct 2021 21:26:41 +0000 (15:26 -0600)
committerGitHub <noreply@github.com>
Mon, 25 Oct 2021 21:26:41 +0000 (15:26 -0600)
We hadn't explicitly added any tests for this, so here they are.

https://bugs.python.org/issue45020

Lib/test/test_cmd_line.py
Lib/test/test_embed.py
Programs/_testembed.c

index 1dc8c45885cbefd944e77d34d668233e0071c934..86ee27485c964217b5a4ff363ad40d4258edfa7d 100644 (file)
@@ -123,6 +123,21 @@ class CmdLineTest(unittest.TestCase):
         else:
             self.assertEqual(err, b'')
 
+    def test_xoption_frozen_modules(self):
+        tests = {
+            ('=on', 'FrozenImporter'),
+            ('=off', 'SourceFileLoader'),
+            ('=', 'FrozenImporter'),
+            ('', 'FrozenImporter'),
+        }
+        for raw, expected in tests:
+            cmd = ['-X', f'frozen_modules{raw}',
+                   #'-c', 'import os; print(os.__spec__.loader.__name__, end="")']
+                   '-c', 'import os; print(os.__spec__.loader, end="")']
+            with self.subTest(raw):
+                res = assert_python_ok(*cmd)
+                self.assertRegex(res.out.decode('utf-8'), expected)
+
     def test_run_module(self):
         # Test expected operation of the '-m' switch
         # Switch needs an argument
index 4186f011e2388a4a707ac6433273b5d652d21314..7858f68d971cdab5b796a9c12fe688ea018f2905 100644 (file)
@@ -1466,6 +1466,30 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
         self.run_embedded_interpreter("test_get_argc_argv")
         # ignore output
 
+    def test_init_use_frozen_modules(self):
+        tests = {
+            ('=on', 1),
+            ('=off', 0),
+            ('=', 1),
+            ('', 1),
+        }
+        for raw, expected in tests:
+            optval = f'frozen_modules{raw}'
+            config = {
+                'parse_argv': 2,
+                'argv': ['-c'],
+                'orig_argv': ['./argv0', '-X', optval, '-c', 'pass'],
+                'program_name': './argv0',
+                'run_command': 'pass\n',
+                'use_environment': 1,
+                'xoptions': [optval],
+                'use_frozen_modules': expected,
+            }
+            env = {'TESTFROZEN': raw[1:]} if raw else None
+            with self.subTest(repr(raw)):
+                self.check_all_configs("test_init_use_frozen_modules", config,
+                                       api=API_PYTHON, env=env)
+
 
 class SetConfigTests(unittest.TestCase):
     def test_set_config(self):
index b61fe3461bdc98dd292471dbbf76f888e5af10b7..773c6c3e9900a5a562b72107cb09ab9f8ca38af5 100644 (file)
@@ -1726,6 +1726,44 @@ static int test_get_argc_argv(void)
 }
 
 
+static int check_use_frozen_modules(const char *rawval)
+{
+    wchar_t optval[100];
+    if (rawval == NULL) {
+        wcscpy(optval, L"frozen_modules");
+    }
+    else if (swprintf(optval, 100, L"frozen_modules=%s", rawval) < 0) {
+        error("rawval is too long");
+        return -1;
+    }
+
+    PyConfig config;
+    PyConfig_InitPythonConfig(&config);
+
+    config.parse_argv = 1;
+
+    wchar_t* argv[] = {
+        L"./argv0",
+        L"-X",
+        optval,
+        L"-c",
+        L"pass",
+    };
+    config_set_argv(&config, Py_ARRAY_LENGTH(argv), argv);
+    init_from_config_clear(&config);
+
+    dump_config();
+    Py_Finalize();
+    return 0;
+}
+
+static int test_init_use_frozen_modules(void)
+{
+    const char *envvar = getenv("TESTFROZEN");
+    return check_use_frozen_modules(envvar);
+}
+
+
 static int test_unicode_id_init(void)
 {
     // bpo-42882: Test that _PyUnicode_FromId() works
@@ -1912,6 +1950,7 @@ static struct TestCase TestCases[] = {
     {"test_run_main", test_run_main},
     {"test_run_main_loop", test_run_main_loop},
     {"test_get_argc_argv", test_get_argc_argv},
+    {"test_init_use_frozen_modules", test_init_use_frozen_modules},
 
     // Audit
     {"test_open_code_hook", test_open_code_hook},