Commit 462dbb4e authored by Sidnei da Silva's avatar Sidnei da Silva

- Provide a helper function to upgrade a string exception to a real exception.

parent 6a5dfc32
...@@ -26,8 +26,12 @@ ...@@ -26,8 +26,12 @@
__rcs_id__='$Id$' __rcs_id__='$Id$'
__version__='$Revision: 1.13 $'[11:-2] __version__='$Revision: 1.13 $'[11:-2]
from zExceptions import upgradeException
from DT_Util import parse_params, name_param, render_blocks, str from DT_Util import parse_params, name_param, render_blocks, str
class InvalidErrorTypeExpression(Exception):
pass
class Raise: class Raise:
blockContinuations=() blockContinuations=()
name='raise' name='raise'
...@@ -44,15 +48,17 @@ class Raise: ...@@ -44,15 +48,17 @@ class Raise:
expr=self.expr expr=self.expr
if expr is None: if expr is None:
t=self.__name__ t=self.__name__
if t[-5:]=='Error' and __builtins__.has_key(t):
t=__builtins__[t]
else: else:
try: t=expr.eval(md) try: t=expr.eval(md)
except: t='Invalid Error Type Expression' except: t=InvalidErrorTypeExpression
try: v=render_blocks(self.section,md) try: v=render_blocks(self.section,md)
except: v='Invalid Error Value' 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 raise t, v
__call__=render __call__=render
...@@ -36,7 +36,7 @@ from DocumentTemplate.html_quote import html_quote ...@@ -36,7 +36,7 @@ from DocumentTemplate.html_quote import html_quote
from DocumentTemplate.ustr import ustr from DocumentTemplate.ustr import ustr
from ExtensionClass import Base from ExtensionClass import Base
from webdav.Resource import Resource from webdav.Resource import Resource
from zExceptions import Redirect, InternalError from zExceptions import Redirect, upgradeException
from zExceptions.ExceptionFormatter import format_exception from zExceptions.ExceptionFormatter import format_exception
from zope.interface import implements from zope.interface import implements
...@@ -185,15 +185,10 @@ class Item(Base, Resource, CopySource, App.Management.Tabs, Traversable, ...@@ -185,15 +185,10 @@ class Item(Base, Resource, CopySource, App.Management.Tabs, Traversable,
error_name = 'Unknown' error_name = 'Unknown'
if isinstance(error_type, basestring): if isinstance(error_type, basestring):
# String Exceptions are deprecated on Python 2.5 and # String Exceptions are deprecated on Python 2.5 and
# plain won't work at all on Python 2.6. So upgrade it # plain won't work at all on Python 2.6. So try to upgrade it
# to an InternalError exception but keep the original # to a real exception.
# exception in the value.
error_name = error_type error_name = error_type
error_type = InternalError error_type, error_value = upgradeException(error_type, error_value)
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: else:
if hasattr(error_type, '__name__'): if hasattr(error_type, '__name__'):
error_name = error_type.__name__ error_name = error_type.__name__
......
...@@ -18,12 +18,13 @@ application-specific packages. ...@@ -18,12 +18,13 @@ application-specific packages.
$Id$ $Id$
""" """
from unauthorized import Unauthorized import warnings
from zope.interface import implements from zope.interface import implements
from zope.interface.common.interfaces import IException from zope.interface.common.interfaces import IException
from zope.publisher.interfaces import INotFound from zope.publisher.interfaces import INotFound
from zope.security.interfaces import IForbidden from zope.security.interfaces import IForbidden
from zExceptions.unauthorized import Unauthorized
class BadRequest(Exception): class BadRequest(Exception):
implements(IException) implements(IException)
...@@ -42,3 +43,29 @@ class MethodNotAllowed(Exception): ...@@ -42,3 +43,29 @@ class MethodNotAllowed(Exception):
class Redirect(Exception): class Redirect(Exception):
pass 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
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