Skip to content
Myr

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

1 file+100 added−4 removed

modified:flask/_compat.py+100 added-4 removed
1 unmodified line
2"""3 flask._compat4 ~~~~~~~~~~~~~56 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.910 :copyright: (c) 2013 by Armin Ronacher.11 :license: BSD, see LICENSE for more details.12"""13import sys1415PY2 = sys.version_info[0] == 216PYPY = hasattr(sys, 'pypy_translation_info')17_identity = lambda x: x181920if not PY2:21 unichr = chr22 range_type = range23 text_type = str24 string_types = (str,)2526 iterkeys = lambda d: iter(d.keys())27 itervalues = lambda d: iter(d.values())28 iteritems = lambda d: iter(d.items())2930 import pickle31 from io import BytesIO, StringIO32 NativeStringIO = StringIO3334 def reraise(tp, value, tb=None):35 if value.__traceback__ is not tb:36 raise value.with_traceback(tb)37 raise value3839 ifilter = filter40 imap = map41 izip = zip42 intern = sys.intern4344 implements_iterator = _identity45 implements_to_string = _identity46 encode_filename = _identity47 get_next = lambda x: x.__next__4849else:50 unichr = unichr51 text_type = unicode52 range_type = xrange53 string_types = (str, unicode)5455 iterkeys = lambda d: d.iterkeys()56 itervalues = lambda d: d.itervalues()57 iteritems = lambda d: d.iteritems()5859 import cPickle as pickle60 from cStringIO import StringIO as BytesIO, StringIO61 NativeStringIO = BytesIO6263 exec('def reraise(tp, value, tb=None):\n raise tp, value, tb')6465 from itertools import imap, izip, ifilter66 intern = intern6768 def implements_iterator(cls):69 cls.next = cls.__next__70 del cls.__next__71 return cls7273 def implements_to_string(cls):74 cls.__unicode__ = cls.__str__75 cls.__str__ = lambda x: x.__unicode__().encode('utf-8')76 return cls7778 get_next = lambda x: x.next7980 def encode_filename(filename):81 if isinstance(filename, unicode):82 return filename.encode('utf-8')83 return filename848586def 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, {})104105106try:107 from urllib.parse import quote_from_bytes as url_quote108except ImportError:109 from urllib import quote as url_quote