Rev 3432: Disable suppression if there is already a filter present for Warnings in http://bzr.arbash-meinel.com/branches/bzr/1.6-dev/dep_warnings
John Arbash Meinel
john at arbash-meinel.com
Thu May 29 00:08:49 BST 2008
At http://bzr.arbash-meinel.com/branches/bzr/1.6-dev/dep_warnings
------------------------------------------------------------
revno: 3432
revision-id: john at arbash-meinel.com-20080528230834-m1qw9peoydd3koty
parent: john at arbash-meinel.com-20080528215346-l8plys9h9ps624pn
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: dep_warnings
timestamp: Wed 2008-05-28 18:08:34 -0500
message:
Disable suppression if there is already a filter present for Warnings
modified:
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/symbol_versioning.py'
--- a/bzrlib/symbol_versioning.py 2008-05-28 21:53:46 +0000
+++ b/bzrlib/symbol_versioning.py 2008-05-28 23:08:34 +0000
@@ -333,6 +333,21 @@
return _DeprecatedList(initial_value)
+def _check_for_filter(error_only):
+ """Check if there is already a filter for deprecation warnings.
+
+ :param error_only: Only match an 'error' filter
+ :return: True if a filter is found, False otherwise
+ """
+ import warnings
+ for filter in warnings.filters:
+ if issubclass(DeprecationWarning, filter[2]):
+ # This filter will effect DeprecationWarning
+ if not error_only or filter[0] == 'error':
+ return True
+ return False
+
+
def suppress_deprecation_warnings():
"""Call this function to suppress all deprecation warnings.
@@ -341,6 +356,10 @@
running a dev or release candidate.
"""
import warnings
+ if _check_for_filter(False):
+ # If there is already a filter effecting suppress_deprecation_warnings,
+ # then skip it.
+ return
warnings.filterwarnings('ignore', category=DeprecationWarning)
@@ -359,11 +378,8 @@
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
+ if not always and _check_for_filter(True):
+ # 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 2008-05-28 21:44:15 +0000
+++ b/bzrlib/tests/test_symbol_versioning.py 2008-05-28 23:08:34 +0000
@@ -218,6 +218,8 @@
def restore():
warnings.filters[:] = existing_filters
self.addCleanup(restore)
+ # Clean out the filters so we have a clean slate.
+ warnings.resetwarnings()
def assertFirstWarning(self, action, category):
"""Test the first warning in the filters is correct"""
@@ -229,19 +231,35 @@
symbol_versioning.suppress_deprecation_warnings()
self.assertFirstWarning('ignore', DeprecationWarning)
+ def test_suppress_deprecation_with_warning_filter(self):
+ """don't suppress if we already have a filter"""
+ warnings.filterwarnings('ignore', category=Warning)
+ self.assertFirstWarning('ignore', Warning)
+ self.assertEqual(1, len(warnings.filters))
+ symbol_versioning.suppress_deprecation_warnings()
+ self.assertFirstWarning('ignore', Warning)
+ self.assertEqual(1, len(warnings.filters))
+
+ def test_suppress_deprecation_with_filter(self):
+ """don't suppress if we already have a filter"""
+ warnings.filterwarnings('ignore', category=DeprecationWarning)
+ self.assertFirstWarning('ignore', DeprecationWarning)
+ self.assertEqual(1, len(warnings.filters))
+ symbol_versioning.suppress_deprecation_warnings()
+ self.assertFirstWarning('ignore', DeprecationWarning)
+ self.assertEqual(1, len(warnings.filters))
+
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)
@@ -253,7 +271,6 @@
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)
More information about the bazaar-commits
mailing list