summaryrefslogtreecommitdiff
path: root/Lib/warnings.py
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2008-09-11 12:11:06 +0000
committerNick Coghlan <ncoghlan@gmail.com>2008-09-11 12:11:06 +0000
commitd2e09383624944add0e670b857c572129d56988e (patch)
treef77e6631636c1d33d9bd5cf1e3e905c571ac9cd9 /Lib/warnings.py
parent9fa5a2828c8007dfb678b883d65aecac93e995e4 (diff)
downloadcpython-git-d2e09383624944add0e670b857c572129d56988e.tar.gz
Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Diffstat (limited to 'Lib/warnings.py')
-rw-r--r--Lib/warnings.py15
1 files changed, 15 insertions, 0 deletions
diff --git a/Lib/warnings.py b/Lib/warnings.py
index 04e7b5878c..59011caa46 100644
--- a/Lib/warnings.py
+++ b/Lib/warnings.py
@@ -331,8 +331,21 @@ class catch_warnings(object):
"""
self._record = record
self._module = sys.modules['warnings'] if module is None else module
+ self._entered = False
+
+ def __repr__(self):
+ args = []
+ if self._record:
+ args.append("record=True")
+ if self._module is not sys.modules['warnings']:
+ args.append("module=%r" % self._module)
+ name = type(self).__name__
+ return "%s(%s)" % (name, ", ".join(args))
def __enter__(self):
+ if self._entered:
+ raise RuntimeError("Cannot enter %r twice" % self)
+ self._entered = True
self._filters = self._module.filters
self._module.filters = self._filters[:]
self._showwarning = self._module.showwarning
@@ -346,6 +359,8 @@ class catch_warnings(object):
return None
def __exit__(self, *exc_info):
+ if not self._entered:
+ raise RuntimeError("Cannot exit %r without entering first" % self)
self._module.filters = self._filters
self._module.showwarning = self._showwarning