blob: f40a676c7accafd1476c2130bba5e91442f29701 [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 Pitrouf51d8d32010-10-08 18:05:42 +000018def _assert_python(expected_success, *args):
Nick Coghlan260bd3e2009-11-16 06:49:25 +000019 cmd_line = [sys.executable, '-E']
20 cmd_line.extend(args)
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000021 p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
22 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
23 try:
24 out, err = p.communicate()
25 finally:
26 subprocess._cleanup()
27 rc = p.returncode
28 if (rc and expected_success) or (not rc and not expected_success):
29 raise AssertionError(
30 "Process return code is %d, "
31 "stderr follows:\n%s" % (rc, err.decode('ascii', 'ignore')))
32 return rc, out, err
33
34def assert_python_ok(*args):
35 return _assert_python(True, *args)
36
37def assert_python_failure(*args):
38 return _assert_python(False, *args)
Nick Coghlan260bd3e2009-11-16 06:49:25 +000039
40def spawn_python(*args):
41 cmd_line = [sys.executable, '-E']
42 cmd_line.extend(args)
43 return subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
44 stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
45
46def kill_python(p):
47 p.stdin.close()
48 data = p.stdout.read()
49 p.stdout.close()
50 # try to cleanup the child so we don't appear to leak when running
Antoine Pitrou4e7dc5f2009-12-08 19:27:24 +000051 # with regrtest -R.
52 p.wait()
Nick Coghlan260bd3e2009-11-16 06:49:25 +000053 subprocess._cleanup()
54 return data
55
Nick Coghlan260bd3e2009-11-16 06:49:25 +000056# Script creation utilities
57@contextlib.contextmanager
58def temp_dir():
59 dirname = tempfile.mkdtemp()
60 dirname = os.path.realpath(dirname)
61 try:
62 yield dirname
63 finally:
64 shutil.rmtree(dirname)
65
66def make_script(script_dir, script_basename, source):
67 script_filename = script_basename+os.extsep+'py'
68 script_name = os.path.join(script_dir, script_filename)
Florent Xicluna8de42e22010-02-27 16:12:22 +000069 # The script should be encoded to UTF-8, the default string encoding
70 script_file = open(script_name, 'w', encoding='utf-8')
Nick Coghlan260bd3e2009-11-16 06:49:25 +000071 script_file.write(source)
72 script_file.close()
73 return script_name
74
Nick Coghlan260bd3e2009-11-16 06:49:25 +000075def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None):
76 zip_filename = zip_basename+os.extsep+'zip'
77 zip_name = os.path.join(zip_dir, zip_filename)
78 zip_file = zipfile.ZipFile(zip_name, 'w')
79 if name_in_zip is None:
Barry Warsaw28a691b2010-04-17 00:19:56 +000080 parts = script_name.split(os.sep)
81 if len(parts) >= 2 and parts[-2] == '__pycache__':
82 legacy_pyc = make_legacy_pyc(source_from_cache(script_name))
83 name_in_zip = os.path.basename(legacy_pyc)
84 script_name = legacy_pyc
85 else:
86 name_in_zip = os.path.basename(script_name)
Nick Coghlan260bd3e2009-11-16 06:49:25 +000087 zip_file.write(script_name, name_in_zip)
88 zip_file.close()
Florent Xicluna02ea12b2010-07-28 16:39:41 +000089 #if test.support.verbose:
Nick Coghlan260bd3e2009-11-16 06:49:25 +000090 # zip_file = zipfile.ZipFile(zip_name, 'r')
91 # print 'Contents of %r:' % zip_name
92 # zip_file.printdir()
93 # zip_file.close()
94 return zip_name, os.path.join(zip_name, name_in_zip)
95
Nick Coghland26c18a2010-08-17 13:06:11 +000096def make_pkg(pkg_dir, init_source=''):
Nick Coghlan260bd3e2009-11-16 06:49:25 +000097 os.mkdir(pkg_dir)
Nick Coghland26c18a2010-08-17 13:06:11 +000098 make_script(pkg_dir, '__init__', init_source)
Nick Coghlan260bd3e2009-11-16 06:49:25 +000099
100def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,
101 source, depth=1, compiled=False):
102 unlink = []
103 init_name = make_script(zip_dir, '__init__', '')
104 unlink.append(init_name)
105 init_basename = os.path.basename(init_name)
106 script_name = make_script(zip_dir, script_basename, source)
107 unlink.append(script_name)
108 if compiled:
Barry Warsaw28a691b2010-04-17 00:19:56 +0000109 init_name = py_compile(init_name, doraise=True)
110 script_name = py_compile(script_name, doraise=True)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000111 unlink.extend((init_name, script_name))
112 pkg_names = [os.sep.join([pkg_name]*i) for i in range(1, depth+1)]
113 script_name_in_zip = os.path.join(pkg_names[-1], os.path.basename(script_name))
114 zip_filename = zip_basename+os.extsep+'zip'
115 zip_name = os.path.join(zip_dir, zip_filename)
116 zip_file = zipfile.ZipFile(zip_name, 'w')
117 for name in pkg_names:
118 init_name_in_zip = os.path.join(name, init_basename)
119 zip_file.write(init_name, init_name_in_zip)
120 zip_file.write(script_name, script_name_in_zip)
121 zip_file.close()
122 for name in unlink:
123 os.unlink(name)
Florent Xicluna02ea12b2010-07-28 16:39:41 +0000124 #if test.support.verbose:
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000125 # zip_file = zipfile.ZipFile(zip_name, 'r')
126 # print 'Contents of %r:' % zip_name
127 # zip_file.printdir()
128 # zip_file.close()
129 return zip_name, os.path.join(zip_name, script_name_in_zip)