[Repoze-checkins] r1071 - in repoze.obob/trunk: . repoze/obob repoze/obob/tests
Chris McDonough
chrism at agendaless.com
Wed Jun 11 17:40:35 EDT 2008
Author: Chris McDonough <chrism at agendaless.com>
Date: Wed Jun 11 17:40:34 2008
New Revision: 1071
Log:
Change the contract of 'handle_exception'. As before, if this
method exists on a helper, it is called by the publisher when any
other helper step raises an exception. However, unlike its contract
in 0.3, it *must* either raise an exception *or* return a
three-tuple representing the (status, headers, and body_iter) that
will be used to send to our WSGI upstream caller. In particular, it
is no longer legal to return None from 'handle_exception'.
'handle_exception' is now a required method on helpers.
Modified:
repoze.obob/trunk/CHANGES.txt
repoze.obob/trunk/repoze/obob/publisher.py
repoze.obob/trunk/repoze/obob/tests/test_publisher.py
repoze.obob/trunk/setup.py
Modified: repoze.obob/trunk/CHANGES.txt
==============================================================================
--- repoze.obob/trunk/CHANGES.txt (original)
+++ repoze.obob/trunk/CHANGES.txt Wed Jun 11 17:40:34 2008
@@ -1,8 +1,20 @@
+0.4
+
+ Change the contract of 'handle_exception'. As before, if this
+ method exists on a helper, it is called by the publisher when any
+ other helper step raises an exception. However, unlike its contract
+ in 0.3, it *must* either raise an exception *or* return a
+ three-tuple representing the (status, headers, and body_iter) that
+ will be used to send to our WSGI upstream caller. In particular, it
+ is no longer legal to return None from 'handle_exception'.
+
+ 'handle_exception' is now a required method on helpers.
+
0.3
Allow helpers to have a 'handle_exception' method. If this method
exists on a helper, it is called by the publisher when any other
- helper step raises an exception. It is called with a single
+ helper step raises an exception. It is called with a single
argument, which will be a three-tuple, representing the type, value,
and traceback of the exception that occurred (the result of
sys.exc_info()). The 'handle_exception' method should either
Modified: repoze.obob/trunk/repoze/obob/publisher.py
==============================================================================
--- repoze.obob/trunk/repoze/obob/publisher.py (original)
+++ repoze.obob/trunk/repoze/obob/publisher.py Wed Jun 11 17:40:34 2008
@@ -1,3 +1,4 @@
+import sys
""" repoze.obob publisher: perform policy-driven graph traversal.
"""
@@ -87,11 +88,16 @@
pass
def handle_exception(self, exc_info):
- """ Handle any exceptions that happen during helper consultation
+ """ Handle any exceptions that happen during helper consultation.
+ Reraise or return a WSGI triple.
- @return None
+ @return tuple WSGI triple: (status, headers, body_iter)
"""
- pass
+ t, v, tb = exc_info
+ try:
+ raise t, v, tb
+ finally:
+ del tb
class ObobPublisher:
""" repoze graph-traversal publisher.
@@ -166,23 +172,12 @@
status, headers, body_iter = helper.map_result(result)
except:
- import sys
exc_info = sys.exc_info()
- # handle_exception may raise a new exception
- if hasattr(helper, 'handle_exception'):
- helper.handle_exception(exc_info)
- # if the helper doesn't have this method or if the
- # handle_exception method doesn't raise anything,
- # reraise the original exception
- try:
- t, v, tb = exc_info
- raise t, v, tb
- finally:
- del tb
-
+ status, headers, body_iter = helper.handle_exception(exc_info)
+
start_response(status, headers)
return body_iter
-
+
finally:
helper.teardown()
Modified: repoze.obob/trunk/repoze/obob/tests/test_publisher.py
==============================================================================
--- repoze.obob/trunk/repoze/obob/tests/test_publisher.py (original)
+++ repoze.obob/trunk/repoze/obob/tests/test_publisher.py Wed Jun 11 17:40:34 2008
@@ -76,8 +76,7 @@
pass
helper = self._makeOne()
exc_info = (MyException, MyException('foo'), None)
- result = helper.handle_exception(exc_info)
- self.assertEqual(result, None)
+ self.assertRaises(MyException, helper.handle_exception, exc_info)
class ObobPublisherTests(unittest.TestCase):
@@ -242,16 +241,24 @@
def teardown(self):
pass
def handle_exception(self, exc_info):
- t, v = exc_info[:2]
+ t, v = exc_info[:2]
self.t.append(t)
self.v.append(v)
+ return '200 OK', (), ['body']
+ started = []
+ def _start_response(status, headers):
+ started.append((status, headers))
obob = self._makeOne(helper_factory=NonRaisingHelperFactory)
environ = {'PATH_INFO': '/baz'}
- self.assertRaises(ValueError, obob, environ, None)
+ self.assertEqual(obob(environ, _start_response), ['body'])
self.assertEqual(len(NonRaisingHelperFactory.t), 1)
self.assertEqual(NonRaisingHelperFactory.t[0], ValueError)
self.assertEqual(len(NonRaisingHelperFactory.v), 1)
self.assertEqual(NonRaisingHelperFactory.v[0].args[0], 'i am raising')
+ self.assertEqual(len(started), 1)
+ status, headers = started[0]
+ self.assertEqual(status, '200 OK')
+ self.assertEqual(headers, ())
def test___call__helperfactory_handle_exception_raises(self):
class RaisingHelperFactory:
@@ -276,14 +283,3 @@
self.assertEqual(len(RaisingHelperFactory.v), 1)
self.assertEqual(RaisingHelperFactory.v[0].args[0], 'i am raising')
- def test___call__helperfactory_handle_exception_helper_has_no_method(self):
- class LegacyHelperFactory:
- def __init__(self, environ, **kw):
- pass
- def setup(self):
- raise ValueError('i am raising')
- def teardown(self):
- pass
- obob = self._makeOne(helper_factory=LegacyHelperFactory)
- environ = {'PATH_INFO': '/baz'}
- self.assertRaises(ValueError, obob, environ, None)
Modified: repoze.obob/trunk/setup.py
==============================================================================
--- repoze.obob/trunk/setup.py (original)
+++ repoze.obob/trunk/setup.py Wed Jun 11 17:40:34 2008
@@ -1,4 +1,4 @@
-__version__ = '0.3'
+__version__ = '0.4'
import os
from setuptools import setup, find_packages
More information about the Repoze-checkins
mailing list