Request local objects now fail properly with a RuntimeError. This fixes #105
This commit is contained in:
parent
d17b6d738a
commit
faa1c71e45
4 changed files with 26 additions and 5 deletions
2
CHANGES
2
CHANGES
|
|
@ -11,6 +11,8 @@ Release date to be announced, codename to be selected
|
||||||
- Added :meth:`~flask.Flask.make_default_options_response`
|
- Added :meth:`~flask.Flask.make_default_options_response`
|
||||||
which can be used by subclasses to alter the default
|
which can be used by subclasses to alter the default
|
||||||
behaviour for `OPTIONS` responses.
|
behaviour for `OPTIONS` responses.
|
||||||
|
- Unbound locals now raise a proper :exc:`RuntimeError` instead
|
||||||
|
of an :exc:`AttributeError`.
|
||||||
|
|
||||||
Version 0.6.1
|
Version 0.6.1
|
||||||
-------------
|
-------------
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,14 @@ installation, make sure to pass it the ``-U`` parameter::
|
||||||
|
|
||||||
$ easy_install -U Flask
|
$ 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
|
Version 0.6
|
||||||
-----------
|
-----------
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,11 +10,18 @@
|
||||||
:license: BSD, see LICENSE for more details.
|
:license: BSD, see LICENSE for more details.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from functools import partial
|
||||||
from werkzeug import LocalStack, LocalProxy
|
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
|
# context locals
|
||||||
_request_ctx_stack = LocalStack()
|
_request_ctx_stack = LocalStack()
|
||||||
current_app = LocalProxy(lambda: _request_ctx_stack.top.app)
|
current_app = LocalProxy(partial(_lookup_object, 'app'))
|
||||||
request = LocalProxy(lambda: _request_ctx_stack.top.request)
|
request = LocalProxy(partial(_lookup_object, 'request'))
|
||||||
session = LocalProxy(lambda: _request_ctx_stack.top.session)
|
session = LocalProxy(partial(_lookup_object, 'session'))
|
||||||
g = LocalProxy(lambda: _request_ctx_stack.top.g)
|
g = LocalProxy(partial(_lookup_object, 'g'))
|
||||||
|
|
|
||||||
|
|
@ -72,7 +72,7 @@ class ContextTestCase(unittest.TestCase):
|
||||||
ctx.pop()
|
ctx.pop()
|
||||||
try:
|
try:
|
||||||
index()
|
index()
|
||||||
except AttributeError:
|
except RuntimeError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
assert 0, 'expected runtime error'
|
assert 0, 'expected runtime error'
|
||||||
|
|
@ -469,6 +469,10 @@ class BasicFunctionalityTestCase(unittest.TestCase):
|
||||||
else:
|
else:
|
||||||
assert "Expected ValueError"
|
assert "Expected ValueError"
|
||||||
|
|
||||||
|
def test_request_locals(self):
|
||||||
|
self.assertEqual(repr(flask.g), '<LocalProxy unbound>')
|
||||||
|
self.assertFalse(flask.g)
|
||||||
|
|
||||||
|
|
||||||
class JSONTestCase(unittest.TestCase):
|
class JSONTestCase(unittest.TestCase):
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue