Changed interface for `flask.g`
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This now makes it behave like it did before, it's just an object. It did however gain ``__contains__`` and ``__iter__`` and I added a ``get()`` method to fetch an attribute without raising an error. This fixes #759.
authored by Armin Ronacher · committed by Armin Ronacher · parent efd6e46 · Browse files · 4 files·+29 added−19 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+29 added−19 removed
- other:+29 added−19 removed
Provenance
- Commit
c889fbc- Tree
76fa504- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
efd6e46
4 files changed+29 added−19 removed
modified:docs/api.rst+4 added-4 removed
274 unmodified lines275 only the application context is bound and not yet a request. This276 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 to280- get an attribute or `None` if it's not set. These two usages are now281- equivalent::279+ Additionally as of 0.10 you can use the :meth:`get` method to280+ get an attribute or `None` (or the second argument) if it's not set.281+ These two usages are now equivalent::282 283 user = getattr(flask.g, 'user', None)284- user = flask.g['user']284+ user = flask.g.get('user', None)285 286 This is a proxy. See :ref:`notes-on-proxies` for more information.287 288 455 unmodified linesmodified:flask/testsuite/basic.py+15 added-6 removed
1114 unmodified lines1115 c.get('/success')1116 self.assert_equal(len(errors), 3)1117 self.assert_equal(errors[1], None)1118 1119- def test_subscript_syntax_on_g(self):1119+ def test_get_method_on_g(self):1120 app = flask.Flask(__name__)1121 app.testing = True1122 1123 with app.app_context():1124- self.assert_equal(flask.g['x'], None)1124+ self.assert_equal(flask.g.get('x'), None)1125+ self.assert_equal(flask.g.get('x', 11), 11)1126 flask.g.x = 421126- self.assert_equal(flask.g['x'], 42)1127+ self.assert_equal(flask.g.get('x'), 42)1128 self.assert_equal(flask.g.x, 42)1128- flask.g['x'] = 231129- self.assert_equal(flask.g['x'], 23)1130- self.assert_equal(flask.g.x, 23)1129 1130+ def test_g_iteration_protocol(self):1131+ app = flask.Flask(__name__)1132+ app.testing = True1133 1134+ with app.app_context():1135+ flask.g.foo = 231136+ flask.g.bar = 421137+ self.assert_equal('foo' in flask.g, True)1138+ self.assert_equal('foos' in flask.g, False)1139+ self.assert_equal(sorted(flask.g), ['bar', 'foo'])1140+1141+1142 class SubdomainTestCase(FlaskTestCase):1143 1144 def test_basic_support(self):1145 app = flask.Flask(__name__)77 unmodified linesmodified:flask/ctx.py+6 added-9 removed
22 unmodified lines23 24 class _AppCtxGlobals(object):25 """A plain object."""26 27- def __getitem__(self, name):28- try:29- return getattr(self, name)30- except AttributeError:31- return None27+ def get(self, name, default=None):28+ return self.__dict__.get(name, default)29 33- def __setitem__(self, name, value):34- setattr(self, name, value)30+ def __contains__(self, item):31+ return item in self.__dict__32 36- def __delitem__(self, name, value):37- delattr(self, name, value)33+ def __iter__(self):34+ return iter(self.__dict__)35 36 def __repr__(self):37 top = _app_ctx_stack.top38 if top is not None:356 unmodified linesmodified:CHANGES+4 added
38 unmodified lines39 default to not cause confusion with existing libraries that might expect40 ``flask.json.dumps`` to return bytestrings by default.41 - ``flask.g`` is now stored on the app context instead of the request42 context.43+- ``flask.g`` now gained a ``get()`` method for not erroring out on non44+ existing items.45+- ``flask.g`` now can be used with the ``in`` operator to see what's defined46+ and it now is iterable and will yield all attributes stored.47 - ``flask.Flask.request_globals_class`` got renamed to48 ``flask.Flask.app_ctx_globals_class`` which is a better name to what it49 does since 0.10.50 - `request`, `session` and `g` are now also added as proxies to the template398 unmodified lines