diff options
Diffstat (limited to 'Lib/test/script_helper.py')
| -rw-r--r-- | Lib/test/script_helper.py | 69 |
1 files changed, 25 insertions, 44 deletions
diff --git a/Lib/test/script_helper.py b/Lib/test/script_helper.py index 7f7c70e..ba446cd 100644 --- a/Lib/test/script_helper.py +++ b/Lib/test/script_helper.py @@ -3,33 +3,31 @@ import sys import os -import re import os.path import tempfile import subprocess import py_compile import contextlib import shutil -try: - import zipfile -except ImportError: - # If Python is build without Unicode support, importing _io will - # fail, which, in turn, means that zipfile cannot be imported - # Most of this module can then still be used. - pass +import zipfile -from test.test_support import strip_python_stderr +from imp import source_from_cache +from test.support import make_legacy_pyc, strip_python_stderr # Executing the interpreter in a subprocess def _assert_python(expected_success, *args, **env_vars): cmd_line = [sys.executable] if not env_vars: cmd_line.append('-E') - cmd_line.extend(args) # Need to preserve the original environment, for in-place testing of # shared library builds. env = os.environ.copy() + # But a special flag that can be set to override -- in this case, the + # caller is responsible to pass the full environment. + if env_vars.pop('__cleanenv', None): + env = {} env.update(env_vars) + cmd_line.extend(args) p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) @@ -61,19 +59,11 @@ def assert_python_failure(*args, **env_vars): """ return _assert_python(False, *args, **env_vars) -def python_exit_code(*args): - cmd_line = [sys.executable, '-E'] - cmd_line.extend(args) - with open(os.devnull, 'w') as devnull: - return subprocess.call(cmd_line, stdout=devnull, - stderr=subprocess.STDOUT) - -def spawn_python(*args, **kwargs): +def spawn_python(*args): cmd_line = [sys.executable, '-E'] cmd_line.extend(args) return subprocess.Popen(cmd_line, stdin=subprocess.PIPE, - stdout=subprocess.PIPE, stderr=subprocess.STDOUT, - **kwargs) + stdout=subprocess.PIPE, stderr=subprocess.STDOUT) def kill_python(p): p.stdin.close() @@ -85,14 +75,6 @@ def kill_python(p): subprocess._cleanup() return data -def run_python(*args, **kwargs): - if __debug__: - p = spawn_python(*args, **kwargs) - else: - p = spawn_python('-O', *args, **kwargs) - stdout_data = kill_python(p) - return p.wait(), stdout_data - # Script creation utilities @contextlib.contextmanager def temp_dir(): @@ -106,37 +88,36 @@ def temp_dir(): def make_script(script_dir, script_basename, source): script_filename = script_basename+os.extsep+'py' script_name = os.path.join(script_dir, script_filename) - script_file = open(script_name, 'w') + # The script should be encoded to UTF-8, the default string encoding + script_file = open(script_name, 'w', encoding='utf-8') script_file.write(source) script_file.close() return script_name -def compile_script(script_name): - py_compile.compile(script_name, doraise=True) - if __debug__: - compiled_name = script_name + 'c' - else: - compiled_name = script_name + 'o' - return compiled_name - def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None): zip_filename = zip_basename+os.extsep+'zip' zip_name = os.path.join(zip_dir, zip_filename) zip_file = zipfile.ZipFile(zip_name, 'w') if name_in_zip is None: - name_in_zip = os.path.basename(script_name) + parts = script_name.split(os.sep) + if len(parts) >= 2 and parts[-2] == '__pycache__': + legacy_pyc = make_legacy_pyc(source_from_cache(script_name)) + name_in_zip = os.path.basename(legacy_pyc) + script_name = legacy_pyc + else: + name_in_zip = os.path.basename(script_name) zip_file.write(script_name, name_in_zip) zip_file.close() - #if test.test_support.verbose: + #if test.support.verbose: # zip_file = zipfile.ZipFile(zip_name, 'r') # print 'Contents of %r:' % zip_name # zip_file.printdir() # zip_file.close() return zip_name, os.path.join(zip_name, name_in_zip) -def make_pkg(pkg_dir): +def make_pkg(pkg_dir, init_source=''): os.mkdir(pkg_dir) - make_script(pkg_dir, '__init__', '') + make_script(pkg_dir, '__init__', init_source) def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename, source, depth=1, compiled=False): @@ -147,8 +128,8 @@ def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename, script_name = make_script(zip_dir, script_basename, source) unlink.append(script_name) if compiled: - init_name = compile_script(init_name) - script_name = compile_script(script_name) + init_name = py_compile(init_name, doraise=True) + script_name = py_compile(script_name, doraise=True) unlink.extend((init_name, script_name)) pkg_names = [os.sep.join([pkg_name]*i) for i in range(1, depth+1)] script_name_in_zip = os.path.join(pkg_names[-1], os.path.basename(script_name)) @@ -162,7 +143,7 @@ def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename, zip_file.close() for name in unlink: os.unlink(name) - #if test.test_support.verbose: + #if test.support.verbose: # zip_file = zipfile.ZipFile(zip_name, 'r') # print 'Contents of %r:' % zip_name # zip_file.printdir() |
