diff options
author | Brett Cannon <bcannon@gmail.com> | 2008-05-08 19:50:51 (GMT) |
---|---|---|
committer | Brett Cannon <bcannon@gmail.com> | 2008-05-08 19:50:51 (GMT) |
commit | bfb997d6a5a7fa79a67d63355390337d4e796b6c (patch) | |
tree | e1dfb78f5cc6679dd00a96d605198055a07f1de4 /Lib/test/test_support.py | |
parent | 9d4418242757f616ca7041409c7015e166b2c9f9 (diff) | |
download | cpython-bfb997d6a5a7fa79a67d63355390337d4e796b6c.zip cpython-bfb997d6a5a7fa79a67d63355390337d4e796b6c.tar.gz cpython-bfb997d6a5a7fa79a67d63355390337d4e796b6c.tar.bz2 |
Make test.test_support.catch_warning() take an argument specifying if any
triggered warnings should be captured. This allows the context manager to be
used to just prevent the internal state of the 'warnings' framework and thus
allow triggered warnings to be displayed.
Diffstat (limited to 'Lib/test/test_support.py')
-rw-r--r-- | Lib/test/test_support.py | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py index fe5cf0f..25494a9 100644 --- a/Lib/test/test_support.py +++ b/Lib/test/test_support.py @@ -40,7 +40,7 @@ class ResourceDenied(TestSkipped): def import_module(name, deprecated=False): """Import the module to be tested, raising TestSkipped if it is not available.""" - with catch_warning(): + with catch_warning(record=False): if deprecated: warnings.filterwarnings("ignore", ".+ module", DeprecationWarning) try: @@ -395,7 +395,7 @@ class WarningMessage(object): @contextlib.contextmanager -def catch_warning(module=warnings): +def catch_warning(module=warnings, record=True): """ Guard the warnings filter from being permanently changed and record the data of the last warning that has been issued. @@ -406,12 +406,13 @@ def catch_warning(module=warnings): warnings.warn("foo") assert str(w.message) == "foo" """ - warning_obj = WarningMessage() + if record: + warning_obj = WarningMessage() + module.showwarning = warning_obj._showwarning original_filters = module.filters[:] original_showwarning = module.showwarning - module.showwarning = warning_obj._showwarning try: - yield warning_obj + yield warning_obj if record else None finally: module.showwarning = original_showwarning module.filters = original_filters |