diff options
author | Brett Cannon <bcannon@gmail.com> | 2008-04-12 23:44:07 (GMT) |
---|---|---|
committer | Brett Cannon <bcannon@gmail.com> | 2008-04-12 23:44:07 (GMT) |
commit | e9746890388178bb1e4cdad3c0586bf1862c3727 (patch) | |
tree | 77e228e19ac5673aac13dac8292281fc9a3010ab /Lib/test/test_support.py | |
parent | e6c03033afc58804cfdb143bef67e9cd37e25507 (diff) | |
download | cpython-e9746890388178bb1e4cdad3c0586bf1862c3727.zip cpython-e9746890388178bb1e4cdad3c0586bf1862c3727.tar.gz cpython-e9746890388178bb1e4cdad3c0586bf1862c3727.tar.bz2 |
Re-implement the 'warnings' module in C. This allows for usage of the
'warnings' code in places where it was previously not possible (e.g., the
parser). It could also potentially lead to a speed-up in interpreter start-up
if the C version of the code (_warnings) is imported over the use of the
Python version in key places.
Closes issue #1631171.
Diffstat (limited to 'Lib/test/test_support.py')
-rw-r--r-- | Lib/test/test_support.py | 30 |
1 files changed, 21 insertions, 9 deletions
diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py index 2a9474e..d2d530f 100644 --- a/Lib/test/test_support.py +++ b/Lib/test/test_support.py @@ -363,14 +363,26 @@ class WarningMessage(object): self.filename = None self.lineno = None - def _showwarning(self, message, category, filename, lineno, file=None): + def _showwarning(self, message, category, filename, lineno, file=None, + line=None): self.message = message self.category = category self.filename = filename self.lineno = lineno + self.line = line + + def reset(self): + self._showwarning(*((None,)*6)) + + def __str__(self): + return ("{message : %r, category : %r, filename : %r, lineno : %s, " + "line : %r}" % (self.message, + self.category.__name__ if self.category else None, + self.filename, self.lineno, self.line)) + @contextlib.contextmanager -def catch_warning(): +def catch_warning(module=warnings): """ Guard the warnings filter from being permanently changed and record the data of the last warning that has been issued. @@ -381,15 +393,15 @@ def catch_warning(): warnings.warn("foo") assert str(w.message) == "foo" """ - warning = WarningMessage() - original_filters = warnings.filters[:] - original_showwarning = warnings.showwarning - warnings.showwarning = warning._showwarning + warning_obj = WarningMessage() + original_filters = module.filters[:] + original_showwarning = module.showwarning + module.showwarning = warning_obj._showwarning try: - yield warning + yield warning_obj finally: - warnings.showwarning = original_showwarning - warnings.filters = original_filters + module.showwarning = original_showwarning + module.filters = original_filters class EnvironmentVarGuard(object): |