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 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 15 | from imp 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): |
| 20 | cmd_line = [sys.executable] |
Antoine Pitrou | adffced | 2010-11-09 22:04:44 +0000 | [diff] [blame] | 21 | if not env_vars: |
Antoine Pitrou | 9bc3568 | 2010-11-09 21:33:55 +0000 | [diff] [blame] | 22 | cmd_line.append('-E') |
Antoine Pitrou | adffced | 2010-11-09 22:04:44 +0000 | [diff] [blame] | 23 | # Need to preserve the original environment, for in-place testing of |
| 24 | # shared library builds. |
| 25 | env = os.environ.copy() |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 26 | # But a special flag that can be set to override -- in this case, the |
| 27 | # caller is responsible to pass the full environment. |
| 28 | if env_vars.pop('__cleanenv', None): |
| 29 | env = {} |
Antoine Pitrou | adffced | 2010-11-09 22:04:44 +0000 | [diff] [blame] | 30 | env.update(env_vars) |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 31 | cmd_line.extend(args) |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 32 | p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE, |
Antoine Pitrou | 9bc3568 | 2010-11-09 21:33:55 +0000 | [diff] [blame] | 33 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 34 | env=env) |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 35 | try: |
| 36 | out, err = p.communicate() |
| 37 | finally: |
| 38 | subprocess._cleanup() |
Brian Curtin | c4ac887 | 2010-11-01 14:00:33 +0000 | [diff] [blame] | 39 | p.stdout.close() |
| 40 | p.stderr.close() |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 41 | rc = p.returncode |
R. David Murray | 1b2bd3b | 2010-12-08 22:53:00 +0000 | [diff] [blame] | 42 | err = strip_python_stderr(err) |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 43 | if (rc and expected_success) or (not rc and not expected_success): |
| 44 | raise AssertionError( |
| 45 | "Process return code is %d, " |
| 46 | "stderr follows:\n%s" % (rc, err.decode('ascii', 'ignore'))) |
| 47 | return rc, out, err |
| 48 | |
Antoine Pitrou | 9bc3568 | 2010-11-09 21:33:55 +0000 | [diff] [blame] | 49 | def assert_python_ok(*args, **env_vars): |
| 50 | """ |
| 51 | Assert that running the interpreter with `args` and optional environment |
| 52 | variables `env_vars` is ok and return a (return code, stdout, stderr) tuple. |
| 53 | """ |
| 54 | return _assert_python(True, *args, **env_vars) |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 55 | |
Antoine Pitrou | 9bc3568 | 2010-11-09 21:33:55 +0000 | [diff] [blame] | 56 | def assert_python_failure(*args, **env_vars): |
| 57 | """ |
| 58 | Assert that running the interpreter with `args` and optional environment |
| 59 | variables `env_vars` fails and return a (return code, stdout, stderr) tuple. |
| 60 | """ |
| 61 | return _assert_python(False, *args, **env_vars) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 62 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 63 | def spawn_python(*args, **kw): |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 64 | cmd_line = [sys.executable, '-E'] |
| 65 | cmd_line.extend(args) |
| 66 | return subprocess.Popen(cmd_line, stdin=subprocess.PIPE, |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 67 | stdout=subprocess.PIPE, stderr=subprocess.STDOUT, |
| 68 | **kw) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 69 | |
| 70 | def kill_python(p): |
| 71 | p.stdin.close() |
| 72 | data = p.stdout.read() |
| 73 | p.stdout.close() |
| 74 | # 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] | 75 | # with regrtest -R. |
| 76 | p.wait() |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 77 | subprocess._cleanup() |
| 78 | return data |
| 79 | |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 80 | def make_script(script_dir, script_basename, source): |
| 81 | script_filename = script_basename+os.extsep+'py' |
| 82 | script_name = os.path.join(script_dir, script_filename) |
Florent Xicluna | 8de42e2 | 2010-02-27 16:12:22 +0000 | [diff] [blame] | 83 | # The script should be encoded to UTF-8, the default string encoding |
| 84 | script_file = open(script_name, 'w', encoding='utf-8') |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 85 | script_file.write(source) |
| 86 | script_file.close() |
Brett Cannon | c8287ef | 2012-04-27 13:52:03 -0400 | [diff] [blame] | 87 | importlib.invalidate_caches() |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 88 | return script_name |
| 89 | |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 90 | def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None): |
| 91 | zip_filename = zip_basename+os.extsep+'zip' |
| 92 | zip_name = os.path.join(zip_dir, zip_filename) |
| 93 | zip_file = zipfile.ZipFile(zip_name, 'w') |
| 94 | if name_in_zip is None: |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 95 | parts = script_name.split(os.sep) |
| 96 | if len(parts) >= 2 and parts[-2] == '__pycache__': |
| 97 | legacy_pyc = make_legacy_pyc(source_from_cache(script_name)) |
| 98 | name_in_zip = os.path.basename(legacy_pyc) |
| 99 | script_name = legacy_pyc |
| 100 | else: |
| 101 | name_in_zip = os.path.basename(script_name) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 102 | zip_file.write(script_name, name_in_zip) |
| 103 | zip_file.close() |
Florent Xicluna | 02ea12b2 | 2010-07-28 16:39:41 +0000 | [diff] [blame] | 104 | #if test.support.verbose: |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 105 | # zip_file = zipfile.ZipFile(zip_name, 'r') |
| 106 | # print 'Contents of %r:' % zip_name |
| 107 | # zip_file.printdir() |
| 108 | # zip_file.close() |
| 109 | return zip_name, os.path.join(zip_name, name_in_zip) |
| 110 | |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 111 | def make_pkg(pkg_dir, init_source=''): |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 112 | os.mkdir(pkg_dir) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 113 | make_script(pkg_dir, '__init__', init_source) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 114 | |
| 115 | def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename, |
| 116 | source, depth=1, compiled=False): |
| 117 | unlink = [] |
| 118 | init_name = make_script(zip_dir, '__init__', '') |
| 119 | unlink.append(init_name) |
| 120 | init_basename = os.path.basename(init_name) |
| 121 | script_name = make_script(zip_dir, script_basename, source) |
| 122 | unlink.append(script_name) |
| 123 | if compiled: |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 124 | init_name = py_compile(init_name, doraise=True) |
| 125 | script_name = py_compile(script_name, doraise=True) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 126 | unlink.extend((init_name, script_name)) |
| 127 | pkg_names = [os.sep.join([pkg_name]*i) for i in range(1, depth+1)] |
| 128 | script_name_in_zip = os.path.join(pkg_names[-1], os.path.basename(script_name)) |
| 129 | zip_filename = zip_basename+os.extsep+'zip' |
| 130 | zip_name = os.path.join(zip_dir, zip_filename) |
| 131 | zip_file = zipfile.ZipFile(zip_name, 'w') |
| 132 | for name in pkg_names: |
| 133 | init_name_in_zip = os.path.join(name, init_basename) |
| 134 | zip_file.write(init_name, init_name_in_zip) |
| 135 | zip_file.write(script_name, script_name_in_zip) |
| 136 | zip_file.close() |
| 137 | for name in unlink: |
| 138 | os.unlink(name) |
Florent Xicluna | 02ea12b2 | 2010-07-28 16:39:41 +0000 | [diff] [blame] | 139 | #if test.support.verbose: |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 140 | # zip_file = zipfile.ZipFile(zip_name, 'r') |
| 141 | # print 'Contents of %r:' % zip_name |
| 142 | # zip_file.printdir() |
| 143 | # zip_file.close() |
| 144 | return zip_name, os.path.join(zip_name, script_name_in_zip) |