1"""
2Backward compatibility support for Python 3.5
3"""
4
5import sys
6import test.support
7import subprocess
8
9
10# copied from Python 3.9 test.support module
11def _missing_compiler_executable(cmd_names=[]):
12    """Check if the compiler components used to build the interpreter exist.
13
14    Check for the existence of the compiler executables whose names are listed
15    in 'cmd_names' or all the compiler executables when 'cmd_names' is empty
16    and return the first missing executable or None when none is found
17    missing.
18
19    """
20    from distutils import ccompiler, sysconfig, spawn
21    compiler = ccompiler.new_compiler()
22    sysconfig.customize_compiler(compiler)
23    for name in compiler.executables:
24        if cmd_names and name not in cmd_names:
25            continue
26        cmd = getattr(compiler, name)
27        if cmd_names:
28            assert cmd is not None, \
29                    "the '%s' executable is not configured" % name
30        elif not cmd:
31            continue
32        if spawn.find_executable(cmd[0]) is None:
33            return cmd[0]
34
35
36missing_compiler_executable = vars(test.support).setdefault(
37    'missing_compiler_executable',
38    _missing_compiler_executable,
39)
40
41
42try:
43    from test.support import unix_shell
44except ImportError:
45    # Adapted from Python 3.9 test.support module
46    is_android = hasattr(sys, 'getandroidapilevel')
47    unix_shell = (
48        None if sys.platform == 'win32' else
49        '/system/bin/sh' if is_android else
50        '/bin/sh'
51    )
52
53
54# copied from Python 3.9 subprocess module
55def _optim_args_from_interpreter_flags():
56    """Return a list of command-line arguments reproducing the current
57    optimization settings in sys.flags."""
58    args = []
59    value = sys.flags.optimize
60    if value > 0:
61        args.append('-' + 'O' * value)
62    return args
63
64
65vars(subprocess).setdefault(
66    '_optim_args_from_interpreter_flags',
67    _optim_args_from_interpreter_flags,
68)
69
70
71def adapt_glob(regex):
72    """
73    Supply legacy expectation on Python 3.5
74    """
75    if sys.version_info > (3, 6):
76        return regex
77    return regex.replace('(?s:', '').replace(r')\Z', r'\Z(?ms)')
78