| 1 | n/a | # Common utility functions used by various script execution tests |
|---|
| 2 | n/a | # e.g. test_cmd_line, test_cmd_line_script and test_runpy |
|---|
| 3 | n/a | |
|---|
| 4 | n/a | import collections |
|---|
| 5 | n/a | import importlib |
|---|
| 6 | n/a | import sys |
|---|
| 7 | n/a | import os |
|---|
| 8 | n/a | import os.path |
|---|
| 9 | n/a | import subprocess |
|---|
| 10 | n/a | import py_compile |
|---|
| 11 | n/a | import zipfile |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | from importlib.util import source_from_cache |
|---|
| 14 | n/a | from test.support import make_legacy_pyc, strip_python_stderr |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | # Cached result of the expensive test performed in the function below. |
|---|
| 18 | n/a | __cached_interp_requires_environment = None |
|---|
| 19 | n/a | |
|---|
| 20 | n/a | def interpreter_requires_environment(): |
|---|
| 21 | n/a | """ |
|---|
| 22 | n/a | Returns True if our sys.executable interpreter requires environment |
|---|
| 23 | n/a | variables in order to be able to run at all. |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | This is designed to be used with @unittest.skipIf() to annotate tests |
|---|
| 26 | n/a | that need to use an assert_python*() function to launch an isolated |
|---|
| 27 | n/a | mode (-I) or no environment mode (-E) sub-interpreter process. |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | A normal build & test does not run into this situation but it can happen |
|---|
| 30 | n/a | when trying to run the standard library test suite from an interpreter that |
|---|
| 31 | n/a | doesn't have an obvious home with Python's current home finding logic. |
|---|
| 32 | n/a | |
|---|
| 33 | n/a | Setting PYTHONHOME is one way to get most of the testsuite to run in that |
|---|
| 34 | n/a | situation. PYTHONPATH or PYTHONUSERSITE are other common environment |
|---|
| 35 | n/a | variables that might impact whether or not the interpreter can start. |
|---|
| 36 | n/a | """ |
|---|
| 37 | n/a | global __cached_interp_requires_environment |
|---|
| 38 | n/a | if __cached_interp_requires_environment is None: |
|---|
| 39 | n/a | # Try running an interpreter with -E to see if it works or not. |
|---|
| 40 | n/a | try: |
|---|
| 41 | n/a | subprocess.check_call([sys.executable, '-E', |
|---|
| 42 | n/a | '-c', 'import sys; sys.exit(0)']) |
|---|
| 43 | n/a | except subprocess.CalledProcessError: |
|---|
| 44 | n/a | __cached_interp_requires_environment = True |
|---|
| 45 | n/a | else: |
|---|
| 46 | n/a | __cached_interp_requires_environment = False |
|---|
| 47 | n/a | |
|---|
| 48 | n/a | return __cached_interp_requires_environment |
|---|
| 49 | n/a | |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | _PythonRunResult = collections.namedtuple("_PythonRunResult", |
|---|
| 52 | n/a | ("rc", "out", "err")) |
|---|
| 53 | n/a | |
|---|
| 54 | n/a | |
|---|
| 55 | n/a | # Executing the interpreter in a subprocess |
|---|
| 56 | n/a | def run_python_until_end(*args, **env_vars): |
|---|
| 57 | n/a | env_required = interpreter_requires_environment() |
|---|
| 58 | n/a | if '__isolated' in env_vars: |
|---|
| 59 | n/a | isolated = env_vars.pop('__isolated') |
|---|
| 60 | n/a | else: |
|---|
| 61 | n/a | isolated = not env_vars and not env_required |
|---|
| 62 | n/a | cmd_line = [sys.executable, '-X', 'faulthandler'] |
|---|
| 63 | n/a | if isolated: |
|---|
| 64 | n/a | # isolated mode: ignore Python environment variables, ignore user |
|---|
| 65 | n/a | # site-packages, and don't add the current directory to sys.path |
|---|
| 66 | n/a | cmd_line.append('-I') |
|---|
| 67 | n/a | elif not env_vars and not env_required: |
|---|
| 68 | n/a | # ignore Python environment variables |
|---|
| 69 | n/a | cmd_line.append('-E') |
|---|
| 70 | n/a | |
|---|
| 71 | n/a | # But a special flag that can be set to override -- in this case, the |
|---|
| 72 | n/a | # caller is responsible to pass the full environment. |
|---|
| 73 | n/a | if env_vars.pop('__cleanenv', None): |
|---|
| 74 | n/a | env = {} |
|---|
| 75 | n/a | if sys.platform == 'win32': |
|---|
| 76 | n/a | # Windows requires at least the SYSTEMROOT environment variable to |
|---|
| 77 | n/a | # start Python. |
|---|
| 78 | n/a | env['SYSTEMROOT'] = os.environ['SYSTEMROOT'] |
|---|
| 79 | n/a | |
|---|
| 80 | n/a | # Other interesting environment variables, not copied currently: |
|---|
| 81 | n/a | # COMSPEC, HOME, PATH, TEMP, TMPDIR, TMP. |
|---|
| 82 | n/a | else: |
|---|
| 83 | n/a | # Need to preserve the original environment, for in-place testing of |
|---|
| 84 | n/a | # shared library builds. |
|---|
| 85 | n/a | env = os.environ.copy() |
|---|
| 86 | n/a | |
|---|
| 87 | n/a | # set TERM='' unless the TERM environment variable is passed explicitly |
|---|
| 88 | n/a | # see issues #11390 and #18300 |
|---|
| 89 | n/a | if 'TERM' not in env_vars: |
|---|
| 90 | n/a | env['TERM'] = '' |
|---|
| 91 | n/a | |
|---|
| 92 | n/a | env.update(env_vars) |
|---|
| 93 | n/a | cmd_line.extend(args) |
|---|
| 94 | n/a | proc = subprocess.Popen(cmd_line, stdin=subprocess.PIPE, |
|---|
| 95 | n/a | stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
|---|
| 96 | n/a | env=env) |
|---|
| 97 | n/a | with proc: |
|---|
| 98 | n/a | try: |
|---|
| 99 | n/a | out, err = proc.communicate() |
|---|
| 100 | n/a | finally: |
|---|
| 101 | n/a | proc.kill() |
|---|
| 102 | n/a | subprocess._cleanup() |
|---|
| 103 | n/a | rc = proc.returncode |
|---|
| 104 | n/a | err = strip_python_stderr(err) |
|---|
| 105 | n/a | return _PythonRunResult(rc, out, err), cmd_line |
|---|
| 106 | n/a | |
|---|
| 107 | n/a | def _assert_python(expected_success, *args, **env_vars): |
|---|
| 108 | n/a | res, cmd_line = run_python_until_end(*args, **env_vars) |
|---|
| 109 | n/a | if (res.rc and expected_success) or (not res.rc and not expected_success): |
|---|
| 110 | n/a | # Limit to 80 lines to ASCII characters |
|---|
| 111 | n/a | maxlen = 80 * 100 |
|---|
| 112 | n/a | out, err = res.out, res.err |
|---|
| 113 | n/a | if len(out) > maxlen: |
|---|
| 114 | n/a | out = b'(... truncated stdout ...)' + out[-maxlen:] |
|---|
| 115 | n/a | if len(err) > maxlen: |
|---|
| 116 | n/a | err = b'(... truncated stderr ...)' + err[-maxlen:] |
|---|
| 117 | n/a | out = out.decode('ascii', 'replace').rstrip() |
|---|
| 118 | n/a | err = err.decode('ascii', 'replace').rstrip() |
|---|
| 119 | n/a | raise AssertionError("Process return code is %d\n" |
|---|
| 120 | n/a | "command line: %r\n" |
|---|
| 121 | n/a | "\n" |
|---|
| 122 | n/a | "stdout:\n" |
|---|
| 123 | n/a | "---\n" |
|---|
| 124 | n/a | "%s\n" |
|---|
| 125 | n/a | "---\n" |
|---|
| 126 | n/a | "\n" |
|---|
| 127 | n/a | "stderr:\n" |
|---|
| 128 | n/a | "---\n" |
|---|
| 129 | n/a | "%s\n" |
|---|
| 130 | n/a | "---" |
|---|
| 131 | n/a | % (res.rc, cmd_line, |
|---|
| 132 | n/a | out, |
|---|
| 133 | n/a | err)) |
|---|
| 134 | n/a | return res |
|---|
| 135 | n/a | |
|---|
| 136 | n/a | def assert_python_ok(*args, **env_vars): |
|---|
| 137 | n/a | """ |
|---|
| 138 | n/a | Assert that running the interpreter with `args` and optional environment |
|---|
| 139 | n/a | variables `env_vars` succeeds (rc == 0) and return a (return code, stdout, |
|---|
| 140 | n/a | stderr) tuple. |
|---|
| 141 | n/a | |
|---|
| 142 | n/a | If the __cleanenv keyword is set, env_vars is used as a fresh environment. |
|---|
| 143 | n/a | |
|---|
| 144 | n/a | Python is started in isolated mode (command line option -I), |
|---|
| 145 | n/a | except if the __isolated keyword is set to False. |
|---|
| 146 | n/a | """ |
|---|
| 147 | n/a | return _assert_python(True, *args, **env_vars) |
|---|
| 148 | n/a | |
|---|
| 149 | n/a | def assert_python_failure(*args, **env_vars): |
|---|
| 150 | n/a | """ |
|---|
| 151 | n/a | Assert that running the interpreter with `args` and optional environment |
|---|
| 152 | n/a | variables `env_vars` fails (rc != 0) and return a (return code, stdout, |
|---|
| 153 | n/a | stderr) tuple. |
|---|
| 154 | n/a | |
|---|
| 155 | n/a | See assert_python_ok() for more options. |
|---|
| 156 | n/a | """ |
|---|
| 157 | n/a | return _assert_python(False, *args, **env_vars) |
|---|
| 158 | n/a | |
|---|
| 159 | n/a | def spawn_python(*args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, **kw): |
|---|
| 160 | n/a | """Run a Python subprocess with the given arguments. |
|---|
| 161 | n/a | |
|---|
| 162 | n/a | kw is extra keyword args to pass to subprocess.Popen. Returns a Popen |
|---|
| 163 | n/a | object. |
|---|
| 164 | n/a | """ |
|---|
| 165 | n/a | cmd_line = [sys.executable, '-E'] |
|---|
| 166 | n/a | cmd_line.extend(args) |
|---|
| 167 | n/a | # Under Fedora (?), GNU readline can output junk on stderr when initialized, |
|---|
| 168 | n/a | # depending on the TERM setting. Setting TERM=vt100 is supposed to disable |
|---|
| 169 | n/a | # that. References: |
|---|
| 170 | n/a | # - http://reinout.vanrees.org/weblog/2009/08/14/readline-invisible-character-hack.html |
|---|
| 171 | n/a | # - http://stackoverflow.com/questions/15760712/python-readline-module-prints-escape-character-during-import |
|---|
| 172 | n/a | # - http://lists.gnu.org/archive/html/bug-readline/2007-08/msg00004.html |
|---|
| 173 | n/a | env = kw.setdefault('env', dict(os.environ)) |
|---|
| 174 | n/a | env['TERM'] = 'vt100' |
|---|
| 175 | n/a | return subprocess.Popen(cmd_line, stdin=subprocess.PIPE, |
|---|
| 176 | n/a | stdout=stdout, stderr=stderr, |
|---|
| 177 | n/a | **kw) |
|---|
| 178 | n/a | |
|---|
| 179 | n/a | def kill_python(p): |
|---|
| 180 | n/a | """Run the given Popen process until completion and return stdout.""" |
|---|
| 181 | n/a | p.stdin.close() |
|---|
| 182 | n/a | data = p.stdout.read() |
|---|
| 183 | n/a | p.stdout.close() |
|---|
| 184 | n/a | # try to cleanup the child so we don't appear to leak when running |
|---|
| 185 | n/a | # with regrtest -R. |
|---|
| 186 | n/a | p.wait() |
|---|
| 187 | n/a | subprocess._cleanup() |
|---|
| 188 | n/a | return data |
|---|
| 189 | n/a | |
|---|
| 190 | n/a | def make_script(script_dir, script_basename, source, omit_suffix=False): |
|---|
| 191 | n/a | script_filename = script_basename |
|---|
| 192 | n/a | if not omit_suffix: |
|---|
| 193 | n/a | script_filename += os.extsep + 'py' |
|---|
| 194 | n/a | script_name = os.path.join(script_dir, script_filename) |
|---|
| 195 | n/a | # The script should be encoded to UTF-8, the default string encoding |
|---|
| 196 | n/a | script_file = open(script_name, 'w', encoding='utf-8') |
|---|
| 197 | n/a | script_file.write(source) |
|---|
| 198 | n/a | script_file.close() |
|---|
| 199 | n/a | importlib.invalidate_caches() |
|---|
| 200 | n/a | return script_name |
|---|
| 201 | n/a | |
|---|
| 202 | n/a | def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None): |
|---|
| 203 | n/a | zip_filename = zip_basename+os.extsep+'zip' |
|---|
| 204 | n/a | zip_name = os.path.join(zip_dir, zip_filename) |
|---|
| 205 | n/a | zip_file = zipfile.ZipFile(zip_name, 'w') |
|---|
| 206 | n/a | if name_in_zip is None: |
|---|
| 207 | n/a | parts = script_name.split(os.sep) |
|---|
| 208 | n/a | if len(parts) >= 2 and parts[-2] == '__pycache__': |
|---|
| 209 | n/a | legacy_pyc = make_legacy_pyc(source_from_cache(script_name)) |
|---|
| 210 | n/a | name_in_zip = os.path.basename(legacy_pyc) |
|---|
| 211 | n/a | script_name = legacy_pyc |
|---|
| 212 | n/a | else: |
|---|
| 213 | n/a | name_in_zip = os.path.basename(script_name) |
|---|
| 214 | n/a | zip_file.write(script_name, name_in_zip) |
|---|
| 215 | n/a | zip_file.close() |
|---|
| 216 | n/a | #if test.support.verbose: |
|---|
| 217 | n/a | # zip_file = zipfile.ZipFile(zip_name, 'r') |
|---|
| 218 | n/a | # print 'Contents of %r:' % zip_name |
|---|
| 219 | n/a | # zip_file.printdir() |
|---|
| 220 | n/a | # zip_file.close() |
|---|
| 221 | n/a | return zip_name, os.path.join(zip_name, name_in_zip) |
|---|
| 222 | n/a | |
|---|
| 223 | n/a | def make_pkg(pkg_dir, init_source=''): |
|---|
| 224 | n/a | os.mkdir(pkg_dir) |
|---|
| 225 | n/a | make_script(pkg_dir, '__init__', init_source) |
|---|
| 226 | n/a | |
|---|
| 227 | n/a | def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename, |
|---|
| 228 | n/a | source, depth=1, compiled=False): |
|---|
| 229 | n/a | unlink = [] |
|---|
| 230 | n/a | init_name = make_script(zip_dir, '__init__', '') |
|---|
| 231 | n/a | unlink.append(init_name) |
|---|
| 232 | n/a | init_basename = os.path.basename(init_name) |
|---|
| 233 | n/a | script_name = make_script(zip_dir, script_basename, source) |
|---|
| 234 | n/a | unlink.append(script_name) |
|---|
| 235 | n/a | if compiled: |
|---|
| 236 | n/a | init_name = py_compile.compile(init_name, doraise=True) |
|---|
| 237 | n/a | script_name = py_compile.compile(script_name, doraise=True) |
|---|
| 238 | n/a | unlink.extend((init_name, script_name)) |
|---|
| 239 | n/a | pkg_names = [os.sep.join([pkg_name]*i) for i in range(1, depth+1)] |
|---|
| 240 | n/a | script_name_in_zip = os.path.join(pkg_names[-1], os.path.basename(script_name)) |
|---|
| 241 | n/a | zip_filename = zip_basename+os.extsep+'zip' |
|---|
| 242 | n/a | zip_name = os.path.join(zip_dir, zip_filename) |
|---|
| 243 | n/a | zip_file = zipfile.ZipFile(zip_name, 'w') |
|---|
| 244 | n/a | for name in pkg_names: |
|---|
| 245 | n/a | init_name_in_zip = os.path.join(name, init_basename) |
|---|
| 246 | n/a | zip_file.write(init_name, init_name_in_zip) |
|---|
| 247 | n/a | zip_file.write(script_name, script_name_in_zip) |
|---|
| 248 | n/a | zip_file.close() |
|---|
| 249 | n/a | for name in unlink: |
|---|
| 250 | n/a | os.unlink(name) |
|---|
| 251 | n/a | #if test.support.verbose: |
|---|
| 252 | n/a | # zip_file = zipfile.ZipFile(zip_name, 'r') |
|---|
| 253 | n/a | # print 'Contents of %r:' % zip_name |
|---|
| 254 | n/a | # zip_file.printdir() |
|---|
| 255 | n/a | # zip_file.close() |
|---|
| 256 | n/a | return zip_name, os.path.join(zip_name, script_name_in_zip) |
|---|