diff options
author | Andrey Lebedev <andrey@lebedev.lt> | 2013-02-19 11:59:47 +0200 |
---|---|---|
committer | Andrey Lebedev <andrey@lebedev.lt> | 2013-02-19 11:59:47 +0200 |
commit | ec3ec97c92703bfb7827e7c9472bcbe0f254253e (patch) | |
tree | 104824146b2585cf987c2cda77674654d7288132 /docs | |
parent | 18a6987860cdb622fca9c98fca22692c90fc45f8 (diff) | |
download | zope-security-ec3ec97c92703bfb7827e7c9472bcbe0f254253e.tar.gz |
More tests are running under python-3
Include documentation doctests into main test suite. Documentation is
updated to be valid under python-3 and some critical bugs in python-3
are revealed.
Diffstat (limited to 'docs')
-rw-r--r-- | docs/api/checker.rst | 119 | ||||
-rw-r--r-- | docs/api/decorator.rst | 25 | ||||
-rw-r--r-- | docs/api/zcml.rst | 11 |
3 files changed, 85 insertions, 70 deletions
diff --git a/docs/api/checker.rst b/docs/api/checker.rst index 8b321db..83c347a 100644 --- a/docs/api/checker.rst +++ b/docs/api/checker.rst @@ -82,8 +82,7 @@ directly, or via interface: >>> context = AContext() >>> allow(context, attributes=['foo', 'bar'], interface=[I1, I2]) - >>> context.actions.sort( - ... lambda a, b: cmp(a['discriminator'], b['discriminator'])) + >>> context.actions.sort(key=lambda a: a['discriminator']) >>> pprint(context.actions) [{'args': ('testmodule', 'a', 'zope.Public'), 'callable': 1, @@ -136,8 +135,7 @@ directly, or via interface: >>> require(context, attributes=['foo', 'bar'], ... interface=[I1, I2], permission='p') - >>> context.actions.sort( - ... lambda a, b: cmp(a['discriminator'], b['discriminator'])) + >>> context.actions.sort(key=lambda a: a['discriminator']) >>> pprint(context.actions) [{'args': ('testmodule', 'a', 'p'), 'callable': 1, @@ -182,26 +180,26 @@ Protections for standard objects ... from zope.security.interfaces import ForbiddenAttribute ... try: ... return getattr(object, attr) - ... except ForbiddenAttribute, e: - ... return 'ForbiddenAttribute: %s' % e[0] + ... except ForbiddenAttribute as e: + ... return 'ForbiddenAttribute: %s' % e.args[0] >>> def check_forbidden_setitem(object, item, value): ... from zope.security.interfaces import ForbiddenAttribute ... try: ... object[item] = value - ... except ForbiddenAttribute, e: - ... return 'ForbiddenAttribute: %s' % e[0] + ... except ForbiddenAttribute as e: + ... return 'ForbiddenAttribute: %s' % e.args[0] >>> def check_forbidden_delitem(object, item): ... from zope.security.interfaces import ForbiddenAttribute ... try: ... del object[item] - ... except ForbiddenAttribute, e: - ... return 'ForbiddenAttribute: %s' % e[0] + ... except ForbiddenAttribute as e: + ... return 'ForbiddenAttribute: %s' % e.args[0] >>> def check_forbidden_call(callable, *args): # ** ... from zope.security.interfaces import ForbiddenAttribute ... try: ... return callable(*args) # ** - ... except ForbiddenAttribute, e: - ... return 'ForbiddenAttribute: %s' % e[0] + ... except ForbiddenAttribute as e: + ... return 'ForbiddenAttribute: %s' % e.args[0] Rocks ##### @@ -217,19 +215,25 @@ Rocks are immuatle, non-callable objects without interesting methods. They 1 >>> int(type(ProxyFactory( 1.0 )) is float) 1 - >>> int(type(ProxyFactory( 1l )) is long) - 1 >>> int(type(ProxyFactory( 1j )) is complex) 1 >>> int(type(ProxyFactory( None )) is type(None)) 1 >>> int(type(ProxyFactory( 'xxx' )) is str) 1 - >>> int(type(ProxyFactory( u'xxx' )) is unicode) - 1 >>> int(type(ProxyFactory( True )) is type(True)) 1 +Additionally, check some python-2.x specific types. + +.. doctest:: + >>> import sys + >>> PY2 = sys.version_info[0] == 2 + >>> int(type(ProxyFactory( long(1) )) is long) if PY2 else 1 + 1 + >>> int(type(ProxyFactory( u'xxx' )) is unicode) if PY2 else 1 + 1 + Datetime-reltatd instances are rocks, too: .. doctest:: @@ -270,18 +274,18 @@ We can do everything we expect to be able to do with proxied dicts. 1 >>> len(d) 2 - >>> list(d) + >>> sorted(list(d)) ['a', 'b'] >>> d.get('a') 1 - >>> int(d.has_key('a')) + >>> int('a' in d) 1 >>> c = d.copy() >>> check_forbidden_get(c, 'clear') 'ForbiddenAttribute: clear' >>> int(str(c) in ("{'a': 1, 'b': 2}", "{'b': 2, 'a': 1}")) 1 - >>> int(`c` in ("{'a': 1, 'b': 2}", "{'b': 2, 'a': 1}")) + >>> int(repr(c) in ("{'a': 1, 'b': 2}", "{'b': 2, 'a': 1}")) 1 >>> def sorted(x): ... x = list(x) @@ -293,26 +297,27 @@ We can do everything we expect to be able to do with proxied dicts. [1, 2] >>> sorted(d.items()) [('a', 1), ('b', 2)] - >>> sorted(d.iterkeys()) + >>> sorted(d.iterkeys()) if PY2 else ['a', 'b'] ['a', 'b'] - >>> sorted(d.itervalues()) + >>> sorted(d.itervalues()) if PY2 else [1, 2] [1, 2] - >>> sorted(d.iteritems()) + >>> sorted(d.iteritems()) if PY2 else [('a', 1), ('b', 2)] [('a', 1), ('b', 2)] -Always available: +Always available (note, that dicts in python-3.x are not orderable, so we are +not checking that under python > 2): .. doctest:: - >>> int(d < d) + >>> int(d < d) if PY2 else 0 0 - >>> int(d > d) + >>> int(d > d) if PY2 else 0 0 - >>> int(d <= d) + >>> int(d <= d) if PY2 else 1 1 - >>> int(d >= d) + >>> int(d >= d) if PY2 else 1 1 - >>> int(d == d) + >>> int(d == d) if PY2 else 1 1 >>> int(d != d) 0 @@ -351,7 +356,7 @@ We can do everything we expect to be able to do with proxied lists. 1 >>> str(l) '[1, 2]' - >>> `l` + >>> repr(l) '[1, 2]' >>> l + l [1, 2, 1, 2] @@ -398,7 +403,7 @@ We can do everything we expect to be able to do with proxied tuples. 1 >>> str(l) '(1, 2)' - >>> `l` + >>> repr(l) '(1, 2)' >>> l + l (1, 2, 1, 2) @@ -607,8 +612,8 @@ we can do everything we expect to be able to do with proxied frozensets. ... from zope.security.interfaces import ForbiddenAttribute ... try: ... return getattr(object, attr) - ... except ForbiddenAttribute, e: - ... return 'ForbiddenAttribute: %s' % e[0] + ... except ForbiddenAttribute as e: + ... return 'ForbiddenAttribute: %s' % e.args[0] >>> from zope.security.checker import ProxyFactory >>> from zope.security.interfaces import ForbiddenAttribute >>> us = frozenset((1, 2)) @@ -856,7 +861,7 @@ New-style classes >>> check_forbidden_get(C, '__dict__') 'ForbiddenAttribute: __dict__' >>> s = str(C) - >>> s = `C` + >>> s = repr(C) >>> int(C.__module__ == __name__) 1 >>> len(C.__bases__) @@ -868,13 +873,13 @@ Always available: .. doctest:: - >>> int(C < C) + >>> int(C < C) if PY2 else 0 0 - >>> int(C > C) + >>> int(C > C) if PY2 else 0 0 - >>> int(C <= C) + >>> int(C <= C) if PY2 else 1 1 - >>> int(C >= C) + >>> int(C >= C) if PY2 else 1 1 >>> int(C == C) 1 @@ -907,13 +912,13 @@ Always available: .. doctest:: - >>> int(c < c) + >>> int(c < c) if PY2 else 0 0 - >>> int(c > c) + >>> int(c > c) if PY2 else 0 0 - >>> int(c <= c) + >>> int(c <= c) if PY2 else 1 1 - >>> int(c >= c) + >>> int(c >= c) if PY2 else 1 1 >>> int(c == c) 1 @@ -938,23 +943,23 @@ Classic Classes >>> check_forbidden_get(C, '__dict__') 'ForbiddenAttribute: __dict__' >>> s = str(C) - >>> s = `C` + >>> s = repr(C) >>> int(C.__module__ == __name__) 1 - >>> len(C.__bases__) + >>> len(C.__bases__) if PY2 else 0 0 Always available: .. doctest:: - >>> int(C < C) + >>> int(C < C) if PY2 else 0 0 - >>> int(C > C) + >>> int(C > C) if PY2 else 0 0 - >>> int(C <= C) + >>> int(C <= C) if PY2 else 1 1 - >>> int(C >= C) + >>> int(C >= C) if PY2 else 1 1 >>> int(C == C) 1 @@ -984,13 +989,13 @@ Always available: .. doctest:: - >>> int(c < c) + >>> int(c < c) if PY2 else 0 0 - >>> int(c > c) + >>> int(c > c) if PY2 else 0 0 - >>> int(c <= c) + >>> int(c <= c) if PY2 else 1 1 - >>> int(c >= c) + >>> int(c >= c) if PY2 else 1 1 >>> int(c == c) 1 @@ -1055,7 +1060,7 @@ We work with the ABCMeta meta class: >>> check_forbidden_get(PBar, '__dict__') 'ForbiddenAttribute: __dict__' >>> s = str(PBar) - >>> s = `PBar` + >>> s = repr(PBar) >>> int(PBar.__module__ == __name__) 1 >>> len(PBar.__bases__) @@ -1065,13 +1070,13 @@ Always available: .. doctest:: - >>> int(PBar < PBar) + >>> int(PBar < PBar) if PY2 else 0 0 - >>> int(PBar > PBar) + >>> int(PBar > PBar) if PY2 else 0 0 - >>> int(PBar <= PBar) + >>> int(PBar <= PBar) if PY2 else 1 1 - >>> int(PBar >= PBar) + >>> int(PBar >= PBar) if PY2 else 1 1 >>> int(PBar == PBar) 1 @@ -1079,5 +1084,5 @@ Always available: 0 >>> int(bool(PBar)) 1 - >>> int(PBar.__class__ == abc.ABCMeta) + >>> int(PBar.__class__ == abc.ABCMeta) if PY2 else 1 1 diff --git a/docs/api/decorator.rst b/docs/api/decorator.rst index c4741b9..c05c692 100644 --- a/docs/api/decorator.rst +++ b/docs/api/decorator.rst @@ -48,13 +48,16 @@ Using `selectChecker()`, we can confirm that a `Foo` object uses .. doctest:: >>> from zope.security.checker import selectChecker + >>> from zope.security.interfaces import ForbiddenAttribute >>> foo = Foo() >>> selectChecker(foo) is fooChecker True >>> fooChecker.check(foo, 'a') - >>> fooChecker.check(foo, 'b') # doctest: +ELLIPSIS - Traceback (most recent call last): - ForbiddenAttribute: ('b', <zope.security.decorator.Foo object ...>) + >>> try: + ... fooChecker.check(foo, 'b') # doctest: +ELLIPSIS + ... except ForbiddenAttribute as e: + ... e + ForbiddenAttribute('b', <Foo object ...>) and that a `Wrapper` object uses `wrappeChecker`: @@ -64,9 +67,11 @@ and that a `Wrapper` object uses `wrappeChecker`: >>> selectChecker(wrapper) is wrapperChecker True >>> wrapperChecker.check(wrapper, 'b') - >>> wrapperChecker.check(wrapper, 'a') # doctest: +ELLIPSIS - Traceback (most recent call last): - ForbiddenAttribute: ('a', <zope.security.decorator.Foo object ...>) + >>> try: + ... wrapperChecker.check(wrapper, 'a') # doctest: +ELLIPSIS + ... except ForbiddenAttribute as e: + ... e + ForbiddenAttribute('a', <Foo object ...>) (Note that the object description says `Foo` because the object is a proxy and generally looks and acts like the object it's proxying.) @@ -94,9 +99,11 @@ illustrate, we'll proxify `foo`: >>> secure_foo = ProxyFactory(foo) >>> secure_foo.a 'a' - >>> secure_foo.b # doctest: +ELLIPSIS - Traceback (most recent call last): - ForbiddenAttribute: ('b', <zope.security.decorator.Foo object ...>) + >>> try: + ... secure_foo.b # doctest: +ELLIPSIS + ... except ForbiddenAttribute as e: + ... e + ForbiddenAttribute('b', <Foo object ...>) when we wrap the secured `foo`: diff --git a/docs/api/zcml.rst b/docs/api/zcml.rst index 5fdc3a0..7c03405 100644 --- a/docs/api/zcml.rst +++ b/docs/api/zcml.rst @@ -64,10 +64,13 @@ Now let's see whether validation works alright >>> field._validate('zope.ManageCode') >>> context._actions[0]['args'] (None, 'zope.foo') - >>> field._validate('3 foo') - Traceback (most recent call last): - ... - InvalidId: 3 foo + + >>> from zope.schema.interfaces import InvalidId + >>> try: + ... field._validate('3 foo') + ... except InvalidId as e: + ... e + InvalidId('3 foo') zope.Public is always valid >>> field._validate('zope.Public') |