[Zope-Checkins] SVN: Zope/branches/gsoc-python-2.5/lib/python/OFS/ - Avoid raising string exceptions. If one is found, upgrade it to InternalError and keep the original exception name as part of exc_value

Sidnei da Silva sidnei at enfoldsystems.com
Fri Oct 10 10:36:40 EDT 2008


Log message for revision 91977:
   - Avoid raising string exceptions. If one is found, upgrade it to InternalError and keep the original exception name as part of exc_value

Changed:
  U   Zope/branches/gsoc-python-2.5/lib/python/OFS/CopySupport.py
  U   Zope/branches/gsoc-python-2.5/lib/python/OFS/SimpleItem.py
  U   Zope/branches/gsoc-python-2.5/lib/python/OFS/tests/testCopySupport.py

-=-
Modified: Zope/branches/gsoc-python-2.5/lib/python/OFS/CopySupport.py
===================================================================
--- Zope/branches/gsoc-python-2.5/lib/python/OFS/CopySupport.py	2008-10-10 10:24:04 UTC (rev 91976)
+++ Zope/branches/gsoc-python-2.5/lib/python/OFS/CopySupport.py	2008-10-10 14:36:38 UTC (rev 91977)
@@ -48,7 +48,7 @@
 from OFS.interfaces import ICopySource
 
 
-CopyError='Copy Error'
+class CopyError(Exception): pass
 
 copy_re = re.compile('^copy([0-9]*)_of_(.*)')
 

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 10:24:04 UTC (rev 91976)
+++ Zope/branches/gsoc-python-2.5/lib/python/OFS/SimpleItem.py	2008-10-10 14:36:38 UTC (rev 91977)
@@ -36,7 +36,7 @@
 from DocumentTemplate.ustr import ustr
 from ExtensionClass import Base
 from webdav.Resource import Resource
-from zExceptions import Redirect
+from zExceptions import Redirect, InternalError
 from zExceptions.ExceptionFormatter import format_exception
 from zope.interface import implements
 
@@ -181,16 +181,29 @@
             elif type(tb) is type('') and not error_tb:
                 error_tb = tb
 
-            # turn error_type into a string
-            if hasattr(error_type, '__name__'):
-                error_type=error_type.__name__
+            # warn if error_type is a string
+            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.
+                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)
+            else:
+                if hasattr(error_type, '__name__'):
+                    error_name = error_type.__name__
 
             if hasattr(self, '_v_eek'):
                 # Stop if there is recursion.
                 raise error_type, error_value, tb
             self._v_eek=1
 
-            if str(error_type).lower() in ('redirect',):
+            if error_name.lower() in ('redirect',):
                 raise error_type, error_value, tb
 
             if not error_message:
@@ -216,7 +229,10 @@
                 else:
                     client = aq_parent(client)
                     s=getattr(client, 'standard_error_message')
-                kwargs = {'error_type': error_type,
+                # For backward compatibility, we pass 'error_name' as
+                # 'error_type' here as historically this has always
+                # been a string.
+                kwargs = {'error_type': error_name,
                           'error_value': error_value,
                           'error_tb': error_tb,
                           'error_traceback': error_tb,

Modified: Zope/branches/gsoc-python-2.5/lib/python/OFS/tests/testCopySupport.py
===================================================================
--- Zope/branches/gsoc-python-2.5/lib/python/OFS/tests/testCopySupport.py	2008-10-10 10:24:04 UTC (rev 91976)
+++ Zope/branches/gsoc-python-2.5/lib/python/OFS/tests/testCopySupport.py	2008-10-10 14:36:38 UTC (rev 91977)
@@ -347,7 +347,7 @@
             if ce_regex is not None:
 
                 pattern = re.compile( ce_regex, re.DOTALL )
-                if pattern.search( e ) is None:
+                if pattern.search( e.args[0] ) is None:
                     self.fail( "Paste failed; didn't match pattern:\n%s" % e )
 
             else:



More information about the Zope-Checkins mailing list