Skip to content

Commit

Permalink
Bugfix, added custom verify module which does
Browse files Browse the repository at this point in the history
not raise exceptions on decorated methods
  • Loading branch information
projekt01 committed Jan 22, 2007
1 parent 0628a86 commit dab9720
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/z3c/testing/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def makeTestObject(self, object=None, *pos, **kws):
# Public Base Tests
#
###############################################################################
from zope.interface.verify import verifyClass
from zope.interface.verify import verifyObject
from z3c.testing.verify import verifyClass


class InterfaceBaseTest(TestCase):
Expand Down
84 changes: 84 additions & 0 deletions src/z3c/testing/verify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation 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.
#
##############################################################################
"""Verify interface implementations
$Id$
"""
from zope.interface.exceptions import BrokenImplementation, DoesNotImplement
from zope.interface.exceptions import BrokenMethodImplementation
from types import FunctionType, MethodType
from zope.interface.interface import fromMethod, fromFunction, Method
from zope.interface.verify import _incompat, MethodTypes


def _verify(iface, candidate, tentative=0, vtype=None):
"""Fix broken testing decorated methods.
You can find such a broken interfaces test in z3c.proxy where the
methods are decorated within ``@non_overridable``.
"""

if vtype == 'c':
tester = iface.implementedBy
else:
tester = iface.providedBy

if not tentative and not tester(candidate):
raise DoesNotImplement(iface)

# Here the `desc` is either an `Attribute` or `Method` instance
for name, desc in iface.namesAndDescriptions(1):
if not hasattr(candidate, name):
if (not isinstance(desc, Method)) and vtype == 'c':
# We can't verify non-methods on classes, since the
# class may provide attrs in it's __init__.
continue

raise BrokenImplementation(iface, name)

attr = getattr(candidate, name)
if not isinstance(desc, Method):
# If it's not a method, there's nothing else we can test
continue

if isinstance(attr, FunctionType):
# should never get here, since classes should not provide functions
meth = fromFunction(attr, iface, name=name)
elif (isinstance(attr, MethodTypes)
and type(attr.im_func) is FunctionType):
meth = fromMethod(attr, iface, name)
elif isinstance(attr, property):
# We can't verify decorated methods on classes.
continue
else:
import pdb; pdb.set_trace()
if not callable(attr):
raise BrokenMethodImplementation(name, "Not a method")
# sigh, it's callable, but we don't know how to intrspect it, so
# we have to give it a pass.
continue

# Make sure that the required and implemented method signatures are
# the same.
desc = desc.getSignatureInfo()
meth = meth.getSignatureInfo()

mess = _incompat(desc, meth)
if mess:
raise BrokenMethodImplementation(name, mess)

return True

def verifyClass(iface, candidate, tentative=0):
return _verify(iface, candidate, tentative, vtype='c')

0 comments on commit dab9720

Please sign in to comment.