diff options
Diffstat (limited to 'Lib/test/support/__init__.py')
-rw-r--r-- | Lib/test/support/__init__.py | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index 58dcc8a..a2ef93d 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -102,7 +102,8 @@ __all__ = [ # threads "threading_setup", "threading_cleanup", "reap_threads", "start_threads", # miscellaneous - "check_warnings", "EnvironmentVarGuard", "run_with_locale", "swap_item", + "check_warnings", "check_no_resource_warning", "EnvironmentVarGuard", + "run_with_locale", "swap_item", "swap_attr", "Matcher", "set_memlimit", "SuppressCrashReport", "sortdict", "run_with_tz", ] @@ -1149,6 +1150,27 @@ def check_warnings(*filters, **kwargs): return _filterwarnings(filters, quiet) +@contextlib.contextmanager +def check_no_resource_warning(testcase): + """Context manager to check that no ResourceWarning is emitted. + + Usage: + + with check_no_resource_warning(self): + f = open(...) + ... + del f + + You must remove the object which may emit ResourceWarning before + the end of the context manager. + """ + with warnings.catch_warnings(record=True) as warns: + warnings.filterwarnings('always', category=ResourceWarning) + yield + gc_collect() + testcase.assertEqual(warns, []) + + class CleanImport(object): """Context manager to force import to return a new module reference. |