blob: 10ada6d0d1e63541a551bc71a4540299ff771cbd [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
4import sys
5import os
6import os.path
7import tempfile
8import subprocess
9import py_compile
10import contextlib
11import shutil
12import zipfile
13
Barry Warsaw28a691b2010-04-17 00:19:56 +000014from imp import source_from_cache
R. David Murray1b2bd3b2010-12-08 22:53:00 +000015from test.support import make_legacy_pyc, strip_python_stderr
Barry Warsaw28a691b2010-04-17 00:19:56 +000016
Nick Coghlan260bd3e2009-11-16 06:49:25 +000017# Executing the interpreter in a subprocess
Antoine Pitrou9bc35682010-11-09 21:33:55 +000018def _assert_python(expected_success, *args, **env_vars):
19 cmd_line = [sys.executable]
Antoine Pitrouadffced2010-11-09 22:04:44 +000020 if not env_vars:
Antoine Pitrou9bc35682010-11-09 21:33:55 +000021 cmd_line.append('-E')
Antoine Pitrouadffced2010-11-09 22:04:44 +000022 # Need to preserve the original environment, for in-place testing of
23 # shared library builds.
24 env = os.environ.copy()
Georg Brandl2daf6ae2012-02-20 19:54:16 +010025 # But a special flag that can be set to override -- in this case, the
26 # caller is responsible to pass the full environment.
27 if env_vars.pop('__cleanenv', None):
28 env = {}
Antoine Pitrouadffced2010-11-09 22:04:44 +000029 env.update(env_vars)
Georg Brandl2daf6ae2012-02-20 19:54:16 +010030 cmd_line.extend(args)
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000031 p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
Antoine Pitrou9bc35682010-11-09 21:33:55 +000032 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
33 env=env)
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000034 try:
35 out, err = p.communicate()
36 finally:
37 subprocess._cleanup()
Brian Curtinc4ac8872010-11-01 14:00:33 +000038 p.stdout.close()
39 p.stderr.close()
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000040 rc = p.returncode
R. David Murray1b2bd3b2010-12-08 22:53:00 +000041 err = strip_python_stderr(err)
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000042 if (rc and expected_success) or (not rc and not expected_success):
43 raise AssertionError(
44 "Process return code is %d, "
45 "stderr follows:\n%s" % (rc, err.decode('ascii', 'ignore')))
46 return rc, out, err
47
Antoine Pitrou9bc35682010-11-09 21:33:55 +000048def assert_python_ok(*args, **env_vars):
49 """
50 Assert that running the interpreter with `args` and optional environment
51 variables `env_vars` is ok and return a (return code, stdout, stderr) tuple.
52 """
53 return _assert_python(True, *args, **env_vars)
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000054
Antoine Pitrou9bc35682010-11-09 21:33:55 +000055def assert_python_failure(*args, **env_vars):
56 """
57 Assert that running the interpreter with `args` and optional environment
58 variables `env_vars` fails and return a (return code, stdout, stderr) tuple.
59 """
60 return _assert_python(False, *args, **env_vars)
Nick Coghlan260bd3e2009-11-16 06:49:25 +000061
Victor Stinner024e37a2011-03-31 01:31:06 +020062def spawn_python(*args, **kw):
Nick Coghlan260bd3e2009-11-16 06:49:25 +000063 cmd_line = [sys.executable, '-E']
64 cmd_line.extend(args)
65 return subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
Victor Stinner024e37a2011-03-31 01:31:06 +020066 stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
67 **kw)
Nick Coghlan260bd3e2009-11-16 06:49:25 +000068
69def kill_python(p):
70 p.stdin.close()
71 data = p.stdout.read()
72 p.stdout.close()
73 # try to cleanup the child so we don't appear to leak when running
Antoine Pitrou4e7dc5f2009-12-08 19:27:24 +000074 # with regrtest -R.
75 p.wait()
Nick Coghlan260bd3e2009-11-16 06:49:25 +000076 subprocess._cleanup()
77 return data
78
Nick Coghlan260bd3e2009-11-16 06:49:25 +000079# Script creation utilities
80@contextlib.contextmanager
81def temp_dir():
82 dirname = tempfile.mkdtemp()
83 dirname = os.path.realpath(dirname)
84 try:
85 yield dirname
86 finally:
87 shutil.rmtree(dirname)
88
89def make_script(script_dir, script_basename, source):
90 script_filename = script_basename+os.extsep+'py'
91 script_name = os.path.join(script_dir, script_filename)
Florent Xicluna8de42e22010-02-27 16:12:22 +000092 # The script should be encoded to UTF-8, the default string encoding
93 script_file = open(script_name, 'w', encoding='utf-8')
Nick Coghlan260bd3e2009-11-16 06:49:25 +000094 script_file.write(source)
95 script_file.close()
96 return script_name
97
Nick Coghlan260bd3e2009-11-16 06:49:25 +000098def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None):
99 zip_filename = zip_basename+os.extsep+'zip'
100 zip_name = os.path.join(zip_dir, zip_filename)
101 zip_file = zipfile.ZipFile(zip_name, 'w')
102 if name_in_zip is None:
Barry Warsaw28a691b2010-04-17 00:19:56 +0000103 parts = script_name.split(os.sep)
104 if len(parts) >= 2 and parts[-2] == '__pycache__':
105 legacy_pyc = make_legacy_pyc(source_from_cache(script_name))
106 name_in_zip = os.path.basename(legacy_pyc)
107 script_name = legacy_pyc
108 else:
109 name_in_zip = os.path.basename(script_name)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000110 zip_file.write(script_name, name_in_zip)
111 zip_file.close()
Florent Xicluna02ea12b22010-07-28 16:39:41 +0000112 #if test.support.verbose:
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000113 # zip_file = zipfile.ZipFile(zip_name, 'r')
114 # print 'Contents of %r:' % zip_name
115 # zip_file.printdir()
116 # zip_file.close()
117 return zip_name, os.path.join(zip_name, name_in_zip)
118
Nick Coghland26c18a2010-08-17 13:06:11 +0000119def make_pkg(pkg_dir, init_source=''):
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000120 os.mkdir(pkg_dir)
Nick Coghland26c18a2010-08-17 13:06:11 +0000121 make_script(pkg_dir, '__init__', init_source)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000122
123def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,
124 source, depth=1, compiled=False):
125 unlink = []
126 init_name = make_script(zip_dir, '__init__', '')
127 unlink.append(init_name)
128 init_basename = os.path.basename(init_name)
129 script_name = make_script(zip_dir, script_basename, source)
130 unlink.append(script_name)
131 if compiled:
Barry Warsaw28a691b2010-04-17 00:19:56 +0000132 init_name = py_compile(init_name, doraise=True)
133 script_name = py_compile(script_name, doraise=True)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000134 unlink.extend((init_name, script_name))
135 pkg_names = [os.sep.join([pkg_name]*i) for i in range(1, depth+1)]
136 script_name_in_zip = os.path.join(pkg_names[-1], os.path.basename(script_name))
137 zip_filename = zip_basename+os.extsep+'zip'
138 zip_name = os.path.join(zip_dir, zip_filename)
139 zip_file = zipfile.ZipFile(zip_name, 'w')
140 for name in pkg_names:
141 init_name_in_zip = os.path.join(name, init_basename)
142 zip_file.write(init_name, init_name_in_zip)
143 zip_file.write(script_name, script_name_in_zip)
144 zip_file.close()
145 for name in unlink:
146 os.unlink(name)
Florent Xicluna02ea12b22010-07-28 16:39:41 +0000147 #if test.support.verbose:
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000148 # zip_file = zipfile.ZipFile(zip_name, 'r')
149 # print 'Contents of %r:' % zip_name
150 # zip_file.printdir()
151 # zip_file.close()
152 return zip_name, os.path.join(zip_name, script_name_in_zip)