[Checkins] SVN: zope.security/trunk/ Convert set checker doctests to Sphinx.

Tres Seaver cvs-admin at zope.org
Sun Dec 23 20:54:15 UTC 2012


Log message for revision 128884:
  Convert set checker doctests to Sphinx.

Changed:
  _U  zope.security/trunk/
  U   zope.security/trunk/docs/api/checker.rst
  D   zope.security/trunk/src/zope/security/tests/test_set_checkers.py

-=-
Modified: zope.security/trunk/docs/api/checker.rst
===================================================================
--- zope.security/trunk/docs/api/checker.rst	2012-12-23 20:54:14 UTC (rev 128883)
+++ zope.security/trunk/docs/api/checker.rst	2012-12-23 20:54:15 UTC (rev 128884)
@@ -4,3 +4,356 @@
 .. automodule:: zope.security.checker
    :members:
    :member-order: bysource
+
+Protections for set objects
+---------------------------
+
+we can do everything we expect to be able to do with proxied sets.
+
+.. doctest::
+
+   >>> def check_forbidden_get(object, attr):
+   ...     from zope.security.interfaces import ForbiddenAttribute
+   ...     try:
+   ...         return getattr(object, attr)
+   ...     except ForbiddenAttribute, e:
+   ...         return 'ForbiddenAttribute: %s' % e[0]
+   >>> from zope.security.checker import ProxyFactory
+   >>> from zope.security.interfaces import ForbiddenAttribute
+   >>> us = set((1, 2))
+   >>> s = ProxyFactory(us)
+
+   >>> check_forbidden_get(s, 'add') # Verify that we are protected
+   'ForbiddenAttribute: add'
+   >>> check_forbidden_get(s, 'remove') # Verify that we are protected
+   'ForbiddenAttribute: remove'
+   >>> check_forbidden_get(s, 'discard') # Verify that we are protected
+   'ForbiddenAttribute: discard'
+   >>> check_forbidden_get(s, 'pop') # Verify that we are protected
+   'ForbiddenAttribute: pop'
+   >>> check_forbidden_get(s, 'clear') # Verify that we are protected
+   'ForbiddenAttribute: clear'
+
+   >>> len(s)
+   2
+
+   >>> 1 in s
+   True
+
+   >>> 1 not in s
+   False
+
+   >>> s.issubset(set((1,2,3)))
+   True
+
+   >>> s.issuperset(set((1,2,3)))
+   False
+
+   >>> c = s.union(set((2, 3)))
+   >>> sorted(c)
+   [1, 2, 3]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> c = s | set((2, 3))
+   >>> sorted(c)
+   [1, 2, 3]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> c = s | ProxyFactory(set((2, 3)))
+   >>> sorted(c)
+   [1, 2, 3]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> c = set((2, 3)) | s
+   >>> sorted(c)
+   [1, 2, 3]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> c = s.intersection(set((2, 3)))
+   >>> sorted(c)
+   [2]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> c = s & set((2, 3))
+   >>> sorted(c)
+   [2]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> c = s & ProxyFactory(set((2, 3)))
+   >>> sorted(c)
+   [2]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> c = set((2, 3)) & s
+   >>> sorted(c)
+   [2]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> c = s.difference(set((2, 3)))
+   >>> sorted(c)
+   [1]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> c = s - ProxyFactory(set((2, 3)))
+   >>> sorted(c)
+   [1]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> c = s - set((2, 3))
+   >>> sorted(c)
+   [1]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> c = set((2, 3)) - s
+   >>> sorted(c)
+   [3]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> c = s.symmetric_difference(set((2, 3)))
+   >>> sorted(c)
+   [1, 3]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> c = s ^ set((2, 3))
+   >>> sorted(c)
+   [1, 3]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> c = s ^ ProxyFactory(set((2, 3)))
+   >>> sorted(c)
+   [1, 3]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> c = set((2, 3)) ^ s
+   >>> sorted(c)
+   [1, 3]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> c = s.copy()
+   >>> sorted(c)
+   [1, 2]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> str(s) == str(us)
+   True
+   
+   >>> repr(s) == repr(us)
+   True
+
+   Always available:
+
+   >>> s < us
+   False
+   >>> s > us
+   False
+   >>> s <= us
+   True
+   >>> s >= us
+   True
+   >>> s == us
+   True
+   >>> s != us
+   False
+
+Note that you can't compare proxied sets with other proxied sets
+due a limitaion in the set comparison functions which won't work
+with any kind of proxy.
+   
+.. doctest::
+
+   >>> bool(s)
+   True
+   >>> s.__class__ == set
+   True
+
+Likewise with proxied frozensets.
+
+.. doctest::
+
+   >>> def check_forbidden_get(object, attr):
+   ...     from zope.security.interfaces import ForbiddenAttribute
+   ...     try:
+   ...         return getattr(object, attr)
+   ...     except ForbiddenAttribute, e:
+   ...         return 'ForbiddenAttribute: %s' % e[0]
+   >>> from zope.security.checker import ProxyFactory
+   >>> from zope.security.interfaces import ForbiddenAttribute
+   >>> us = frozenset((1, 2))
+   >>> s = ProxyFactory(us)
+
+   >>> check_forbidden_get(s, 'add') # Verify that we are protected
+   'ForbiddenAttribute: add'
+   >>> check_forbidden_get(s, 'remove') # Verify that we are protected
+   'ForbiddenAttribute: remove'
+   >>> check_forbidden_get(s, 'discard') # Verify that we are protected
+   'ForbiddenAttribute: discard'
+   >>> check_forbidden_get(s, 'pop') # Verify that we are protected
+   'ForbiddenAttribute: pop'
+   >>> check_forbidden_get(s, 'clear') # Verify that we are protected
+   'ForbiddenAttribute: clear'
+
+   >>> len(s)
+   2
+
+   >>> 1 in s
+   True
+
+   >>> 1 not in s
+   False
+
+   >>> s.issubset(frozenset((1,2,3)))
+   True
+
+   >>> s.issuperset(frozenset((1,2,3)))
+   False
+
+   >>> c = s.union(frozenset((2, 3)))
+   >>> sorted(c)
+   [1, 2, 3]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> c = s | frozenset((2, 3))
+   >>> sorted(c)
+   [1, 2, 3]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> c = s | ProxyFactory(frozenset((2, 3)))
+   >>> sorted(c)
+   [1, 2, 3]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> c = frozenset((2, 3)) | s
+   >>> sorted(c)
+   [1, 2, 3]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> c = s.intersection(frozenset((2, 3)))
+   >>> sorted(c)
+   [2]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> c = s & frozenset((2, 3))
+   >>> sorted(c)
+   [2]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> c = s & ProxyFactory(frozenset((2, 3)))
+   >>> sorted(c)
+   [2]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> c = frozenset((2, 3)) & s
+   >>> sorted(c)
+   [2]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> c = s.difference(frozenset((2, 3)))
+   >>> sorted(c)
+   [1]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> c = s - ProxyFactory(frozenset((2, 3)))
+   >>> sorted(c)
+   [1]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> c = s - frozenset((2, 3))
+   >>> sorted(c)
+   [1]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> c = frozenset((2, 3)) - s
+   >>> sorted(c)
+   [3]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> c = s.symmetric_difference(frozenset((2, 3)))
+   >>> sorted(c)
+   [1, 3]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> c = s ^ frozenset((2, 3))
+   >>> sorted(c)
+   [1, 3]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> c = s ^ ProxyFactory(frozenset((2, 3)))
+   >>> sorted(c)
+   [1, 3]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> c = frozenset((2, 3)) ^ s
+   >>> sorted(c)
+   [1, 3]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> c = s.copy()
+   >>> sorted(c)
+   [1, 2]
+   >>> check_forbidden_get(c, 'add')
+   'ForbiddenAttribute: add'
+
+   >>> str(s) == str(us)
+   True
+   
+   >>> repr(s) == repr(us)
+   True
+
+   Always available:
+
+   >>> s < us
+   False
+   >>> s > us
+   False
+   >>> s <= us
+   True
+   >>> s >= us
+   True
+   >>> s == us
+   True
+   >>> s != us
+   False
+
+Note that you can't compare proxied sets with other proxied sets
+due a limitaion in the frozenset comparison functions which won't work
+with any kind of proxy.
+   
+.. doctest::
+
+   >>> bool(s)
+   True
+   >>> s.__class__ == frozenset
+   True

