Log message for revision 113793:
  - LP #597594: Performance optimization in OFS.subscriber.maybeWarnDeprecated.
  
  

Changed:
  U   Zope/branches/2.12/doc/CHANGES.rst
  U   Zope/branches/2.12/src/OFS/subscribers.py
  A   Zope/branches/2.12/src/OFS/tests/test_subscribers.py

-=-
Modified: Zope/branches/2.12/doc/CHANGES.rst
===================================================================
--- Zope/branches/2.12/doc/CHANGES.rst  2010-06-24 06:16:59 UTC (rev 113792)
+++ Zope/branches/2.12/doc/CHANGES.rst  2010-06-24 09:33:46 UTC (rev 113793)
@@ -11,6 +11,8 @@
 Bugs Fixed
 ++++++++++
 
+- LP #597594: Performance optimization in OFS.subscriber.maybeWarnDeprecated.
+  
 - LP #143639: When the last cache manager in a container is 
   deleted, we need to remove all traces of it from the 
   container.

Modified: Zope/branches/2.12/src/OFS/subscribers.py
===================================================================
--- Zope/branches/2.12/src/OFS/subscribers.py   2010-06-24 06:16:59 UTC (rev 
113792)
+++ Zope/branches/2.12/src/OFS/subscribers.py   2010-06-24 09:33:46 UTC (rev 
113793)
@@ -1,6 +1,6 @@
 ##############################################################################
 #
-# Copyright (c) 2005 Zope Foundation and Contributors.
+# Copyright (c) 2005,2010 Zope Foundation and Contributors.
 # All Rights Reserved.
 #
 # This software is subject to the provisions of the Zope Public License,
@@ -56,13 +56,16 @@
     if not deprecatedManageAddDeleteClasses:
         # Directives not fully loaded
         return
-    for cls in deprecatedManageAddDeleteClasses:
-        if isinstance(ob, cls):
-            # Already deprecated through zcml
-            return
     if getattr(getattr(ob, method_name), '__five_method__', False):
         # Method knows it's deprecated
         return
+    ob_type = type(ob)
+    # Quick check for directly deprecated classes
+    if ob_type in deprecatedManageAddDeleteClasses:
+        return
+    if any(issubclass(ob_type, cls)
+           for cls in deprecatedManageAddDeleteClasses):
+        return
     class_ = ob.__class__
     LOG.debug(
         "%s.%s.%s is discouraged. You should use event subscribers instead." %

Added: Zope/branches/2.12/src/OFS/tests/test_subscribers.py
===================================================================
--- Zope/branches/2.12/src/OFS/tests/test_subscribers.py                        
        (rev 0)
+++ Zope/branches/2.12/src/OFS/tests/test_subscribers.py        2010-06-24 
09:33:46 UTC (rev 113793)
@@ -0,0 +1,88 @@
+##############################################################################
+#
+# Copyright (c) 2010 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+
+
+import StringIO
+import logging
+import unittest
+
+
+class TestMaybeWarnDeprecated(unittest.TestCase):
+
+    def setUp(self):
+        # Make a copy so we can freely modify the contents
+        from OFS.subscribers import deprecatedManageAddDeleteClasses
+        self._orig_deprecatedManageAddDeleteClasses = (
+            deprecatedManageAddDeleteClasses[:])
+        self.deprecatedManageAddDeleteClasses = (
+            deprecatedManageAddDeleteClasses)
+        # Add a class to make sure there is at least one because an empty 
+        # deprecatedManageAddDeleteClasses list is special cased
+        self.deprecatedManageAddDeleteClasses.append(int)
+        # Pick up log messages
+        self.logfile = StringIO.StringIO()
+        self.log_handler = logging.StreamHandler(self.logfile)
+        logging.root.addHandler(self.log_handler)
+        self.old_log_level = logging.root.level
+        logging.root.setLevel(logging.DEBUG)
+
+    def tearDown(self):
+        self.deprecatedManageAddDeleteClasses[:] = (
+            self._orig_deprecatedManageAddDeleteClasses)
+        logging.root.removeHandler(self.log_handler)
+        logging.root.setLevel(self.old_log_level)
+
+    def assertLog(self, class_, expected):
+        from OFS.subscribers import maybeWarnDeprecated
+        maybeWarnDeprecated(class_(), 'manage_afterAdd')
+        self.assertEquals(expected, self.logfile.getvalue())
+
+    def test_method_deprecated(self):
+        class Deprecated(object):
+            def manage_afterAdd(self):
+                pass
+            manage_afterAdd.__five_method__ = True
+        self.assertLog(Deprecated, '')
+
+    def test_class_deprecated(self):
+        class Deprecated(object):
+            def manage_afterAdd(self):
+                pass
+        self.deprecatedManageAddDeleteClasses.append(Deprecated)
+        self.assertLog(Deprecated, '')
+
+    def test_subclass_deprecated(self):
+        class Deprecated(object):
+            def manage_afterAdd(self):
+                pass
+        class ASubClass(Deprecated):
+            pass
+        self.deprecatedManageAddDeleteClasses.append(Deprecated)
+        self.assertLog(ASubClass, '')
+
+    def test_not_deprecated(self):
+        class Deprecated(object):
+            def manage_afterAdd(self):
+                pass
+        self.assertLog(
+            Deprecated,
+            'OFS.tests.test_subscribers.Deprecated.manage_afterAdd is '
+            'discouraged. You should use event subscribers instead.\n')
+
+    def test_not_deprecated_when_there_are_no_classes(self):
+        class Deprecated(object):
+            def manage_afterAdd(self):
+                pass
+        self.deprecatedManageAddDeleteClasses[:] = []
+        self.assertLog(Deprecated, '')


Property changes on: Zope/branches/2.12/src/OFS/tests/test_subscribers.py
___________________________________________________________________
Added: svn:keywords
   + Id Rev Date
Added: svn:eol-style
   + native

_______________________________________________
Zope-Checkins maillist  -  Zope-Checkins@zope.org
https://mail.zope.org/mailman/listinfo/zope-checkins

Reply via email to