summaryrefslogtreecommitdiffstats
path: root/Lib/warnings.py
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2008-10-16 23:24:44 (GMT)
committerBenjamin Peterson <benjamin@python.org>2008-10-16 23:24:44 (GMT)
commitfcf5d639f508b5a7ebf42d858390a6bd3bbb2c61 (patch)
tree193a1dd1d41e2403c61cd021409c28b843760cc6 /Lib/warnings.py
parentd31fdc547b1805516f6013af672e43cc66bd8c22 (diff)
downloadcpython-fcf5d639f508b5a7ebf42d858390a6bd3bbb2c61.zip
cpython-fcf5d639f508b5a7ebf42d858390a6bd3bbb2c61.tar.gz
cpython-fcf5d639f508b5a7ebf42d858390a6bd3bbb2c61.tar.bz2
forward port r66386
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 0e147db..b87d1be 100644
--- a/Lib/warnings.py
+++ b/Lib/warnings.py
@@ -301,8 +301,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
@@ -316,6 +329,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