verify.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. ##############################################################################
  2. #
  3. # Copyright (c) 2001, 2002 Zope Foundation and Contributors.
  4. # All Rights Reserved.
  5. #
  6. # This software is subject to the provisions of the Zope Public License,
  7. # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
  8. # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
  9. # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  10. # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
  11. # FOR A PARTICULAR PURPOSE.
  12. #
  13. ##############################################################################
  14. """Verify interface implementations
  15. """
  16. from zope.interface.exceptions import BrokenImplementation, DoesNotImplement
  17. from zope.interface.exceptions import BrokenMethodImplementation
  18. from types import FunctionType, MethodType
  19. from zope.interface.interface import fromMethod, fromFunction, Method
  20. import sys
  21. # This will be monkey-patched when running under Zope 2, so leave this
  22. # here:
  23. MethodTypes = (MethodType, )
  24. def _verify(iface, candidate, tentative=0, vtype=None):
  25. """Verify that 'candidate' might correctly implements 'iface'.
  26. This involves:
  27. o Making sure the candidate defines all the necessary methods
  28. o Making sure the methods have the correct signature
  29. o Making sure the candidate asserts that it implements the interface
  30. Note that this isn't the same as verifying that the class does
  31. implement the interface.
  32. If optional tentative is true, suppress the "is implemented by" test.
  33. """
  34. if vtype == 'c':
  35. tester = iface.implementedBy
  36. else:
  37. tester = iface.providedBy
  38. if not tentative and not tester(candidate):
  39. raise DoesNotImplement(iface)
  40. # Here the `desc` is either an `Attribute` or `Method` instance
  41. for name, desc in iface.namesAndDescriptions(1):
  42. try:
  43. attr = getattr(candidate, name)
  44. except AttributeError:
  45. if (not isinstance(desc, Method)) and vtype == 'c':
  46. # We can't verify non-methods on classes, since the
  47. # class may provide attrs in it's __init__.
  48. continue
  49. raise BrokenImplementation(iface, name)
  50. if not isinstance(desc, Method):
  51. # If it's not a method, there's nothing else we can test
  52. continue
  53. if isinstance(attr, FunctionType):
  54. if sys.version_info[0] >= 3 and isinstance(candidate, type):
  55. # This is an "unbound method" in Python 3.
  56. meth = fromFunction(attr, iface, name=name,
  57. imlevel=1)
  58. else:
  59. # Nope, just a normal function
  60. meth = fromFunction(attr, iface, name=name)
  61. elif (isinstance(attr, MethodTypes)
  62. and type(attr.__func__) is FunctionType):
  63. meth = fromMethod(attr, iface, name)
  64. elif isinstance(attr, property) and vtype == 'c':
  65. # We without an instance we cannot be sure it's not a
  66. # callable.
  67. continue
  68. else:
  69. if not callable(attr):
  70. raise BrokenMethodImplementation(name, "Not a method")
  71. # sigh, it's callable, but we don't know how to introspect it, so
  72. # we have to give it a pass.
  73. continue
  74. # Make sure that the required and implemented method signatures are
  75. # the same.
  76. desc = desc.getSignatureInfo()
  77. meth = meth.getSignatureInfo()
  78. mess = _incompat(desc, meth)
  79. if mess:
  80. raise BrokenMethodImplementation(name, mess)
  81. return True
  82. def verifyClass(iface, candidate, tentative=0):
  83. return _verify(iface, candidate, tentative, vtype='c')
  84. def verifyObject(iface, candidate, tentative=0):
  85. return _verify(iface, candidate, tentative, vtype='o')
  86. def _incompat(required, implemented):
  87. #if (required['positional'] !=
  88. # implemented['positional'][:len(required['positional'])]
  89. # and implemented['kwargs'] is None):
  90. # return 'imlementation has different argument names'
  91. if len(implemented['required']) > len(required['required']):
  92. return 'implementation requires too many arguments'
  93. if ((len(implemented['positional']) < len(required['positional']))
  94. and not implemented['varargs']):
  95. return "implementation doesn't allow enough arguments"
  96. if required['kwargs'] and not implemented['kwargs']:
  97. return "implementation doesn't support keyword arguments"
  98. if required['varargs'] and not implemented['varargs']:
  99. return "implementation doesn't support variable arguments"