summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_support.py
diff options
context:
space:
mode:
authorWalter Dörwald <walter@livinglogic.de>2007-04-03 18:33:29 (GMT)
committerWalter Dörwald <walter@livinglogic.de>2007-04-03 18:33:29 (GMT)
commite6dae6c655bf1e5542f8d3821e2710e9fc0b5762 (patch)
tree8065ac4c779d5d4e446af8fa45fe160658b73e62 /Lib/test/test_support.py
parentc4a106733cd464ec2d2746fd3e868f09734eeb10 (diff)
downloadcpython-e6dae6c655bf1e5542f8d3821e2710e9fc0b5762.zip
cpython-e6dae6c655bf1e5542f8d3821e2710e9fc0b5762.tar.gz
cpython-e6dae6c655bf1e5542f8d3821e2710e9fc0b5762.tar.bz2
Implement a contextmanager test.test_support.catch_warning that can
be used to catch the last warning issued by the warning framework. Change test_warnings.py and test_structmembers.py to use this new contextmanager.
Diffstat (limited to 'Lib/test/test_support.py')
-rw-r--r--Lib/test/test_support.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py
index b04a9a0..7e24395 100644
--- a/Lib/test/test_support.py
+++ b/Lib/test/test_support.py
@@ -282,6 +282,42 @@ def guard_warnings_filter():
finally:
warnings.filters = original_filters
+class WarningMessage(object):
+ "Holds the result of the latest showwarning() call"
+ def __init__(self):
+ self.message = None
+ self.category = None
+ self.filename = None
+ self.lineno = None
+
+ def _showwarning(self, message, category, filename, lineno, file=None):
+ self.message = message
+ self.category = category
+ self.filename = filename
+ self.lineno = lineno
+
+@contextlib.contextmanager
+def catch_warning():
+ """
+ Guard the warnings filter from being permanently changed and record the
+ data of the last warning that has been issued.
+
+ Use like this:
+
+ with catch_warning as w:
+ warnings.warn("foo")
+ assert str(w.message) == "foo"
+ """
+ warning = WarningMessage()
+ original_filters = warnings.filters[:]
+ original_showwarning = warnings.showwarning
+ warnings.showwarning = warning._showwarning
+ try:
+ yield warning
+ finally:
+ warnings.showwarning = original_showwarning
+ warnings.filters = original_filters
+
class EnvironmentVarGuard(object):
"""Class to help protect the environment variable properly. Can be used as