1 | n/a | import builtins |
---|
2 | n/a | import locale |
---|
3 | n/a | import logging |
---|
4 | n/a | import os |
---|
5 | n/a | import shutil |
---|
6 | n/a | import sys |
---|
7 | n/a | import sysconfig |
---|
8 | n/a | import warnings |
---|
9 | n/a | from test import support |
---|
10 | n/a | try: |
---|
11 | n/a | import threading |
---|
12 | n/a | except ImportError: |
---|
13 | n/a | threading = None |
---|
14 | n/a | try: |
---|
15 | n/a | import _multiprocessing, multiprocessing.process |
---|
16 | n/a | except ImportError: |
---|
17 | n/a | multiprocessing = None |
---|
18 | n/a | |
---|
19 | n/a | |
---|
20 | n/a | # Unit tests are supposed to leave the execution environment unchanged |
---|
21 | n/a | # once they complete. But sometimes tests have bugs, especially when |
---|
22 | n/a | # tests fail, and the changes to environment go on to mess up other |
---|
23 | n/a | # tests. This can cause issues with buildbot stability, since tests |
---|
24 | n/a | # are run in random order and so problems may appear to come and go. |
---|
25 | n/a | # There are a few things we can save and restore to mitigate this, and |
---|
26 | n/a | # the following context manager handles this task. |
---|
27 | n/a | |
---|
28 | n/a | class saved_test_environment: |
---|
29 | n/a | """Save bits of the test environment and restore them at block exit. |
---|
30 | n/a | |
---|
31 | n/a | with saved_test_environment(testname, verbose, quiet): |
---|
32 | n/a | #stuff |
---|
33 | n/a | |
---|
34 | n/a | Unless quiet is True, a warning is printed to stderr if any of |
---|
35 | n/a | the saved items was changed by the test. The attribute 'changed' |
---|
36 | n/a | is initially False, but is set to True if a change is detected. |
---|
37 | n/a | |
---|
38 | n/a | If verbose is more than 1, the before and after state of changed |
---|
39 | n/a | items is also printed. |
---|
40 | n/a | """ |
---|
41 | n/a | |
---|
42 | n/a | changed = False |
---|
43 | n/a | |
---|
44 | n/a | def __init__(self, testname, verbose=0, quiet=False, *, pgo=False): |
---|
45 | n/a | self.testname = testname |
---|
46 | n/a | self.verbose = verbose |
---|
47 | n/a | self.quiet = quiet |
---|
48 | n/a | self.pgo = pgo |
---|
49 | n/a | |
---|
50 | n/a | # To add things to save and restore, add a name XXX to the resources list |
---|
51 | n/a | # and add corresponding get_XXX/restore_XXX functions. get_XXX should |
---|
52 | n/a | # return the value to be saved and compared against a second call to the |
---|
53 | n/a | # get function when test execution completes. restore_XXX should accept |
---|
54 | n/a | # the saved value and restore the resource using it. It will be called if |
---|
55 | n/a | # and only if a change in the value is detected. |
---|
56 | n/a | # |
---|
57 | n/a | # Note: XXX will have any '.' replaced with '_' characters when determining |
---|
58 | n/a | # the corresponding method names. |
---|
59 | n/a | |
---|
60 | n/a | resources = ('sys.argv', 'cwd', 'sys.stdin', 'sys.stdout', 'sys.stderr', |
---|
61 | n/a | 'os.environ', 'sys.path', 'sys.path_hooks', '__import__', |
---|
62 | n/a | 'warnings.filters', 'asyncore.socket_map', |
---|
63 | n/a | 'logging._handlers', 'logging._handlerList', 'sys.gettrace', |
---|
64 | n/a | 'sys.warnoptions', |
---|
65 | n/a | # multiprocessing.process._cleanup() may release ref |
---|
66 | n/a | # to a thread, so check processes first. |
---|
67 | n/a | 'multiprocessing.process._dangling', 'threading._dangling', |
---|
68 | n/a | 'sysconfig._CONFIG_VARS', 'sysconfig._INSTALL_SCHEMES', |
---|
69 | n/a | 'files', 'locale', 'warnings.showwarning', |
---|
70 | n/a | 'shutil_archive_formats', 'shutil_unpack_formats', |
---|
71 | n/a | ) |
---|
72 | n/a | |
---|
73 | n/a | def get_sys_argv(self): |
---|
74 | n/a | return id(sys.argv), sys.argv, sys.argv[:] |
---|
75 | n/a | def restore_sys_argv(self, saved_argv): |
---|
76 | n/a | sys.argv = saved_argv[1] |
---|
77 | n/a | sys.argv[:] = saved_argv[2] |
---|
78 | n/a | |
---|
79 | n/a | def get_cwd(self): |
---|
80 | n/a | return os.getcwd() |
---|
81 | n/a | def restore_cwd(self, saved_cwd): |
---|
82 | n/a | os.chdir(saved_cwd) |
---|
83 | n/a | |
---|
84 | n/a | def get_sys_stdout(self): |
---|
85 | n/a | return sys.stdout |
---|
86 | n/a | def restore_sys_stdout(self, saved_stdout): |
---|
87 | n/a | sys.stdout = saved_stdout |
---|
88 | n/a | |
---|
89 | n/a | def get_sys_stderr(self): |
---|
90 | n/a | return sys.stderr |
---|
91 | n/a | def restore_sys_stderr(self, saved_stderr): |
---|
92 | n/a | sys.stderr = saved_stderr |
---|
93 | n/a | |
---|
94 | n/a | def get_sys_stdin(self): |
---|
95 | n/a | return sys.stdin |
---|
96 | n/a | def restore_sys_stdin(self, saved_stdin): |
---|
97 | n/a | sys.stdin = saved_stdin |
---|
98 | n/a | |
---|
99 | n/a | def get_os_environ(self): |
---|
100 | n/a | return id(os.environ), os.environ, dict(os.environ) |
---|
101 | n/a | def restore_os_environ(self, saved_environ): |
---|
102 | n/a | os.environ = saved_environ[1] |
---|
103 | n/a | os.environ.clear() |
---|
104 | n/a | os.environ.update(saved_environ[2]) |
---|
105 | n/a | |
---|
106 | n/a | def get_sys_path(self): |
---|
107 | n/a | return id(sys.path), sys.path, sys.path[:] |
---|
108 | n/a | def restore_sys_path(self, saved_path): |
---|
109 | n/a | sys.path = saved_path[1] |
---|
110 | n/a | sys.path[:] = saved_path[2] |
---|
111 | n/a | |
---|
112 | n/a | def get_sys_path_hooks(self): |
---|
113 | n/a | return id(sys.path_hooks), sys.path_hooks, sys.path_hooks[:] |
---|
114 | n/a | def restore_sys_path_hooks(self, saved_hooks): |
---|
115 | n/a | sys.path_hooks = saved_hooks[1] |
---|
116 | n/a | sys.path_hooks[:] = saved_hooks[2] |
---|
117 | n/a | |
---|
118 | n/a | def get_sys_gettrace(self): |
---|
119 | n/a | return sys.gettrace() |
---|
120 | n/a | def restore_sys_gettrace(self, trace_fxn): |
---|
121 | n/a | sys.settrace(trace_fxn) |
---|
122 | n/a | |
---|
123 | n/a | def get___import__(self): |
---|
124 | n/a | return builtins.__import__ |
---|
125 | n/a | def restore___import__(self, import_): |
---|
126 | n/a | builtins.__import__ = import_ |
---|
127 | n/a | |
---|
128 | n/a | def get_warnings_filters(self): |
---|
129 | n/a | return id(warnings.filters), warnings.filters, warnings.filters[:] |
---|
130 | n/a | def restore_warnings_filters(self, saved_filters): |
---|
131 | n/a | warnings.filters = saved_filters[1] |
---|
132 | n/a | warnings.filters[:] = saved_filters[2] |
---|
133 | n/a | |
---|
134 | n/a | def get_asyncore_socket_map(self): |
---|
135 | n/a | asyncore = sys.modules.get('asyncore') |
---|
136 | n/a | # XXX Making a copy keeps objects alive until __exit__ gets called. |
---|
137 | n/a | return asyncore and asyncore.socket_map.copy() or {} |
---|
138 | n/a | def restore_asyncore_socket_map(self, saved_map): |
---|
139 | n/a | asyncore = sys.modules.get('asyncore') |
---|
140 | n/a | if asyncore is not None: |
---|
141 | n/a | asyncore.close_all(ignore_all=True) |
---|
142 | n/a | asyncore.socket_map.update(saved_map) |
---|
143 | n/a | |
---|
144 | n/a | def get_shutil_archive_formats(self): |
---|
145 | n/a | # we could call get_archives_formats() but that only returns the |
---|
146 | n/a | # registry keys; we want to check the values too (the functions that |
---|
147 | n/a | # are registered) |
---|
148 | n/a | return shutil._ARCHIVE_FORMATS, shutil._ARCHIVE_FORMATS.copy() |
---|
149 | n/a | def restore_shutil_archive_formats(self, saved): |
---|
150 | n/a | shutil._ARCHIVE_FORMATS = saved[0] |
---|
151 | n/a | shutil._ARCHIVE_FORMATS.clear() |
---|
152 | n/a | shutil._ARCHIVE_FORMATS.update(saved[1]) |
---|
153 | n/a | |
---|
154 | n/a | def get_shutil_unpack_formats(self): |
---|
155 | n/a | return shutil._UNPACK_FORMATS, shutil._UNPACK_FORMATS.copy() |
---|
156 | n/a | def restore_shutil_unpack_formats(self, saved): |
---|
157 | n/a | shutil._UNPACK_FORMATS = saved[0] |
---|
158 | n/a | shutil._UNPACK_FORMATS.clear() |
---|
159 | n/a | shutil._UNPACK_FORMATS.update(saved[1]) |
---|
160 | n/a | |
---|
161 | n/a | def get_logging__handlers(self): |
---|
162 | n/a | # _handlers is a WeakValueDictionary |
---|
163 | n/a | return id(logging._handlers), logging._handlers, logging._handlers.copy() |
---|
164 | n/a | def restore_logging__handlers(self, saved_handlers): |
---|
165 | n/a | # Can't easily revert the logging state |
---|
166 | n/a | pass |
---|
167 | n/a | |
---|
168 | n/a | def get_logging__handlerList(self): |
---|
169 | n/a | # _handlerList is a list of weakrefs to handlers |
---|
170 | n/a | return id(logging._handlerList), logging._handlerList, logging._handlerList[:] |
---|
171 | n/a | def restore_logging__handlerList(self, saved_handlerList): |
---|
172 | n/a | # Can't easily revert the logging state |
---|
173 | n/a | pass |
---|
174 | n/a | |
---|
175 | n/a | def get_sys_warnoptions(self): |
---|
176 | n/a | return id(sys.warnoptions), sys.warnoptions, sys.warnoptions[:] |
---|
177 | n/a | def restore_sys_warnoptions(self, saved_options): |
---|
178 | n/a | sys.warnoptions = saved_options[1] |
---|
179 | n/a | sys.warnoptions[:] = saved_options[2] |
---|
180 | n/a | |
---|
181 | n/a | # Controlling dangling references to Thread objects can make it easier |
---|
182 | n/a | # to track reference leaks. |
---|
183 | n/a | def get_threading__dangling(self): |
---|
184 | n/a | if not threading: |
---|
185 | n/a | return None |
---|
186 | n/a | # This copies the weakrefs without making any strong reference |
---|
187 | n/a | return threading._dangling.copy() |
---|
188 | n/a | def restore_threading__dangling(self, saved): |
---|
189 | n/a | if not threading: |
---|
190 | n/a | return |
---|
191 | n/a | threading._dangling.clear() |
---|
192 | n/a | threading._dangling.update(saved) |
---|
193 | n/a | |
---|
194 | n/a | # Same for Process objects |
---|
195 | n/a | def get_multiprocessing_process__dangling(self): |
---|
196 | n/a | if not multiprocessing: |
---|
197 | n/a | return None |
---|
198 | n/a | # Unjoined process objects can survive after process exits |
---|
199 | n/a | multiprocessing.process._cleanup() |
---|
200 | n/a | # This copies the weakrefs without making any strong reference |
---|
201 | n/a | return multiprocessing.process._dangling.copy() |
---|
202 | n/a | def restore_multiprocessing_process__dangling(self, saved): |
---|
203 | n/a | if not multiprocessing: |
---|
204 | n/a | return |
---|
205 | n/a | multiprocessing.process._dangling.clear() |
---|
206 | n/a | multiprocessing.process._dangling.update(saved) |
---|
207 | n/a | |
---|
208 | n/a | def get_sysconfig__CONFIG_VARS(self): |
---|
209 | n/a | # make sure the dict is initialized |
---|
210 | n/a | sysconfig.get_config_var('prefix') |
---|
211 | n/a | return (id(sysconfig._CONFIG_VARS), sysconfig._CONFIG_VARS, |
---|
212 | n/a | dict(sysconfig._CONFIG_VARS)) |
---|
213 | n/a | def restore_sysconfig__CONFIG_VARS(self, saved): |
---|
214 | n/a | sysconfig._CONFIG_VARS = saved[1] |
---|
215 | n/a | sysconfig._CONFIG_VARS.clear() |
---|
216 | n/a | sysconfig._CONFIG_VARS.update(saved[2]) |
---|
217 | n/a | |
---|
218 | n/a | def get_sysconfig__INSTALL_SCHEMES(self): |
---|
219 | n/a | return (id(sysconfig._INSTALL_SCHEMES), sysconfig._INSTALL_SCHEMES, |
---|
220 | n/a | sysconfig._INSTALL_SCHEMES.copy()) |
---|
221 | n/a | def restore_sysconfig__INSTALL_SCHEMES(self, saved): |
---|
222 | n/a | sysconfig._INSTALL_SCHEMES = saved[1] |
---|
223 | n/a | sysconfig._INSTALL_SCHEMES.clear() |
---|
224 | n/a | sysconfig._INSTALL_SCHEMES.update(saved[2]) |
---|
225 | n/a | |
---|
226 | n/a | def get_files(self): |
---|
227 | n/a | return sorted(fn + ('/' if os.path.isdir(fn) else '') |
---|
228 | n/a | for fn in os.listdir()) |
---|
229 | n/a | def restore_files(self, saved_value): |
---|
230 | n/a | fn = support.TESTFN |
---|
231 | n/a | if fn not in saved_value and (fn + '/') not in saved_value: |
---|
232 | n/a | if os.path.isfile(fn): |
---|
233 | n/a | support.unlink(fn) |
---|
234 | n/a | elif os.path.isdir(fn): |
---|
235 | n/a | support.rmtree(fn) |
---|
236 | n/a | |
---|
237 | n/a | _lc = [getattr(locale, lc) for lc in dir(locale) |
---|
238 | n/a | if lc.startswith('LC_')] |
---|
239 | n/a | def get_locale(self): |
---|
240 | n/a | pairings = [] |
---|
241 | n/a | for lc in self._lc: |
---|
242 | n/a | try: |
---|
243 | n/a | pairings.append((lc, locale.setlocale(lc, None))) |
---|
244 | n/a | except (TypeError, ValueError): |
---|
245 | n/a | continue |
---|
246 | n/a | return pairings |
---|
247 | n/a | def restore_locale(self, saved): |
---|
248 | n/a | for lc, setting in saved: |
---|
249 | n/a | locale.setlocale(lc, setting) |
---|
250 | n/a | |
---|
251 | n/a | def get_warnings_showwarning(self): |
---|
252 | n/a | return warnings.showwarning |
---|
253 | n/a | def restore_warnings_showwarning(self, fxn): |
---|
254 | n/a | warnings.showwarning = fxn |
---|
255 | n/a | |
---|
256 | n/a | def resource_info(self): |
---|
257 | n/a | for name in self.resources: |
---|
258 | n/a | method_suffix = name.replace('.', '_') |
---|
259 | n/a | get_name = 'get_' + method_suffix |
---|
260 | n/a | restore_name = 'restore_' + method_suffix |
---|
261 | n/a | yield name, getattr(self, get_name), getattr(self, restore_name) |
---|
262 | n/a | |
---|
263 | n/a | def __enter__(self): |
---|
264 | n/a | self.saved_values = dict((name, get()) for name, get, restore |
---|
265 | n/a | in self.resource_info()) |
---|
266 | n/a | return self |
---|
267 | n/a | |
---|
268 | n/a | def __exit__(self, exc_type, exc_val, exc_tb): |
---|
269 | n/a | saved_values = self.saved_values |
---|
270 | n/a | del self.saved_values |
---|
271 | n/a | support.gc_collect() # Some resources use weak references |
---|
272 | n/a | for name, get, restore in self.resource_info(): |
---|
273 | n/a | current = get() |
---|
274 | n/a | original = saved_values.pop(name) |
---|
275 | n/a | # Check for changes to the resource's value |
---|
276 | n/a | if current != original: |
---|
277 | n/a | self.changed = True |
---|
278 | n/a | restore(original) |
---|
279 | n/a | if not self.quiet and not self.pgo: |
---|
280 | n/a | print(f"Warning -- {name} was modified by {self.testname}", |
---|
281 | n/a | file=sys.stderr, flush=True) |
---|
282 | n/a | if self.verbose > 1: |
---|
283 | n/a | print(f" Before: {original}\n After: {current} ", |
---|
284 | n/a | file=sys.stderr, flush=True) |
---|
285 | n/a | return False |
---|