diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2016-02-11 11:12:19 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2016-02-11 11:12:19 (GMT) |
commit | 2feb64258558fc280f1d87bec211d83def48a9f0 (patch) | |
tree | d370a07dff4adfca5ba740cbdf603d7b6b31c9c1 /Lib/test/support | |
parent | 885bdc4946890f4bb80557fab80c3874b2cc4d39 (diff) | |
parent | 94a619d48b90aba5b5b42004e84b290bb68f0664 (diff) | |
download | cpython-2feb64258558fc280f1d87bec211d83def48a9f0.zip cpython-2feb64258558fc280f1d87bec211d83def48a9f0.tar.gz cpython-2feb64258558fc280f1d87bec211d83def48a9f0.tar.bz2 |
Issue #26325: Added test.support.check_no_resource_warning() to check that
no ResourceWarning is emitted.
Diffstat (limited to 'Lib/test/support')
-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. |