File tree Expand file tree Collapse file tree 3 files changed +24
-1
lines changed Expand file tree Collapse file tree 3 files changed +24
-1
lines changed Original file line number Diff line number Diff line change @@ -272,7 +272,16 @@ thing, like it does for :class:`request` and :class:`session`.
272
272
273
273
Starting with Flask 0.10 this is stored on the application context and
274
274
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']
276
285
277
286
This is a proxy. See :ref: `notes-on-proxies ` for more information.
278
287
Original file line number Diff line number Diff line change @@ -250,6 +250,7 @@ requires that you pass it a response object::
250
250
This in general is less useful because at that point you can directly
251
251
start using the test client.
252
252
253
+ .. _faking-resources :
253
254
254
255
Faking Resources and Context
255
256
----------------------------
Original file line number Diff line number Diff line change @@ -1116,6 +1116,19 @@ def teardown_handler(exc):
1116
1116
self .assert_equal (len (errors ), 3 )
1117
1117
self .assert_equal (errors [1 ], None )
1118
1118
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
+
1119
1132
1120
1133
class SubdomainTestCase (FlaskTestCase ):
1121
1134
You can’t perform that action at this time.
0 commit comments