2015-09-26 05:38:01 -03:00
|
|
|
import builtins
|
|
|
|
import locale
|
|
|
|
import os
|
|
|
|
import sys
|
2017-09-07 13:56:24 -03:00
|
|
|
import threading
|
2015-09-26 05:38:01 -03:00
|
|
|
from test import support
|
2020-06-25 07:38:51 -03:00
|
|
|
from test.support import os_helper
|
2019-04-25 23:08:53 -03:00
|
|
|
from test.libregrtest.utils import print_warning
|
2021-03-22 19:52:13 -03:00
|
|
|
|
|
|
|
|
|
|
|
class SkipTestEnvironment(Exception):
|
|
|
|
pass
|
2015-09-26 05:38:01 -03:00
|
|
|
|
|
|
|
|
2016-03-25 15:13:06 -03:00
|
|
|
# Unit tests are supposed to leave the execution environment unchanged
|
|
|
|
# once they complete. But sometimes tests have bugs, especially when
|
|
|
|
# tests fail, and the changes to environment go on to mess up other
|
|
|
|
# tests. This can cause issues with buildbot stability, since tests
|
|
|
|
# are run in random order and so problems may appear to come and go.
|
|
|
|
# There are a few things we can save and restore to mitigate this, and
|
|
|
|
# the following context manager handles this task.
|
2016-03-25 13:36:33 -03:00
|
|
|
|
2016-03-25 15:13:06 -03:00
|
|
|
class saved_test_environment:
|
|
|
|
"""Save bits of the test environment and restore them at block exit.
|
2016-03-25 13:36:33 -03:00
|
|
|
|
2016-03-25 15:13:06 -03:00
|
|
|
with saved_test_environment(testname, verbose, quiet):
|
|
|
|
#stuff
|
2016-03-25 13:36:33 -03:00
|
|
|
|
2016-03-25 15:13:06 -03:00
|
|
|
Unless quiet is True, a warning is printed to stderr if any of
|
2021-03-22 19:52:13 -03:00
|
|
|
the saved items was changed by the test. The support.environment_altered
|
|
|
|
attribute is set to True if a change is detected.
|
2016-03-25 13:36:33 -03:00
|
|
|
|
2016-03-25 15:13:06 -03:00
|
|
|
If verbose is more than 1, the before and after state of changed
|
|
|
|
items is also printed.
|
|
|
|
"""
|
2016-03-25 13:36:33 -03:00
|
|
|
|
2016-03-25 15:13:06 -03:00
|
|
|
def __init__(self, testname, verbose=0, quiet=False, *, pgo=False):
|
|
|
|
self.testname = testname
|
|
|
|
self.verbose = verbose
|
|
|
|
self.quiet = quiet
|
|
|
|
self.pgo = pgo
|
2016-03-25 13:36:33 -03:00
|
|
|
|
2016-03-25 15:13:06 -03:00
|
|
|
# To add things to save and restore, add a name XXX to the resources list
|
|
|
|
# and add corresponding get_XXX/restore_XXX functions. get_XXX should
|
|
|
|
# return the value to be saved and compared against a second call to the
|
|
|
|
# get function when test execution completes. restore_XXX should accept
|
|
|
|
# the saved value and restore the resource using it. It will be called if
|
|
|
|
# and only if a change in the value is detected.
|
|
|
|
#
|
|
|
|
# Note: XXX will have any '.' replaced with '_' characters when determining
|
|
|
|
# the corresponding method names.
|
|
|
|
|
|
|
|
resources = ('sys.argv', 'cwd', 'sys.stdin', 'sys.stdout', 'sys.stderr',
|
|
|
|
'os.environ', 'sys.path', 'sys.path_hooks', '__import__',
|
2021-12-07 07:31:04 -04:00
|
|
|
'warnings.filters', 'asyncore.socket_map',
|
2016-03-25 15:13:06 -03:00
|
|
|
'logging._handlers', 'logging._handlerList', 'sys.gettrace',
|
|
|
|
'sys.warnoptions',
|
|
|
|
# multiprocessing.process._cleanup() may release ref
|
|
|
|
# to a thread, so check processes first.
|
|
|
|
'multiprocessing.process._dangling', 'threading._dangling',
|
|
|
|
'sysconfig._CONFIG_VARS', 'sysconfig._INSTALL_SCHEMES',
|
|
|
|
'files', 'locale', 'warnings.showwarning',
|
2016-03-29 08:34:06 -03:00
|
|
|
'shutil_archive_formats', 'shutil_unpack_formats',
|
2018-06-02 00:34:09 -03:00
|
|
|
'asyncio.events._event_loop_policy',
|
2019-07-02 09:50:19 -03:00
|
|
|
'urllib.requests._url_tempfiles', 'urllib.requests._opener',
|
2016-03-25 15:13:06 -03:00
|
|
|
)
|
|
|
|
|
2021-03-22 19:52:13 -03:00
|
|
|
def get_module(self, name):
|
|
|
|
# function for restore() methods
|
|
|
|
return sys.modules[name]
|
|
|
|
|
|
|
|
def try_get_module(self, name):
|
|
|
|
# function for get() methods
|
|
|
|
try:
|
|
|
|
return self.get_module(name)
|
|
|
|
except KeyError:
|
|
|
|
raise SkipTestEnvironment
|
|
|
|
|
2019-07-02 09:50:19 -03:00
|
|
|
def get_urllib_requests__url_tempfiles(self):
|
2021-03-22 19:52:13 -03:00
|
|
|
urllib_request = self.try_get_module('urllib.request')
|
|
|
|
return list(urllib_request._url_tempfiles)
|
2019-07-02 09:50:19 -03:00
|
|
|
def restore_urllib_requests__url_tempfiles(self, tempfiles):
|
|
|
|
for filename in tempfiles:
|
2020-08-08 08:05:24 -03:00
|
|
|
os_helper.unlink(filename)
|
2019-07-02 09:50:19 -03:00
|
|
|
|
|
|
|
def get_urllib_requests__opener(self):
|
2021-03-22 19:52:13 -03:00
|
|
|
urllib_request = self.try_get_module('urllib.request')
|
|
|
|
return urllib_request._opener
|
2019-07-02 09:50:19 -03:00
|
|
|
def restore_urllib_requests__opener(self, opener):
|
2021-03-22 19:52:13 -03:00
|
|
|
urllib_request = self.get_module('urllib.request')
|
|
|
|
urllib_request._opener = opener
|
2019-07-02 09:50:19 -03:00
|
|
|
|
2018-06-02 00:34:09 -03:00
|
|
|
def get_asyncio_events__event_loop_policy(self):
|
2021-03-22 19:52:13 -03:00
|
|
|
self.try_get_module('asyncio')
|
2018-06-02 00:34:09 -03:00
|
|
|
return support.maybe_get_event_loop_policy()
|
|
|
|
def restore_asyncio_events__event_loop_policy(self, policy):
|
2021-03-22 19:52:13 -03:00
|
|
|
asyncio = self.get_module('asyncio')
|
2018-06-02 00:34:09 -03:00
|
|
|
asyncio.set_event_loop_policy(policy)
|
|
|
|
|
2016-03-25 15:13:06 -03:00
|
|
|
def get_sys_argv(self):
|
|
|
|
return id(sys.argv), sys.argv, sys.argv[:]
|
|
|
|
def restore_sys_argv(self, saved_argv):
|
|
|
|
sys.argv = saved_argv[1]
|
|
|
|
sys.argv[:] = saved_argv[2]
|
|
|
|
|
|
|
|
def get_cwd(self):
|
2016-03-25 13:36:33 -03:00
|
|
|
return os.getcwd()
|
2016-03-25 15:13:06 -03:00
|
|
|
def restore_cwd(self, saved_cwd):
|
|
|
|
os.chdir(saved_cwd)
|
|
|
|
|
|
|
|
def get_sys_stdout(self):
|
|
|
|
return sys.stdout
|
|
|
|
def restore_sys_stdout(self, saved_stdout):
|
|
|
|
sys.stdout = saved_stdout
|
|
|
|
|
|
|
|
def get_sys_stderr(self):
|
|
|
|
return sys.stderr
|
|
|
|
def restore_sys_stderr(self, saved_stderr):
|
|
|
|
sys.stderr = saved_stderr
|
|
|
|
|
|
|
|
def get_sys_stdin(self):
|
|
|
|
return sys.stdin
|
|
|
|
def restore_sys_stdin(self, saved_stdin):
|
|
|
|
sys.stdin = saved_stdin
|
|
|
|
|
|
|
|
def get_os_environ(self):
|
|
|
|
return id(os.environ), os.environ, dict(os.environ)
|
|
|
|
def restore_os_environ(self, saved_environ):
|
|
|
|
os.environ = saved_environ[1]
|
|
|
|
os.environ.clear()
|
|
|
|
os.environ.update(saved_environ[2])
|
|
|
|
|
|
|
|
def get_sys_path(self):
|
|
|
|
return id(sys.path), sys.path, sys.path[:]
|
|
|
|
def restore_sys_path(self, saved_path):
|
|
|
|
sys.path = saved_path[1]
|
|
|
|
sys.path[:] = saved_path[2]
|
|
|
|
|
|
|
|
def get_sys_path_hooks(self):
|
|
|
|
return id(sys.path_hooks), sys.path_hooks, sys.path_hooks[:]
|
|
|
|
def restore_sys_path_hooks(self, saved_hooks):
|
|
|
|
sys.path_hooks = saved_hooks[1]
|
|
|
|
sys.path_hooks[:] = saved_hooks[2]
|
|
|
|
|
|
|
|
def get_sys_gettrace(self):
|
|
|
|
return sys.gettrace()
|
|
|
|
def restore_sys_gettrace(self, trace_fxn):
|
|
|
|
sys.settrace(trace_fxn)
|
2016-03-25 13:36:33 -03:00
|
|
|
|
2016-03-25 15:13:06 -03:00
|
|
|
def get___import__(self):
|
|
|
|
return builtins.__import__
|
|
|
|
def restore___import__(self, import_):
|
|
|
|
builtins.__import__ = import_
|
2016-03-25 13:36:33 -03:00
|
|
|
|
2016-03-25 15:13:06 -03:00
|
|
|
def get_warnings_filters(self):
|
2021-03-22 19:52:13 -03:00
|
|
|
warnings = self.try_get_module('warnings')
|
2016-03-25 15:13:06 -03:00
|
|
|
return id(warnings.filters), warnings.filters, warnings.filters[:]
|
|
|
|
def restore_warnings_filters(self, saved_filters):
|
2021-03-22 19:52:13 -03:00
|
|
|
warnings = self.get_module('warnings')
|
2016-03-25 15:13:06 -03:00
|
|
|
warnings.filters = saved_filters[1]
|
|
|
|
warnings.filters[:] = saved_filters[2]
|
2016-03-25 13:36:33 -03:00
|
|
|
|
2021-12-07 07:31:04 -04:00
|
|
|
def get_asyncore_socket_map(self):
|
|
|
|
asyncore = sys.modules.get('asyncore')
|
|
|
|
# XXX Making a copy keeps objects alive until __exit__ gets called.
|
|
|
|
return asyncore and asyncore.socket_map.copy() or {}
|
|
|
|
def restore_asyncore_socket_map(self, saved_map):
|
|
|
|
asyncore = sys.modules.get('asyncore')
|
|
|
|
if asyncore is not None:
|
|
|
|
asyncore.close_all(ignore_all=True)
|
|
|
|
asyncore.socket_map.update(saved_map)
|
|
|
|
|
2016-03-25 15:13:06 -03:00
|
|
|
def get_shutil_archive_formats(self):
|
2021-03-22 19:52:13 -03:00
|
|
|
shutil = self.try_get_module('shutil')
|
2016-03-25 15:13:06 -03:00
|
|
|
# we could call get_archives_formats() but that only returns the
|
|
|
|
# registry keys; we want to check the values too (the functions that
|
|
|
|
# are registered)
|
|
|
|
return shutil._ARCHIVE_FORMATS, shutil._ARCHIVE_FORMATS.copy()
|
|
|
|
def restore_shutil_archive_formats(self, saved):
|
2021-03-22 19:52:13 -03:00
|
|
|
shutil = self.get_module('shutil')
|
2016-03-25 15:13:06 -03:00
|
|
|
shutil._ARCHIVE_FORMATS = saved[0]
|
|
|
|
shutil._ARCHIVE_FORMATS.clear()
|
|
|
|
shutil._ARCHIVE_FORMATS.update(saved[1])
|
|
|
|
|
|
|
|
def get_shutil_unpack_formats(self):
|
2021-03-22 19:52:13 -03:00
|
|
|
shutil = self.try_get_module('shutil')
|
2016-03-25 15:13:06 -03:00
|
|
|
return shutil._UNPACK_FORMATS, shutil._UNPACK_FORMATS.copy()
|
|
|
|
def restore_shutil_unpack_formats(self, saved):
|
2021-03-22 19:52:13 -03:00
|
|
|
shutil = self.get_module('shutil')
|
2016-03-25 15:13:06 -03:00
|
|
|
shutil._UNPACK_FORMATS = saved[0]
|
|
|
|
shutil._UNPACK_FORMATS.clear()
|
|
|
|
shutil._UNPACK_FORMATS.update(saved[1])
|
|
|
|
|
|
|
|
def get_logging__handlers(self):
|
2021-03-22 19:52:13 -03:00
|
|
|
logging = self.try_get_module('logging')
|
2016-03-25 15:13:06 -03:00
|
|
|
# _handlers is a WeakValueDictionary
|
|
|
|
return id(logging._handlers), logging._handlers, logging._handlers.copy()
|
|
|
|
def restore_logging__handlers(self, saved_handlers):
|
2015-09-26 05:38:01 -03:00
|
|
|
# Can't easily revert the logging state
|
|
|
|
pass
|
|
|
|
|
2016-03-25 15:13:06 -03:00
|
|
|
def get_logging__handlerList(self):
|
2021-03-22 19:52:13 -03:00
|
|
|
logging = self.try_get_module('logging')
|
2016-03-25 15:13:06 -03:00
|
|
|
# _handlerList is a list of weakrefs to handlers
|
|
|
|
return id(logging._handlerList), logging._handlerList, logging._handlerList[:]
|
|
|
|
def restore_logging__handlerList(self, saved_handlerList):
|
2015-09-26 05:38:01 -03:00
|
|
|
# Can't easily revert the logging state
|
|
|
|
pass
|
|
|
|
|
2016-03-25 15:13:06 -03:00
|
|
|
def get_sys_warnoptions(self):
|
|
|
|
return id(sys.warnoptions), sys.warnoptions, sys.warnoptions[:]
|
|
|
|
def restore_sys_warnoptions(self, saved_options):
|
|
|
|
sys.warnoptions = saved_options[1]
|
|
|
|
sys.warnoptions[:] = saved_options[2]
|
2015-09-26 05:38:01 -03:00
|
|
|
|
|
|
|
# Controlling dangling references to Thread objects can make it easier
|
|
|
|
# to track reference leaks.
|
2016-03-25 15:13:06 -03:00
|
|
|
def get_threading__dangling(self):
|
2015-09-26 05:38:01 -03:00
|
|
|
# This copies the weakrefs without making any strong reference
|
|
|
|
return threading._dangling.copy()
|
2016-03-25 15:13:06 -03:00
|
|
|
def restore_threading__dangling(self, saved):
|
2015-09-26 05:38:01 -03:00
|
|
|
threading._dangling.clear()
|
2016-03-25 15:13:06 -03:00
|
|
|
threading._dangling.update(saved)
|
2016-03-25 13:36:33 -03:00
|
|
|
|
2016-03-25 15:13:06 -03:00
|
|
|
# Same for Process objects
|
|
|
|
def get_multiprocessing_process__dangling(self):
|
2021-03-22 19:52:13 -03:00
|
|
|
multiprocessing_process = self.try_get_module('multiprocessing.process')
|
2016-03-25 15:13:06 -03:00
|
|
|
# Unjoined process objects can survive after process exits
|
2021-03-22 19:52:13 -03:00
|
|
|
multiprocessing_process._cleanup()
|
2016-03-25 15:13:06 -03:00
|
|
|
# This copies the weakrefs without making any strong reference
|
2021-03-22 19:52:13 -03:00
|
|
|
return multiprocessing_process._dangling.copy()
|
2016-03-25 15:13:06 -03:00
|
|
|
def restore_multiprocessing_process__dangling(self, saved):
|
2021-03-22 19:52:13 -03:00
|
|
|
multiprocessing_process = self.get_module('multiprocessing.process')
|
|
|
|
multiprocessing_process._dangling.clear()
|
|
|
|
multiprocessing_process._dangling.update(saved)
|
2016-03-25 13:36:33 -03:00
|
|
|
|
2016-03-25 15:13:06 -03:00
|
|
|
def get_sysconfig__CONFIG_VARS(self):
|
2015-09-26 05:38:01 -03:00
|
|
|
# make sure the dict is initialized
|
2021-03-22 19:52:13 -03:00
|
|
|
sysconfig = self.try_get_module('sysconfig')
|
2015-09-26 05:38:01 -03:00
|
|
|
sysconfig.get_config_var('prefix')
|
2016-03-25 15:13:06 -03:00
|
|
|
return (id(sysconfig._CONFIG_VARS), sysconfig._CONFIG_VARS,
|
|
|
|
dict(sysconfig._CONFIG_VARS))
|
|
|
|
def restore_sysconfig__CONFIG_VARS(self, saved):
|
2021-03-22 19:52:13 -03:00
|
|
|
sysconfig = self.get_module('sysconfig')
|
2016-03-25 15:13:06 -03:00
|
|
|
sysconfig._CONFIG_VARS = saved[1]
|
|
|
|
sysconfig._CONFIG_VARS.clear()
|
|
|
|
sysconfig._CONFIG_VARS.update(saved[2])
|
|
|
|
|
|
|
|
def get_sysconfig__INSTALL_SCHEMES(self):
|
2021-03-22 19:52:13 -03:00
|
|
|
sysconfig = self.try_get_module('sysconfig')
|
2016-03-25 15:13:06 -03:00
|
|
|
return (id(sysconfig._INSTALL_SCHEMES), sysconfig._INSTALL_SCHEMES,
|
|
|
|
sysconfig._INSTALL_SCHEMES.copy())
|
|
|
|
def restore_sysconfig__INSTALL_SCHEMES(self, saved):
|
2021-03-22 19:52:13 -03:00
|
|
|
sysconfig = self.get_module('sysconfig')
|
2016-03-25 15:13:06 -03:00
|
|
|
sysconfig._INSTALL_SCHEMES = saved[1]
|
|
|
|
sysconfig._INSTALL_SCHEMES.clear()
|
|
|
|
sysconfig._INSTALL_SCHEMES.update(saved[2])
|
|
|
|
|
|
|
|
def get_files(self):
|
2015-09-26 05:38:01 -03:00
|
|
|
return sorted(fn + ('/' if os.path.isdir(fn) else '')
|
|
|
|
for fn in os.listdir())
|
2016-03-25 15:13:06 -03:00
|
|
|
def restore_files(self, saved_value):
|
2020-06-25 07:38:51 -03:00
|
|
|
fn = os_helper.TESTFN
|
2016-03-25 15:13:06 -03:00
|
|
|
if fn not in saved_value and (fn + '/') not in saved_value:
|
2015-09-26 05:38:01 -03:00
|
|
|
if os.path.isfile(fn):
|
2020-08-08 08:05:24 -03:00
|
|
|
os_helper.unlink(fn)
|
2015-09-26 05:38:01 -03:00
|
|
|
elif os.path.isdir(fn):
|
2020-08-08 08:05:24 -03:00
|
|
|
os_helper.rmtree(fn)
|
2015-09-26 05:38:01 -03:00
|
|
|
|
|
|
|
_lc = [getattr(locale, lc) for lc in dir(locale)
|
|
|
|
if lc.startswith('LC_')]
|
2016-03-25 15:13:06 -03:00
|
|
|
def get_locale(self):
|
2015-09-26 05:38:01 -03:00
|
|
|
pairings = []
|
|
|
|
for lc in self._lc:
|
|
|
|
try:
|
|
|
|
pairings.append((lc, locale.setlocale(lc, None)))
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
continue
|
|
|
|
return pairings
|
2016-03-25 15:13:06 -03:00
|
|
|
def restore_locale(self, saved):
|
|
|
|
for lc, setting in saved:
|
2015-09-26 05:38:01 -03:00
|
|
|
locale.setlocale(lc, setting)
|
|
|
|
|
2016-03-25 15:13:06 -03:00
|
|
|
def get_warnings_showwarning(self):
|
2021-03-22 19:52:13 -03:00
|
|
|
warnings = self.try_get_module('warnings')
|
2016-03-25 15:13:06 -03:00
|
|
|
return warnings.showwarning
|
|
|
|
def restore_warnings_showwarning(self, fxn):
|
2021-03-22 19:52:13 -03:00
|
|
|
warnings = self.get_module('warnings')
|
2016-03-25 15:13:06 -03:00
|
|
|
warnings.showwarning = fxn
|
2015-09-26 05:38:01 -03:00
|
|
|
|
2016-03-25 15:13:06 -03:00
|
|
|
def resource_info(self):
|
|
|
|
for name in self.resources:
|
|
|
|
method_suffix = name.replace('.', '_')
|
|
|
|
get_name = 'get_' + method_suffix
|
|
|
|
restore_name = 'restore_' + method_suffix
|
|
|
|
yield name, getattr(self, get_name), getattr(self, restore_name)
|
2015-09-26 05:38:01 -03:00
|
|
|
|
|
|
|
def __enter__(self):
|
2021-03-22 19:52:13 -03:00
|
|
|
self.saved_values = []
|
|
|
|
for name, get, restore in self.resource_info():
|
|
|
|
try:
|
|
|
|
original = get()
|
|
|
|
except SkipTestEnvironment:
|
|
|
|
continue
|
|
|
|
|
|
|
|
self.saved_values.append((name, get, restore, original))
|
2015-09-26 05:38:01 -03:00
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
2016-03-25 15:13:06 -03:00
|
|
|
saved_values = self.saved_values
|
2021-03-22 19:52:13 -03:00
|
|
|
self.saved_values = None
|
2017-06-30 05:59:52 -03:00
|
|
|
|
|
|
|
# Some resources use weak references
|
|
|
|
support.gc_collect()
|
|
|
|
|
2021-03-22 19:52:13 -03:00
|
|
|
for name, get, restore, original in saved_values:
|
2016-03-25 15:13:06 -03:00
|
|
|
current = get()
|
2015-09-26 05:38:01 -03:00
|
|
|
# Check for changes to the resource's value
|
2016-03-25 15:13:06 -03:00
|
|
|
if current != original:
|
2021-03-22 19:52:13 -03:00
|
|
|
support.environment_altered = True
|
2016-03-25 15:13:06 -03:00
|
|
|
restore(original)
|
|
|
|
if not self.quiet and not self.pgo:
|
2021-10-13 12:35:21 -03:00
|
|
|
print_warning(
|
|
|
|
f"{name} was modified by {self.testname}\n"
|
|
|
|
f" Before: {original}\n"
|
|
|
|
f" After: {current} ")
|
2015-09-26 05:38:01 -03:00
|
|
|
return False
|