[Zope3-checkins] SVN: Zope3/trunk/src/zope/ Introduced a new exception, PrincipalLookupError

Jim Fulton jim at zope.com
Tue Oct 26 16:40:08 EDT 2004


Log message for revision 28259:
  Introduced a new exception, PrincipalLookupError
  
  This is to replace use of (the imfamous) NotFoundError
  when doing principal lookup.  In the course of this, found
  and fixed code that incorrectly expected getPrincipal to
  return None when a principal can't be found.
  

Changed:
  U   Zope3/trunk/src/zope/app/demo/passwdauth/__init__.py
  U   Zope3/trunk/src/zope/app/demo/passwdauth/tests.py
  U   Zope3/trunk/src/zope/app/pluggableauth/__init__.py
  U   Zope3/trunk/src/zope/app/pluggableauth/interfaces.py
  U   Zope3/trunk/src/zope/app/pluggableauth/tests/test_pluggableauth.py
  U   Zope3/trunk/src/zope/app/security/browser/principalterms.txt
  U   Zope3/trunk/src/zope/app/security/interfaces.py
  U   Zope3/trunk/src/zope/app/security/principal.py
  U   Zope3/trunk/src/zope/app/security/principalregistry.py
  U   Zope3/trunk/src/zope/app/security/tests/test_principalregistry.py
  U   Zope3/trunk/src/zope/app/security/vocabulary.py
  U   Zope3/trunk/src/zope/app/securitypolicy/browser/granting.txt
  U   Zope3/trunk/src/zope/app/undo/__init__.py
  U   Zope3/trunk/src/zope/exceptions/_notfounderror.py

-=-
Modified: Zope3/trunk/src/zope/app/demo/passwdauth/__init__.py
===================================================================
--- Zope3/trunk/src/zope/app/demo/passwdauth/__init__.py	2004-10-26 19:31:03 UTC (rev 28258)
+++ Zope3/trunk/src/zope/app/demo/passwdauth/__init__.py	2004-10-26 20:40:08 UTC (rev 28259)
@@ -26,7 +26,7 @@
 from zope.app.location import locate
 from zope.app.pluggableauth import SimplePrincipal
 from zope.app.pluggableauth.interfaces import ILoginPasswordPrincipalSource
-from zope.exceptions import NotFoundError
+from zope.app.security.interfaces import PrincipalLookupError
 from zope.interface import implements
 from interfaces import IFileBasedPrincipalSource
 
@@ -58,7 +58,7 @@
         for p in self.readPrincipals():
             if p._id == id:
                 return p
-        raise NotFoundError, id
+        raise PrincipalLookupError, id
 
     def getPrincipals(self, name):
         """See `IPrincipalSource`."""

Modified: Zope3/trunk/src/zope/app/demo/passwdauth/tests.py
===================================================================
--- Zope3/trunk/src/zope/app/demo/passwdauth/tests.py	2004-10-26 19:31:03 UTC (rev 28258)
+++ Zope3/trunk/src/zope/app/demo/passwdauth/tests.py	2004-10-26 20:40:08 UTC (rev 28259)
@@ -17,7 +17,7 @@
 """
 import os
 from zope.app.demo import passwdauth
-from zope.exceptions import NotFoundError
+from zope.app.security.interfaces import PrincipalLookupError
 from unittest import TestCase, main, makeSuite
 
 class PasswdPrincipalSourceTest(TestCase):
@@ -30,7 +30,7 @@
     def test_getPrincipal(self):
         self.assertEqual(self.source.getPrincipal('\t\tfoo1').password, 'bar1')
         self.assertEqual(self.source.getPrincipal('\t\tfoo2').password, 'bar2')
-        self.assertRaises(NotFoundError, self.source.getPrincipal, '\t\tfoo')
+        self.assertRaises(PrincipalLookupError, self.source.getPrincipal, '\t\tfoo')
 
     def test_getPrincipals(self):
         self.assertEqual(len(self.source.getPrincipals('foo')), 2)

Modified: Zope3/trunk/src/zope/app/pluggableauth/__init__.py
===================================================================
--- Zope3/trunk/src/zope/app/pluggableauth/__init__.py	2004-10-26 19:31:03 UTC (rev 28258)
+++ Zope3/trunk/src/zope/app/pluggableauth/__init__.py	2004-10-26 20:40:08 UTC (rev 28259)
@@ -30,7 +30,7 @@
 
 from zope.interface import implements
 from zope.component.interfaces import IViewFactory
-from zope.exceptions import NotFoundError
+from zope.app.security.interfaces import PrincipalLookupError
 
 from zope.app import zapi
 from zope.app.location import locate
@@ -135,7 +135,7 @@
 
         source = self.get(principal_src_id)
         if source is None:
-            raise NotFoundError, principal_src_id
+            raise PrincipalLookupError, principal_src_id
         return source.getPrincipal(id)
 
     def getPrincipals(self, name):
@@ -449,7 +449,7 @@
         try:
             return self._principals_by_number[id]
         except KeyError:
-            raise NotFoundError, id
+            raise PrincipalLookupError, id
 
     def getPrincipals(self, name):
         """ See `IPrincipalSource`.

