diff options
author | Victor Stinner <vstinner@redhat.com> | 2018-07-26 16:57:56 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-26 16:57:56 (GMT) |
commit | 56b29b6d6fa3eb32bb1533ee3f21b1e7135648a0 (patch) | |
tree | 9c9ee5697c1c1dc873a21617fd4e9de30d04f0b2 /Lib | |
parent | e42b705188271da108de42b55d9344642170aa2b (diff) | |
download | cpython-56b29b6d6fa3eb32bb1533ee3f21b1e7135648a0.zip cpython-56b29b6d6fa3eb32bb1533ee3f21b1e7135648a0.tar.gz cpython-56b29b6d6fa3eb32bb1533ee3f21b1e7135648a0.tar.bz2 |
bpo-34170, test_embed: write Py_Initialize() tests (GH-8484)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_embed.py | 159 |
1 files changed, 158 insertions, 1 deletions
diff --git a/Lib/test/test_embed.py b/Lib/test/test_embed.py index 024c3f9..e00b1d8 100644 --- a/Lib/test/test_embed.py +++ b/Lib/test/test_embed.py @@ -9,7 +9,7 @@ import subprocess import sys -class EmbeddingTests(unittest.TestCase): +class EmbeddingTestsMixin: def setUp(self): here = os.path.abspath(__file__) basepath = os.path.dirname(os.path.dirname(os.path.dirname(here))) @@ -110,6 +110,8 @@ class EmbeddingTests(unittest.TestCase): yield current_run current_run = [] + +class EmbeddingTests(EmbeddingTestsMixin, unittest.TestCase): def test_subinterps_main(self): for run in self.run_repeated_init_and_subinterpreters(): main = run[0] @@ -247,5 +249,160 @@ class EmbeddingTests(unittest.TestCase): self.assertEqual(err, '') +class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase): + maxDiff = 4096 + DEFAULT_CONFIG = { + 'install_signal_handlers': 1, + 'use_environment': 1, + 'use_hash_seed': 0, + 'hash_seed': 0, + 'allocator': '(null)', + 'dev_mode': 0, + 'faulthandler': 0, + 'tracemalloc': 0, + 'import_time': 0, + 'show_ref_count': 0, + 'show_alloc_count': 0, + 'dump_refs': 0, + 'malloc_stats': 0, + 'utf8_mode': 0, + + 'coerce_c_locale': 0, + 'coerce_c_locale_warn': 0, + + 'pycache_prefix': '(null)', + 'program_name': './_testembed', + 'program': '(null)', + + 'isolated': 0, + 'site_import': 1, + 'bytes_warning': 0, + 'inspect': 0, + 'interactive': 0, + 'optimization_level': 0, + 'debug': 0, + 'write_bytecode': 1, + 'verbose': 0, + 'quiet': 0, + 'user_site_directory': 1, + 'unbuffered_stdio': 0, + + '_install_importlib': 1, + '_check_hash_pycs_mode': 'default', + } + + def check_config(self, testname, expected): + env = dict(os.environ) + for key in list(env): + if key.startswith('PYTHON'): + del env[key] + # Disable C locale coercion and UTF-8 mode to not depend + # on the current locale + env['PYTHONCOERCECLOCALE'] = '0' + env['PYTHONUTF8'] = '0' + out, err = self.run_embedded_interpreter(testname, env=env) + # Ignore err + + expected = dict(self.DEFAULT_CONFIG, **expected) + for key, value in expected.items(): + expected[key] = str(value) + + config = {} + for line in out.splitlines(): + key, value = line.split(' = ', 1) + config[key] = value + self.assertEqual(config, expected) + + def test_init_default_config(self): + self.check_config("init_default_config", {}) + + def test_init_global_config(self): + config = { + 'program_name': './globalvar', + 'site_import': 0, + 'bytes_warning': 1, + 'inspect': 1, + 'interactive': 1, + 'optimization_level': 2, + 'write_bytecode': 0, + 'verbose': 1, + 'quiet': 1, + 'unbuffered_stdio': 1, + 'utf8_mode': 1, + 'user_site_directory': 0, + } + self.check_config("init_global_config", config) + + def test_init_from_config(self): + config = { + 'install_signal_handlers': 0, + 'use_hash_seed': 1, + 'hash_seed': 123, + 'allocator': 'malloc_debug', + 'tracemalloc': 2, + 'import_time': 1, + 'show_ref_count': 1, + 'show_alloc_count': 1, + 'malloc_stats': 1, + + 'utf8_mode': 1, + + 'pycache_prefix': 'conf_pycache_prefix', + 'program_name': './conf_program_name', + 'program': 'conf_program', + + 'site_import': 0, + 'bytes_warning': 1, + 'inspect': 1, + 'interactive': 1, + 'optimization_level': 2, + 'write_bytecode': 0, + 'verbose': 1, + 'quiet': 1, + 'unbuffered_stdio': 1, + 'user_site_directory': 0, + 'faulthandler': 1, + '_check_hash_pycs_mode': 'always', + } + self.check_config("init_from_config", config) + + def test_init_env(self): + config = { + 'use_hash_seed': 1, + 'hash_seed': 42, + 'allocator': 'malloc_debug', + 'tracemalloc': 2, + 'import_time': 1, + 'malloc_stats': 1, + 'utf8_mode': 1, + 'inspect': 1, + 'optimization_level': 2, + 'pycache_prefix': 'env_pycache_prefix', + 'write_bytecode': 0, + 'verbose': 1, + 'unbuffered_stdio': 1, + 'user_site_directory': 0, + 'faulthandler': 1, + 'dev_mode': 1, + } + self.check_config("init_env", config) + + def test_init_dev_mode(self): + config = { + 'dev_mode': 1, + 'faulthandler': 1, + 'allocator': 'debug', + } + self.check_config("init_dev_mode", config) + + def test_init_isolated(self): + config = { + 'isolated': 1, + 'use_environment': 0, + 'user_site_directory': 0, + } + self.check_config("init_isolated", config) + + if __name__ == "__main__": unittest.main() |