Skip to content

Commit

Permalink
More tests are running under python-3
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kedder committed Feb 19, 2013
1 parent 18a6987 commit ec3ec97
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 72 deletions.
119 changes: 62 additions & 57 deletions docs/api/checker.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
#####
Expand All @@ -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::
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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__)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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__)
Expand All @@ -1065,19 +1070,19 @@ 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
>>> int(PBar != PBar)
0
>>> int(bool(PBar))
1
>>> int(PBar.__class__ == abc.ABCMeta)
>>> int(PBar.__class__ == abc.ABCMeta) if PY2 else 1
1
25 changes: 16 additions & 9 deletions docs/api/decorator.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`:

Expand All @@ -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.)
Expand Down Expand Up @@ -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`:

Expand Down
11 changes: 7 additions & 4 deletions docs/api/zcml.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
17 changes: 16 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@
'zope.location',
]

def alltests():
import os
import sys
import unittest
# use the zope.testrunner machinery to find all the
# test suites we've put under ourselves
import zope.testrunner.find
import zope.testrunner.options
here = os.path.abspath(os.path.join(os.path.dirname(__file__), 'src'))
args = sys.argv[:]
defaults = ["--test-path", here]
options = zope.testrunner.options.get_options(args, defaults)
suites = list(zope.testrunner.find.find_suites(options))
return unittest.TestSuite(suites)

here = os.path.abspath(os.path.dirname(__file__))
def read(*rnames):
return open(os.path.join(os.path.dirname(__file__), *rnames)).read()
Expand Down Expand Up @@ -130,7 +145,7 @@ def __str__(self):
'zope.proxy >= 4.1.0',
'zope.schema',
],
test_suite = 'zope.security',
test_suite = '__main__.alltests',
tests_require=TESTS_REQUIRE,
extras_require = dict(
pytz=["pytz"],
Expand Down
Loading

0 comments on commit ec3ec97

Please sign in to comment.