save_env.py 13 KB

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