blob: 095895ee50d5b38f43aeac84f7eca06ce2cde6c6 [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]
20 if env_vars:
21 env = env_vars
22 else:
23 env = os.environ
24 cmd_line.append('-E')
Nick Coghlan260bd3e2009-11-16 06:49:25 +000025 cmd_line.extend(args)
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000026 p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
Antoine Pitrou9bc35682010-11-09 21:33:55 +000027 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
28 env=env)
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000029 try:
30 out, err = p.communicate()
31 finally:
32 subprocess._cleanup()
Brian Curtinc4ac8872010-11-01 14:00:33 +000033 p.stdout.close()
34 p.stderr.close()
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000035 rc = p.returncode
36 if (rc and expected_success) or (not rc and not expected_success):
37 raise AssertionError(
38 "Process return code is %d, "
39 "stderr follows:\n%s" % (rc, err.decode('ascii', 'ignore')))
40 return rc, out, err
41
Antoine Pitrou9bc35682010-11-09 21:33:55 +000042def assert_python_ok(*args, **env_vars):
43 """
44 Assert that running the interpreter with `args` and optional environment
45 variables `env_vars` is ok and return a (return code, stdout, stderr) tuple.
46 """
47 return _assert_python(True, *args, **env_vars)
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000048
Antoine Pitrou9bc35682010-11-09 21:33:55 +000049def assert_python_failure(*args, **env_vars):
50 """
51 Assert that running the interpreter with `args` and optional environment
52 variables `env_vars` fails and return a (return code, stdout, stderr) tuple.
53 """
54 return _assert_python(False, *args, **env_vars)
Nick Coghlan260bd3e2009-11-16 06:49:25 +000055
56def spawn_python(*args):
57 cmd_line = [sys.executable, '-E']
58 cmd_line.extend(args)
59 return subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
60 stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
61
62def kill_python(p):
63 p.stdin.close()
64 data = p.stdout.read()
65 p.stdout.close()
66 # try to cleanup the child so we don't appear to leak when running
Antoine Pitrou4e7dc5f2009-12-08 19:27:24 +000067 # with regrtest -R.
68 p.wait()
Nick Coghlan260bd3e2009-11-16 06:49:25 +000069 subprocess._cleanup()
70 return data
71
Nick Coghlan260bd3e2009-11-16 06:49:25 +000072# Script creation utilities
73@contextlib.contextmanager
74def temp_dir():
75 dirname = tempfile.mkdtemp()
76 dirname = os.path.realpath(dirname)
77 try:
78 yield dirname
79 finally:
80 shutil.rmtree(dirname)
81
82def make_script(script_dir, script_basename, source):
83 script_filename = script_basename+os.extsep+'py'
84 script_name = os.path.join(script_dir, script_filename)
Florent Xicluna8de42e22010-02-27 16:12:22 +000085 # The script should be encoded to UTF-8, the default string encoding
86 script_file = open(script_name, 'w', encoding='utf-8')
Nick Coghlan260bd3e2009-11-16 06:49:25 +000087 script_file.write(source)
88 script_file.close()
89 return script_name
90
Nick Coghlan260bd3e2009-11-16 06:49:25 +000091def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None):
92 zip_filename = zip_basename+os.extsep+'zip'
93 zip_name = os.path.join(zip_dir, zip_filename)
94 zip_file = zipfile.ZipFile(zip_name, 'w')
95 if name_in_zip is None:
Barry Warsaw28a691b2010-04-17 00:19:56 +000096 parts = script_name.split(os.sep)
97 if len(parts) >= 2 and parts[-2] == '__pycache__':
98 legacy_pyc = make_legacy_pyc(source_from_cache(script_name))
99 name_in_zip = os.path.basename(legacy_pyc)
100 script_name = legacy_pyc
101 else:
102 name_in_zip = os.path.basename(script_name)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000103 zip_file.write(script_name, name_in_zip)
104 zip_file.close()
Florent Xicluna02ea12b2010-07-28 16:39:41 +0000105 #if test.support.verbose:
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000106 # zip_file = zipfile.ZipFile(zip_name, 'r')
107 # print 'Contents of %r:' % zip_name
108 # zip_file.printdir()
109 # zip_file.close()
110 return zip_name, os.path.join(zip_name, name_in_zip)
111
Nick Coghland26c18a2010-08-17 13:06:11 +0000112def make_pkg(pkg_dir, init_source=''):
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000113 os.mkdir(pkg_dir)
Nick Coghland26c18a2010-08-17 13:06:11 +0000114 make_script(pkg_dir, '__init__', init_source)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000115
116def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,
117 source, depth=1, compiled=False):
118 unlink = []
119 init_name = make_script(zip_dir, '__init__', '')
120 unlink.append(init_name)
121 init_basename = os.path.basename(init_name)
122 script_name = make_script(zip_dir, script_basename, source)
123 unlink.append(script_name)
124 if compiled:
Barry Warsaw28a691b2010-04-17 00:19:56 +0000125 init_name = py_compile(init_name, doraise=True)
126 script_name = py_compile(script_name, doraise=True)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000127 unlink.extend((init_name, script_name))
128 pkg_names = [os.sep.join([pkg_name]*i) for i in range(1, depth+1)]
129 script_name_in_zip = os.path.join(pkg_names[-1], os.path.basename(script_name))
130 zip_filename = zip_basename+os.extsep+'zip'
131 zip_name = os.path.join(zip_dir, zip_filename)
132 zip_file = zipfile.ZipFile(zip_name, 'w')
133 for name in pkg_names:
134 init_name_in_zip = os.path.join(name, init_basename)
135 zip_file.write(init_name, init_name_in_zip)
136 zip_file.write(script_name, script_name_in_zip)
137 zip_file.close()
138 for name in unlink:
139 os.unlink(name)
Florent Xicluna02ea12b2010-07-28 16:39:41 +0000140 #if test.support.verbose:
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000141 # zip_file = zipfile.ZipFile(zip_name, 'r')
142 # print 'Contents of %r:' % zip_name
143 # zip_file.printdir()
144 # zip_file.close()
145 return zip_name, os.path.join(zip_name, script_name_in_zip)