Debian Codegraph @

Lib/test/libregrtest/save_env.py

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