[Zope3-checkins] SVN: Zope3/branches/ZopeX3-3.0/src/zope/app/zapi/ Added a zapi.isinstance method that works with security proxies.

Jim Fulton jim at zope.com
Fri Aug 20 16:29:43 EDT 2004


Log message for revision 27195:
  
  Added a zapi.isinstance method that works with security proxies.
  
  


Changed:
  U   Zope3/branches/ZopeX3-3.0/src/zope/app/zapi/__init__.py
  U   Zope3/branches/ZopeX3-3.0/src/zope/app/zapi/interfaces.py
  A   Zope3/branches/ZopeX3-3.0/src/zope/app/zapi/tests.py


-=-
Modified: Zope3/branches/ZopeX3-3.0/src/zope/app/zapi/__init__.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/app/zapi/__init__.py	2004-08-20 19:26:59 UTC (rev 27194)
+++ Zope3/branches/ZopeX3-3.0/src/zope/app/zapi/__init__.py	2004-08-20 20:29:43 UTC (rev 27195)
@@ -20,6 +20,9 @@
 
 from interfaces import IZAPI
 from zope.interface import moduleProvides
+
+from zope.security.proxy import removeSecurityProxy
+
 from zope.app import servicenames
 from zope.app.interface import queryType
 
@@ -33,3 +36,38 @@
 from zope.app.exception.interfaces import UserError
 
 name = getName
+
+builtin_isinstance = isinstance
+def isinstance(object, cls):
+    """Test whether an object is an instance of a type.
+
+    This works even if the object is security proxied:
+
+      >>> class C1(object):
+      ...     pass
+
+      >>> c = C1()
+      >>> isinstance(c, C1)
+      True
+
+      >>> from zope.security.checker import ProxyFactory
+      >>> isinstance(ProxyFactory(c), C1)
+      True
+
+      >>> class C2(C1):
+      ...     pass
+
+      >>> c = C2()
+      >>> isinstance(c, C1)
+      True
+
+      >>> from zope.security.checker import ProxyFactory
+      >>> isinstance(ProxyFactory(c), C1)
+      True
+      
+    """
+
+    # The removeSecurityProxy call is OK here because it is *only*
+    # being used for isinstance
+    
+    return builtin_isinstance(removeSecurityProxy(object), cls)

Modified: Zope3/branches/ZopeX3-3.0/src/zope/app/zapi/interfaces.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/app/zapi/interfaces.py	2004-08-20 19:26:59 UTC (rev 27194)
+++ Zope3/branches/ZopeX3-3.0/src/zope/app/zapi/interfaces.py	2004-08-20 20:29:43 UTC (rev 27195)
@@ -69,3 +69,10 @@
         """
         
     servicenames = Attribute("Service Names")
+
+    def isinstance(object, cls):
+        """Test whether an object is an instance of the given type
+
+        This function is useful because it works even if the instance
+        is security proxied.
+        """

Added: Zope3/branches/ZopeX3-3.0/src/zope/app/zapi/tests.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/app/zapi/tests.py	2004-08-20 19:26:59 UTC (rev 27194)
+++ Zope3/branches/ZopeX3-3.0/src/zope/app/zapi/tests.py	2004-08-20 20:29:43 UTC (rev 27195)
@@ -0,0 +1,28 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (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 zapi-provided implementation
+
+$Id$
+"""
+import unittest
+from zope.testing.doctestunit import DocTestSuite
+
+def test_suite():
+    return unittest.TestSuite((
+        DocTestSuite('zope.app.zapi.__init__'),
+        ))
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')
+


Property changes on: Zope3/branches/ZopeX3-3.0/src/zope/app/zapi/tests.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native



More information about the Zope3-Checkins mailing list