Modified: Zope3/trunk/src/zope/app/pluggableauth/interfaces.py
===================================================================
--- Zope3/trunk/src/zope/app/pluggableauth/interfaces.py	2004-10-26 19:31:03 UTC (rev 28258)
+++ Zope3/trunk/src/zope/app/pluggableauth/interfaces.py	2004-10-26 20:40:08 UTC (rev 28259)
@@ -51,7 +51,7 @@
         """Get principal meta-data.
 
         Returns an object of type `IPrincipal` for the given principal
-        id. A ``NotFoundError`` is raised if the principal cannot be
+        id. A ``PrincipalLookupError`` is raised if the principal cannot be
         found.
 
         Note that the id has three parts, separated by tabs.  The

Modified: Zope3/trunk/src/zope/app/pluggableauth/tests/test_pluggableauth.py
===================================================================
--- Zope3/trunk/src/zope/app/pluggableauth/tests/test_pluggableauth.py	2004-10-26 19:31:03 UTC (rev 28258)
+++ Zope3/trunk/src/zope/app/pluggableauth/tests/test_pluggableauth.py	2004-10-26 20:40:08 UTC (rev 28259)
@@ -23,10 +23,9 @@
 from zope.app.tests import ztapi
 from zope.app.site.tests import placefulsetup
 
-from zope.exceptions import NotFoundError
+from zope.app.security.interfaces import PrincipalLookupError
 from zope.publisher.interfaces.http import IHTTPCredentials
 from zope.app.tests import setup
-from zope.exceptions import NotFoundError
 
 from zope.app.pluggableauth import BTreePrincipalSource, \
      SimplePrincipal, PluggableAuthenticationService, \
@@ -125,9 +124,9 @@
         auth = self._auth
         id = self._slinkp.id
         self.assertEqual(self._slinkp, auth.getPrincipal(id))
-        self.assertRaises(NotFoundError, self._fail_NoSourceId)
-        self.assertRaises(NotFoundError, self._fail_BadIdType)
-        self.assertRaises(NotFoundError, self._fail_BadIdLength)
+        self.assertRaises(PrincipalLookupError, self._fail_NoSourceId)
+        self.assertRaises(PrincipalLookupError, self._fail_BadIdType)
+        self.assertRaises(PrincipalLookupError, self._fail_BadIdLength)
 
     def testGetPrincipals(self):
         auth = self._auth

Modified: Zope3/trunk/src/zope/app/security/browser/principalterms.txt
===================================================================
--- Zope3/trunk/src/zope/app/security/browser/principalterms.txt	2004-10-26 19:31:03 UTC (rev 28258)
+++ Zope3/trunk/src/zope/app/security/browser/principalterms.txt	2004-10-26 20:40:08 UTC (rev 28259)
@@ -12,6 +12,7 @@
 
   >>> from zope.interface import implements
   >>> from zope.app.security.interfaces import IAuthenticationService
+  >>> from zope.app.security.interfaces import PrincipalLookupError
   >>> class AuthService:
   ...     implements(IAuthenticationService)
   ...     data = {'jim': 'Jim Fulton', 'stephan': 'Stephan Richter'}
@@ -20,6 +21,7 @@
   ...         title = self.data.get(id)
   ...         if title is not None:
   ...             return Principal(id, title)
+  ...         raise PrincipalLookupError
 
 Now we need to install the authentication service:
 
@@ -33,8 +35,12 @@
   >>> class PrincipalSource:
   ...     def __contains__(self, id):
   ...          auth = zapi.getService('Authentication')
-  ...          principal = auth.getPrincipal(id)
-  ...          return principal is not None
+  ...          try:
+  ...              auth.getPrincipal(id)
+  ...          except PrincipalLookupError:
+  ...              return False
+  ...          else:
+  ...              return True
 
 Now we can create an terms view:
 

Modified: Zope3/trunk/src/zope/app/security/interfaces.py
===================================================================
--- Zope3/trunk/src/zope/app/security/interfaces.py	2004-10-26 19:31:03 UTC (rev 28258)
+++ Zope3/trunk/src/zope/app/security/interfaces.py	2004-10-26 20:40:08 UTC (rev 28259)
@@ -21,7 +21,12 @@
 from zope.security.interfaces import IPrincipal, IPermission
 from zope.schema.interfaces import ISource
 
+from zope.exceptions import NotFoundError
 
+class PrincipalLookupError(NotFoundError):
+    """A prncipal could not be found for a principal id
+    """
+
 class IUnauthenticatedPrincipal(IPrincipal):
     """A principal that hasn't been authenticated.
 
