Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 56b29b6

Browse files
authored
bpo-34170, test_embed: write Py_Initialize() tests (GH-8484)
1 parent e42b705 commit 56b29b6

3 files changed

Lines changed: 495 additions & 2 deletions

File tree

Include/pystate.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ typedef struct {
5555
int malloc_stats; /* PYTHONMALLOCSTATS */
5656
int coerce_c_locale; /* PYTHONCOERCECLOCALE, -1 means unknown */
5757
int coerce_c_locale_warn; /* PYTHONCOERCECLOCALE=warn */
58-
int utf8_mode; /* PYTHONUTF8, -X utf8; -1 means unknown */
58+
59+
/* Enable UTF-8 mode?
60+
Set by -X utf8 command line option and PYTHONUTF8 environment variable.
61+
If set to -1 (default), inherit Py_UTF8Mode value. */
62+
int utf8_mode;
63+
5964
wchar_t *pycache_prefix; /* PYTHONPYCACHEPREFIX, -X pycache_prefix=PATH */
6065

6166
wchar_t *program_name; /* Program name, see also Py_GetProgramName() */

Lib/test/test_embed.py

Lines changed: 158 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import sys
1010

1111

12-
class EmbeddingTests(unittest.TestCase):
12+
class EmbeddingTestsMixin:
1313
def setUp(self):
1414
here = os.path.abspath(__file__)
1515
basepath = os.path.dirname(os.path.dirname(os.path.dirname(here)))
@@ -110,6 +110,8 @@ def run_repeated_init_and_subinterpreters(self):
110110
yield current_run
111111
current_run = []
112112

113+
114+
class EmbeddingTests(EmbeddingTestsMixin, unittest.TestCase):
113115
def test_subinterps_main(self):
114116
for run in self.run_repeated_init_and_subinterpreters():
115117
main = run[0]
@@ -247,5 +249,160 @@ def test_initialize_pymain(self):
247249
self.assertEqual(err, '')
248250

249251

252+
class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
253+
maxDiff = 4096
254+
DEFAULT_CONFIG = {
255+
'install_signal_handlers': 1,
256+
'use_environment': 1,
257+
'use_hash_seed': 0,
258+
'hash_seed': 0,
259+
'allocator': '(null)',
260+
'dev_mode': 0,
261+
'faulthandler': 0,
262+
'tracemalloc': 0,
263+
'import_time': 0,
264+
'show_ref_count': 0,
265+
'show_alloc_count': 0,
266+
'dump_refs': 0,
267+
'malloc_stats': 0,
268+
'utf8_mode': 0,
269+
270+
'coerce_c_locale': 0,
271+
'coerce_c_locale_warn': 0,
272+
273+
'pycache_prefix': '(null)',
274+
'program_name': './_testembed',
275+
'program': '(null)',
276+
277+
'isolated': 0,
278+
'site_import': 1,
279+
'bytes_warning': 0,
280+
'inspect': 0,
281+
'interactive': 0,
282+
'optimization_level': 0,
283+
'debug': 0,
284+
'write_bytecode': 1,
285+
'verbose': 0,
286+
'quiet': 0,
287+
'user_site_directory': 1,
288+
'unbuffered_stdio': 0,
289+
290+
'_install_importlib': 1,
291+
'_check_hash_pycs_mode': 'default',
292+
}
293+
294+
def check_config(self, testname, expected):
295+
env = dict(os.environ)
296+
for key in list(env):
297+
if key.startswith('PYTHON'):
298+
del env[key]
299+
# Disable C locale coercion and UTF-8 mode to not depend
300+
# on the current locale
301+
env['PYTHONCOERCECLOCALE'] = '0'
302+
env['PYTHONUTF8'] = '0'
303+
out, err = self.run_embedded_interpreter(testname, env=env)
304+
# Ignore err
305+
306+
expected = dict(self.DEFAULT_CONFIG, **expected)
307+
for key, value in expected.items():
308+
expected[key] = str(value)
309+
310+
config = {}
311+
for line in out.splitlines():
312+
key, value = line.split(' = ', 1)
313+
config[key] = value
314+
self.assertEqual(config, expected)
315+
316+
def test_init_default_config(self):
317+
self.check_config("init_default_config", {})
318+
319+
def test_init_global_config(self):
320+
config = {
321+
'program_name': './globalvar',
322+
'site_import': 0,
323+
'bytes_warning': 1,
324+
'inspect': 1,
325+
'interactive': 1,
326+
'optimization_level': 2,
327+
'write_bytecode': 0,
328+
'verbose': 1,
329+
'quiet': 1,
330+
'unbuffered_stdio': 1,
331+
'utf8_mode': 1,
332+
'user_site_directory': 0,
333+
}
334+
self.check_config("init_global_config", config)
335+
336+
def test_init_from_config(self):
337+
config = {
338+
'install_signal_handlers': 0,
339+
'use_hash_seed': 1,
340+
'hash_seed': 123,
341+
'allocator': 'malloc_debug',
342+
'tracemalloc': 2,
343+
'import_time': 1,
344+
'show_ref_count': 1,
345+
'show_alloc_count': 1,
346+
'malloc_stats': 1,
347+
348+
'utf8_mode': 1,
349+
350+
'pycache_prefix': 'conf_pycache_prefix',
351+
'program_name': './conf_program_name',
352+
'program': 'conf_program',
353+
354+
'site_import': 0,
355+
'bytes_warning': 1,
356+
'inspect': 1,
357+
'interactive': 1,
358+
'optimization_level': 2,
359+
'write_bytecode': 0,
360+
'verbose': 1,
361+
'quiet': 1,
362+
'unbuffered_stdio': 1,
363+
'user_site_directory': 0,
364+
'faulthandler': 1,
365+
'_check_hash_pycs_mode': 'always',
366+
}
367+
self.check_config("init_from_config", config)
368+
369+
def test_init_env(self):
370+
config = {
371+
'use_hash_seed': 1,
372+
'hash_seed': 42,
373+
'allocator': 'malloc_debug',
374+
'tracemalloc': 2,
375+
'import_time': 1,
376+
'malloc_stats': 1,
377+
'utf8_mode': 1,
378+
'inspect': 1,
379+
'optimization_level': 2,
380+
'pycache_prefix': 'env_pycache_prefix',
381+
'write_bytecode': 0,
382+
'verbose': 1,
383+
'unbuffered_stdio': 1,
384+
'user_site_directory': 0,
385+
'faulthandler': 1,
386+
'dev_mode': 1,
387+
}
388+
self.check_config("init_env", config)
389+
390+
def test_init_dev_mode(self):
391+
config = {
392+
'dev_mode': 1,
393+
'faulthandler': 1,
394+
'allocator': 'debug',
395+
}
396+
self.check_config("init_dev_mode", config)
397+
398+
def test_init_isolated(self):
399+
config = {
400+
'isolated': 1,
401+
'use_environment': 0,
402+
'user_site_directory': 0,
403+
}
404+
self.check_config("init_isolated", config)
405+
406+
250407
if __name__ == "__main__":
251408
unittest.main()

0 commit comments

Comments
 (0)