blob: e556eca52acf40fccc9a68c875489f59f32e9697 [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
R. David Murray1b2bd3b2010-12-08 22:53:00 +00006import re
Nick Coghlan260bd3e2009-11-16 06:49:25 +00007import os.path
8import tempfile
9import subprocess
10import py_compile
11import contextlib
12import shutil
13import zipfile
14
Barry Warsaw28a691b2010-04-17 00:19:56 +000015from imp import source_from_cache
R. David Murray1b2bd3b2010-12-08 22:53:00 +000016from test.support import make_legacy_pyc, strip_python_stderr
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):
20 cmd_line = [sys.executable]
Antoine Pitrouadffced2010-11-09 22:04:44 +000021 if not env_vars:
Antoine Pitrou9bc35682010-11-09 21:33:55 +000022 cmd_line.append('-E')
Nick Coghlan260bd3e2009-11-16 06:49:25 +000023 cmd_line.extend(args)
Antoine Pitrouadffced2010-11-09 22:04:44 +000024 # Need to preserve the original environment, for in-place testing of
25 # shared library builds.
26 env = os.environ.copy()
27 env.update(env_vars)
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000028 p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
Antoine Pitrou9bc35682010-11-09 21:33:55 +000029 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
30 env=env)
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000031 try:
32 out, err = p.communicate()
33 finally:
34 subprocess._cleanup()
Brian Curtinc4ac8872010-11-01 14:00:33 +000035 p.stdout.close()
36 p.stderr.close()
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000037 rc = p.returncode
R. David Murray1b2bd3b2010-12-08 22:53:00 +000038 err = strip_python_stderr(err)
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000039 if (rc and expected_success) or (not rc and not expected_success):
40 raise AssertionError(
41 "Process return code is %d, "
42 "stderr follows:\n%s" % (rc, err.decode('ascii', 'ignore')))
43 return rc, out, err
44
Antoine Pitrou9bc35682010-11-09 21:33:55 +000045def assert_python_ok(*args, **env_vars):
46 """
47 Assert that running the interpreter with `args` and optional environment
48 variables `env_vars` is ok and return a (return code, stdout, stderr) tuple.
49 """
50 return _assert_python(True, *args, **env_vars)
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000051
Antoine Pitrou9bc35682010-11-09 21:33:55 +000052def assert_python_failure(*args, **env_vars):
53 """
54 Assert that running the interpreter with `args` and optional environment
55 variables `env_vars` fails and return a (return code, stdout, stderr) tuple.
56 """
57 return _assert_python(False, *args, **env_vars)
Nick Coghlan260bd3e2009-11-16 06:49:25 +000058
Victor Stinner024e37a2011-03-31 01:31:06 +020059def spawn_python(*args, **kw):
Nick Coghlan260bd3e2009-11-16 06:49:25 +000060 cmd_line = [sys.executable, '-E']
61 cmd_line.extend(args)
62 return subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
Victor Stinner024e37a2011-03-31 01:31:06 +020063 stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
64 **kw)
Nick Coghlan260bd3e2009-11-16 06:49:25 +000065
66def kill_python(p):
67 p.stdin.close()
68 data = p.stdout.read()
69 p.stdout.close()
70 # try to cleanup the child so we don't appear to leak when running
Antoine Pitrou4e7dc5f2009-12-08 19:27:24 +000071 # with regrtest -R.
72 p.wait()
Nick Coghlan260bd3e2009-11-16 06:49:25 +000073 subprocess._cleanup()
74 return data
75
Nick Coghlan260bd3e2009-11-16 06:49:25 +000076# Script creation utilities
77@contextlib.contextmanager
78def temp_dir():
79 dirname = tempfile.mkdtemp()
80 dirname = os.path.realpath(dirname)
81 try:
82 yield dirname
83 finally:
84 shutil.rmtree(dirname)
85
86def make_script(script_dir, script_basename, source):
87 script_filename = script_basename+os.extsep+'py'
88 script_name = os.path.join(script_dir, script_filename)
Florent Xicluna8de42e22010-02-27 16:12:22 +000089 # The script should be encoded to UTF-8, the default string encoding
90 script_file = open(script_name, 'w', encoding='utf-8')
Nick Coghlan260bd3e2009-11-16 06:49:25 +000091 script_file.write(source)
92 script_file.close()
93 return script_name
94
Nick Coghlan260bd3e2009-11-16 06:49:25 +000095def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None):
96 zip_filename = zip_basename+os.extsep+'zip'
97 zip_name = os.path.join(zip_dir, zip_filename)
98 zip_file = zipfile.ZipFile(zip_name, 'w')
99 if name_in_zip is None:
Barry Warsaw28a691b2010-04-17 00:19:56 +0000100 parts = script_name.split(os.sep)
101 if len(parts) >= 2 and parts[-2] == '__pycache__':
102 legacy_pyc = make_legacy_pyc(source_from_cache(script_name))
103 name_in_zip = os.path.basename(legacy_pyc)
104 script_name = legacy_pyc
105 else:
106 name_in_zip = os.path.basename(script_name)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000107 zip_file.write(script_name, name_in_zip)
108 zip_file.close()
Florent Xicluna02ea12b22010-07-28 16:39:41 +0000109 #if test.support.verbose:
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000110 # zip_file = zipfile.ZipFile(zip_name, 'r')
111 # print 'Contents of %r:' % zip_name
112 # zip_file.printdir()
113 # zip_file.close()
114 return zip_name, os.path.join(zip_name, name_in_zip)
115
Nick Coghland26c18a2010-08-17 13:06:11 +0000116def make_pkg(pkg_dir, init_source=''):
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000117 os.mkdir(pkg_dir)
Nick Coghland26c18a2010-08-17 13:06:11 +0000118 make_script(pkg_dir, '__init__', init_source)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000119
120def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,
121 source, depth=1, compiled=False):
122 unlink = []
123 init_name = make_script(zip_dir, '__init__', '')
124 unlink.append(init_name)
125 init_basename = os.path.basename(init_name)
126 script_name = make_script(zip_dir, script_basename, source)
127 unlink.append(script_name)
128 if compiled:
Barry Warsaw28a691b2010-04-17 00:19:56 +0000129 init_name = py_compile(init_name, doraise=True)
130 script_name = py_compile(script_name, doraise=True)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000131 unlink.extend((init_name, script_name))
132 pkg_names = [os.sep.join([pkg_name]*i) for i in range(1, depth+1)]
133 script_name_in_zip = os.path.join(pkg_names[-1], os.path.basename(script_name))
134 zip_filename = zip_basename+os.extsep+'zip'
135 zip_name = os.path.join(zip_dir, zip_filename)
136 zip_file = zipfile.ZipFile(zip_name, 'w')
137 for name in pkg_names:
138 init_name_in_zip = os.path.join(name, init_basename)
139 zip_file.write(init_name, init_name_in_zip)
140 zip_file.write(script_name, script_name_in_zip)
141 zip_file.close()
142 for name in unlink:
143 os.unlink(name)
Florent Xicluna02ea12b22010-07-28 16:39:41 +0000144 #if test.support.verbose:
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000145 # zip_file = zipfile.ZipFile(zip_name, 'r')
146 # print 'Contents of %r:' % zip_name
147 # zip_file.printdir()
148 # zip_file.close()
149 return zip_name, os.path.join(zip_name, script_name_in_zip)