Skip to content

Commit 9307348

Browse files
committed
Documented changes on the g object some more
1 parent 1f6be7f commit 9307348

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

docs/api.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,16 @@ thing, like it does for :class:`request` and :class:`session`.
272272

273273
Starting with Flask 0.10 this is stored on the application context and
274274
no longer on the request context which means it becomes available if
275-
only the application context is bound and not yet a request.
275+
only the application context is bound and not yet a request. This
276+
is especially useful when combined with the :ref:`faking-resources`
277+
pattern for testing.
278+
279+
Additionally as of 0.10 you can use the subscription operator syntax to
280+
get an attribute or `None` if it's not set. These two usages are now
281+
equivalent::
282+
283+
user = getattr(flask.g, 'user', None)
284+
user = flask.g['user']
276285

277286
This is a proxy. See :ref:`notes-on-proxies` for more information.
278287

docs/testing.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ requires that you pass it a response object::
250250
This in general is less useful because at that point you can directly
251251
start using the test client.
252252

253+
.. _faking-resources:
253254

254255
Faking Resources and Context
255256
----------------------------

flask/testsuite/basic.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,19 @@ def teardown_handler(exc):
11161116
self.assert_equal(len(errors), 3)
11171117
self.assert_equal(errors[1], None)
11181118

1119+
def test_subscript_syntax_on_g(self):
1120+
app = flask.Flask(__name__)
1121+
app.testing = True
1122+
1123+
with app.app_context():
1124+
self.assert_equal(flask.g['x'], None)
1125+
flask.g.x = 42
1126+
self.assert_equal(flask.g['x'], 42)
1127+
self.assert_equal(flask.g.x, 42)
1128+
flask.g['x'] = 23
1129+
self.assert_equal(flask.g['x'], 23)
1130+
self.assert_equal(flask.g.x, 23)
1131+
11191132

11201133
class SubdomainTestCase(FlaskTestCase):
11211134

0 commit comments

Comments
 (0)