diff options
author | Nick Coghlan <ncoghlan@gmail.com> | 2018-03-25 10:44:30 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-25 10:44:30 (GMT) |
commit | bc77eff8b96be4f035e665ab35c1d06e22f46491 (patch) | |
tree | 68ca923578c2ac466dace4ce07b1571c77bae519 /Lib | |
parent | d02ac25ab0879f1a6de6937573bf00a16b7bd22e (diff) | |
download | cpython-bc77eff8b96be4f035e665ab35c1d06e22f46491.zip cpython-bc77eff8b96be4f035e665ab35c1d06e22f46491.tar.gz cpython-bc77eff8b96be4f035e665ab35c1d06e22f46491.tar.bz2 |
bpo-33042: Fix pre-initialization sys module configuration (GH-6157)
- new test case for pre-initialization of sys.warnoptions and sys._xoptions
- restored ability to call these APIs prior to Py_Initialize
- updated the docs for the affected APIs to make it clear they can be
called before Py_Initialize
- also enhanced the existing embedding test cases
to check for expected settings in the sys module
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_embed.py | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/Lib/test/test_embed.py b/Lib/test/test_embed.py index c7f45b5..f926301 100644 --- a/Lib/test/test_embed.py +++ b/Lib/test/test_embed.py @@ -51,7 +51,7 @@ class EmbeddingTests(unittest.TestCase): if p.returncode != 0 and support.verbose: print(f"--- {cmd} failed ---") print(f"stdout:\n{out}") - print(f"stderr:\n{out}") + print(f"stderr:\n{err}") print(f"------") self.assertEqual(p.returncode, 0, @@ -83,7 +83,7 @@ class EmbeddingTests(unittest.TestCase): for line in out.splitlines(): if line == "--- Pass {} ---".format(numloops): self.assertEqual(len(current_run), 0) - if support.verbose: + if support.verbose > 1: print(line) numloops += 1 continue @@ -96,7 +96,7 @@ class EmbeddingTests(unittest.TestCase): # Parse the line from the loop. The first line is the main # interpreter and the 3 afterward are subinterpreters. interp = Interp(*match.groups()) - if support.verbose: + if support.verbose > 1: print(interp) self.assertTrue(interp.interp) self.assertTrue(interp.tstate) @@ -190,12 +190,33 @@ class EmbeddingTests(unittest.TestCase): def test_pre_initialization_api(self): """ - Checks the few parts of the C-API that work before the runtine + Checks some key parts of the C-API that need to work before the runtine is initialized (via Py_Initialize()). """ env = dict(os.environ, PYTHONPATH=os.pathsep.join(sys.path)) out, err = self.run_embedded_interpreter("pre_initialization_api", env=env) - self.assertEqual(out, '') + if sys.platform == "win32": + expected_path = self.test_exe + else: + expected_path = os.path.join(os.getcwd(), "spam") + expected_output = f"sys.executable: {expected_path}\n" + self.assertIn(expected_output, out) + self.assertEqual(err, '') + + def test_pre_initialization_sys_options(self): + """ + Checks that sys.warnoptions and sys._xoptions can be set before the + runtime is initialized (otherwise they won't be effective). + """ + env = dict(PYTHONPATH=os.pathsep.join(sys.path)) + out, err = self.run_embedded_interpreter( + "pre_initialization_sys_options", env=env) + expected_output = ( + "sys.warnoptions: ['once', 'module', 'default']\n" + "sys._xoptions: {'not_an_option': '1', 'also_not_an_option': '2'}\n" + "warnings.filters[:3]: ['default', 'module', 'once']\n" + ) + self.assertIn(expected_output, out) self.assertEqual(err, '') def test_bpo20891(self): |