Browse files

Request local objects now fail properly with a RuntimeError. This fixes

1 parent d17b6d7 commit faa1c71e455a99e9b098aa9bb4667c07a1bab6aa @mitsuhiko mitsuhiko committed Aug 7, 2010
Showing with 26 additions and 5 deletions.
  1. +2 −0 CHANGES
  2. +8 −0 docs/upgrading.rst
  3. +11 −4 flask/globals.py
  4. +5 −1 tests/flask_tests.py
View
2 CHANGES
@@ -11,6 +11,8 @@ Release date to be announced, codename to be selected
- Added :meth:`~flask.Flask.make_default_options_response`
which can be used by subclasses to alter the default
behaviour for `OPTIONS` responses.
+- Unbound locals now raise a proper :exc:`RuntimeError` instead
+ of an :exc:`AttributeError`.
Version 0.6.1
-------------
View
8 docs/upgrading.rst
@@ -19,6 +19,14 @@ installation, make sure to pass it the ``-U`` parameter::
$ easy_install -U Flask
+Version 0.7
+-----------
+
+Due to a bug in earlier implementations the request local proxies now
+raise a :exc:`RuntimeError` instead of an :exc:`AttributeError` when they
+are unbound. If you cought these exceptions with :exc:`AttributeError`
+before, you should catch them with :exc:`RuntimeError` now.
+
Version 0.6
-----------
View
15 flask/globals.py
@@ -10,11 +10,18 @@
:license: BSD, see LICENSE for more details.
"""
+from functools import partial
from werkzeug import LocalStack, LocalProxy
+def _lookup_object(name):
+ top = _request_ctx_stack.top
+ if top is None:
+ raise RuntimeError('working outside of request context')
+ return getattr(top, name)
+
# context locals
_request_ctx_stack = LocalStack()
-current_app = LocalProxy(lambda: _request_ctx_stack.top.app)
-request = LocalProxy(lambda: _request_ctx_stack.top.request)
-session = LocalProxy(lambda: _request_ctx_stack.top.session)
-g = LocalProxy(lambda: _request_ctx_stack.top.g)
+current_app = LocalProxy(partial(_lookup_object, 'app'))
+request = LocalProxy(partial(_lookup_object, 'request'))
+session = LocalProxy(partial(_lookup_object, 'session'))
+g = LocalProxy(partial(_lookup_object, 'g'))
View
6 tests/flask_tests.py
@@ -72,7 +72,7 @@ def index():
ctx.pop()
try:
index()
- except AttributeError:
+ except RuntimeError:
pass
else:
assert 0, 'expected runtime error'
@@ -469,6 +469,10 @@ def test():
else:
assert "Expected ValueError"
+ def test_request_locals(self):
+ self.assertEqual(repr(flask.g), '<LocalProxy unbound>')
+ self.assertFalse(flask.g)
+
class JSONTestCase(unittest.TestCase):

0 comments on commit faa1c71

Please sign in to comment.