Merge pull request #1079 from smarkets/master
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Get rid of _tag->closure->_tag reference cycle
authored by Armin Ronacher · committed by Armin Ronacher · parents 76456dc, 4541910 · Browse files · 1 file·+26 added−24 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+26 added−24 removed
- other:+26 added−24 removed
Provenance
- Commit
7cfe833- Tree
2f03d56- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent 1
76456dc- Parent 2
4541910
1 file changed+26 added−24 removed
modified:flask/sessions.py+26 added-24 removed
50 unmodified lines51 #: The default mixin implementation just hardcodes `True` in.52 modified = True53 54 55+def _tag(value):56+ if isinstance(value, tuple):57+ return {' t': [_tag(x) for x in value]}58+ elif isinstance(value, uuid.UUID):59+ return {' u': value.hex}60+ elif isinstance(value, bytes):61+ return {' b': b64encode(value).decode('ascii')}62+ elif callable(getattr(value, '__html__', None)):63+ return {' m': text_type(value.__html__())}64+ elif isinstance(value, list):65+ return [_tag(x) for x in value]66+ elif isinstance(value, datetime):67+ return {' d': http_date(value)}68+ elif isinstance(value, dict):69+ return dict((k, _tag(v)) for k, v in iteritems(value))70+ elif isinstance(value, str):71+ try:72+ return text_type(value)73+ except UnicodeError:74+ raise UnexpectedUnicodeError(u'A byte string with '75+ u'non-ASCII data was passed to the session system '76+ u'which can only store unicode strings. Consider '77+ u'base64 encoding your string (String was %r)' % value)78+ return value79+80+81 class TaggedJSONSerializer(object):82 """A customized JSON serializer that supports a few extra types that83 we take for granted when serializing (tuples, markup objects, datetime).84 """85 86 def dumps(self, value):61- def _tag(value):62- if isinstance(value, tuple):63- return {' t': [_tag(x) for x in value]}64- elif isinstance(value, uuid.UUID):65- return {' u': value.hex}66- elif isinstance(value, bytes):67- return {' b': b64encode(value).decode('ascii')}68- elif callable(getattr(value, '__html__', None)):69- return {' m': text_type(value.__html__())}70- elif isinstance(value, list):71- return [_tag(x) for x in value]72- elif isinstance(value, datetime):73- return {' d': http_date(value)}74- elif isinstance(value, dict):75- return dict((k, _tag(v)) for k, v in iteritems(value))76- elif isinstance(value, str):77- try:78- return text_type(value)79- except UnicodeError:80- raise UnexpectedUnicodeError(u'A byte string with '81- u'non-ASCII data was passed to the session system '82- u'which can only store unicode strings. Consider '83- u'base64 encoding your string (String was %r)' % value)84- return value87 return json.dumps(_tag(value), separators=(',', ':'))88 89 def loads(self, value):90 def object_hook(obj):277 unmodified lines