Anonymous committed 4b907ae

Add normal old-style exceptions

  • Participants
  • Parent commits db8a84e

Comments (0)

Files changed (3)

File docs/index.txt

 you have Paste installed then these exceptions will be subclasses of
 the Paste exceptions.
+Note that on Python 2.4 and before, new-style classes could not be
+used as exceptions.  ``Response`` objects must be new-style classes,
+so this causes a bit of a conflict.  The base class
+``webob.exc.HTTPException`` *is* an exception, so you can catch that,
+and it *is* a WSGI application.  But you may not be able to use
+Response methods.  You can always get ``obj.exception`` to get an
+exception that you can raise, and ``obj.wsgi_response`` to get the
+``Response`` object that you can use.
 Conditional WSGI Application

File docs/news.txt

 * Python 2.3 compatibility: backport of ``reversed(seq)``
+* Made separate ``.exception`` attribute on ``webob.exc`` objects,
+  since new-style classes can't be raised as exceptions.

File webob/

     value = tag_re.sub('', value)
     return value
-class HTTPException(Exception, Response):
+class HTTPException(Exception):
+    """
+    Exception used on pre-Python-2.5, where new-style classes cannot be used as
+    an exception.
+    """
+    def __init__(self, message, wsgi_response):
+        Exception.__init__(self, message)
+        self.wsgi_response = wsgi_response
+    def __call__(self, environ, start_response):
+        return self.wsgi_response(environ, start_response)
+    @property
+    def exception(self):
+        return self
+class WSGIHTTPException(HTTPException, Response):
     ## You should set in subclasses:
     # code = 200
             return self.generate_response(environ, start_response)
         return Response.__call__(self, environ, start_response)
+    @property
+    def wsgi_response(self):
+        return self
+    @property
+    def exception(self):
+        if sys.version_info >= (2, 5):
+            return self
+        else:
+            return RealHTTPException(self.detail, self)
 class HTTPError(HTTPException):
     base class for status codes in the 400's and 500's