annotate Lib/test/script_helper.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents 72b2e7b74307
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
66138
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1 # Common utility functions used by various script execution tests
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2 # e.g. test_cmd_line, test_cmd_line_script and test_runpy
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
3
76577
4182c0e6a389 Invalidate finder caches after creating a new script.
Brett Cannon <brett@python.org>
parents: 75097
diff changeset
4 import importlib
66138
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
5 import sys
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
6 import os
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
7 import os.path
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
8 import tempfile
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
9 import subprocess
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
10 import py_compile
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
11 import contextlib
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
12 import shutil
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
13 import zipfile
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
14
84154
bc18b5d4920e Issue #17177: Stop using imp in a bunch of tests
Brett Cannon <brett@python.org>
parents: 76577
diff changeset
15 from importlib.util import source_from_cache
84887
4f0034477ba9 Close #15415: Factor out temp dir helpers to test.support
Nick Coghlan <ncoghlan@gmail.com>
parents: 76577
diff changeset
16 from test.support import make_legacy_pyc, strip_python_stderr, temp_dir
66138
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
17
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
18 # Executing the interpreter in a subprocess
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
19 def _assert_python(expected_success, *args, **env_vars):
86222
72b2e7b74307 Close #18754: Run Python child processes in isolated more in the test suite.
Victor Stinner <victor.stinner@gmail.com>
parents: 85126
diff changeset
20 if '__isolated' in env_vars:
72b2e7b74307 Close #18754: Run Python child processes in isolated more in the test suite.
Victor Stinner <victor.stinner@gmail.com>
parents: 85126
diff changeset
21 isolated = env_vars.pop('__isolated')
72b2e7b74307 Close #18754: Run Python child processes in isolated more in the test suite.
Victor Stinner <victor.stinner@gmail.com>
parents: 85126
diff changeset
22 else:
72b2e7b74307 Close #18754: Run Python child processes in isolated more in the test suite.
Victor Stinner <victor.stinner@gmail.com>
parents: 85126
diff changeset
23 isolated = not env_vars
84344
c31bec42e411 Issue #17206: test.regrtest and test.script_helper enable faulthandler module
Victor Stinner <victor.stinner@gmail.com>
parents: 84154
diff changeset
24 cmd_line = [sys.executable, '-X', 'faulthandler']
86222
72b2e7b74307 Close #18754: Run Python child processes in isolated more in the test suite.
Victor Stinner <victor.stinner@gmail.com>
parents: 85126
diff changeset
25 if isolated:
72b2e7b74307 Close #18754: Run Python child processes in isolated more in the test suite.
Victor Stinner <victor.stinner@gmail.com>
parents: 85126
diff changeset
26 # isolated mode: ignore Python environment variables, ignore user
72b2e7b74307 Close #18754: Run Python child processes in isolated more in the test suite.
Victor Stinner <victor.stinner@gmail.com>
parents: 85126
diff changeset
27 # site-packages, and don't add the current directory to sys.path
72b2e7b74307 Close #18754: Run Python child processes in isolated more in the test suite.
Victor Stinner <victor.stinner@gmail.com>
parents: 85126
diff changeset
28 cmd_line.append('-I')
72b2e7b74307 Close #18754: Run Python child processes in isolated more in the test suite.
Victor Stinner <victor.stinner@gmail.com>
parents: 85126
diff changeset
29 elif not env_vars:
72b2e7b74307 Close #18754: Run Python child processes in isolated more in the test suite.
Victor Stinner <victor.stinner@gmail.com>
parents: 85126
diff changeset
30 # ignore Python environment variables
66138
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
31 cmd_line.append('-E')
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
32 # Need to preserve the original environment, for in-place testing of
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
33 # shared library builds.
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
34 env = os.environ.copy()
75066
f4b7ecf8a5f8 Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents: 67917
diff changeset
35 # But a special flag that can be set to override -- in this case, the
f4b7ecf8a5f8 Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents: 67917
diff changeset
36 # caller is responsible to pass the full environment.
f4b7ecf8a5f8 Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents: 67917
diff changeset
37 if env_vars.pop('__cleanenv', None):
f4b7ecf8a5f8 Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents: 67917
diff changeset
38 env = {}
66138
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
39 env.update(env_vars)
75066
f4b7ecf8a5f8 Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents: 67917
diff changeset
40 cmd_line.extend(args)
66138
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
41 p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
42 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
43 env=env)
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
44 try:
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
45 out, err = p.communicate()
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
46 finally:
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
47 subprocess._cleanup()
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
48 p.stdout.close()
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
49 p.stderr.close()
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
50 rc = p.returncode
85126
9e61563edb67 Add some docstrings, clarify others, and fix formatting.
Eli Bendersky <eliben@gmail.com>
parents: 84888
diff changeset
51 err = strip_python_stderr(err)
66138
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
52 if (rc and expected_success) or (not rc and not expected_success):
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
53 raise AssertionError(
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
54 "Process return code is %d, "
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
55 "stderr follows:\n%s" % (rc, err.decode('ascii', 'ignore')))
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
56 return rc, out, err
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
57
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
58 def assert_python_ok(*args, **env_vars):
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
59 """
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
60 Assert that running the interpreter with `args` and optional environment
85126
9e61563edb67 Add some docstrings, clarify others, and fix formatting.
Eli Bendersky <eliben@gmail.com>
parents: 84888
diff changeset
61 variables `env_vars` succeeds (rc == 0) and return a (return code, stdout,
9e61563edb67 Add some docstrings, clarify others, and fix formatting.
Eli Bendersky <eliben@gmail.com>
parents: 84888
diff changeset
62 stderr) tuple.
86222
72b2e7b74307 Close #18754: Run Python child processes in isolated more in the test suite.
Victor Stinner <victor.stinner@gmail.com>
parents: 85126
diff changeset
63
72b2e7b74307 Close #18754: Run Python child processes in isolated more in the test suite.
Victor Stinner <victor.stinner@gmail.com>
parents: 85126
diff changeset
64 If the __cleanenv keyword is set, env_vars is used a fresh environment.
72b2e7b74307 Close #18754: Run Python child processes in isolated more in the test suite.
Victor Stinner <victor.stinner@gmail.com>
parents: 85126
diff changeset
65
72b2e7b74307 Close #18754: Run Python child processes in isolated more in the test suite.
Victor Stinner <victor.stinner@gmail.com>
parents: 85126
diff changeset
66 Python is started in isolated mode (command line option -I),
72b2e7b74307 Close #18754: Run Python child processes in isolated more in the test suite.
Victor Stinner <victor.stinner@gmail.com>
parents: 85126
diff changeset
67 except if the __isolated keyword is set to False.
66138
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
68 """
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
69 return _assert_python(True, *args, **env_vars)
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
70
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
71 def assert_python_failure(*args, **env_vars):
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
72 """
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
73 Assert that running the interpreter with `args` and optional environment
85126
9e61563edb67 Add some docstrings, clarify others, and fix formatting.
Eli Bendersky <eliben@gmail.com>
parents: 84888
diff changeset
74 variables `env_vars` fails (rc != 0) and return a (return code, stdout,
9e61563edb67 Add some docstrings, clarify others, and fix formatting.
Eli Bendersky <eliben@gmail.com>
parents: 84888
diff changeset
75 stderr) tuple.
86222
72b2e7b74307 Close #18754: Run Python child processes in isolated more in the test suite.
Victor Stinner <victor.stinner@gmail.com>
parents: 85126
diff changeset
76
72b2e7b74307 Close #18754: Run Python child processes in isolated more in the test suite.
Victor Stinner <victor.stinner@gmail.com>
parents: 85126
diff changeset
77 See assert_python_ok() for more options.
66138
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
78 """
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
79 return _assert_python(False, *args, **env_vars)
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
80
69070
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
81 def spawn_python(*args, **kw):
85126
9e61563edb67 Add some docstrings, clarify others, and fix formatting.
Eli Bendersky <eliben@gmail.com>
parents: 84888
diff changeset
82 """Run a Python subprocess with the given arguments.
9e61563edb67 Add some docstrings, clarify others, and fix formatting.
Eli Bendersky <eliben@gmail.com>
parents: 84888
diff changeset
83
9e61563edb67 Add some docstrings, clarify others, and fix formatting.
Eli Bendersky <eliben@gmail.com>
parents: 84888
diff changeset
84 kw is extra keyword args to pass to subprocess.Popen. Returns a Popen
9e61563edb67 Add some docstrings, clarify others, and fix formatting.
Eli Bendersky <eliben@gmail.com>
parents: 84888
diff changeset
85 object.
9e61563edb67 Add some docstrings, clarify others, and fix formatting.
Eli Bendersky <eliben@gmail.com>
parents: 84888
diff changeset
86 """
66138
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
87 cmd_line = [sys.executable, '-E']
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
88 cmd_line.extend(args)
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
89 return subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
69070
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
90 stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
91 **kw)
66138
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
92
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
93 def kill_python(p):
85126
9e61563edb67 Add some docstrings, clarify others, and fix formatting.
Eli Bendersky <eliben@gmail.com>
parents: 84888
diff changeset
94 """Run the given Popen process until completion and return stdout."""
66138
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
95 p.stdin.close()
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
96 data = p.stdout.read()
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
97 p.stdout.close()
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
98 # try to cleanup the child so we don't appear to leak when running
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
99 # with regrtest -R.
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
100 p.wait()
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
101 subprocess._cleanup()
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
102 return data
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
103
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
104 def make_script(script_dir, script_basename, source):
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
105 script_filename = script_basename+os.extsep+'py'
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
106 script_name = os.path.join(script_dir, script_filename)
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
107 # The script should be encoded to UTF-8, the default string encoding
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
108 script_file = open(script_name, 'w', encoding='utf-8')
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
109 script_file.write(source)
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
110 script_file.close()
76577
4182c0e6a389 Invalidate finder caches after creating a new script.
Brett Cannon <brett@python.org>
parents: 75097
diff changeset
111 importlib.invalidate_caches()
66138
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
112 return script_name
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
113
57291
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
114 def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None):
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
115 zip_filename = zip_basename+os.extsep+'zip'
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
116 zip_name = os.path.join(zip_dir, zip_filename)
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
117 zip_file = zipfile.ZipFile(zip_name, 'w')
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
118 if name_in_zip is None:
60648
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59186
diff changeset
119 parts = script_name.split(os.sep)
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59186
diff changeset
120 if len(parts) >= 2 and parts[-2] == '__pycache__':
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59186
diff changeset
121 legacy_pyc = make_legacy_pyc(source_from_cache(script_name))
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59186
diff changeset
122 name_in_zip = os.path.basename(legacy_pyc)
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59186
diff changeset
123 script_name = legacy_pyc
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59186
diff changeset
124 else:
7b69e630d237 PEP 3147
Barry Warsaw <barry@python.org>
parents: 59186
diff changeset
125 name_in_zip = os.path.basename(script_name)
57291
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
126 zip_file.write(script_name, name_in_zip)
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
127 zip_file.close()
63273
aa19a7cc1bf3 Syntax cleanup.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 60648
diff changeset
128 #if test.support.verbose:
57291
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
129 # zip_file = zipfile.ZipFile(zip_name, 'r')
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
130 # print 'Contents of %r:' % zip_name
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
131 # zip_file.printdir()
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
132 # zip_file.close()
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
133 return zip_name, os.path.join(zip_name, name_in_zip)
fd417cd64341 Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
134
66138
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
135 def make_pkg(pkg_dir, init_source=''):
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
136 os.mkdir(pkg_dir)
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
137 make_script(pkg_dir, '__init__', init_source)
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
138
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
139 def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
140 source, depth=1, compiled=False):
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
141 unlink = []
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
142 init_name = make_script(zip_dir, '__init__', '')
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
143 unlink.append(init_name)
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
144 init_basename = os.path.basename(init_name)
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
145 script_name = make_script(zip_dir, script_basename, source)
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
146 unlink.append(script_name)
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
147 if compiled:
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
148 init_name = py_compile(init_name, doraise=True)
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
149 script_name = py_compile(script_name, doraise=True)
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
150 unlink.extend((init_name, script_name))
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
151 pkg_names = [os.sep.join([pkg_name]*i) for i in range(1, depth+1)]
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
152 script_name_in_zip = os.path.join(pkg_names[-1], os.path.basename(script_name))
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
153 zip_filename = zip_basename+os.extsep+'zip'
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
154 zip_name = os.path.join(zip_dir, zip_filename)
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
155 zip_file = zipfile.ZipFile(zip_name, 'w')
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
156 for name in pkg_names:
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
157 init_name_in_zip = os.path.join(name, init_basename)
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
158 zip_file.write(init_name, init_name_in_zip)
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
159 zip_file.write(script_name, script_name_in_zip)
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
160 zip_file.close()
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
161 for name in unlink:
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
162 os.unlink(name)
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
163 #if test.support.verbose:
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
164 # zip_file = zipfile.ZipFile(zip_name, 'r')
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
165 # print 'Contents of %r:' % zip_name
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
166 # zip_file.printdir()
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
167 # zip_file.close()
dea4f7641940 I'm only backporting the tests (which run fine), as well as
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
168 return zip_name, os.path.join(zip_name, script_name_in_zip)