[Checkins] SVN: Zope/trunk/src/zExceptions/ Break out logic for converting string name to exception type.

Tres Seaver tseaver at palladion.com
Wed May 6 11:48:10 EDT 2009


Log message for revision 99791:
  Break out logic for converting string name to exception type.

Changed:
  U   Zope/trunk/src/zExceptions/__init__.py
  A   Zope/trunk/src/zExceptions/tests/test___init__.py

-=-
Modified: Zope/trunk/src/zExceptions/__init__.py
===================================================================
--- Zope/trunk/src/zExceptions/__init__.py	2009-05-06 07:14:10 UTC (rev 99790)
+++ Zope/trunk/src/zExceptions/__init__.py	2009-05-06 15:48:10 UTC (rev 99791)
@@ -18,6 +18,7 @@
 $Id$
 """
 
+from types import ClassType
 import warnings
 
 from zope.interface import implements
@@ -44,28 +45,32 @@
 class Redirect(Exception):
     pass
 
+def convertExceptionType(name):
+    import zExceptions
+    etype = None
+    if __builtins__.has_key(name):
+        etype = __builtins__[name]
+    elif hasattr(zExceptions, name):
+        etype = getattr(zExceptions, name)
+    if (etype is not None and
+        isinstance(etype, (type, ClassType)) and
+        issubclass(etype, Exception)):
+        return etype
+
 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 isinstance(t, basestring):
+        warnings.warn('String exceptions are deprecated starting '
+                    'with Python 2.5 and will be removed in a '
+                    'future release', DeprecationWarning, stacklevel=2)
 
-    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, stacklevel=2)
-
-    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
+        etype = convertExceptionType(t)
+        if etype is not None:
+            t = etype
+        else:
+            v = t, v
+            t = InternalError
     return t, v

Added: Zope/trunk/src/zExceptions/tests/test___init__.py
===================================================================
--- Zope/trunk/src/zExceptions/tests/test___init__.py	                        (rev 0)
+++ Zope/trunk/src/zExceptions/tests/test___init__.py	2009-05-06 15:48:10 UTC (rev 99791)
@@ -0,0 +1,54 @@
+import unittest
+
+class Test_convertExceptionType(unittest.TestCase):
+
+    def _callFUT(self, name):
+        from zExceptions import convertExceptionType
+        return convertExceptionType(name)
+
+    def test_name_in___builtins__(self):
+        self.failUnless(self._callFUT('SyntaxError') is SyntaxError)
+
+    def test_name_in___builtins___not_an_exception_returns_None(self):
+        self.failUnless(self._callFUT('unichr') is None)
+
+    def test_name_in_zExceptions(self):
+        from zExceptions import Redirect
+        self.failUnless(self._callFUT('Redirect') is Redirect)
+
+    def test_name_in_zExceptions_not_an_exception_returns_None(self):
+        self.failUnless(self._callFUT('convertExceptionType') is None)
+
+class Test_upgradeException(unittest.TestCase):
+
+    def _callFUT(self, t, v):
+        from zExceptions import upgradeException
+        return upgradeException(t, v)
+
+    def test_non_string(self):
+        t, v = self._callFUT(SyntaxError, 'TEST')
+        self.assertEqual(t, SyntaxError)
+        self.assertEqual(v, 'TEST')
+
+    def test_string_in___builtins__(self):
+        t, v = self._callFUT('SyntaxError', 'TEST')
+        self.assertEqual(t, SyntaxError)
+        self.assertEqual(v, 'TEST')
+
+    def test_string_in_zExceptions(self):
+        from zExceptions import Redirect
+        t, v = self._callFUT('Redirect', 'http://example.com/')
+        self.assertEqual(t, Redirect)
+        self.assertEqual(v, 'http://example.com/')
+
+    def test_string_miss_returns_InternalError(self):
+        from zExceptions import InternalError
+        t, v = self._callFUT('Nonesuch', 'TEST')
+        self.assertEqual(t, InternalError)
+        self.assertEqual(v, ('Nonesuch', 'TEST'))
+
+def test_suite():
+    return unittest.TestSuite((
+        unittest.makeSuite(Test_convertExceptionType),
+        unittest.makeSuite(Test_upgradeException),
+        ))



More information about the Checkins mailing list