Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 1 | # Common utility functions used by various script execution tests |
| 2 | # e.g. test_cmd_line, test_cmd_line_script and test_runpy |
| 3 | |
Brett Cannon | c8287ef | 2012-04-27 13:52:03 -0400 | [diff] [blame] | 4 | import importlib |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 5 | import sys |
| 6 | import os |
| 7 | import os.path |
| 8 | import tempfile |
| 9 | import subprocess |
| 10 | import py_compile |
| 11 | import contextlib |
| 12 | import shutil |
| 13 | import zipfile |
| 14 | |
Brett Cannon | 9529fbf | 2013-06-15 17:11:25 -0400 | [diff] [blame] | 15 | from importlib.util import source_from_cache |
Nick Coghlan | 5517596 | 2013-07-28 22:11:50 +1000 | [diff] [blame] | 16 | from test.support import make_legacy_pyc, strip_python_stderr, temp_dir |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 17 | |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 18 | # Executing the interpreter in a subprocess |
Antoine Pitrou | 9bc3568 | 2010-11-09 21:33:55 +0000 | [diff] [blame] | 19 | def _assert_python(expected_success, *args, **env_vars): |
Victor Stinner | e8785ff | 2013-10-12 14:44:01 +0200 | [diff] [blame] | 20 | if '__isolated' in env_vars: |
| 21 | isolated = env_vars.pop('__isolated') |
| 22 | else: |
| 23 | isolated = not env_vars |
Victor Stinner | 383a820 | 2013-06-25 21:24:36 +0200 | [diff] [blame] | 24 | cmd_line = [sys.executable, '-X', 'faulthandler'] |
Victor Stinner | e8785ff | 2013-10-12 14:44:01 +0200 | [diff] [blame] | 25 | if isolated: |
| 26 | # isolated mode: ignore Python environment variables, ignore user |
| 27 | # site-packages, and don't add the current directory to sys.path |
| 28 | cmd_line.append('-I') |
| 29 | elif not env_vars: |
| 30 | # ignore Python environment variables |
Antoine Pitrou | 9bc3568 | 2010-11-09 21:33:55 +0000 | [diff] [blame] | 31 | cmd_line.append('-E') |
Antoine Pitrou | adffced | 2010-11-09 22:04:44 +0000 | [diff] [blame] | 32 | # Need to preserve the original environment, for in-place testing of |
| 33 | # shared library builds. |
| 34 | env = os.environ.copy() |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 35 | # But a special flag that can be set to override -- in this case, the |
| 36 | # caller is responsible to pass the full environment. |
| 37 | if env_vars.pop('__cleanenv', None): |
| 38 | env = {} |
Antoine Pitrou | adffced | 2010-11-09 22:04:44 +0000 | [diff] [blame] | 39 | env.update(env_vars) |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 40 | cmd_line.extend(args) |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 41 | p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE, |
Antoine Pitrou | 9bc3568 | 2010-11-09 21:33:55 +0000 | [diff] [blame] | 42 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 43 | env=env) |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 44 | try: |
| 45 | out, err = p.communicate() |
| 46 | finally: |
| 47 | subprocess._cleanup() |
Brian Curtin | c4ac887 | 2010-11-01 14:00:33 +0000 | [diff] [blame] | 48 | p.stdout.close() |
| 49 | p.stderr.close() |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 50 | rc = p.returncode |
Eli Bendersky | 8f2c2bc | 2013-08-11 16:48:44 -0700 | [diff] [blame] | 51 | err = strip_python_stderr(err) |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 52 | if (rc and expected_success) or (not rc and not expected_success): |
| 53 | raise AssertionError( |
| 54 | "Process return code is %d, " |
| 55 | "stderr follows:\n%s" % (rc, err.decode('ascii', 'ignore'))) |
| 56 | return rc, out, err |
| 57 | |
Antoine Pitrou | 9bc3568 | 2010-11-09 21:33:55 +0000 | [diff] [blame] | 58 | def assert_python_ok(*args, **env_vars): |
| 59 | """ |
| 60 | Assert that running the interpreter with `args` and optional environment |
Eli Bendersky | 8f2c2bc | 2013-08-11 16:48:44 -0700 | [diff] [blame] | 61 | variables `env_vars` succeeds (rc == 0) and return a (return code, stdout, |
| 62 | stderr) tuple. |
Victor Stinner | e8785ff | 2013-10-12 14:44:01 +0200 | [diff] [blame] | 63 | |
| 64 | If the __cleanenv keyword is set, env_vars is used a fresh environment. |
| 65 | |
| 66 | Python is started in isolated mode (command line option -I), |
| 67 | except if the __isolated keyword is set to False. |
Antoine Pitrou | 9bc3568 | 2010-11-09 21:33:55 +0000 | [diff] [blame] | 68 | """ |
| 69 | return _assert_python(True, *args, **env_vars) |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 70 | |
Antoine Pitrou | 9bc3568 | 2010-11-09 21:33:55 +0000 | [diff] [blame] | 71 | def assert_python_failure(*args, **env_vars): |
| 72 | """ |
| 73 | Assert that running the interpreter with `args` and optional environment |
Eli Bendersky | 8f2c2bc | 2013-08-11 16:48:44 -0700 | [diff] [blame] | 74 | variables `env_vars` fails (rc != 0) and return a (return code, stdout, |
| 75 | stderr) tuple. |
Victor Stinner | e8785ff | 2013-10-12 14:44:01 +0200 | [diff] [blame] | 76 | |
| 77 | See assert_python_ok() for more options. |
Antoine Pitrou | 9bc3568 | 2010-11-09 21:33:55 +0000 | [diff] [blame] | 78 | """ |
| 79 | return _assert_python(False, *args, **env_vars) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 80 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 81 | def spawn_python(*args, **kw): |
Eli Bendersky | 8f2c2bc | 2013-08-11 16:48:44 -0700 | [diff] [blame] | 82 | """Run a Python subprocess with the given arguments. |
| 83 | |
| 84 | kw is extra keyword args to pass to subprocess.Popen. Returns a Popen |
| 85 | object. |
| 86 | """ |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 87 | cmd_line = [sys.executable, '-E'] |
| 88 | cmd_line.extend(args) |
| 89 | return subprocess.Popen(cmd_line, stdin=subprocess.PIPE, |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 90 | stdout=subprocess.PIPE, stderr=subprocess.STDOUT, |
| 91 | **kw) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 92 | |
| 93 | def kill_python(p): |
Eli Bendersky | 8f2c2bc | 2013-08-11 16:48:44 -0700 | [diff] [blame] | 94 | """Run the given Popen process until completion and return stdout.""" |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 95 | p.stdin.close() |
| 96 | data = p.stdout.read() |
| 97 | p.stdout.close() |
| 98 | # try to cleanup the child so we don't appear to leak when running |
Antoine Pitrou | 4e7dc5f | 2009-12-08 19:27:24 +0000 | [diff] [blame] | 99 | # with regrtest -R. |
| 100 | p.wait() |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 101 | subprocess._cleanup() |
| 102 | return data |
| 103 | |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 104 | def make_script(script_dir, script_basename, source): |
| 105 | script_filename = script_basename+os.extsep+'py' |
| 106 | script_name = os.path.join(script_dir, script_filename) |
Florent Xicluna | 8de42e2 | 2010-02-27 16:12:22 +0000 | [diff] [blame] | 107 | # The script should be encoded to UTF-8, the default string encoding |
| 108 | script_file = open(script_name, 'w', encoding='utf-8') |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 109 | script_file.write(source) |
| 110 | script_file.close() |
Brett Cannon | c8287ef | 2012-04-27 13:52:03 -0400 | [diff] [blame] | 111 | importlib.invalidate_caches() |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 112 | return script_name |
| 113 | |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 114 | def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None): |
| 115 | zip_filename = zip_basename+os.extsep+'zip' |
| 116 | zip_name = os.path.join(zip_dir, zip_filename) |
| 117 | zip_file = zipfile.ZipFile(zip_name, 'w') |
| 118 | if name_in_zip is None: |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 119 | parts = script_name.split(os.sep) |
| 120 | if len(parts) >= 2 and parts[-2] == '__pycache__': |
| 121 | legacy_pyc = make_legacy_pyc(source_from_cache(script_name)) |
| 122 | name_in_zip = os.path.basename(legacy_pyc) |
| 123 | script_name = legacy_pyc |
| 124 | else: |
| 125 | name_in_zip = os.path.basename(script_name) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 126 | zip_file.write(script_name, name_in_zip) |
| 127 | zip_file.close() |
Florent Xicluna | 02ea12b2 | 2010-07-28 16:39:41 +0000 | [diff] [blame] | 128 | #if test.support.verbose: |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 129 | # zip_file = zipfile.ZipFile(zip_name, 'r') |
| 130 | # print 'Contents of %r:' % zip_name |
| 131 | # zip_file.printdir() |
| 132 | # zip_file.close() |
| 133 | return zip_name, os.path.join(zip_name, name_in_zip) |
| 134 | |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 135 | def make_pkg(pkg_dir, init_source=''): |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 136 | os.mkdir(pkg_dir) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 137 | make_script(pkg_dir, '__init__', init_source) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 138 | |
| 139 | def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename, |
| 140 | source, depth=1, compiled=False): |
| 141 | unlink = [] |
| 142 | init_name = make_script(zip_dir, '__init__', '') |
| 143 | unlink.append(init_name) |
| 144 | init_basename = os.path.basename(init_name) |
| 145 | script_name = make_script(zip_dir, script_basename, source) |
| 146 | unlink.append(script_name) |
| 147 | if compiled: |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 148 | init_name = py_compile(init_name, doraise=True) |
| 149 | script_name = py_compile(script_name, doraise=True) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 150 | unlink.extend((init_name, script_name)) |
| 151 | pkg_names = [os.sep.join([pkg_name]*i) for i in range(1, depth+1)] |
| 152 | script_name_in_zip = os.path.join(pkg_names[-1], os.path.basename(script_name)) |
| 153 | zip_filename = zip_basename+os.extsep+'zip' |
| 154 | zip_name = os.path.join(zip_dir, zip_filename) |
| 155 | zip_file = zipfile.ZipFile(zip_name, 'w') |
| 156 | for name in pkg_names: |
| 157 | init_name_in_zip = os.path.join(name, init_basename) |
| 158 | zip_file.write(init_name, init_name_in_zip) |
| 159 | zip_file.write(script_name, script_name_in_zip) |
| 160 | zip_file.close() |
| 161 | for name in unlink: |
| 162 | os.unlink(name) |
Florent Xicluna | 02ea12b2 | 2010-07-28 16:39:41 +0000 | [diff] [blame] | 163 | #if test.support.verbose: |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 164 | # zip_file = zipfile.ZipFile(zip_name, 'r') |
| 165 | # print 'Contents of %r:' % zip_name |
| 166 | # zip_file.printdir() |
| 167 | # zip_file.close() |
| 168 | return zip_name, os.path.join(zip_name, script_name_in_zip) |