@@ -106,7 +111,7 @@
         """Get principal meta-data.
 
         Returns an object of type IPrincipal for the given principal
-        id. A NotFoundError is raised if the principal cannot be
+        id. A PrincipalLookupError is raised if the principal cannot be
         found.
 
         Note that the authentication service nearest to the requested

Modified: Zope3/trunk/src/zope/app/security/principal.py
===================================================================
--- Zope3/trunk/src/zope/app/security/principal.py	2004-10-26 19:31:03 UTC (rev 28258)
+++ Zope3/trunk/src/zope/app/security/principal.py	2004-10-26 20:40:08 UTC (rev 28259)
@@ -15,7 +15,7 @@
 
 $Id$
 """
-from zope.exceptions import NotFoundError
+from zope.app.security.interfaces import PrincipalLookupError
 from zope.app import zapi
 from zope.app.servicenames import Authentication
 
@@ -24,7 +24,7 @@
     try:
         if zapi.getService(Authentication, context).getPrincipal(principal_id):
             return
-    except NotFoundError:
+    except PrincipalLookupError:
         pass
     
     raise ValueError("Undefined principal id", principal_id)

Modified: Zope3/trunk/src/zope/app/security/principalregistry.py
===================================================================
--- Zope3/trunk/src/zope/app/security/principalregistry.py	2004-10-26 19:31:03 UTC (rev 28258)
+++ Zope3/trunk/src/zope/app/security/principalregistry.py	2004-10-26 20:40:08 UTC (rev 28259)
@@ -17,7 +17,7 @@
 """
 from warnings import warn
 from zope.interface import implements
-from zope.exceptions import NotFoundError
+from zope.app.security.interfaces import PrincipalLookupError
 from zope.app import zapi
 from zope.app.security.interfaces import ILoginPassword
 from zope.app.security.interfaces import IAuthenticationService, IPrincipal
@@ -71,13 +71,11 @@
         if r is None:
             if id == self.__defaultid:
                 return self.__defaultObject
-            raise NotFoundError(id)
+            raise PrincipalLookupError(id)
         return r
 
     def getPrincipalByLogin(self, login):
-        r = self.__principalsByLogin.get(login)
-        if r is None: raise NotFoundError(login)
-        return r
+        return self.__principalsByLogin[login]
 
     def getPrincipals(self, name):
         name = name.lower()

Modified: Zope3/trunk/src/zope/app/security/tests/test_principalregistry.py
===================================================================
--- Zope3/trunk/src/zope/app/security/tests/test_principalregistry.py	2004-10-26 19:31:03 UTC (rev 28258)
+++ Zope3/trunk/src/zope/app/security/tests/test_principalregistry.py	2004-10-26 20:40:08 UTC (rev 28259)
@@ -17,7 +17,7 @@
 """
 import unittest
 from zope.interface import implements
-from zope.exceptions import NotFoundError
+from zope.app.security.interfaces import PrincipalLookupError
 from zope.publisher.interfaces.http import IHTTPCredentials
 
 from zope.app import zapi
@@ -74,7 +74,7 @@
         self.assertEqual(len(self.reg.getPrincipals('')), 2)
 
     def testUnRegistered(self):
-        self.assertRaises(NotFoundError, self.reg.getPrincipal, '3')
+        self.assertRaises(PrincipalLookupError, self.reg.getPrincipal, '3')
 
     def testDup(self):
         self.assertRaises(DuplicateId,
@@ -85,7 +85,7 @@
                           self.reg.definePrincipal,
                           '3', 'Tim Peters', 'Sir Tim Peters',
                           'tim', '123')
-        self.assertRaises(NotFoundError, self.reg.getPrincipal, '3')
+        self.assertRaises(PrincipalLookupError, self.reg.getPrincipal, '3')
         self.assertEqual(len(self.reg.getPrincipals('')), 2)
 
     def testSearch(self):
@@ -98,7 +98,7 @@
         self.assertEquals(tim.getLogin(), 'tim')
         jim = self.reg.getPrincipalByLogin('jim')
         self.assertEquals(jim.getLogin(), 'jim')
