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.
$Id$
"""
from types import ClassType
import warnings
from zope.interface import implements
......@@ -44,28 +45,32 @@ class MethodNotAllowed(Exception):
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 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)
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)
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
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