1 # Common utility functions used by various script execution tests
2 # e.g. test_cmd_line, test_cmd_line_script and test_runpy
14 # Executing the interpreter in a subprocess
15 def _assert_python(expected_success, *args, **env_vars):
16 cmd_line = [sys.executable]
20 # Need to preserve the original environment, for in-place testing of
21 # shared library builds.
22 env = os.environ.copy()
24 p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
25 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
28 out, err = p.communicate()
34 if (rc and expected_success) or (not rc and not expected_success):
36 "Process return code is %d, "
37 "stderr follows:\n%s" % (rc, err.decode('ascii', 'ignore')))
40 def assert_python_ok(*args, **env_vars):
42 Assert that running the interpreter with `args` and optional environment
43 variables `env_vars` is ok and return a (return code, stdout, stderr) tuple.
45 return _assert_python(True, *args, **env_vars)
47 def assert_python_failure(*args, **env_vars):
49 Assert that running the interpreter with `args` and optional environment
50 variables `env_vars` fails and return a (return code, stdout, stderr) tuple.
52 return _assert_python(False, *args, **env_vars)
54 def python_exit_code(*args):
55 cmd_line = [sys.executable, '-E']
57 with open(os.devnull, 'w') as devnull:
58 return subprocess.call(cmd_line, stdout=devnull,
59 stderr=subprocess.STDOUT)
61 def spawn_python(*args, **kwargs):
62 cmd_line = [sys.executable, '-E']
64 return subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
65 stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
70 data = p.stdout.read()
72 # try to cleanup the child so we don't appear to leak when running
78 def run_python(*args, **kwargs):
80 p = spawn_python(*args, **kwargs)
82 p = spawn_python('-O', *args, **kwargs)
83 stdout_data = kill_python(p)
84 return p.wait(), stdout_data
86 # Script creation utilities
87 @contextlib.contextmanager
89 dirname = tempfile.mkdtemp()
90 dirname = os.path.realpath(dirname)
94 shutil.rmtree(dirname)
96 def make_script(script_dir, script_basename, source):
97 script_filename = script_basename+os.extsep+'py'
98 script_name = os.path.join(script_dir, script_filename)
99 script_file = open(script_name, 'w')
100 script_file.write(source)
104 def compile_script(script_name):
105 py_compile.compile(script_name, doraise=True)
107 compiled_name = script_name + 'c'
109 compiled_name = script_name + 'o'
112 def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None):
113 zip_filename = zip_basename+os.extsep+'zip'
114 zip_name = os.path.join(zip_dir, zip_filename)
115 zip_file = zipfile.ZipFile(zip_name, 'w')
116 if name_in_zip is None:
117 name_in_zip = os.path.basename(script_name)
118 zip_file.write(script_name, name_in_zip)
120 #if test.test_support.verbose:
121 # zip_file = zipfile.ZipFile(zip_name, 'r')
122 # print 'Contents of %r:' % zip_name
123 # zip_file.printdir()
125 return zip_name, os.path.join(zip_name, name_in_zip)
127 def make_pkg(pkg_dir):
129 make_script(pkg_dir, '__init__', '')
131 def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,
132 source, depth=1, compiled=False):
134 init_name = make_script(zip_dir, '__init__', '')
135 unlink.append(init_name)
136 init_basename = os.path.basename(init_name)
137 script_name = make_script(zip_dir, script_basename, source)
138 unlink.append(script_name)
140 init_name = compile_script(init_name)
141 script_name = compile_script(script_name)
142 unlink.extend((init_name, script_name))
143 pkg_names = [os.sep.join([pkg_name]*i) for i in range(1, depth+1)]
144 script_name_in_zip = os.path.join(pkg_names[-1], os.path.basename(script_name))
145 zip_filename = zip_basename+os.extsep+'zip'
146 zip_name = os.path.join(zip_dir, zip_filename)
147 zip_file = zipfile.ZipFile(zip_name, 'w')
148 for name in pkg_names:
149 init_name_in_zip = os.path.join(name, init_basename)
150 zip_file.write(init_name, init_name_in_zip)
151 zip_file.write(script_name, script_name_in_zip)
155 #if test.test_support.verbose:
156 # zip_file = zipfile.ZipFile(zip_name, 'r')
157 # print 'Contents of %r:' % zip_name
158 # zip_file.printdir()
160 return zip_name, os.path.join(zip_name, script_name_in_zip)