Rev 133: Add a helper for issuing deprecation warnings, deprecate 'referrers' in http://bazaar.launchpad.net/~meliae-dev/meliae/trunk

John Arbash Meinel john at arbash-meinel.com
Fri Jan 8 22:33:48 GMT 2010


At http://bazaar.launchpad.net/~meliae-dev/meliae/trunk

------------------------------------------------------------
revno: 133
revision-id: john at arbash-meinel.com-20100108223336-hfnjdiejvvzyi5c5
parent: john at arbash-meinel.com-20100108222026-oa7vumiv821whlrh
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: trunk
timestamp: Fri 2010-01-08 16:33:36 -0600
message:
  Add a helper for issuing deprecation warnings, deprecate 'referrers'
-------------- next part --------------
=== modified file 'meliae/_loader.pyx'
--- a/meliae/_loader.pyx	2010-01-08 22:20:26 +0000
+++ b/meliae/_loader.pyx	2010-01-08 22:33:36 +0000
@@ -44,6 +44,7 @@
     # void *stderr
 
 import gc
+from meliae import warn
 
 
 ctypedef struct RefList:
@@ -398,12 +399,15 @@
             _free_ref_list(self._obj.children)
             self._obj.children = _list_to_ref_list(value)
 
-    # TODO: deprecated for clarity
     property referrers:
         def __get__(self):
+            warn.deprecated('Attribute .referrers deprecated.'
+                            ' Use .parents instead.')
             return self.parents
 
         def __set__(self, value):
+            warn.deprecated('Attribute .referrers deprecated.'
+                            ' Use .parents instead.')
             self.parents = value
 
     property parents:

=== modified file 'meliae/tests/test__loader.py'
--- a/meliae/tests/test__loader.py	2010-01-08 22:20:26 +0000
+++ b/meliae/tests/test__loader.py	2010-01-08 22:33:36 +0000
@@ -16,6 +16,7 @@
 
 from meliae import (
     _loader,
+    warn,
     tests,
     )
 
@@ -320,6 +321,29 @@
         self.assertEqual(1, mop255.num_parents)
         self.assertEqual([1234567], mop255.parents)
 
+    def test_referrers(self):
+        mop = self.moc.add(1234567, 'type', 256, children=[0, 255])
+        # Referrers is deprecated
+        logged = []
+        def log_warn(msg, klass, stacklevel=None):
+            logged.append((msg, klass, stacklevel))
+        old_func = warn.trap_warnings(log_warn)
+        try:
+            mop0 = self.moc[0]
+            self.assertEqual((), mop0.referrers)
+            self.assertEqual([('Attribute .referrers deprecated.'
+                               ' Use .parents instead.',
+                               DeprecationWarning, 3)
+                             ], logged)
+            mop0.referrers = [1234567]
+            self.assertEqual([('Attribute .referrers deprecated.'
+                               ' Use .parents instead.',
+                               DeprecationWarning, 3)
+                             ]*2, logged)
+            self.assertEqual([1234567], mop0.parents)
+        finally:
+            warn.trap_warnings(old_func)
+
     def test_parents(self):
         mop = self.moc.add(1234567, 'type', 256, children=[0, 255])
         mop0 = self.moc[0]

=== added file 'meliae/warn.py'
--- a/meliae/warn.py	1970-01-01 00:00:00 +0000
+++ b/meliae/warn.py	2010-01-08 22:33:36 +0000
@@ -0,0 +1,41 @@
+# Copyright (C) 2010 Canonical Ltd
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 3 as
+# published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+"""Routines for giving warnings."""
+
+import warnings
+
+_warn_func = warnings.warn
+
+
+def deprecated(msg):
+    """Issue a warning about something that is deprecated."""
+    warn(msg, DeprecationWarning, stacklevel=3)
+
+
+def warn(msg, klass=None, stacklevel=1):
+    """Emit a warning about something."""
+    _warn_func(msg, klass, stacklevel=stacklevel)
+
+
+def trap_warnings(new_warning_func):
+    """Redirect warnings to a different function.
+    
+    :param new_warning_func: A function with the same signature as warning.warn
+    :return: The old warning function
+    """
+    global _warn_func
+    old = _warn_func
+    _warn_func = new_warning_func
+    return old



More information about the bazaar-commits mailing list