Skip to content
Myr

Merge pull request #1509 from ThiefMaster/appctxglobals-candy

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Add pop and setdefault to AppCtxGlobals

authored by Markus Unterwaditzer · committed by Markus Unterwaditzer · parents 595fb7e, bbaf20d · Browse files · 4 files+35 added−0 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+35 added−0 removed

  • tests:+22 added−0 removed
  • other:+13 added−0 removed

4 files+35 added−0 removed

modified:docs/api.rst+3 added
287 unmodified lines
288289 It's now also possible to use the ``in`` operator on it to see if an290 attribute is defined and it yields all keys on iteration.291292 As of 1.0 you can use :meth:`pop` and :meth:`setdefault` in the same293 way you would use them on a dictionary.294295 This is a proxy. See :ref:`notes-on-proxies` for more information.296297298Useful Functions and Classes
604 unmodified lines
modified:flask/ctx.py+9 added
29 unmodified lines
3031 def get(self, name, default=None):32 return self.__dict__.get(name, default)3334 def pop(self, name, default=_sentinel):35 if default is _sentinel:36 return self.__dict__.pop(name)37 else:38 return self.__dict__.pop(name, default)3940 def setdefault(self, name, default=None):41 self.__dict__.setdefault(name, default)4243 def __contains__(self, item):44 return item in self.__dict__4546 def __iter__(self):
362 unmodified lines
modified:tests/test_appctx.py+22 added
91 unmodified lines
92 pass9394 assert cleanup_stuff == [None]9596def test_app_ctx_globals_methods():97 app = flask.Flask(__name__)98 with app.app_context():99 # get100 assert flask.g.get('foo') is None101 assert flask.g.get('foo', 'bar') == 'bar'102 # __contains__103 assert 'foo' not in flask.g104 flask.g.foo = 'bar'105 assert 'foo' in flask.g106 # setdefault107 flask.g.setdefault('bar', 'the cake is a lie')108 flask.g.setdefault('bar', 'hello world')109 assert flask.g.bar == 'the cake is a lie'110 # pop111 assert flask.g.pop('bar') == 'the cake is a lie'112 with pytest.raises(KeyError):113 flask.g.pop('bar')114 assert flask.g.pop('bar', 'more cake') == 'more cake'115 # __iter__116 assert list(flask.g) == ['foo']117118def test_custom_app_ctx_globals_class():119 class CustomRequestGlobals(object):120 def __init__(self):121 self.spam = 'eggs'
27 unmodified lines
modified:CHANGES+1 added
65 unmodified lines
66 ``#1326``).67- Don't leak exception info of already catched exceptions to context teardown68 handlers (pull request ``#1393``).69- Allow custom Jinja environment subclasses (pull request ``#1422``).70- ``flask.g`` now has ``pop()`` and ``setdefault`` methods.7172Version 0.10.273--------------74
478 unmodified lines