[Python-checkins] distutils2: Add WarningsCatcher (patch from Konrad)

tarek.ziade python-checkins at python.org
Thu Aug 19 08:34:14 CEST 2010


tarek.ziade pushed dfa3979a1d56 to distutils2:

http://hg.python.org/distutils2/rev/dfa3979a1d56
changeset:   583:dfa3979a1d56
user:        ?ric Araujo <merwok at netwok.org>
date:        Sun Aug 15 03:35:45 2010 +0200
summary:     Add WarningsCatcher (patch from Konrad)
files:       src/distutils2/tests/support.py

diff --git a/src/distutils2/tests/support.py b/src/distutils2/tests/support.py
--- a/src/distutils2/tests/support.py
+++ b/src/distutils2/tests/support.py
@@ -4,8 +4,9 @@
 (standard library unittest for 3.2 and higher, third-party unittest2
 release for older versions).
 
-Three helper classes are provided: LoggingSilencer, TempdirManager and
-EnvironGuard. They are written to be used as mixins, e.g. ::
+Four helper classes are provided: LoggingSilencer, TempdirManager,
+EnvironGuard and WarningsCatcher. They are written to be used as mixins,
+e.g. ::
 
     from distutils2.tests.support import unittest
     from distutils2.tests.support import LoggingSilencer
@@ -30,6 +31,7 @@
 import sys
 import shutil
 import tempfile
+import warnings
 from copy import deepcopy
 
 from distutils2 import log
@@ -42,8 +44,8 @@
     # external release of same package for older versions
     import unittest2 as unittest
 
-__all__ = ['LoggingSilencer', 'TempdirManager', 'EnvironGuard',
-           'DummyCommand', 'unittest']
+__all__ = ['LoggingSilencer', 'WarningsCatcher', 'TempdirManager',
+           'EnvironGuard', 'DummyCommand', 'unittest']
 
 
 class LoggingSilencer(object):
@@ -58,7 +60,6 @@
     def setUp(self):
         super(LoggingSilencer, self).setUp()
         self.threshold = log.set_threshold(FATAL)
-        # catching warnings
         # when log is replaced by logging we won't need
         # such monkey-patching anymore
         self._old_log = log.Log._log
@@ -92,6 +93,23 @@
         del self.logs[:]
 
 
+class WarningsCatcher(object):
+
+    def setUp(self):
+        self._orig_showwarning = warnings.showwarning
+        warnings.showwarning = self._record_showwarning
+        self.warnings = []
+
+    def _record_showwarning(self, message, category, filename, lineno,
+                            file=None, line=None):
+        self.warnings.append({"message": message, "category": category,
+                              "filename": filename, "lineno": lineno,
+                              "file": file, "line": line})
+
+    def tearDown(self):
+        warnings.showwarning = self._orig_showwarning
+
+
 class TempdirManager(object):
     """TestCase-compatible mixin to create temporary directories and files.
 

--
Repository URL: http://hg.python.org/distutils2


More information about the Python-checkins mailing list