Deleted: zope.security/trunk/src/zope/security/tests/test_set_checkers.py
===================================================================
--- zope.security/trunk/src/zope/security/tests/test_set_checkers.py	2012-12-23 20:54:14 UTC (rev 128883)
+++ zope.security/trunk/src/zope/security/tests/test_set_checkers.py	2012-12-23 20:54:15 UTC (rev 128884)
@@ -1,206 +0,0 @@
-##############################################################################
-#
-# Copyright (c) 2003 Zope Foundation 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.
-#
-##############################################################################
-"""Test checkers for standard types
-
-This is a test of the assertions made in
-zope.security.checkers._default_checkers.
-"""
-import unittest
-
-
-def check_forbidden_get(object, attr):
-    from zope.security.interfaces import ForbiddenAttribute
-    try:
-        return getattr(object, attr)
-    except ForbiddenAttribute, e:
-        return 'ForbiddenAttribute: %s' % e[0]
-
-def test_set():
-    """Test that we can do everything we expect to be able to do
-
-    with proxied sets.
-
-    >>> from zope.security.checker import ProxyFactory
-    >>> from zope.security.interfaces import ForbiddenAttribute
-    >>> us = set((1, 2))
-    >>> s = ProxyFactory(us)
-
-    >>> check_forbidden_get(s, 'add') # Verify that we are protected
-    'ForbiddenAttribute: add'
-    >>> check_forbidden_get(s, 'remove') # Verify that we are protected
-    'ForbiddenAttribute: remove'
-    >>> check_forbidden_get(s, 'discard') # Verify that we are protected
-    'ForbiddenAttribute: discard'
-    >>> check_forbidden_get(s, 'pop') # Verify that we are protected
-    'ForbiddenAttribute: pop'
-    >>> check_forbidden_get(s, 'clear') # Verify that we are protected
-    'ForbiddenAttribute: clear'
-
-    >>> len(s)
-    2
-
-    >>> 1 in s
-    True
-
-    >>> 1 not in s
-    False
-
-    >>> s.issubset(set((1,2,3)))
-    True
-
-    >>> s.issuperset(set((1,2,3)))
-    False
-
-    >>> c = s.union(set((2, 3)))
-    >>> sorted(c)
-    [1, 2, 3]
-    >>> check_forbidden_get(c, 'add')
-    'ForbiddenAttribute: add'
-
-    >>> c = s | set((2, 3))
-    >>> sorted(c)
-    [1, 2, 3]
-    >>> check_forbidden_get(c, 'add')
-    'ForbiddenAttribute: add'
-
-    >>> c = s | ProxyFactory(set((2, 3)))
-    >>> sorted(c)
-    [1, 2, 3]
-    >>> check_forbidden_get(c, 'add')
-    'ForbiddenAttribute: add'
-
-    >>> c = set((2, 3)) | s
-    >>> sorted(c)
-    [1, 2, 3]
-    >>> check_forbidden_get(c, 'add')
-    'ForbiddenAttribute: add'
-
-    >>> c = s.intersection(set((2, 3)))
-    >>> sorted(c)
-    [2]
-    >>> check_forbidden_get(c, 'add')
-    'ForbiddenAttribute: add'
-
-    >>> c = s & set((2, 3))
-    >>> sorted(c)
-    [2]
-    >>> check_forbidden_get(c, 'add')
-    'ForbiddenAttribute: add'
-
-    >>> c = s & ProxyFactory(set((2, 3)))
-    >>> sorted(c)
-    [2]
-    >>> check_forbidden_get(c, 'add')
-    'ForbiddenAttribute: add'
-
-    >>> c = set((2, 3)) & s
-    >>> sorted(c)
-    [2]
-    >>> check_forbidden_get(c, 'add')
-    'ForbiddenAttribute: add'
-
-    >>> c = s.difference(set((2, 3)))
-    >>> sorted(c)
-    [1]
-    >>> check_forbidden_get(c, 'add')
-    'ForbiddenAttribute: add'
-
-    >>> c = s - ProxyFactory(set((2, 3)))
-    >>> sorted(c)
-    [1]
-    >>> check_forbidden_get(c, 'add')
-    'ForbiddenAttribute: add'
-
-    >>> c = s - set((2, 3))
-    >>> sorted(c)
-    [1]
-    >>> check_forbidden_get(c, 'add')
-    'ForbiddenAttribute: add'
-
-    >>> c = set((2, 3)) - s
-    >>> sorted(c)
-    [3]
-    >>> check_forbidden_get(c, 'add')
-    'ForbiddenAttribute: add'
-
-    >>> c = s.symmetric_difference(set((2, 3)))
-    >>> sorted(c)
-    [1, 3]
-    >>> check_forbidden_get(c, 'add')
-    'ForbiddenAttribute: add'
-
-    >>> c = s ^ set((2, 3))
-    >>> sorted(c)
-    [1, 3]
-    >>> check_forbidden_get(c, 'add')
-    'ForbiddenAttribute: add'
-
-    >>> c = s ^ ProxyFactory(set((2, 3)))
-    >>> sorted(c)
-    [1, 3]
-    >>> check_forbidden_get(c, 'add')
-    'ForbiddenAttribute: add'
-
-    >>> c = set((2, 3)) ^ s
-    >>> sorted(c)
-    [1, 3]
-    >>> check_forbidden_get(c, 'add')
-    'ForbiddenAttribute: add'
-
-    >>> c = s.copy()
-    >>> sorted(c)
-    [1, 2]
-    >>> check_forbidden_get(c, 'add')
-    'ForbiddenAttribute: add'
-
-    >>> str(s) == str(us)
-    True
-    
-    >>> repr(s) == repr(us)
-    True
-
-    Always available:
-
-    >>> s < us
-    False
-    >>> s > us
-    False
-    >>> s <= us
-    True
-    >>> s >= us
-    True
-    >>> s == us
-    True
-    >>> s != us
-    False
-
-    Note that you can't compare proxied sets with other proxied sets
-    due a limitaion in the set comparison functions which won't work
-    with any kind of proxy.
-    
-    >>> bool(s)
-    True
-    >>> s.__class__ == set
-    True
-    """
-
-def setUpFrozenSet(test):
-    test.globs['set'] = frozenset
-
-def test_suite():
-    from doctest import DocTestSuite
-    return unittest.TestSuite((
-        DocTestSuite(),
-        DocTestSuite(setUp=setUpFrozenSet),
-    ))



More information about the checkins mailing list