Commit 92754b71 authored by Tres Seaver's avatar Tres Seaver

Break out logic for converting string name to exception type.

parent 190edec1
...@@ -18,6 +18,7 @@ application-specific packages. ...@@ -18,6 +18,7 @@ application-specific packages.
$Id$ $Id$
""" """
from types import ClassType
import warnings import warnings
from zope.interface import implements from zope.interface import implements
...@@ -44,27 +45,31 @@ class MethodNotAllowed(Exception): ...@@ -44,27 +45,31 @@ class MethodNotAllowed(Exception):
class Redirect(Exception): class Redirect(Exception):
pass 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): def upgradeException(t, v):
# If a string exception is found, convert it to an equivalent # If a string exception is found, convert it to an equivalent
# exception defined either in builtins or zExceptions. If none of # exception defined either in builtins or zExceptions. If none of
# that works, tehn convert it to an InternalError and keep the # that works, tehn convert it to an InternalError and keep the
# original exception name as part of the exception value. # original exception name as part of the exception value.
import zExceptions if isinstance(t, basestring):
if not isinstance(t, basestring):
return t, v
warnings.warn('String exceptions are deprecated starting ' warnings.warn('String exceptions are deprecated starting '
'with Python 2.5 and will be removed in a ' 'with Python 2.5 and will be removed in a '
'future release', DeprecationWarning, stacklevel=2) 'future release', DeprecationWarning, stacklevel=2)
n = None etype = convertExceptionType(t)
if __builtins__.has_key(t): if etype is not None:
n = __builtins__[t] t = etype
elif hasattr(zExceptions, t):
n = getattr(zExceptions, t)
if n is not None and issubclass(n, Exception):
t = n
else: else:
v = t, v v = t, v
t = InternalError t = InternalError
......
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),
))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment