2017-11-27 18:11:51 -04:00
|
|
|
# Run the tests in Programs/_testembed.c (tests for the CPython embedding APIs)
|
|
|
|
from test import support
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
from collections import namedtuple
|
2018-11-13 19:24:28 -04:00
|
|
|
import json
|
2017-11-27 18:11:51 -04:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2018-11-26 06:54:12 -04:00
|
|
|
import textwrap
|
2017-11-27 18:11:51 -04:00
|
|
|
|
|
|
|
|
2018-11-14 12:39:45 -04:00
|
|
|
MS_WINDOWS = (os.name == 'nt')
|
2019-05-22 18:58:50 -03:00
|
|
|
|
2019-05-17 10:20:52 -03:00
|
|
|
PYMEM_ALLOCATOR_NOT_SET = 0
|
|
|
|
PYMEM_ALLOCATOR_DEBUG = 2
|
|
|
|
PYMEM_ALLOCATOR_MALLOC = 3
|
2018-11-14 12:39:45 -04:00
|
|
|
|
2019-05-22 18:58:50 -03:00
|
|
|
# _PyCoreConfig_InitCompatConfig()
|
|
|
|
API_COMPAT = 1
|
|
|
|
# _PyCoreConfig_InitPythonConfig()
|
|
|
|
API_PYTHON = 2
|
|
|
|
# _PyCoreConfig_InitIsolatedConfig()
|
|
|
|
API_ISOLATED = 3
|
2019-05-20 06:02:00 -03:00
|
|
|
|
2018-11-14 12:39:45 -04:00
|
|
|
|
2019-06-17 19:11:00 -03:00
|
|
|
def remove_python_envvars():
|
|
|
|
env = dict(os.environ)
|
|
|
|
# Remove PYTHON* environment variables to get deterministic environment
|
|
|
|
for key in list(env):
|
|
|
|
if key.startswith('PYTHON'):
|
|
|
|
del env[key]
|
|
|
|
return env
|
|
|
|
|
|
|
|
|
2018-07-26 13:57:56 -03:00
|
|
|
class EmbeddingTestsMixin:
|
2017-11-27 18:11:51 -04:00
|
|
|
def setUp(self):
|
|
|
|
here = os.path.abspath(__file__)
|
|
|
|
basepath = os.path.dirname(os.path.dirname(os.path.dirname(here)))
|
|
|
|
exename = "_testembed"
|
2018-11-14 12:39:45 -04:00
|
|
|
if MS_WINDOWS:
|
2017-11-27 18:11:51 -04:00
|
|
|
ext = ("_d" if "_d" in sys.executable else "") + ".exe"
|
|
|
|
exename += ext
|
|
|
|
exepath = os.path.dirname(sys.executable)
|
|
|
|
else:
|
|
|
|
exepath = os.path.join(basepath, "Programs")
|
|
|
|
self.test_exe = exe = os.path.join(exepath, exename)
|
|
|
|
if not os.path.exists(exe):
|
|
|
|
self.skipTest("%r doesn't exist" % exe)
|
|
|
|
# This is needed otherwise we get a fatal error:
|
|
|
|
# "Py_Initialize: Unable to get the locale encoding
|
|
|
|
# LookupError: no codec search functions registered: can't find encoding"
|
|
|
|
self.oldcwd = os.getcwd()
|
|
|
|
os.chdir(basepath)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
os.chdir(self.oldcwd)
|
|
|
|
|
2019-07-01 20:03:53 -03:00
|
|
|
def run_embedded_interpreter(self, *args, env=None,
|
|
|
|
timeout=None, returncode=0, input=None):
|
2017-11-27 18:11:51 -04:00
|
|
|
"""Runs a test in the embedded interpreter"""
|
|
|
|
cmd = [self.test_exe]
|
|
|
|
cmd.extend(args)
|
2018-11-14 12:39:45 -04:00
|
|
|
if env is not None and MS_WINDOWS:
|
2017-11-27 18:11:51 -04:00
|
|
|
# Windows requires at least the SYSTEMROOT environment variable to
|
|
|
|
# start Python.
|
|
|
|
env = env.copy()
|
|
|
|
env['SYSTEMROOT'] = os.environ['SYSTEMROOT']
|
|
|
|
|
|
|
|
p = subprocess.Popen(cmd,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
universal_newlines=True,
|
|
|
|
env=env)
|
2019-03-29 11:13:46 -03:00
|
|
|
try:
|
2019-07-01 20:03:53 -03:00
|
|
|
(out, err) = p.communicate(input=input, timeout=timeout)
|
2019-03-29 11:13:46 -03:00
|
|
|
except:
|
|
|
|
p.terminate()
|
|
|
|
p.wait()
|
|
|
|
raise
|
2019-07-01 20:03:53 -03:00
|
|
|
if p.returncode != returncode and support.verbose:
|
2017-11-27 18:11:51 -04:00
|
|
|
print(f"--- {cmd} failed ---")
|
|
|
|
print(f"stdout:\n{out}")
|
2018-03-25 07:44:30 -03:00
|
|
|
print(f"stderr:\n{err}")
|
2017-11-27 18:11:51 -04:00
|
|
|
print(f"------")
|
|
|
|
|
2019-07-01 20:03:53 -03:00
|
|
|
self.assertEqual(p.returncode, returncode,
|
2017-11-27 18:11:51 -04:00
|
|
|
"bad returncode %d, stderr is %r" %
|
|
|
|
(p.returncode, err))
|
|
|
|
return out, err
|
|
|
|
|
|
|
|
def run_repeated_init_and_subinterpreters(self):
|
2019-05-22 19:57:57 -03:00
|
|
|
out, err = self.run_embedded_interpreter("test_repeated_init_and_subinterpreters")
|
2017-11-27 18:11:51 -04:00
|
|
|
self.assertEqual(err, "")
|
|
|
|
|
|
|
|
# The output from _testembed looks like this:
|
|
|
|
# --- Pass 0 ---
|
|
|
|
# interp 0 <0x1cf9330>, thread state <0x1cf9700>: id(modules) = 139650431942728
|
|
|
|
# interp 1 <0x1d4f690>, thread state <0x1d35350>: id(modules) = 139650431165784
|
|
|
|
# interp 2 <0x1d5a690>, thread state <0x1d99ed0>: id(modules) = 139650413140368
|
|
|
|
# interp 3 <0x1d4f690>, thread state <0x1dc3340>: id(modules) = 139650412862200
|
|
|
|
# interp 0 <0x1cf9330>, thread state <0x1cf9700>: id(modules) = 139650431942728
|
|
|
|
# --- Pass 1 ---
|
|
|
|
# ...
|
|
|
|
|
|
|
|
interp_pat = (r"^interp (\d+) <(0x[\dA-F]+)>, "
|
|
|
|
r"thread state <(0x[\dA-F]+)>: "
|
|
|
|
r"id\(modules\) = ([\d]+)$")
|
|
|
|
Interp = namedtuple("Interp", "id interp tstate modules")
|
|
|
|
|
|
|
|
numloops = 0
|
|
|
|
current_run = []
|
|
|
|
for line in out.splitlines():
|
|
|
|
if line == "--- Pass {} ---".format(numloops):
|
|
|
|
self.assertEqual(len(current_run), 0)
|
2018-03-25 07:44:30 -03:00
|
|
|
if support.verbose > 1:
|
2017-11-27 18:11:51 -04:00
|
|
|
print(line)
|
|
|
|
numloops += 1
|
|
|
|
continue
|
|
|
|
|
|
|
|
self.assertLess(len(current_run), 5)
|
|
|
|
match = re.match(interp_pat, line)
|
|
|
|
if match is None:
|
|
|
|
self.assertRegex(line, interp_pat)
|
|
|
|
|
|
|
|
# Parse the line from the loop. The first line is the main
|
|
|
|
# interpreter and the 3 afterward are subinterpreters.
|
|
|
|
interp = Interp(*match.groups())
|
2018-03-25 07:44:30 -03:00
|
|
|
if support.verbose > 1:
|
2017-11-27 18:11:51 -04:00
|
|
|
print(interp)
|
|
|
|
self.assertTrue(interp.interp)
|
|
|
|
self.assertTrue(interp.tstate)
|
|
|
|
self.assertTrue(interp.modules)
|
|
|
|
current_run.append(interp)
|
|
|
|
|
|
|
|
# The last line in the loop should be the same as the first.
|
|
|
|
if len(current_run) == 5:
|
|
|
|
main = current_run[0]
|
|
|
|
self.assertEqual(interp, main)
|
|
|
|
yield current_run
|
|
|
|
current_run = []
|
|
|
|
|
2018-07-26 13:57:56 -03:00
|
|
|
|
|
|
|
class EmbeddingTests(EmbeddingTestsMixin, unittest.TestCase):
|
2017-11-27 18:11:51 -04:00
|
|
|
def test_subinterps_main(self):
|
|
|
|
for run in self.run_repeated_init_and_subinterpreters():
|
|
|
|
main = run[0]
|
|
|
|
|
|
|
|
self.assertEqual(main.id, '0')
|
|
|
|
|
|
|
|
def test_subinterps_different_ids(self):
|
|
|
|
for run in self.run_repeated_init_and_subinterpreters():
|
|
|
|
main, *subs, _ = run
|
|
|
|
|
|
|
|
mainid = int(main.id)
|
|
|
|
for i, sub in enumerate(subs):
|
|
|
|
self.assertEqual(sub.id, str(mainid + i + 1))
|
|
|
|
|
|
|
|
def test_subinterps_distinct_state(self):
|
|
|
|
for run in self.run_repeated_init_and_subinterpreters():
|
|
|
|
main, *subs, _ = run
|
|
|
|
|
|
|
|
if '0x0' in main:
|
|
|
|
# XXX Fix on Windows (and other platforms): something
|
|
|
|
# is going on with the pointers in Programs/_testembed.c.
|
|
|
|
# interp.interp is 0x0 and interp.modules is the same
|
|
|
|
# between interpreters.
|
|
|
|
raise unittest.SkipTest('platform prints pointers as 0x0')
|
|
|
|
|
|
|
|
for sub in subs:
|
|
|
|
# A new subinterpreter may have the same
|
|
|
|
# PyInterpreterState pointer as a previous one if
|
|
|
|
# the earlier one has already been destroyed. So
|
|
|
|
# we compare with the main interpreter. The same
|
|
|
|
# applies to tstate.
|
|
|
|
self.assertNotEqual(sub.interp, main.interp)
|
|
|
|
self.assertNotEqual(sub.tstate, main.tstate)
|
|
|
|
self.assertNotEqual(sub.modules, main.modules)
|
|
|
|
|
|
|
|
def test_forced_io_encoding(self):
|
|
|
|
# Checks forced configuration of embedded interpreter IO streams
|
|
|
|
env = dict(os.environ, PYTHONIOENCODING="utf-8:surrogateescape")
|
2019-05-22 19:57:57 -03:00
|
|
|
out, err = self.run_embedded_interpreter("test_forced_io_encoding", env=env)
|
2017-11-27 18:11:51 -04:00
|
|
|
if support.verbose > 1:
|
|
|
|
print()
|
|
|
|
print(out)
|
|
|
|
print(err)
|
|
|
|
expected_stream_encoding = "utf-8"
|
|
|
|
expected_errors = "surrogateescape"
|
|
|
|
expected_output = '\n'.join([
|
|
|
|
"--- Use defaults ---",
|
|
|
|
"Expected encoding: default",
|
|
|
|
"Expected errors: default",
|
|
|
|
"stdin: {in_encoding}:{errors}",
|
|
|
|
"stdout: {out_encoding}:{errors}",
|
|
|
|
"stderr: {out_encoding}:backslashreplace",
|
|
|
|
"--- Set errors only ---",
|
|
|
|
"Expected encoding: default",
|
|
|
|
"Expected errors: ignore",
|
|
|
|
"stdin: {in_encoding}:ignore",
|
|
|
|
"stdout: {out_encoding}:ignore",
|
|
|
|
"stderr: {out_encoding}:backslashreplace",
|
|
|
|
"--- Set encoding only ---",
|
2018-08-28 18:26:33 -03:00
|
|
|
"Expected encoding: iso8859-1",
|
2017-11-27 18:11:51 -04:00
|
|
|
"Expected errors: default",
|
2018-08-28 18:26:33 -03:00
|
|
|
"stdin: iso8859-1:{errors}",
|
|
|
|
"stdout: iso8859-1:{errors}",
|
|
|
|
"stderr: iso8859-1:backslashreplace",
|
2017-11-27 18:11:51 -04:00
|
|
|
"--- Set encoding and errors ---",
|
2018-08-28 18:26:33 -03:00
|
|
|
"Expected encoding: iso8859-1",
|
2017-11-27 18:11:51 -04:00
|
|
|
"Expected errors: replace",
|
2018-08-28 18:26:33 -03:00
|
|
|
"stdin: iso8859-1:replace",
|
|
|
|
"stdout: iso8859-1:replace",
|
|
|
|
"stderr: iso8859-1:backslashreplace"])
|
2017-11-27 18:11:51 -04:00
|
|
|
expected_output = expected_output.format(
|
|
|
|
in_encoding=expected_stream_encoding,
|
|
|
|
out_encoding=expected_stream_encoding,
|
|
|
|
errors=expected_errors)
|
|
|
|
# This is useful if we ever trip over odd platform behaviour
|
|
|
|
self.maxDiff = None
|
|
|
|
self.assertEqual(out.strip(), expected_output)
|
|
|
|
|
|
|
|
def test_pre_initialization_api(self):
|
|
|
|
"""
|
2018-03-25 07:44:30 -03:00
|
|
|
Checks some key parts of the C-API that need to work before the runtine
|
2017-11-27 18:11:51 -04:00
|
|
|
is initialized (via Py_Initialize()).
|
|
|
|
"""
|
|
|
|
env = dict(os.environ, PYTHONPATH=os.pathsep.join(sys.path))
|
2019-05-22 19:57:57 -03:00
|
|
|
out, err = self.run_embedded_interpreter("test_pre_initialization_api", env=env)
|
2018-11-14 12:39:45 -04:00
|
|
|
if MS_WINDOWS:
|
2018-03-25 07:44:30 -03:00
|
|
|
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).
|
|
|
|
"""
|
2019-06-17 19:11:00 -03:00
|
|
|
env = remove_python_envvars()
|
|
|
|
env['PYTHONPATH'] = os.pathsep.join(sys.path)
|
2018-03-25 07:44:30 -03:00
|
|
|
out, err = self.run_embedded_interpreter(
|
2019-05-22 19:57:57 -03:00
|
|
|
"test_pre_initialization_sys_options", env=env)
|
2018-03-25 07:44:30 -03:00
|
|
|
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)
|
2017-11-27 18:11:51 -04:00
|
|
|
self.assertEqual(err, '')
|
|
|
|
|
2017-11-30 17:05:00 -04:00
|
|
|
def test_bpo20891(self):
|
|
|
|
"""
|
|
|
|
bpo-20891: Calling PyGILState_Ensure in a non-Python thread before
|
|
|
|
calling PyEval_InitThreads() must not crash. PyGILState_Ensure() must
|
|
|
|
call PyEval_InitThreads() for us in this case.
|
|
|
|
"""
|
2019-05-22 19:57:57 -03:00
|
|
|
out, err = self.run_embedded_interpreter("test_bpo20891")
|
2017-11-30 17:05:00 -04:00
|
|
|
self.assertEqual(out, '')
|
|
|
|
self.assertEqual(err, '')
|
|
|
|
|
2018-06-22 14:14:51 -03:00
|
|
|
def test_initialize_twice(self):
|
|
|
|
"""
|
|
|
|
bpo-33932: Calling Py_Initialize() twice should do nothing (and not
|
|
|
|
crash!).
|
|
|
|
"""
|
2019-05-22 19:57:57 -03:00
|
|
|
out, err = self.run_embedded_interpreter("test_initialize_twice")
|
2018-06-22 14:14:51 -03:00
|
|
|
self.assertEqual(out, '')
|
|
|
|
self.assertEqual(err, '')
|
|
|
|
|
2018-07-20 12:34:23 -03:00
|
|
|
def test_initialize_pymain(self):
|
|
|
|
"""
|
|
|
|
bpo-34008: Calling Py_Main() after Py_Initialize() must not fail.
|
|
|
|
"""
|
2019-05-22 19:57:57 -03:00
|
|
|
out, err = self.run_embedded_interpreter("test_initialize_pymain")
|
2018-07-20 12:34:23 -03:00
|
|
|
self.assertEqual(out.rstrip(), "Py_Main() after Py_Initialize: sys.argv=['-c', 'arg2']")
|
|
|
|
self.assertEqual(err, '')
|
|
|
|
|
2019-03-29 11:13:46 -03:00
|
|
|
def test_run_main(self):
|
2019-05-22 19:57:57 -03:00
|
|
|
out, err = self.run_embedded_interpreter("test_run_main")
|
2019-05-27 11:39:22 -03:00
|
|
|
self.assertEqual(out.rstrip(), "Py_RunMain(): sys.argv=['-c', 'arg2']")
|
2019-03-29 11:13:46 -03:00
|
|
|
self.assertEqual(err, '')
|
|
|
|
|
2017-11-27 18:11:51 -04:00
|
|
|
|
2018-07-26 13:57:56 -03:00
|
|
|
class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
|
|
|
|
maxDiff = 4096
|
2018-11-14 12:39:45 -04:00
|
|
|
UTF8_MODE_ERRORS = ('surrogatepass' if MS_WINDOWS else 'surrogateescape')
|
|
|
|
|
2019-05-17 17:44:16 -03:00
|
|
|
# Marker to read the default configuration: get_default_config()
|
2018-11-26 06:54:12 -04:00
|
|
|
GET_DEFAULT_CONFIG = object()
|
2019-05-17 17:44:16 -03:00
|
|
|
|
|
|
|
# Marker to ignore a configuration parameter
|
|
|
|
IGNORE_CONFIG = object()
|
|
|
|
|
2019-05-22 18:58:50 -03:00
|
|
|
PRE_CONFIG_COMPAT = {
|
2019-05-22 23:12:27 -03:00
|
|
|
'_config_init': API_COMPAT,
|
2019-05-17 10:20:52 -03:00
|
|
|
'allocator': PYMEM_ALLOCATOR_NOT_SET,
|
2019-05-20 06:02:00 -03:00
|
|
|
'parse_argv': 0,
|
2019-05-17 17:44:16 -03:00
|
|
|
'configure_locale': 1,
|
2019-03-25 19:19:57 -03:00
|
|
|
'coerce_c_locale': 0,
|
|
|
|
'coerce_c_locale_warn': 0,
|
|
|
|
'utf8_mode': 0,
|
|
|
|
}
|
2019-05-20 06:02:00 -03:00
|
|
|
if MS_WINDOWS:
|
2019-05-22 18:58:50 -03:00
|
|
|
PRE_CONFIG_COMPAT.update({
|
2019-05-20 06:02:00 -03:00
|
|
|
'legacy_windows_fs_encoding': 0,
|
|
|
|
})
|
2019-05-22 18:58:50 -03:00
|
|
|
PRE_CONFIG_PYTHON = dict(PRE_CONFIG_COMPAT,
|
2019-05-22 23:12:27 -03:00
|
|
|
_config_init=API_PYTHON,
|
2019-05-20 06:02:00 -03:00
|
|
|
parse_argv=1,
|
2019-05-20 11:38:48 -03:00
|
|
|
coerce_c_locale=GET_DEFAULT_CONFIG,
|
|
|
|
utf8_mode=GET_DEFAULT_CONFIG,
|
2019-05-20 06:02:00 -03:00
|
|
|
)
|
2019-05-22 18:58:50 -03:00
|
|
|
PRE_CONFIG_ISOLATED = dict(PRE_CONFIG_COMPAT,
|
2019-05-22 23:12:27 -03:00
|
|
|
_config_init=API_ISOLATED,
|
2019-05-17 22:21:27 -03:00
|
|
|
configure_locale=0,
|
|
|
|
isolated=1,
|
|
|
|
use_environment=0,
|
|
|
|
utf8_mode=0,
|
|
|
|
dev_mode=0,
|
2019-05-20 11:38:48 -03:00
|
|
|
coerce_c_locale=0,
|
2019-05-17 22:21:27 -03:00
|
|
|
)
|
|
|
|
|
2019-03-25 22:31:11 -03:00
|
|
|
COPY_PRE_CONFIG = [
|
|
|
|
'dev_mode',
|
|
|
|
'isolated',
|
|
|
|
'use_environment',
|
|
|
|
]
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
CONFIG_COMPAT = {
|
2019-05-22 18:58:50 -03:00
|
|
|
'_config_init': API_COMPAT,
|
2019-03-25 22:31:11 -03:00
|
|
|
'isolated': 0,
|
|
|
|
'use_environment': 1,
|
|
|
|
'dev_mode': 0,
|
|
|
|
|
2018-07-26 13:57:56 -03:00
|
|
|
'install_signal_handlers': 1,
|
|
|
|
'use_hash_seed': 0,
|
|
|
|
'hash_seed': 0,
|
|
|
|
'faulthandler': 0,
|
|
|
|
'tracemalloc': 0,
|
|
|
|
'import_time': 0,
|
|
|
|
'show_ref_count': 0,
|
|
|
|
'show_alloc_count': 0,
|
|
|
|
'dump_refs': 0,
|
|
|
|
'malloc_stats': 0,
|
|
|
|
|
2018-11-26 06:54:12 -04:00
|
|
|
'filesystem_encoding': GET_DEFAULT_CONFIG,
|
|
|
|
'filesystem_errors': GET_DEFAULT_CONFIG,
|
2018-08-29 14:32:47 -03:00
|
|
|
|
2018-11-13 19:24:28 -04:00
|
|
|
'pycache_prefix': None,
|
2019-05-14 17:01:51 -03:00
|
|
|
'program_name': GET_DEFAULT_CONFIG,
|
2019-05-17 14:01:14 -03:00
|
|
|
'parse_argv': 0,
|
2019-03-15 12:03:23 -03:00
|
|
|
'argv': [""],
|
2018-11-13 19:24:28 -04:00
|
|
|
|
|
|
|
'xoptions': [],
|
|
|
|
'warnoptions': [],
|
2018-07-26 13:57:56 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
'pythonpath_env': None,
|
2018-11-14 12:39:45 -04:00
|
|
|
'home': None,
|
2019-05-14 17:01:51 -03:00
|
|
|
'executable': GET_DEFAULT_CONFIG,
|
2019-06-29 14:34:11 -03:00
|
|
|
'base_executable': GET_DEFAULT_CONFIG,
|
2018-11-26 06:54:12 -04:00
|
|
|
|
|
|
|
'prefix': GET_DEFAULT_CONFIG,
|
|
|
|
'base_prefix': GET_DEFAULT_CONFIG,
|
|
|
|
'exec_prefix': GET_DEFAULT_CONFIG,
|
|
|
|
'base_exec_prefix': GET_DEFAULT_CONFIG,
|
2019-05-14 21:12:48 -03:00
|
|
|
'module_search_paths': GET_DEFAULT_CONFIG,
|
2018-11-14 12:39:45 -04:00
|
|
|
|
2018-07-26 13:57:56 -03:00
|
|
|
'site_import': 1,
|
|
|
|
'bytes_warning': 0,
|
|
|
|
'inspect': 0,
|
|
|
|
'interactive': 0,
|
|
|
|
'optimization_level': 0,
|
2018-07-31 22:07:00 -03:00
|
|
|
'parser_debug': 0,
|
2018-07-26 13:57:56 -03:00
|
|
|
'write_bytecode': 1,
|
|
|
|
'verbose': 0,
|
|
|
|
'quiet': 0,
|
|
|
|
'user_site_directory': 1,
|
2019-05-17 14:01:14 -03:00
|
|
|
'configure_c_stdio': 0,
|
2018-07-31 22:07:00 -03:00
|
|
|
'buffered_stdio': 1,
|
2018-08-29 14:32:47 -03:00
|
|
|
|
2018-11-26 06:54:12 -04:00
|
|
|
'stdio_encoding': GET_DEFAULT_CONFIG,
|
|
|
|
'stdio_errors': GET_DEFAULT_CONFIG,
|
2018-07-26 13:57:56 -03:00
|
|
|
|
2019-03-01 08:10:14 -04:00
|
|
|
'skip_source_first_line': 0,
|
|
|
|
'run_command': None,
|
|
|
|
'run_module': None,
|
|
|
|
'run_filename': None,
|
|
|
|
|
2018-07-26 13:57:56 -03:00
|
|
|
'_install_importlib': 1,
|
2019-05-02 00:51:56 -03:00
|
|
|
'check_hash_pycs_mode': 'default',
|
2019-05-16 12:38:16 -03:00
|
|
|
'pathconfig_warnings': 1,
|
|
|
|
'_init_main': 1,
|
2018-07-26 13:57:56 -03:00
|
|
|
}
|
2018-11-14 12:39:45 -04:00
|
|
|
if MS_WINDOWS:
|
2019-05-27 11:39:22 -03:00
|
|
|
CONFIG_COMPAT.update({
|
2018-11-14 12:39:45 -04:00
|
|
|
'legacy_windows_stdio': 0,
|
|
|
|
})
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
CONFIG_PYTHON = dict(CONFIG_COMPAT,
|
2019-05-22 23:12:27 -03:00
|
|
|
_config_init=API_PYTHON,
|
2019-05-17 22:21:27 -03:00
|
|
|
configure_c_stdio=1,
|
|
|
|
parse_argv=1,
|
|
|
|
)
|
2019-05-27 11:39:22 -03:00
|
|
|
CONFIG_ISOLATED = dict(CONFIG_COMPAT,
|
2019-05-22 23:12:27 -03:00
|
|
|
_config_init=API_ISOLATED,
|
2019-05-17 22:21:27 -03:00
|
|
|
isolated=1,
|
|
|
|
use_environment=0,
|
|
|
|
user_site_directory=0,
|
|
|
|
dev_mode=0,
|
|
|
|
install_signal_handlers=0,
|
|
|
|
use_hash_seed=0,
|
|
|
|
faulthandler=0,
|
|
|
|
tracemalloc=0,
|
|
|
|
pathconfig_warnings=0,
|
|
|
|
)
|
|
|
|
if MS_WINDOWS:
|
2019-05-27 11:39:22 -03:00
|
|
|
CONFIG_ISOLATED['legacy_windows_stdio'] = 0
|
2019-05-17 22:21:27 -03:00
|
|
|
|
2018-11-14 12:39:45 -04:00
|
|
|
# global config
|
|
|
|
DEFAULT_GLOBAL_CONFIG = {
|
|
|
|
'Py_HasFileSystemDefaultEncoding': 0,
|
|
|
|
'Py_HashRandomizationFlag': 1,
|
|
|
|
'_Py_HasFileSystemDefaultEncodeErrors': 0,
|
|
|
|
}
|
2019-03-25 19:19:57 -03:00
|
|
|
COPY_GLOBAL_PRE_CONFIG = [
|
|
|
|
('Py_UTF8Mode', 'utf8_mode'),
|
|
|
|
]
|
2018-11-14 12:39:45 -04:00
|
|
|
COPY_GLOBAL_CONFIG = [
|
|
|
|
# Copy core config to global config for expected values
|
|
|
|
# True means that the core config value is inverted (0 => 1 and 1 => 0)
|
|
|
|
('Py_BytesWarningFlag', 'bytes_warning'),
|
|
|
|
('Py_DebugFlag', 'parser_debug'),
|
|
|
|
('Py_DontWriteBytecodeFlag', 'write_bytecode', True),
|
|
|
|
('Py_FileSystemDefaultEncodeErrors', 'filesystem_errors'),
|
|
|
|
('Py_FileSystemDefaultEncoding', 'filesystem_encoding'),
|
2019-05-16 12:38:16 -03:00
|
|
|
('Py_FrozenFlag', 'pathconfig_warnings', True),
|
2019-03-25 22:31:11 -03:00
|
|
|
('Py_IgnoreEnvironmentFlag', 'use_environment', True),
|
2018-11-14 12:39:45 -04:00
|
|
|
('Py_InspectFlag', 'inspect'),
|
|
|
|
('Py_InteractiveFlag', 'interactive'),
|
2019-03-25 22:31:11 -03:00
|
|
|
('Py_IsolatedFlag', 'isolated'),
|
2018-11-14 12:39:45 -04:00
|
|
|
('Py_NoSiteFlag', 'site_import', True),
|
|
|
|
('Py_NoUserSiteDirectory', 'user_site_directory', True),
|
|
|
|
('Py_OptimizeFlag', 'optimization_level'),
|
|
|
|
('Py_QuietFlag', 'quiet'),
|
|
|
|
('Py_UnbufferedStdioFlag', 'buffered_stdio', True),
|
|
|
|
('Py_VerboseFlag', 'verbose'),
|
|
|
|
]
|
|
|
|
if MS_WINDOWS:
|
2019-03-25 19:19:57 -03:00
|
|
|
COPY_GLOBAL_PRE_CONFIG.extend((
|
2018-11-14 12:39:45 -04:00
|
|
|
('Py_LegacyWindowsFSEncodingFlag', 'legacy_windows_fs_encoding'),
|
2019-03-25 19:19:57 -03:00
|
|
|
))
|
|
|
|
COPY_GLOBAL_CONFIG.extend((
|
2018-11-14 12:39:45 -04:00
|
|
|
('Py_LegacyWindowsStdioFlag', 'legacy_windows_stdio'),
|
|
|
|
))
|
2018-07-26 13:57:56 -03:00
|
|
|
|
2019-05-20 11:38:48 -03:00
|
|
|
EXPECTED_CONFIG = None
|
|
|
|
|
2018-11-14 12:39:45 -04:00
|
|
|
def main_xoptions(self, xoptions_list):
|
|
|
|
xoptions = {}
|
|
|
|
for opt in xoptions_list:
|
|
|
|
if '=' in opt:
|
|
|
|
key, value = opt.split('=', 1)
|
|
|
|
xoptions[key] = value
|
|
|
|
else:
|
|
|
|
xoptions[opt] = True
|
|
|
|
return xoptions
|
2018-08-29 06:47:29 -03:00
|
|
|
|
2019-05-20 11:38:48 -03:00
|
|
|
def _get_expected_config(self, env):
|
2018-11-26 06:54:12 -04:00
|
|
|
code = textwrap.dedent('''
|
|
|
|
import json
|
|
|
|
import sys
|
2019-05-14 21:12:48 -03:00
|
|
|
import _testinternalcapi
|
2018-11-26 06:54:12 -04:00
|
|
|
|
2019-05-14 21:12:48 -03:00
|
|
|
configs = _testinternalcapi.get_configs()
|
2019-05-20 11:38:48 -03:00
|
|
|
|
|
|
|
data = json.dumps(configs)
|
2018-11-26 06:54:12 -04:00
|
|
|
data = data.encode('utf-8')
|
|
|
|
sys.stdout.buffer.write(data)
|
|
|
|
sys.stdout.buffer.flush()
|
|
|
|
''')
|
|
|
|
|
|
|
|
# Use -S to not import the site module: get the proper configuration
|
|
|
|
# when test_embed is run from a venv (bpo-35313)
|
2019-05-14 21:12:48 -03:00
|
|
|
args = [sys.executable, '-S', '-c', code]
|
2018-11-26 06:54:12 -04:00
|
|
|
proc = subprocess.run(args, env=env,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.STDOUT)
|
|
|
|
if proc.returncode:
|
|
|
|
raise Exception(f"failed to get the default config: "
|
|
|
|
f"stdout={proc.stdout!r} stderr={proc.stderr!r}")
|
|
|
|
stdout = proc.stdout.decode('utf-8')
|
2019-05-02 16:30:21 -03:00
|
|
|
try:
|
2019-05-20 11:38:48 -03:00
|
|
|
return json.loads(stdout)
|
2019-05-02 16:30:21 -03:00
|
|
|
except json.JSONDecodeError:
|
|
|
|
self.fail(f"fail to decode stdout: {stdout!r}")
|
2018-11-26 06:54:12 -04:00
|
|
|
|
2019-05-20 11:38:48 -03:00
|
|
|
def get_expected_config(self, expected_preconfig, expected, env, api,
|
2019-08-23 12:57:54 -03:00
|
|
|
modify_path_cb=None):
|
2019-05-20 11:38:48 -03:00
|
|
|
cls = self.__class__
|
|
|
|
if cls.EXPECTED_CONFIG is None:
|
|
|
|
cls.EXPECTED_CONFIG = self._get_expected_config(env)
|
|
|
|
configs = {key: dict(value)
|
|
|
|
for key, value in self.EXPECTED_CONFIG.items()}
|
|
|
|
|
|
|
|
pre_config = configs['pre_config']
|
|
|
|
for key, value in expected_preconfig.items():
|
|
|
|
if value is self.GET_DEFAULT_CONFIG:
|
|
|
|
expected_preconfig[key] = pre_config[key]
|
|
|
|
|
2019-05-22 18:58:50 -03:00
|
|
|
if not expected_preconfig['configure_locale'] or api == API_COMPAT:
|
2019-05-20 11:38:48 -03:00
|
|
|
# there is no easy way to get the locale encoding before
|
|
|
|
# setlocale(LC_CTYPE, "") is called: don't test encodings
|
|
|
|
for key in ('filesystem_encoding', 'filesystem_errors',
|
|
|
|
'stdio_encoding', 'stdio_errors'):
|
|
|
|
expected[key] = self.IGNORE_CONFIG
|
|
|
|
|
|
|
|
if not expected_preconfig['configure_locale']:
|
|
|
|
# UTF-8 Mode depends on the locale. There is no easy way
|
|
|
|
# to guess if UTF-8 Mode will be enabled or not if the locale
|
|
|
|
# is not configured.
|
|
|
|
expected_preconfig['utf8_mode'] = self.IGNORE_CONFIG
|
|
|
|
|
|
|
|
if expected_preconfig['utf8_mode'] == 1:
|
|
|
|
if expected['filesystem_encoding'] is self.GET_DEFAULT_CONFIG:
|
|
|
|
expected['filesystem_encoding'] = 'utf-8'
|
|
|
|
if expected['filesystem_errors'] is self.GET_DEFAULT_CONFIG:
|
|
|
|
expected['filesystem_errors'] = self.UTF8_MODE_ERRORS
|
|
|
|
if expected['stdio_encoding'] is self.GET_DEFAULT_CONFIG:
|
|
|
|
expected['stdio_encoding'] = 'utf-8'
|
|
|
|
if expected['stdio_errors'] is self.GET_DEFAULT_CONFIG:
|
|
|
|
expected['stdio_errors'] = 'surrogateescape'
|
|
|
|
|
2019-06-29 14:34:11 -03:00
|
|
|
if sys.platform == 'win32':
|
|
|
|
default_executable = self.test_exe
|
|
|
|
elif expected['program_name'] is not self.GET_DEFAULT_CONFIG:
|
|
|
|
default_executable = os.path.abspath(expected['program_name'])
|
|
|
|
else:
|
|
|
|
default_executable = os.path.join(os.getcwd(), '_testembed')
|
2019-05-14 17:01:51 -03:00
|
|
|
if expected['executable'] is self.GET_DEFAULT_CONFIG:
|
2019-06-29 14:34:11 -03:00
|
|
|
expected['executable'] = default_executable
|
|
|
|
if expected['base_executable'] is self.GET_DEFAULT_CONFIG:
|
|
|
|
expected['base_executable'] = default_executable
|
2019-05-14 17:01:51 -03:00
|
|
|
if expected['program_name'] is self.GET_DEFAULT_CONFIG:
|
|
|
|
expected['program_name'] = './_testembed'
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
config = configs['config']
|
2018-11-26 06:54:12 -04:00
|
|
|
for key, value in expected.items():
|
|
|
|
if value is self.GET_DEFAULT_CONFIG:
|
2019-05-27 11:39:22 -03:00
|
|
|
expected[key] = config[key]
|
2019-05-14 21:12:48 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
prepend_path = expected['pythonpath_env']
|
2019-05-20 11:38:48 -03:00
|
|
|
if prepend_path is not None:
|
|
|
|
expected['module_search_paths'] = [prepend_path, *expected['module_search_paths']]
|
2019-08-23 12:57:54 -03:00
|
|
|
if modify_path_cb is not None:
|
|
|
|
expected['module_search_paths'] = expected['module_search_paths'].copy()
|
|
|
|
modify_path_cb(expected['module_search_paths'])
|
2019-05-17 17:44:16 -03:00
|
|
|
|
2019-05-20 11:38:48 -03:00
|
|
|
for key in self.COPY_PRE_CONFIG:
|
|
|
|
if key not in expected_preconfig:
|
|
|
|
expected_preconfig[key] = expected[key]
|
2018-11-26 06:54:12 -04:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
def check_pre_config(self, configs, expected):
|
|
|
|
pre_config = dict(configs['pre_config'])
|
2019-05-20 11:38:48 -03:00
|
|
|
for key, value in list(expected.items()):
|
|
|
|
if value is self.IGNORE_CONFIG:
|
|
|
|
del pre_config[key]
|
|
|
|
del expected[key]
|
|
|
|
self.assertEqual(pre_config, expected)
|
2019-03-25 19:19:57 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
def check_config(self, configs, expected):
|
|
|
|
config = dict(configs['config'])
|
2019-05-17 17:44:16 -03:00
|
|
|
for key, value in list(expected.items()):
|
|
|
|
if value is self.IGNORE_CONFIG:
|
2019-05-27 11:39:22 -03:00
|
|
|
del config[key]
|
2019-05-17 17:44:16 -03:00
|
|
|
del expected[key]
|
2019-05-27 11:39:22 -03:00
|
|
|
self.assertEqual(config, expected)
|
2018-08-29 06:47:29 -03:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
def check_global_config(self, configs):
|
|
|
|
pre_config = configs['pre_config']
|
|
|
|
config = configs['config']
|
2018-11-13 19:24:28 -04:00
|
|
|
|
2018-11-26 06:54:12 -04:00
|
|
|
expected = dict(self.DEFAULT_GLOBAL_CONFIG)
|
2018-11-14 12:39:45 -04:00
|
|
|
for item in self.COPY_GLOBAL_CONFIG:
|
2018-11-13 19:24:28 -04:00
|
|
|
if len(item) == 3:
|
|
|
|
global_key, core_key, opposite = item
|
2019-05-27 11:39:22 -03:00
|
|
|
expected[global_key] = 0 if config[core_key] else 1
|
2018-11-13 19:24:28 -04:00
|
|
|
else:
|
|
|
|
global_key, core_key = item
|
2019-05-27 11:39:22 -03:00
|
|
|
expected[global_key] = config[core_key]
|
2019-03-25 19:19:57 -03:00
|
|
|
for item in self.COPY_GLOBAL_PRE_CONFIG:
|
|
|
|
if len(item) == 3:
|
|
|
|
global_key, core_key, opposite = item
|
|
|
|
expected[global_key] = 0 if pre_config[core_key] else 1
|
|
|
|
else:
|
|
|
|
global_key, core_key = item
|
|
|
|
expected[global_key] = pre_config[core_key]
|
2018-11-13 19:24:28 -04:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
self.assertEqual(configs['global_config'], expected)
|
2018-11-13 19:24:28 -04:00
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
def check_all_configs(self, testname, expected_config=None,
|
2019-08-23 12:57:54 -03:00
|
|
|
expected_preconfig=None, modify_path_cb=None, stderr=None,
|
2019-05-22 18:58:50 -03:00
|
|
|
*, api):
|
2019-06-17 19:11:00 -03:00
|
|
|
env = remove_python_envvars()
|
2018-11-14 12:39:45 -04:00
|
|
|
|
2019-05-22 18:58:50 -03:00
|
|
|
if api == API_ISOLATED:
|
|
|
|
default_preconfig = self.PRE_CONFIG_ISOLATED
|
|
|
|
elif api == API_PYTHON:
|
|
|
|
default_preconfig = self.PRE_CONFIG_PYTHON
|
2019-05-17 22:21:27 -03:00
|
|
|
else:
|
2019-05-22 18:58:50 -03:00
|
|
|
default_preconfig = self.PRE_CONFIG_COMPAT
|
2019-05-17 22:21:27 -03:00
|
|
|
if expected_preconfig is None:
|
|
|
|
expected_preconfig = {}
|
|
|
|
expected_preconfig = dict(default_preconfig, **expected_preconfig)
|
|
|
|
if expected_config is None:
|
|
|
|
expected_config = {}
|
2019-05-20 11:38:48 -03:00
|
|
|
|
2019-05-22 18:58:50 -03:00
|
|
|
if api == API_PYTHON:
|
2019-05-27 11:39:22 -03:00
|
|
|
default_config = self.CONFIG_PYTHON
|
2019-05-22 18:58:50 -03:00
|
|
|
elif api == API_ISOLATED:
|
2019-05-27 11:39:22 -03:00
|
|
|
default_config = self.CONFIG_ISOLATED
|
2019-05-20 11:38:48 -03:00
|
|
|
else:
|
2019-05-27 11:39:22 -03:00
|
|
|
default_config = self.CONFIG_COMPAT
|
2019-05-20 11:38:48 -03:00
|
|
|
expected_config = dict(default_config, **expected_config)
|
|
|
|
|
|
|
|
self.get_expected_config(expected_preconfig,
|
|
|
|
expected_config, env,
|
2019-08-23 12:57:54 -03:00
|
|
|
api, modify_path_cb)
|
2019-03-25 19:19:57 -03:00
|
|
|
|
2019-05-16 12:38:16 -03:00
|
|
|
out, err = self.run_embedded_interpreter(testname, env=env)
|
|
|
|
if stderr is None and not expected_config['verbose']:
|
|
|
|
stderr = ""
|
|
|
|
if stderr is not None:
|
|
|
|
self.assertEqual(err.rstrip(), stderr)
|
|
|
|
try:
|
2019-05-27 11:39:22 -03:00
|
|
|
configs = json.loads(out)
|
2019-05-16 12:38:16 -03:00
|
|
|
except json.JSONDecodeError:
|
|
|
|
self.fail(f"fail to decode stdout: {out!r}")
|
|
|
|
|
2019-05-27 11:39:22 -03:00
|
|
|
self.check_pre_config(configs, expected_preconfig)
|
|
|
|
self.check_config(configs, expected_config)
|
|
|
|
self.check_global_config(configs)
|
2018-11-13 19:24:28 -04:00
|
|
|
|
2018-07-26 13:57:56 -03:00
|
|
|
def test_init_default_config(self):
|
2019-05-27 11:39:22 -03:00
|
|
|
self.check_all_configs("test_init_initialize_config", api=API_COMPAT)
|
2019-05-22 18:58:50 -03:00
|
|
|
|
|
|
|
def test_preinit_compat_config(self):
|
2019-05-27 11:39:22 -03:00
|
|
|
self.check_all_configs("test_preinit_compat_config", api=API_COMPAT)
|
2019-05-22 18:58:50 -03:00
|
|
|
|
|
|
|
def test_init_compat_config(self):
|
2019-05-27 11:39:22 -03:00
|
|
|
self.check_all_configs("test_init_compat_config", api=API_COMPAT)
|
2018-07-26 13:57:56 -03:00
|
|
|
|
|
|
|
def test_init_global_config(self):
|
2019-03-25 19:19:57 -03:00
|
|
|
preconfig = {
|
|
|
|
'utf8_mode': 1,
|
|
|
|
}
|
2018-07-26 13:57:56 -03:00
|
|
|
config = {
|
|
|
|
'program_name': './globalvar',
|
|
|
|
'site_import': 0,
|
|
|
|
'bytes_warning': 1,
|
2019-03-26 12:58:50 -03:00
|
|
|
'warnoptions': ['default::BytesWarning'],
|
2018-07-26 13:57:56 -03:00
|
|
|
'inspect': 1,
|
|
|
|
'interactive': 1,
|
|
|
|
'optimization_level': 2,
|
|
|
|
'write_bytecode': 0,
|
|
|
|
'verbose': 1,
|
|
|
|
'quiet': 1,
|
2018-07-31 22:07:00 -03:00
|
|
|
'buffered_stdio': 0,
|
2018-08-29 06:47:29 -03:00
|
|
|
|
2018-07-26 13:57:56 -03:00
|
|
|
'user_site_directory': 0,
|
2019-05-16 13:30:15 -03:00
|
|
|
'pathconfig_warnings': 0,
|
2018-07-26 13:57:56 -03:00
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
self.check_all_configs("test_init_global_config", config, preconfig,
|
|
|
|
api=API_COMPAT)
|
2018-07-26 13:57:56 -03:00
|
|
|
|
|
|
|
def test_init_from_config(self):
|
2019-03-25 19:19:57 -03:00
|
|
|
preconfig = {
|
2019-05-17 10:20:52 -03:00
|
|
|
'allocator': PYMEM_ALLOCATOR_MALLOC,
|
2019-03-25 19:19:57 -03:00
|
|
|
'utf8_mode': 1,
|
|
|
|
}
|
2018-07-26 13:57:56 -03:00
|
|
|
config = {
|
|
|
|
'install_signal_handlers': 0,
|
|
|
|
'use_hash_seed': 1,
|
|
|
|
'hash_seed': 123,
|
|
|
|
'tracemalloc': 2,
|
|
|
|
'import_time': 1,
|
|
|
|
'show_ref_count': 1,
|
|
|
|
'show_alloc_count': 1,
|
|
|
|
'malloc_stats': 1,
|
|
|
|
|
2018-08-29 06:47:29 -03:00
|
|
|
'stdio_encoding': 'iso8859-1',
|
|
|
|
'stdio_errors': 'replace',
|
2018-07-26 13:57:56 -03:00
|
|
|
|
|
|
|
'pycache_prefix': 'conf_pycache_prefix',
|
|
|
|
'program_name': './conf_program_name',
|
2019-07-01 14:52:45 -03:00
|
|
|
'argv': ['-c', 'arg2', ],
|
2019-05-17 14:01:14 -03:00
|
|
|
'parse_argv': 1,
|
2019-07-01 14:52:45 -03:00
|
|
|
'xoptions': [
|
|
|
|
'config_xoption1=3',
|
|
|
|
'config_xoption2=',
|
|
|
|
'config_xoption3',
|
|
|
|
'cmdline_xoption',
|
|
|
|
],
|
|
|
|
'warnoptions': [
|
|
|
|
'config_warnoption',
|
|
|
|
'cmdline_warnoption',
|
|
|
|
'default::BytesWarning',
|
|
|
|
],
|
2019-03-29 11:13:46 -03:00
|
|
|
'run_command': 'pass\n',
|
2018-07-26 13:57:56 -03:00
|
|
|
|
|
|
|
'site_import': 0,
|
|
|
|
'bytes_warning': 1,
|
|
|
|
'inspect': 1,
|
|
|
|
'interactive': 1,
|
|
|
|
'optimization_level': 2,
|
|
|
|
'write_bytecode': 0,
|
|
|
|
'verbose': 1,
|
|
|
|
'quiet': 1,
|
2019-05-17 14:01:14 -03:00
|
|
|
'configure_c_stdio': 1,
|
2018-07-31 22:07:00 -03:00
|
|
|
'buffered_stdio': 0,
|
2018-07-26 13:57:56 -03:00
|
|
|
'user_site_directory': 0,
|
|
|
|
'faulthandler': 1,
|
2018-07-31 21:13:04 -03:00
|
|
|
|
2019-05-02 00:51:56 -03:00
|
|
|
'check_hash_pycs_mode': 'always',
|
2019-05-16 13:30:15 -03:00
|
|
|
'pathconfig_warnings': 0,
|
2018-07-26 13:57:56 -03:00
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
self.check_all_configs("test_init_from_config", config, preconfig,
|
|
|
|
api=API_COMPAT)
|
2018-07-26 13:57:56 -03:00
|
|
|
|
2019-05-22 23:12:27 -03:00
|
|
|
def test_init_compat_env(self):
|
2019-05-20 11:38:48 -03:00
|
|
|
preconfig = {
|
|
|
|
'allocator': PYMEM_ALLOCATOR_MALLOC,
|
|
|
|
}
|
|
|
|
config = {
|
|
|
|
'use_hash_seed': 1,
|
|
|
|
'hash_seed': 42,
|
|
|
|
'tracemalloc': 2,
|
|
|
|
'import_time': 1,
|
|
|
|
'malloc_stats': 1,
|
|
|
|
'inspect': 1,
|
|
|
|
'optimization_level': 2,
|
2019-05-27 11:39:22 -03:00
|
|
|
'pythonpath_env': '/my/path',
|
2019-05-20 11:38:48 -03:00
|
|
|
'pycache_prefix': 'env_pycache_prefix',
|
|
|
|
'write_bytecode': 0,
|
|
|
|
'verbose': 1,
|
|
|
|
'buffered_stdio': 0,
|
|
|
|
'stdio_encoding': 'iso8859-1',
|
|
|
|
'stdio_errors': 'replace',
|
|
|
|
'user_site_directory': 0,
|
|
|
|
'faulthandler': 1,
|
|
|
|
'warnoptions': ['EnvVar'],
|
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
self.check_all_configs("test_init_compat_env", config, preconfig,
|
|
|
|
api=API_COMPAT)
|
2019-03-05 12:37:44 -04:00
|
|
|
|
2019-05-22 23:12:27 -03:00
|
|
|
def test_init_python_env(self):
|
|
|
|
preconfig = {
|
|
|
|
'allocator': PYMEM_ALLOCATOR_MALLOC,
|
|
|
|
'utf8_mode': 1,
|
|
|
|
}
|
|
|
|
config = {
|
|
|
|
'use_hash_seed': 1,
|
|
|
|
'hash_seed': 42,
|
|
|
|
'tracemalloc': 2,
|
|
|
|
'import_time': 1,
|
|
|
|
'malloc_stats': 1,
|
|
|
|
'inspect': 1,
|
|
|
|
'optimization_level': 2,
|
2019-05-27 11:39:22 -03:00
|
|
|
'pythonpath_env': '/my/path',
|
2019-05-22 23:12:27 -03:00
|
|
|
'pycache_prefix': 'env_pycache_prefix',
|
|
|
|
'write_bytecode': 0,
|
|
|
|
'verbose': 1,
|
|
|
|
'buffered_stdio': 0,
|
|
|
|
'stdio_encoding': 'iso8859-1',
|
|
|
|
'stdio_errors': 'replace',
|
|
|
|
'user_site_directory': 0,
|
|
|
|
'faulthandler': 1,
|
|
|
|
'warnoptions': ['EnvVar'],
|
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
self.check_all_configs("test_init_python_env", config, preconfig,
|
|
|
|
api=API_PYTHON)
|
2019-05-22 23:12:27 -03:00
|
|
|
|
2019-03-05 12:37:44 -04:00
|
|
|
def test_init_env_dev_mode(self):
|
2019-05-20 11:38:48 -03:00
|
|
|
preconfig = dict(allocator=PYMEM_ALLOCATOR_DEBUG)
|
|
|
|
config = dict(dev_mode=1,
|
|
|
|
faulthandler=1,
|
2019-03-26 12:58:50 -03:00
|
|
|
warnoptions=['default'])
|
2019-05-27 11:39:22 -03:00
|
|
|
self.check_all_configs("test_init_env_dev_mode", config, preconfig,
|
|
|
|
api=API_COMPAT)
|
2019-03-25 19:19:57 -03:00
|
|
|
|
2019-03-25 22:31:11 -03:00
|
|
|
def test_init_env_dev_mode_alloc(self):
|
2019-05-20 11:38:48 -03:00
|
|
|
preconfig = dict(allocator=PYMEM_ALLOCATOR_MALLOC)
|
|
|
|
config = dict(dev_mode=1,
|
|
|
|
faulthandler=1,
|
2019-03-26 12:58:50 -03:00
|
|
|
warnoptions=['default'])
|
2019-05-27 11:39:22 -03:00
|
|
|
self.check_all_configs("test_init_env_dev_mode_alloc", config, preconfig,
|
|
|
|
api=API_COMPAT)
|
2019-03-06 07:51:53 -04:00
|
|
|
|
2018-07-26 13:57:56 -03:00
|
|
|
def test_init_dev_mode(self):
|
2019-03-25 19:19:57 -03:00
|
|
|
preconfig = {
|
2019-05-17 10:20:52 -03:00
|
|
|
'allocator': PYMEM_ALLOCATOR_DEBUG,
|
2019-03-25 19:19:57 -03:00
|
|
|
}
|
|
|
|
config = {
|
2018-07-26 13:57:56 -03:00
|
|
|
'faulthandler': 1,
|
2019-03-25 22:31:11 -03:00
|
|
|
'dev_mode': 1,
|
2019-03-26 12:58:50 -03:00
|
|
|
'warnoptions': ['default'],
|
2018-07-26 13:57:56 -03:00
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
self.check_all_configs("test_init_dev_mode", config, preconfig,
|
|
|
|
api=API_PYTHON)
|
2019-05-20 06:02:00 -03:00
|
|
|
|
|
|
|
def test_preinit_parse_argv(self):
|
|
|
|
# Pre-initialize implicitly using argv: make sure that -X dev
|
|
|
|
# is used to configure the allocation in preinitialization
|
|
|
|
preconfig = {
|
|
|
|
'allocator': PYMEM_ALLOCATOR_DEBUG,
|
|
|
|
}
|
2019-06-25 10:02:43 -03:00
|
|
|
script_abspath = os.path.abspath('script.py')
|
2019-05-20 06:02:00 -03:00
|
|
|
config = {
|
2019-06-25 10:02:43 -03:00
|
|
|
'argv': [script_abspath],
|
|
|
|
'run_filename': script_abspath,
|
2019-05-20 06:02:00 -03:00
|
|
|
'dev_mode': 1,
|
|
|
|
'faulthandler': 1,
|
|
|
|
'warnoptions': ['default'],
|
|
|
|
'xoptions': ['dev'],
|
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
self.check_all_configs("test_preinit_parse_argv", config, preconfig,
|
|
|
|
api=API_PYTHON)
|
2019-05-20 06:02:00 -03:00
|
|
|
|
|
|
|
def test_preinit_dont_parse_argv(self):
|
|
|
|
# -X dev must be ignored by isolated preconfiguration
|
|
|
|
preconfig = {
|
|
|
|
'isolated': 0,
|
|
|
|
}
|
|
|
|
config = {
|
|
|
|
'argv': ["python3", "-E", "-I",
|
|
|
|
"-X", "dev", "-X", "utf8", "script.py"],
|
|
|
|
'isolated': 0,
|
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
self.check_all_configs("test_preinit_dont_parse_argv", config, preconfig,
|
|
|
|
api=API_ISOLATED)
|
2018-07-26 13:57:56 -03:00
|
|
|
|
2019-05-17 14:01:14 -03:00
|
|
|
def test_init_isolated_flag(self):
|
2019-03-25 19:19:57 -03:00
|
|
|
config = {
|
2019-03-25 22:31:11 -03:00
|
|
|
'isolated': 1,
|
|
|
|
'use_environment': 0,
|
2018-07-26 13:57:56 -03:00
|
|
|
'user_site_directory': 0,
|
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
self.check_all_configs("test_init_isolated_flag", config, api=API_PYTHON)
|
2018-07-26 13:57:56 -03:00
|
|
|
|
2019-03-26 20:26:18 -03:00
|
|
|
def test_preinit_isolated1(self):
|
|
|
|
# _PyPreConfig.isolated=1, _PyCoreConfig.isolated not set
|
|
|
|
config = {
|
|
|
|
'isolated': 1,
|
|
|
|
'use_environment': 0,
|
|
|
|
'user_site_directory': 0,
|
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
self.check_all_configs("test_preinit_isolated1", config, api=API_COMPAT)
|
2019-03-26 20:26:18 -03:00
|
|
|
|
|
|
|
def test_preinit_isolated2(self):
|
|
|
|
# _PyPreConfig.isolated=0, _PyCoreConfig.isolated=1
|
|
|
|
config = {
|
|
|
|
'isolated': 1,
|
|
|
|
'use_environment': 0,
|
|
|
|
'user_site_directory': 0,
|
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
self.check_all_configs("test_preinit_isolated2", config, api=API_COMPAT)
|
2019-03-26 20:26:18 -03:00
|
|
|
|
2019-05-20 06:02:00 -03:00
|
|
|
def test_preinit_isolated_config(self):
|
2019-05-27 11:39:22 -03:00
|
|
|
self.check_all_configs("test_preinit_isolated_config", api=API_ISOLATED)
|
2019-05-20 06:02:00 -03:00
|
|
|
|
2019-05-17 14:01:14 -03:00
|
|
|
def test_init_isolated_config(self):
|
2019-05-27 11:39:22 -03:00
|
|
|
self.check_all_configs("test_init_isolated_config", api=API_ISOLATED)
|
2019-05-22 19:57:57 -03:00
|
|
|
|
|
|
|
def test_preinit_python_config(self):
|
2019-05-27 11:39:22 -03:00
|
|
|
self.check_all_configs("test_preinit_python_config", api=API_PYTHON)
|
2019-05-17 14:01:14 -03:00
|
|
|
|
|
|
|
def test_init_python_config(self):
|
2019-05-27 11:39:22 -03:00
|
|
|
self.check_all_configs("test_init_python_config", api=API_PYTHON)
|
2019-05-17 14:01:14 -03:00
|
|
|
|
2019-05-17 17:44:16 -03:00
|
|
|
def test_init_dont_configure_locale(self):
|
|
|
|
# _PyPreConfig.configure_locale=0
|
|
|
|
preconfig = {
|
|
|
|
'configure_locale': 0,
|
2019-05-20 11:38:48 -03:00
|
|
|
'coerce_c_locale': 0,
|
2019-05-17 17:44:16 -03:00
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
self.check_all_configs("test_init_dont_configure_locale", {}, preconfig,
|
|
|
|
api=API_PYTHON)
|
2019-05-17 17:44:16 -03:00
|
|
|
|
2019-05-14 17:01:51 -03:00
|
|
|
def test_init_read_set(self):
|
2019-05-27 11:39:22 -03:00
|
|
|
config = {
|
2019-05-14 17:01:51 -03:00
|
|
|
'program_name': './init_read_set',
|
|
|
|
'executable': 'my_executable',
|
|
|
|
}
|
2019-08-23 12:57:54 -03:00
|
|
|
def modify_path(path):
|
|
|
|
path.insert(1, "test_path_insert1")
|
|
|
|
path.append("test_path_append")
|
2019-05-27 11:39:22 -03:00
|
|
|
self.check_all_configs("test_init_read_set", config,
|
|
|
|
api=API_PYTHON,
|
2019-08-23 12:57:54 -03:00
|
|
|
modify_path_cb=modify_path)
|
2019-05-14 17:01:51 -03:00
|
|
|
|
2019-08-23 14:03:08 -03:00
|
|
|
def test_init_sys_add(self):
|
|
|
|
config = {
|
|
|
|
'faulthandler': 1,
|
|
|
|
'xoptions': [
|
|
|
|
'config_xoption',
|
|
|
|
'cmdline_xoption',
|
|
|
|
'sysadd_xoption',
|
|
|
|
'faulthandler',
|
|
|
|
],
|
|
|
|
'warnoptions': [
|
|
|
|
'ignore:::config_warnoption',
|
|
|
|
'ignore:::cmdline_warnoption',
|
|
|
|
'ignore:::sysadd_warnoption',
|
|
|
|
],
|
|
|
|
}
|
|
|
|
self.check_all_configs("test_init_sys_add", config, api=API_PYTHON)
|
|
|
|
|
2019-05-16 12:38:16 -03:00
|
|
|
def test_init_run_main(self):
|
|
|
|
code = ('import _testinternalcapi, json; '
|
|
|
|
'print(json.dumps(_testinternalcapi.get_configs()))')
|
2019-05-27 11:39:22 -03:00
|
|
|
config = {
|
2019-05-16 12:38:16 -03:00
|
|
|
'argv': ['-c', 'arg2'],
|
|
|
|
'program_name': './python3',
|
|
|
|
'run_command': code + '\n',
|
2019-05-17 14:01:14 -03:00
|
|
|
'parse_argv': 1,
|
2019-05-16 12:38:16 -03:00
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
self.check_all_configs("test_init_run_main", config, api=API_PYTHON)
|
2019-05-16 12:38:16 -03:00
|
|
|
|
|
|
|
def test_init_main(self):
|
2019-05-14 21:12:48 -03:00
|
|
|
code = ('import _testinternalcapi, json; '
|
|
|
|
'print(json.dumps(_testinternalcapi.get_configs()))')
|
2019-05-27 11:39:22 -03:00
|
|
|
config = {
|
2019-05-14 21:12:48 -03:00
|
|
|
'argv': ['-c', 'arg2'],
|
|
|
|
'program_name': './python3',
|
|
|
|
'run_command': code + '\n',
|
2019-05-17 14:01:14 -03:00
|
|
|
'parse_argv': 1,
|
2019-05-16 12:38:16 -03:00
|
|
|
'_init_main': 0,
|
2019-05-14 21:12:48 -03:00
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
self.check_all_configs("test_init_main", config,
|
|
|
|
api=API_PYTHON,
|
|
|
|
stderr="Run Python code before _Py_InitializeMain")
|
2019-05-14 21:12:48 -03:00
|
|
|
|
2019-05-17 14:01:14 -03:00
|
|
|
def test_init_parse_argv(self):
|
2019-05-27 11:39:22 -03:00
|
|
|
config = {
|
2019-05-17 22:21:27 -03:00
|
|
|
'parse_argv': 1,
|
2019-05-17 14:01:14 -03:00
|
|
|
'argv': ['-c', 'arg1', '-v', 'arg3'],
|
|
|
|
'program_name': './argv0',
|
|
|
|
'run_command': 'pass\n',
|
|
|
|
'use_environment': 0,
|
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
self.check_all_configs("test_init_parse_argv", config, api=API_PYTHON)
|
2019-05-17 14:01:14 -03:00
|
|
|
|
2019-05-16 12:02:56 -03:00
|
|
|
def test_init_dont_parse_argv(self):
|
2019-05-20 06:02:00 -03:00
|
|
|
pre_config = {
|
|
|
|
'parse_argv': 0,
|
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
config = {
|
2019-05-17 22:21:27 -03:00
|
|
|
'parse_argv': 0,
|
2019-05-17 14:01:14 -03:00
|
|
|
'argv': ['./argv0', '-E', '-c', 'pass', 'arg1', '-v', 'arg3'],
|
|
|
|
'program_name': './argv0',
|
2019-05-16 12:02:56 -03:00
|
|
|
}
|
2019-05-27 11:39:22 -03:00
|
|
|
self.check_all_configs("test_init_dont_parse_argv", config, pre_config,
|
|
|
|
api=API_PYTHON)
|
2019-05-16 12:02:56 -03:00
|
|
|
|
2018-07-26 13:57:56 -03:00
|
|
|
|
2019-05-23 12:45:22 -03:00
|
|
|
class AuditingTests(EmbeddingTestsMixin, unittest.TestCase):
|
|
|
|
def test_open_code_hook(self):
|
|
|
|
self.run_embedded_interpreter("test_open_code_hook")
|
|
|
|
|
|
|
|
def test_audit(self):
|
|
|
|
self.run_embedded_interpreter("test_audit")
|
|
|
|
|
|
|
|
def test_audit_subinterpreter(self):
|
|
|
|
self.run_embedded_interpreter("test_audit_subinterpreter")
|
|
|
|
|
2019-07-01 20:03:53 -03:00
|
|
|
def test_audit_run_command(self):
|
|
|
|
self.run_embedded_interpreter("test_audit_run_command", timeout=3, returncode=1)
|
|
|
|
|
|
|
|
def test_audit_run_file(self):
|
|
|
|
self.run_embedded_interpreter("test_audit_run_file", timeout=3, returncode=1)
|
|
|
|
|
|
|
|
def test_audit_run_interactivehook(self):
|
|
|
|
startup = os.path.join(self.oldcwd, support.TESTFN) + ".py"
|
|
|
|
with open(startup, "w", encoding="utf-8") as f:
|
|
|
|
print("import sys", file=f)
|
|
|
|
print("sys.__interactivehook__ = lambda: None", file=f)
|
|
|
|
try:
|
|
|
|
env = {**remove_python_envvars(), "PYTHONSTARTUP": startup}
|
|
|
|
self.run_embedded_interpreter("test_audit_run_interactivehook", timeout=5,
|
|
|
|
returncode=10, env=env)
|
|
|
|
finally:
|
|
|
|
os.unlink(startup)
|
|
|
|
|
|
|
|
def test_audit_run_startup(self):
|
|
|
|
startup = os.path.join(self.oldcwd, support.TESTFN) + ".py"
|
|
|
|
with open(startup, "w", encoding="utf-8") as f:
|
|
|
|
print("pass", file=f)
|
|
|
|
try:
|
|
|
|
env = {**remove_python_envvars(), "PYTHONSTARTUP": startup}
|
|
|
|
self.run_embedded_interpreter("test_audit_run_startup", timeout=5,
|
|
|
|
returncode=10, env=env)
|
|
|
|
finally:
|
|
|
|
os.unlink(startup)
|
|
|
|
|
|
|
|
def test_audit_run_stdin(self):
|
|
|
|
self.run_embedded_interpreter("test_audit_run_stdin", timeout=3, returncode=1)
|
2019-05-23 12:45:22 -03:00
|
|
|
|
2017-11-27 18:11:51 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|