copy _compat.py from flask in here (and adapt docstring)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Thomas Waldmann · committed by Thomas Waldmann · parent ceb7c7f · Browse files · 1 file·+100 added−4 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+100 added−4 removed
- other:+100 added−4 removed
Provenance
- Commit
a503520- Tree
2bbbaf3- Author
Thomas Waldmann <tw@waldmann-edv.de>- Committer
Thomas Waldmann <tw@waldmann-edv.de>- Parent
ceb7c7f
1 file changed+100 added−4 removed
modified:flask/_compat.py+100 added-4 removed
1 unmodified line2 """3 flask._compat4 ~~~~~~~~~~~~~5 6- Some py2/py3 compatibility support that is not yet available in7- "six" 1.3.0.8- There are bugs open for "six" for all this stuff, so we can remove it9- again from here as soon as we require a new enough "six" release.6+ Some py2/py3 compatibility support based on a stripped down7+ version of six so we don't have to depend on a specific version8+ of it.9 10 :copyright: (c) 2013 by Armin Ronacher.11 :license: BSD, see LICENSE for more details.12 """13+import sys14+15+PY2 = sys.version_info[0] == 216+PYPY = hasattr(sys, 'pypy_translation_info')17+_identity = lambda x: x18+19+20+if not PY2:21+ unichr = chr22+ range_type = range23+ text_type = str24+ string_types = (str,)25+26+ iterkeys = lambda d: iter(d.keys())27+ itervalues = lambda d: iter(d.values())28+ iteritems = lambda d: iter(d.items())29+30+ import pickle31+ from io import BytesIO, StringIO32+ NativeStringIO = StringIO33+34+ def reraise(tp, value, tb=None):35+ if value.__traceback__ is not tb:36+ raise value.with_traceback(tb)37+ raise value38+39+ ifilter = filter40+ imap = map41+ izip = zip42+ intern = sys.intern43+44+ implements_iterator = _identity45+ implements_to_string = _identity46+ encode_filename = _identity47+ get_next = lambda x: x.__next__48+49+else:50+ unichr = unichr51+ text_type = unicode52+ range_type = xrange53+ string_types = (str, unicode)54+55+ iterkeys = lambda d: d.iterkeys()56+ itervalues = lambda d: d.itervalues()57+ iteritems = lambda d: d.iteritems()58+59+ import cPickle as pickle60+ from cStringIO import StringIO as BytesIO, StringIO61+ NativeStringIO = BytesIO62+63+ exec('def reraise(tp, value, tb=None):\n raise tp, value, tb')64+65+ from itertools import imap, izip, ifilter66+ intern = intern67+68+ def implements_iterator(cls):69+ cls.next = cls.__next__70+ del cls.__next__71+ return cls72+73+ def implements_to_string(cls):74+ cls.__unicode__ = cls.__str__75+ cls.__str__ = lambda x: x.__unicode__().encode('utf-8')76+ return cls77+78+ get_next = lambda x: x.next79+80+ def encode_filename(filename):81+ if isinstance(filename, unicode):82+ return filename.encode('utf-8')83+ return filename84+85+86+def with_metaclass(meta, *bases):87+ # This requires a bit of explanation: the basic idea is to make a88+ # dummy metaclass for one level of class instanciation that replaces89+ # itself with the actual metaclass. Because of internal type checks90+ # we also need to make sure that we downgrade the custom metaclass91+ # for one level to something closer to type (that's why __call__ and92+ # __init__ comes back from type etc.).93+ #94+ # This has the advantage over six.with_metaclass in that it does not95+ # introduce dummy classes into the final MRO.96+ class metaclass(meta):97+ __call__ = type.__call__98+ __init__ = type.__init__99+ def __new__(cls, name, this_bases, d):100+ if this_bases is None:101+ return type.__new__(cls, name, (), d)102+ return meta(name, bases, d)103+ return metaclass('temporary_class', None, {})104+105+106+try:107+ from urllib.parse import quote_from_bytes as url_quote108+except ImportError:109+ from urllib import quote as url_quote