Commit 9a31a165 authored by Christian Zagrodnick's avatar Christian Zagrodnick

- LP #597594: Performance optimization in OFS.subscriber.maybeWarnDeprecated.

parent a8977d1a
...@@ -11,6 +11,8 @@ http://docs.zope.org/zope2/releases/. ...@@ -11,6 +11,8 @@ http://docs.zope.org/zope2/releases/.
Bugs Fixed Bugs Fixed
++++++++++ ++++++++++
- LP #597594: Performance optimization in OFS.subscriber.maybeWarnDeprecated.
- LP #143639: When the last cache manager in a container is - LP #143639: When the last cache manager in a container is
deleted, we need to remove all traces of it from the deleted, we need to remove all traces of it from the
container. container.
......
############################################################################## ##############################################################################
# #
# Copyright (c) 2005 Zope Foundation and Contributors. # Copyright (c) 2005,2010 Zope Foundation and Contributors.
# All Rights Reserved. # All Rights Reserved.
# #
# This software is subject to the provisions of the Zope Public License, # This software is subject to the provisions of the Zope Public License,
...@@ -56,13 +56,16 @@ def maybeWarnDeprecated(ob, method_name): ...@@ -56,13 +56,16 @@ def maybeWarnDeprecated(ob, method_name):
if not deprecatedManageAddDeleteClasses: if not deprecatedManageAddDeleteClasses:
# Directives not fully loaded # Directives not fully loaded
return return
for cls in deprecatedManageAddDeleteClasses:
if isinstance(ob, cls):
# Already deprecated through zcml
return
if getattr(getattr(ob, method_name), '__five_method__', False): if getattr(getattr(ob, method_name), '__five_method__', False):
# Method knows it's deprecated # Method knows it's deprecated
return 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__ class_ = ob.__class__
LOG.debug( LOG.debug(
"%s.%s.%s is discouraged. You should use event subscribers instead." % "%s.%s.%s is discouraged. You should use event subscribers instead." %
......
##############################################################################
#
# 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, '')
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment