[Checkins] SVN: Zope/branches/gsoc-python-2.5/lib/python/ - Provide a helper function to upgrade a string exception to a real exception.

Sidnei da Silva sidnei at enfoldsystems.com
Fri Oct 10 11:06:23 EDT 2008


Log message for revision 91983:
   - Provide a helper function to upgrade a string exception to a real exception.

Changed:
  U   Zope/branches/gsoc-python-2.5/lib/python/DocumentTemplate/DT_Raise.py
  U   Zope/branches/gsoc-python-2.5/lib/python/OFS/SimpleItem.py
  U   Zope/branches/gsoc-python-2.5/lib/python/zExceptions/__init__.py

-=-
Modified: Zope/branches/gsoc-python-2.5/lib/python/DocumentTemplate/DT_Raise.py
===================================================================
--- Zope/branches/gsoc-python-2.5/lib/python/DocumentTemplate/DT_Raise.py	2008-10-10 15:06:14 UTC (rev 91982)
+++ Zope/branches/gsoc-python-2.5/lib/python/DocumentTemplate/DT_Raise.py	2008-10-10 15:06:23 UTC (rev 91983)
@@ -26,8 +26,12 @@
 __rcs_id__='$Id$'
 __version__='$Revision: 1.13 $'[11:-2]
 
+from zExceptions import upgradeException
 from DT_Util import parse_params, name_param, render_blocks, str
 
+class InvalidErrorTypeExpression(Exception):
+    pass
+
 class Raise:
     blockContinuations=()
     name='raise'
@@ -44,15 +48,17 @@
         expr=self.expr
         if expr is None:
             t=self.__name__
-            if t[-5:]=='Error' and __builtins__.has_key(t):
-                t=__builtins__[t]
         else:
             try: t=expr.eval(md)
-            except: t='Invalid Error Type Expression'
+            except: t=InvalidErrorTypeExpression
 
         try: v=render_blocks(self.section,md)
         except: v='Invalid Error Value'
-
+        
+        # String Exceptions are deprecated on Python 2.5 and
+        # plain won't work at all on Python 2.6. So try to upgrade it
+        # to a real exception.
+        t, v = upgradeException(t, v)
         raise t, v
 
     __call__=render

Modified: Zope/branches/gsoc-python-2.5/lib/python/OFS/SimpleItem.py
===================================================================
--- Zope/branches/gsoc-python-2.5/lib/python/OFS/SimpleItem.py	2008-10-10 15:06:14 UTC (rev 91982)
+++ Zope/branches/gsoc-python-2.5/lib/python/OFS/SimpleItem.py	2008-10-10 15:06:23 UTC (rev 91983)
@@ -36,7 +36,7 @@
 from DocumentTemplate.ustr import ustr
 from ExtensionClass import Base
 from webdav.Resource import Resource
-from zExceptions import Redirect, InternalError
+from zExceptions import Redirect, upgradeException
 from zExceptions.ExceptionFormatter import format_exception
 from zope.interface import implements
 
@@ -185,15 +185,10 @@
             error_name = 'Unknown'
             if isinstance(error_type, basestring):
                 # String Exceptions are deprecated on Python 2.5 and
-                # plain won't work at all on Python 2.6. So upgrade it
-                # to an InternalError exception but keep the original
-                # exception in the value.
+                # plain won't work at all on Python 2.6. So try to upgrade it
+                # to a real exception.
                 error_name = error_type
-                error_type = InternalError
-                error_value = (error_name, error_value)
-                warnings.warn('String exceptions are deprecated starting '
-                              'with Python 2.5 and will be removed in a '
-                              'future release', DeprecationWarning)
+                error_type, error_value = upgradeException(error_type, error_value)
             else:
                 if hasattr(error_type, '__name__'):
                     error_name = error_type.__name__

Modified: Zope/branches/gsoc-python-2.5/lib/python/zExceptions/__init__.py
===================================================================
--- Zope/branches/gsoc-python-2.5/lib/python/zExceptions/__init__.py	2008-10-10 15:06:14 UTC (rev 91982)
+++ Zope/branches/gsoc-python-2.5/lib/python/zExceptions/__init__.py	2008-10-10 15:06:23 UTC (rev 91983)
@@ -18,12 +18,13 @@
 $Id$
 """
 
-from unauthorized import Unauthorized
+import warnings
 
 from zope.interface import implements
 from zope.interface.common.interfaces import IException
 from zope.publisher.interfaces import INotFound
 from zope.security.interfaces import IForbidden
+from zExceptions.unauthorized import Unauthorized
 
 class BadRequest(Exception):
     implements(IException)
@@ -42,3 +43,29 @@
 
 class Redirect(Exception):
     pass
+
+def upgradeException(t, v):
+    # If a string exception is found, convert it to an equivalent
+    # exception defined either in builtins or zExceptions. If none of
+    # that works, tehn convert it to an InternalError and keep the
+    # original exception name as part of the exception value.
+    import zExceptions
+
+    if not isinstance(t, basestring):
+        return t, v
+
+    warnings.warn('String exceptions are deprecated starting '
+                  'with Python 2.5 and will be removed in a '
+                  'future release', DeprecationWarning)
+
+    n = None
+    if __builtins__.has_key(t):
+        n = __builtins__[t]
+    elif hasattr(zExceptions, t):
+        n = getattr(zExceptions, t)
+    if n is not None and issubclass(n, Exception):
+        t = n
+    else:
+        v = t, v
+        t = InternalError
+    return t, v



More information about the Checkins mailing list