diff options
author | Brett Cannon <bcannon@gmail.com> | 2008-09-09 00:49:16 (GMT) |
---|---|---|
committer | Brett Cannon <bcannon@gmail.com> | 2008-09-09 00:49:16 (GMT) |
commit | 672237dc6ca1498eabac08554bcbc5bd0fd9ddaa (patch) | |
tree | 1847849ac1bcb31cc345ad691242d846350097d6 /Lib/warnings.py | |
parent | 631be01252d99e14ec8632ccb6ee898d11345d22 (diff) | |
download | cpython-672237dc6ca1498eabac08554bcbc5bd0fd9ddaa.zip cpython-672237dc6ca1498eabac08554bcbc5bd0fd9ddaa.tar.gz cpython-672237dc6ca1498eabac08554bcbc5bd0fd9ddaa.tar.bz2 |
warnings.catch_warnings() now returns a list or None instead of the custom
WarningsRecorder object. This makes the API simpler to use as no special object
must be learned.
Closes issue 3781.
Review by Benjamin Peterson.
Diffstat (limited to 'Lib/warnings.py')
-rw-r--r-- | Lib/warnings.py | 55 |
1 files changed, 21 insertions, 34 deletions
diff --git a/Lib/warnings.py b/Lib/warnings.py index 9ec04be..04e7b58 100644 --- a/Lib/warnings.py +++ b/Lib/warnings.py @@ -8,7 +8,7 @@ import sys import types __all__ = ["warn", "showwarning", "formatwarning", "filterwarnings", - "resetwarnings"] + "resetwarnings", "catch_warnings"] def warnpy3k(message, category=None, stacklevel=1): @@ -304,37 +304,20 @@ class WarningMessage(object): self.filename, self.lineno, self.line)) -class WarningsRecorder(list): - - """Record the result of various showwarning() calls.""" - - # Explicitly stated arguments so as to not trigger DeprecationWarning - # about adding 'line'. - def showwarning(self, *args, **kwargs): - self.append(WarningMessage(*args, **kwargs)) - - def __getattr__(self, attr): - """Return attributes from the last caught warning, or raise - AttributeError.""" - try: - return getattr(self[-1], attr) - except IndexError: - raise AttributeError("no recorded warning to read " - "{0!r} attribute from".format(attr)) - - - def reset(self): - del self[:] - - class catch_warnings(object): - """Guard the warnings filter from being permanently changed and optionally - record the details of any warnings that are issued. + """A context manager that copies and restores the warnings filter upon + exiting the context. + + The 'record' argument specifies whether warnings should be captured by a + custom implementation of warnings.showwarning() and be appended to a list + returned by the context manager. Otherwise None is returned by the context + manager. The objects appended to the list are arguments whose attributes + mirror the arguments to showwarning(). - Context manager returns an instance of warnings.WarningRecorder which is a - list of WarningMessage instances. Attributes on WarningRecorder are - redirected to the last created WarningMessage instance. + The 'module' argument is to specify an alternative module to the module + named 'warnings' and imported under that name. This argument is only useful + when testing the warnings module itself. """ @@ -346,17 +329,21 @@ class catch_warnings(object): keyword-only. """ - self._recorder = WarningsRecorder() if record else None + self._record = record self._module = sys.modules['warnings'] if module is None else module def __enter__(self): self._filters = self._module.filters self._module.filters = self._filters[:] self._showwarning = self._module.showwarning - if self._recorder is not None: - self._recorder.reset() # In case the instance is being reused. - self._module.showwarning = self._recorder.showwarning - return self._recorder + if self._record: + log = [] + def showwarning(*args, **kwargs): + log.append(WarningMessage(*args, **kwargs)) + self._module.showwarning = showwarning + return log + else: + return None def __exit__(self, *exc_info): self._module.filters = self._filters |