[Zope3-checkins] SVN: Zope3/trunk/src/zope/interface/ Fixed bug:
the invariants function didn't work as a Python 2.4
Jim Fulton
jim at zope.com
Thu Apr 7 13:44:45 EDT 2005
Log message for revision 29899:
Fixed bug: the invariants function didn't work as a Python 2.4
decorator.
Changed:
U Zope3/trunk/src/zope/interface/interface.py
U Zope3/trunk/src/zope/interface/tests/test_interface.py
-=-
Modified: Zope3/trunk/src/zope/interface/interface.py
===================================================================
--- Zope3/trunk/src/zope/interface/interface.py 2005-04-07 14:41:10 UTC (rev 29898)
+++ Zope3/trunk/src/zope/interface/interface.py 2005-04-07 17:44:44 UTC (rev 29899)
@@ -29,6 +29,8 @@
CO_VARKEYWORDS = 8
TAGGED_DATA = '__interface_tagged_values__'
+_decorator_non_return = object()
+
def invariant(call):
f_locals = sys._getframe(1).f_locals
tags = f_locals.get(TAGGED_DATA)
@@ -38,6 +40,7 @@
if invariants is None:
invariants = tags['invariants'] = []
invariants.append(call)
+ return _decorator_non_return
class Element(object):
@@ -448,6 +451,8 @@
attr.__name__ = name
elif isinstance(attr, FunctionType):
attrs[name] = fromFunction(attr, self, name=name)
+ elif attr is _decorator_non_return:
+ del attrs[name]
else:
raise InvalidInterface("Concrete attribute, %s" %name)
Modified: Zope3/trunk/src/zope/interface/tests/test_interface.py
===================================================================
--- Zope3/trunk/src/zope/interface/tests/test_interface.py 2005-04-07 14:41:10 UTC (rev 29898)
+++ Zope3/trunk/src/zope/interface/tests/test_interface.py 2005-04-07 17:44:44 UTC (rev 29899)
@@ -15,12 +15,14 @@
$Id$
"""
+import sys
import unittest
from zope.testing.doctestunit import DocTestSuite
from zope.interface.tests.unitfixtures import * # hehehe
from zope.interface.exceptions import BrokenImplementation, Invalid
from zope.interface import implementedBy, providedBy
from zope.interface import Interface, directlyProvides, Attribute
+from zope import interface
class InterfaceTests(unittest.TestCase):
@@ -278,10 +280,44 @@
f23 = f22
+
+if sys.version_info >= (2, 4):
+ def test_invariant_as_decorator():
+ """Invaiants can be deined in line
+
+ >>> class IRange(interface.Interface):
+ ... min = interface.Attribute("Lower bound")
+ ... max = interface.Attribute("Upper bound")
+ ...
+ ... @interface.invariant
+ ... def range_invariant(ob):
+ ... if ob.max < ob.min:
+ ... raise Invalid('max < min')
+
+
+ >>> class Range(object):
+ ... interface.implements(IRange)
+ ...
+ ... def __init__(self, min, max):
+ ... self.min, self.max = min, max
+
+ >>> IRange.validateInvariants(Range(1,2))
+ >>> IRange.validateInvariants(Range(1,1))
+ >>> IRange.validateInvariants(Range(2,1))
+ Traceback (most recent call last):
+ ...
+ Invalid: max < min
+
+
+ """
+
+
def test_suite():
from zope.testing import doctest
suite = unittest.makeSuite(InterfaceTests)
suite.addTest(doctest.DocTestSuite("zope.interface.interface"))
+ if sys.version_info >= (2, 4):
+ suite.addTest(doctest.DocTestSuite())
suite.addTest(doctest.DocFileSuite(
'../README.txt',
globs={'__name__': '__main__'},
More information about the Zope3-Checkins
mailing list