| Index: Lib/test/test_support.py |
| =================================================================== |
| --- Lib/test/test_support.py (revision 65979) |
| +++ Lib/test/test_support.py (working copy) |
| @@ -18,7 +18,7 @@ |
| "is_resource_enabled", "requires", "find_unused_port", "bind_port", |
| "fcmp", "have_unicode", "is_jython", "TESTFN", "HOST", "FUZZ", |
| "findfile", "verify", "vereq", "sortdict", "check_syntax_error", |
| - "open_urlresource", "WarningMessage", "catch_warning", "CleanImport", |
| + "open_urlresource", "catch_warning", "CleanImport", |
| "EnvironmentVarGuard", "captured_output", |
| "captured_stdout", "TransientResource", "transient_internet", |
| "run_with_locale", "set_memlimit", "bigmemtest", "bigaddrspacetest", |
| @@ -381,73 +381,10 @@ |
| return open(fn) |
| -class WarningMessage(object): |
| - "Holds the result of a single showwarning() call" |
| - _WARNING_DETAILS = "message category filename lineno line".split() |
| - def __init__(self, message, category, filename, lineno, line=None): |
| - for attr in self._WARNING_DETAILS: |
| - setattr(self, attr, locals()[attr]) |
| - self._category_name = category.__name__ if category else None |
| - |
| - def __str__(self): |
| - return ("{message : %r, category : %r, filename : %r, lineno : %s, " |
| - "line : %r}" % (self.message, self._category_name, |
| - self.filename, self.lineno, self.line)) |
| - |
| -class WarningRecorder(object): |
| - "Records the result of any showwarning calls" |
| - def __init__(self): |
| - self.warnings = [] |
| - self._set_last(None) |
| - |
| - def _showwarning(self, message, category, filename, lineno, |
| - file=None, line=None): |
| - wm = WarningMessage(message, category, filename, lineno, line) |
| - self.warnings.append(wm) |
| - self._set_last(wm) |
| - |
| - def _set_last(self, last_warning): |
| - if last_warning is None: |
| - for attr in WarningMessage._WARNING_DETAILS: |
| - setattr(self, attr, None) |
| - else: |
| - for attr in WarningMessage._WARNING_DETAILS: |
| - setattr(self, attr, getattr(last_warning, attr)) |
| - |
| - def reset(self): |
| - self.warnings = [] |
| - self._set_last(None) |
| - |
| - def __str__(self): |
| - return '[%s]' % (', '.join(map(str, self.warnings))) |
| - |
| -@contextlib.contextmanager |
| def catch_warning(module=warnings, record=True): |
| - """Guard the warnings filter from being permanently changed and |
| - optionally record the details of any warnings that are issued. |
| + return warnings.catch_warnings(record=record, module=module) |
|
Benjamin
2008/08/23 01:46:36
Why not just do "from warnings import catch_warnin
brett.cannon
2008/08/23 02:19:54
On 2008/08/23 01:46:36, Benjamin wrote:
> Why not
|
| - Use like this: |
| - with catch_warning() as w: |
| - warnings.warn("foo") |
| - assert str(w.message) == "foo" |
| - """ |
| - original_filters = module.filters |
| - original_showwarning = module.showwarning |
| - if record: |
| - recorder = WarningRecorder() |
| - module.showwarning = recorder._showwarning |
| - else: |
| - recorder = None |
| - try: |
| - # Replace the filters with a copy of the original |
| - module.filters = module.filters[:] |
| - yield recorder |
| - finally: |
| - module.showwarning = original_showwarning |
| - module.filters = original_filters |
| - |
| - |
| class CleanImport(object): |
| """Context manager to force import to return a new module reference. |