[Zope-Checkins] SVN: Zope/trunk/ Port from 2.12:
Christian Zagrodnick
cz at gocept.com
Thu Jun 24 05:38:46 EDT 2010
Log message for revision 113794:
Port from 2.12:
- LP #597594: Performance optimization in OFS.subscriber.maybeWarnDeprecated.
Changed:
U Zope/trunk/doc/CHANGES.rst
U Zope/trunk/src/OFS/subscribers.py
A Zope/trunk/src/OFS/tests/test_subscribers.py
-=-
Modified: Zope/trunk/doc/CHANGES.rst
===================================================================
--- Zope/trunk/doc/CHANGES.rst 2010-06-24 09:33:46 UTC (rev 113793)
+++ Zope/trunk/doc/CHANGES.rst 2010-06-24 09:38:46 UTC (rev 113794)
@@ -204,6 +204,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/trunk/src/OFS/subscribers.py
===================================================================
--- Zope/trunk/src/OFS/subscribers.py 2010-06-24 09:33:46 UTC (rev 113793)
+++ Zope/trunk/src/OFS/subscribers.py 2010-06-24 09:38:46 UTC (rev 113794)
@@ -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." %
Copied: Zope/trunk/src/OFS/tests/test_subscribers.py (from rev 113793, Zope/branches/2.12/src/OFS/tests/test_subscribers.py)
===================================================================
--- Zope/trunk/src/OFS/tests/test_subscribers.py (rev 0)
+++ Zope/trunk/src/OFS/tests/test_subscribers.py 2010-06-24 09:38:46 UTC (rev 113794)
@@ -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, '')
More information about the Zope-Checkins
mailing list