Rev 3430: Update the activate_deprecation_warnings so it can be skipped if there is already an error set. in http://bzr.arbash-meinel.com/branches/bzr/1.6-dev/dep_warnings

John Arbash Meinel john at arbash-meinel.com
Wed May 28 22:44:26 BST 2008


At http://bzr.arbash-meinel.com/branches/bzr/1.6-dev/dep_warnings

------------------------------------------------------------
revno: 3430
revision-id: john at arbash-meinel.com-20080528214415-avxmnvf04nc0df5a
parent: john at arbash-meinel.com-20080516165227-3r5vc1f15r5hno45
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: dep_warnings
timestamp: Wed 2008-05-28 16:44:15 -0500
message:
  Update the activate_deprecation_warnings so it can be skipped if there is already an error set.
modified:
  bzrlib/builtins.py             builtins.py-20050830033751-fc01482b9ca23183
  bzrlib/symbol_versioning.py    symbol_versioning.py-20060105104851-9ecf8af605d15a80
  bzrlib/tests/test_symbol_versioning.py test_symbol_versioning.py-20060105104851-51d7722c2018d42b
-------------- next part --------------
=== modified file 'bzrlib/builtins.py'
--- a/bzrlib/builtins.py	2008-05-16 16:52:27 +0000
+++ b/bzrlib/builtins.py	2008-05-28 21:44:15 +0000
@@ -2655,6 +2655,9 @@
         import bzrlib.benchmarks as benchmarks
         from bzrlib.benchmarks import tree_creator
 
+        # Make deprecation warnings visible, unless -Werror is set
+        symbol_versioning.activate_deprecation_warnings(always=False)
+
         if cache_dir is not None:
             tree_creator.TreeCreator.CACHE_ROOT = osutils.abspath(cache_dir)
         if not list_only:

=== modified file 'bzrlib/symbol_versioning.py'
--- a/bzrlib/symbol_versioning.py	2008-05-15 15:59:45 +0000
+++ b/bzrlib/symbol_versioning.py	2008-05-28 21:44:15 +0000
@@ -344,7 +344,7 @@
     warnings.filterwarnings('ignore', category=DeprecationWarning)
 
 
-def activate_deprecation_warnings():
+def activate_deprecation_warnings(always=True):
     """Call this function to activate deprecation warnings.
 
     When running in a 'final' release we suppress deprecation warnings.
@@ -354,6 +354,16 @@
     Note: warnings that have already been issued under 'ignore' will not be
     reported after this point. The 'warnings' module has already marked them as
     handled, so they don't get issued again.
+
+    :param always: If False, check warnings.filter to see if warnings are set
+        to be turned into errors. If True, then do not override with 'default'.
     """
     import warnings
+    if not always:
+        for filter in warnings.filters:
+            if (filter[0] == 'error'
+                and issubclass(DeprecationWarning, filter[2])):
+                # DeprecationWarnings are already turned into errors, don't
+                # downgrade them to 'default'.
+                return
     warnings.filterwarnings('default', category=DeprecationWarning)

=== modified file 'bzrlib/tests/test_symbol_versioning.py'
--- a/bzrlib/tests/test_symbol_versioning.py	2007-09-18 04:21:20 +0000
+++ b/bzrlib/tests/test_symbol_versioning.py	2008-05-28 21:44:15 +0000
@@ -18,7 +18,9 @@
 
 """Symbol versioning tests."""
 
-import bzrlib.symbol_versioning as symbol_versioning
+import warnings
+
+from bzrlib import symbol_versioning
 from bzrlib.tests import TestCase
 
 
@@ -207,3 +209,56 @@
             symbol_versioning.deprecation_string(
                 symbol_versioning.deprecated_function,
                 symbol_versioning.zero_eleven))
+
+
+class TestSuppressAndActivate(TestCase):
+
+    def setUp(self):
+        existing_filters = list(warnings.filters)
+        def restore():
+            warnings.filters[:] = existing_filters
+        self.addCleanup(restore)
+
+    def assertFirstWarning(self, action, category):
+        """Test the first warning in the filters is correct"""
+        first = warnings.filters[0]
+        self.assertEqual((action, category), (first[0], first[2]))
+
+    def test_suppress_deprecation_warnings(self):
+        """suppress_deprecation_warnings sets DeprecationWarning to ignored."""
+        symbol_versioning.suppress_deprecation_warnings()
+        self.assertFirstWarning('ignore', DeprecationWarning)
+
+    def test_activate_deprecation_warnings(self):
+        symbol_versioning.activate_deprecation_warnings(always=True)
+        self.assertFirstWarning('default', DeprecationWarning)
+
+    def test_activate_deprecation_no_error(self):
+        # First nuke the filters, so we know it is clean
+        warnings.resetwarnings()
+        symbol_versioning.activate_deprecation_warnings(always=False)
+        self.assertFirstWarning('default', DeprecationWarning)
+
+    def test_activate_deprecation_with_error(self):
+        # First nuke the filters, so we know it is clean
+        warnings.resetwarnings()
+        # Add a warning == error rule
+        warnings.filterwarnings('error', category=Warning)
+        self.assertFirstWarning('error', Warning)
+        self.assertEqual(1, len(warnings.filters))
+        symbol_versioning.activate_deprecation_warnings(always=False)
+        # There should not be a new warning
+        self.assertFirstWarning('error', Warning)
+        self.assertEqual(1, len(warnings.filters))
+
+    def test_activate_deprecation_with_DW_error(self):
+        # First nuke the filters, so we know it is clean
+        warnings.resetwarnings()
+        # Add a warning == error rule
+        warnings.filterwarnings('error', category=DeprecationWarning)
+        self.assertFirstWarning('error', DeprecationWarning)
+        self.assertEqual(1, len(warnings.filters))
+        symbol_versioning.activate_deprecation_warnings(always=False)
+        # There should not be a new warning
+        self.assertFirstWarning('error', DeprecationWarning)
+        self.assertEqual(1, len(warnings.filters))



More information about the bazaar-commits mailing list