blob: 87a781e31a4852887946eea9becb293dcda880c7 [file] [log] [blame]
Nick Coghlan260bd3e2009-11-16 06:49:25 +00001# Common utility functions used by various script execution tests
2# e.g. test_cmd_line, test_cmd_line_script and test_runpy
3
Brett Cannonc8287ef2012-04-27 13:52:03 -04004import importlib
Nick Coghlan260bd3e2009-11-16 06:49:25 +00005import sys
6import os
7import os.path
8import tempfile
9import subprocess
10import py_compile
11import contextlib
12import shutil
13import zipfile
14
Brett Cannon9529fbf2013-06-15 17:11:25 -040015from importlib.util import source_from_cache
Nick Coghlan55175962013-07-28 22:11:50 +100016from test.support import make_legacy_pyc, strip_python_stderr, temp_dir
Barry Warsaw28a691b2010-04-17 00:19:56 +000017
Nick Coghlan260bd3e2009-11-16 06:49:25 +000018# Executing the interpreter in a subprocess
Antoine Pitrou9bc35682010-11-09 21:33:55 +000019def _assert_python(expected_success, *args, **env_vars):
Victor Stinnere8785ff2013-10-12 14:44:01 +020020 if '__isolated' in env_vars:
21 isolated = env_vars.pop('__isolated')
22 else:
23 isolated = not env_vars
Victor Stinner383a8202013-06-25 21:24:36 +020024 cmd_line = [sys.executable, '-X', 'faulthandler']
Victor Stinnere8785ff2013-10-12 14:44:01 +020025 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 Pitrou9bc35682010-11-09 21:33:55 +000031 cmd_line.append('-E')
Antoine Pitrouadffced2010-11-09 22:04:44 +000032 # Need to preserve the original environment, for in-place testing of
33 # shared library builds.
34 env = os.environ.copy()
Georg Brandl2daf6ae2012-02-20 19:54:16 +010035 # 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 Pitrouadffced2010-11-09 22:04:44 +000039 env.update(env_vars)
Georg Brandl2daf6ae2012-02-20 19:54:16 +010040 cmd_line.extend(args)
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000041 p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
Antoine Pitrou9bc35682010-11-09 21:33:55 +000042 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
43 env=env)
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000044 try:
45 out, err = p.communicate()
46 finally:
47 subprocess._cleanup()
Brian Curtinc4ac8872010-11-01 14:00:33 +000048 p.stdout.close()
49 p.stderr.close()
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000050 rc = p.returncode
Eli Bendersky8f2c2bc2013-08-11 16:48:44 -070051 err = strip_python_stderr(err)
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000052 if (rc and expected_success) or (not rc and not expected_success):
53 raise AssertionError(
Gregory P. Smithb5684c42015-01-20 17:19:47 -080054 "Process return code is %d, command line was: %r, "
55 "stderr follows:\n%s" % (rc, cmd_line,
56 err.decode('ascii', 'ignore')))
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000057 return rc, out, err
58
Antoine Pitrou9bc35682010-11-09 21:33:55 +000059def assert_python_ok(*args, **env_vars):
60 """
61 Assert that running the interpreter with `args` and optional environment
Eli Bendersky8f2c2bc2013-08-11 16:48:44 -070062 variables `env_vars` succeeds (rc == 0) and return a (return code, stdout,
63 stderr) tuple.
Victor Stinnere8785ff2013-10-12 14:44:01 +020064
65 If the __cleanenv keyword is set, env_vars is used a fresh environment.
66
67 Python is started in isolated mode (command line option -I),
68 except if the __isolated keyword is set to False.
Antoine Pitrou9bc35682010-11-09 21:33:55 +000069 """
70 return _assert_python(True, *args, **env_vars)
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000071
Antoine Pitrou9bc35682010-11-09 21:33:55 +000072def assert_python_failure(*args, **env_vars):
73 """
74 Assert that running the interpreter with `args` and optional environment
Eli Bendersky8f2c2bc2013-08-11 16:48:44 -070075 variables `env_vars` fails (rc != 0) and return a (return code, stdout,
76 stderr) tuple.
Victor Stinnere8785ff2013-10-12 14:44:01 +020077
78 See assert_python_ok() for more options.
Antoine Pitrou9bc35682010-11-09 21:33:55 +000079 """
80 return _assert_python(False, *args, **env_vars)
Nick Coghlan260bd3e2009-11-16 06:49:25 +000081
Antoine Pitrou9845c7e2014-05-11 13:42:17 +020082def spawn_python(*args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, **kw):
Eli Bendersky8f2c2bc2013-08-11 16:48:44 -070083 """Run a Python subprocess with the given arguments.
84
85 kw is extra keyword args to pass to subprocess.Popen. Returns a Popen
86 object.
87 """
Nick Coghlan260bd3e2009-11-16 06:49:25 +000088 cmd_line = [sys.executable, '-E']
89 cmd_line.extend(args)
Antoine Pitrou6b4b8d02014-05-11 16:59:16 +020090 # Under Fedora (?), GNU readline can output junk on stderr when initialized,
91 # depending on the TERM setting. Setting TERM=vt100 is supposed to disable
92 # that. References:
93 # - http://reinout.vanrees.org/weblog/2009/08/14/readline-invisible-character-hack.html
94 # - http://stackoverflow.com/questions/15760712/python-readline-module-prints-escape-character-during-import
95 # - http://lists.gnu.org/archive/html/bug-readline/2007-08/msg00004.html
Antoine Pitrou5e6b5f22014-05-11 19:13:43 +020096 env = kw.setdefault('env', dict(os.environ))
97 env['TERM'] = 'vt100'
Nick Coghlan260bd3e2009-11-16 06:49:25 +000098 return subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
Antoine Pitrou9845c7e2014-05-11 13:42:17 +020099 stdout=stdout, stderr=stderr,
Victor Stinner024e37a2011-03-31 01:31:06 +0200100 **kw)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000101
102def kill_python(p):
Eli Bendersky8f2c2bc2013-08-11 16:48:44 -0700103 """Run the given Popen process until completion and return stdout."""
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000104 p.stdin.close()
105 data = p.stdout.read()
106 p.stdout.close()
107 # try to cleanup the child so we don't appear to leak when running
Antoine Pitrou4e7dc5f2009-12-08 19:27:24 +0000108 # with regrtest -R.
109 p.wait()
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000110 subprocess._cleanup()
111 return data
112
Nick Coghlan720c7e22013-12-15 20:33:02 +1000113def make_script(script_dir, script_basename, source, omit_suffix=False):
114 script_filename = script_basename
115 if not omit_suffix:
116 script_filename += os.extsep + 'py'
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000117 script_name = os.path.join(script_dir, script_filename)
Florent Xicluna8de42e22010-02-27 16:12:22 +0000118 # The script should be encoded to UTF-8, the default string encoding
119 script_file = open(script_name, 'w', encoding='utf-8')
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000120 script_file.write(source)
121 script_file.close()
Brett Cannonc8287ef2012-04-27 13:52:03 -0400122 importlib.invalidate_caches()
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000123 return script_name
124
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000125def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None):
126 zip_filename = zip_basename+os.extsep+'zip'
127 zip_name = os.path.join(zip_dir, zip_filename)
128 zip_file = zipfile.ZipFile(zip_name, 'w')
129 if name_in_zip is None:
Barry Warsaw28a691b2010-04-17 00:19:56 +0000130 parts = script_name.split(os.sep)
131 if len(parts) >= 2 and parts[-2] == '__pycache__':
132 legacy_pyc = make_legacy_pyc(source_from_cache(script_name))
133 name_in_zip = os.path.basename(legacy_pyc)
134 script_name = legacy_pyc
135 else:
136 name_in_zip = os.path.basename(script_name)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000137 zip_file.write(script_name, name_in_zip)
138 zip_file.close()
Florent Xicluna02ea12b22010-07-28 16:39:41 +0000139 #if test.support.verbose:
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000140 # 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, name_in_zip)
145
Nick Coghland26c18a2010-08-17 13:06:11 +0000146def make_pkg(pkg_dir, init_source=''):
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000147 os.mkdir(pkg_dir)
Nick Coghland26c18a2010-08-17 13:06:11 +0000148 make_script(pkg_dir, '__init__', init_source)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000149
150def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,
151 source, depth=1, compiled=False):
152 unlink = []
153 init_name = make_script(zip_dir, '__init__', '')
154 unlink.append(init_name)
155 init_basename = os.path.basename(init_name)
156 script_name = make_script(zip_dir, script_basename, source)
157 unlink.append(script_name)
158 if compiled:
Terry Jan Reedy5d828952014-06-20 17:49:10 -0400159 init_name = py_compile.compile(init_name, doraise=True)
160 script_name = py_compile.compile(script_name, doraise=True)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000161 unlink.extend((init_name, script_name))
162 pkg_names = [os.sep.join([pkg_name]*i) for i in range(1, depth+1)]
163 script_name_in_zip = os.path.join(pkg_names[-1], os.path.basename(script_name))
164 zip_filename = zip_basename+os.extsep+'zip'
165 zip_name = os.path.join(zip_dir, zip_filename)
166 zip_file = zipfile.ZipFile(zip_name, 'w')
167 for name in pkg_names:
168 init_name_in_zip = os.path.join(name, init_basename)
169 zip_file.write(init_name, init_name_in_zip)
170 zip_file.write(script_name, script_name_in_zip)
171 zip_file.close()
172 for name in unlink:
173 os.unlink(name)
Florent Xicluna02ea12b22010-07-28 16:39:41 +0000174 #if test.support.verbose:
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000175 # zip_file = zipfile.ZipFile(zip_name, 'r')
176 # print 'Contents of %r:' % zip_name
177 # zip_file.printdir()
178 # zip_file.close()
179 return zip_name, os.path.join(zip_name, script_name_in_zip)