-        self.assertRaises(NotFoundError,
+        self.assertRaises(KeyError,
                           self.reg.getPrincipalByLogin, 'kim')
 
     def testValidation(self):
@@ -140,7 +140,8 @@
         self.reg.defineDefaultPrincipal("anybody", "Default Principal",
                                         "This is the default headmaster")
         self.assertEquals(self.reg.unauthenticatedPrincipal().id, "anybody")
-        self.assertRaises(NotFoundError, self.reg.getPrincipal, "everybody")
+        self.assertRaises(PrincipalLookupError,
+                          self.reg.getPrincipal, "everybody")
         p = self.reg.getPrincipal("anybody")
         self.assertEquals(p.id, "anybody")
         self.assertEquals(p.title, "Default Principal")

Modified: Zope3/trunk/src/zope/app/security/vocabulary.py
===================================================================
--- Zope3/trunk/src/zope/app/security/vocabulary.py	2004-10-26 19:31:03 UTC (rev 28258)
+++ Zope3/trunk/src/zope/app/security/vocabulary.py	2004-10-26 20:40:08 UTC (rev 28259)
@@ -23,6 +23,7 @@
 from zope.schema.vocabulary import SimpleTerm, SimpleVocabulary
 from zope.schema.interfaces import ISourceQueriables
 from zope.app.security.interfaces import IPermission
+from zope.app.security.interfaces import PrincipalLookupError
 from zope.app.component.localservice import queryNextService
 
 from interfaces import IPrincipalSource
@@ -121,6 +122,7 @@
         ...     def getPrincipal(self, id):
         ...         if id == 'bob':
         ...             return id
+        ...         raise PrincipalLookupError(id)
 
         Since we do not want to bring up the entire component architecture, we
         simply monkey patch the `getService()` method to always return our
@@ -142,8 +144,12 @@
         >>> zapi.getService = temp
         """
         auth = zapi.getService(zapi.servicenames.Authentication)
-        principal = auth.getPrincipal(id)
-        return principal is not None
+        try:
+            auth.getPrincipal(id)
+        except PrincipalLookupError:
+            return False
+        else:
+            return True
 
     def getQueriables(self):
         """Returns an iteratable of queriables. 

Modified: Zope3/trunk/src/zope/app/securitypolicy/browser/granting.txt
===================================================================
--- Zope3/trunk/src/zope/app/securitypolicy/browser/granting.txt	2004-10-26 19:31:03 UTC (rev 28258)
+++ Zope3/trunk/src/zope/app/securitypolicy/browser/granting.txt	2004-10-26 20:40:08 UTC (rev 28259)
@@ -31,6 +31,7 @@
     ...     def __init__(self, id, title): self.id, self.title = id, title
 
     >>> from zope.app.security.interfaces import IAuthenticationService
+    >>> from zope.app.security.interfaces import PrincipalLookupError
     >>> from zope.interface import implements
     >>> class AuthService:
     ...     implements(IAuthenticationService)
@@ -38,7 +39,10 @@
     ...             'stephan': Principal('stephan', 'Stephan Richter')}
     ...
     ...     def getPrincipal(self, id):
-    ...         return self.data.get(id)
+    ...         try:
+    ...             return self.data.get(id)
+    ...         except KeyError:
+    ...             raise PrincipalLookupError(id)
     ...
     ...     def getPrincipals(self, search):
     ...         return [principal 

Modified: Zope3/trunk/src/zope/app/undo/__init__.py
===================================================================
--- Zope3/trunk/src/zope/app/undo/__init__.py	2004-10-26 19:31:03 UTC (rev 28258)
+++ Zope3/trunk/src/zope/app/undo/__init__.py	2004-10-26 20:40:08 UTC (rev 28259)
@@ -17,7 +17,7 @@
 """
 from datetime import datetime
 from zope.interface import implements
-from zope.exceptions import NotFoundError
+from zope.app.security.interfaces import PrincipalLookupError
 
 from zope.app import zapi
 from zope.app.undo.interfaces import IUndoManager, UndoError
@@ -146,7 +146,7 @@
                 try:
                     entry['principal'] = principalRegistry.getPrincipal(
                         user_name)
-                except NotFoundError:
+                except PrincipalLookupError:
                     # principals might have passed away
                     pass
         return entries

Modified: Zope3/trunk/src/zope/exceptions/_notfounderror.py
===================================================================
--- Zope3/trunk/src/zope/exceptions/_notfounderror.py	2004-10-26 19:31:03 UTC (rev 28258)
+++ Zope3/trunk/src/zope/exceptions/_notfounderror.py	2004-10-26 20:40:08 UTC (rev 28259)
@@ -21,8 +21,15 @@
 class INotFoundError(IKeyError):
     pass
 
-class NotFoundError(KeyError):
+class NotFoundError(KeyError, LookupError):
     """A resource could not be found.
+
+    This exception is deprecated.  It will, over time, be replaced
+    with more specific exception types.
+
+    Eventually, when this exception type is used as a base class, it
+    will become an alias for LookupError.  Client code should not depend
+    on it extnding KeyError.
+    
     """
     implements(INotFoundError)
-



More information about the Zope3-Checkins mailing list