blob: 14e113bd397f6d3136d092454e5066c83515646f [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
15from test.support import make_legacy_pyc
16
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')
Nick Coghlan260bd3e2009-11-16 06:49:25 +000022 cmd_line.extend(args)
Antoine Pitrouadffced2010-11-09 22:04:44 +000023 # Need to preserve the original environment, for in-place testing of
24 # shared library builds.
25 env = os.environ.copy()
26 env.update(env_vars)
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000027 p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
Antoine Pitrou9bc35682010-11-09 21:33:55 +000028 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
29 env=env)
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000030 try:
31 out, err = p.communicate()
32 finally:
33 subprocess._cleanup()
Brian Curtinc4ac8872010-11-01 14:00:33 +000034 p.stdout.close()
35 p.stderr.close()
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000036 rc = p.returncode
37 if (rc and expected_success) or (not rc and not expected_success):
38 raise AssertionError(
39 "Process return code is %d, "
40 "stderr follows:\n%s" % (rc, err.decode('ascii', 'ignore')))
41 return rc, out, err
42
Antoine Pitrou9bc35682010-11-09 21:33:55 +000043def assert_python_ok(*args, **env_vars):
44 """
45 Assert that running the interpreter with `args` and optional environment
46 variables `env_vars` is ok and return a (return code, stdout, stderr) tuple.
47 """
48 return _assert_python(True, *args, **env_vars)
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000049
Antoine Pitrou9bc35682010-11-09 21:33:55 +000050def assert_python_failure(*args, **env_vars):
51 """
52 Assert that running the interpreter with `args` and optional environment
53 variables `env_vars` fails and return a (return code, stdout, stderr) tuple.
54 """
55 return _assert_python(False, *args, **env_vars)
Nick Coghlan260bd3e2009-11-16 06:49:25 +000056
57def spawn_python(*args):
58 cmd_line = [sys.executable, '-E']
59 cmd_line.extend(args)
60 return subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
61 stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
62
63def kill_python(p):
64 p.stdin.close()
65 data = p.stdout.read()
66 p.stdout.close()
67 # try to cleanup the child so we don't appear to leak when running
Antoine Pitrou4e7dc5f2009-12-08 19:27:24 +000068 # with regrtest -R.
69 p.wait()
Nick Coghlan260bd3e2009-11-16 06:49:25 +000070 subprocess._cleanup()
71 return data
72
Nick Coghlan260bd3e2009-11-16 06:49:25 +000073# Script creation utilities
74@contextlib.contextmanager
75def temp_dir():
76 dirname = tempfile.mkdtemp()
77 dirname = os.path.realpath(dirname)
78 try:
79 yield dirname
80 finally:
81 shutil.rmtree(dirname)
82
83def make_script(script_dir, script_basename, source):
84 script_filename = script_basename+os.extsep+'py'
85 script_name = os.path.join(script_dir, script_filename)
Florent Xicluna8de42e22010-02-27 16:12:22 +000086 # The script should be encoded to UTF-8, the default string encoding
87 script_file = open(script_name, 'w', encoding='utf-8')
Nick Coghlan260bd3e2009-11-16 06:49:25 +000088 script_file.write(source)
89 script_file.close()
90 return script_name
91
Nick Coghlan260bd3e2009-11-16 06:49:25 +000092def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None):
93 zip_filename = zip_basename+os.extsep+'zip'
94 zip_name = os.path.join(zip_dir, zip_filename)
95 zip_file = zipfile.ZipFile(zip_name, 'w')
96 if name_in_zip is None:
Barry Warsaw28a691b2010-04-17 00:19:56 +000097 parts = script_name.split(os.sep)
98 if len(parts) >= 2 and parts[-2] == '__pycache__':
99 legacy_pyc = make_legacy_pyc(source_from_cache(script_name))
100 name_in_zip = os.path.basename(legacy_pyc)
101 script_name = legacy_pyc
102 else:
103 name_in_zip = os.path.basename(script_name)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000104 zip_file.write(script_name, name_in_zip)
105 zip_file.close()
Florent Xicluna02ea12b2010-07-28 16:39:41 +0000106 #if test.support.verbose:
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000107 # zip_file = zipfile.ZipFile(zip_name, 'r')
108 # print 'Contents of %r:' % zip_name
109 # zip_file.printdir()
110 # zip_file.close()
111 return zip_name, os.path.join(zip_name, name_in_zip)
112
Nick Coghland26c18a2010-08-17 13:06:11 +0000113def make_pkg(pkg_dir, init_source=''):
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000114 os.mkdir(pkg_dir)
Nick Coghland26c18a2010-08-17 13:06:11 +0000115 make_script(pkg_dir, '__init__', init_source)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000116
117def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,
118 source, depth=1, compiled=False):
119 unlink = []
120 init_name = make_script(zip_dir, '__init__', '')
121 unlink.append(init_name)
122 init_basename = os.path.basename(init_name)
123 script_name = make_script(zip_dir, script_basename, source)
124 unlink.append(script_name)
125 if compiled:
Barry Warsaw28a691b2010-04-17 00:19:56 +0000126 init_name = py_compile(init_name, doraise=True)
127 script_name = py_compile(script_name, doraise=True)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000128 unlink.extend((init_name, script_name))
129 pkg_names = [os.sep.join([pkg_name]*i) for i in range(1, depth+1)]
130 script_name_in_zip = os.path.join(pkg_names[-1], os.path.basename(script_name))
131 zip_filename = zip_basename+os.extsep+'zip'
132 zip_name = os.path.join(zip_dir, zip_filename)
133 zip_file = zipfile.ZipFile(zip_name, 'w')
134 for name in pkg_names:
135 init_name_in_zip = os.path.join(name, init_basename)
136 zip_file.write(init_name, init_name_in_zip)
137 zip_file.write(script_name, script_name_in_zip)
138 zip_file.close()
139 for name in unlink:
140 os.unlink(name)
Florent Xicluna02ea12b2010-07-28 16:39:41 +0000141 #if test.support.verbose:
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000142 # zip_file = zipfile.ZipFile(zip_name, 'r')
143 # print 'Contents of %r:' % zip_name
144 # zip_file.printdir()
145 # zip_file.close()
146 return zip_name, os.path.join(zip_name, script_name_in_zip)