11cb0ef41Sopenharmony_ci# -*- coding: utf-8 -*-
21cb0ef41Sopenharmony_ci"""
31cb0ef41Sopenharmony_ci    jinja2._compat
41cb0ef41Sopenharmony_ci    ~~~~~~~~~~~~~~
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci    Some py2/py3 compatibility support based on a stripped down
71cb0ef41Sopenharmony_ci    version of six so we don't have to depend on a specific version
81cb0ef41Sopenharmony_ci    of it.
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci    :copyright: Copyright 2013 by the Jinja team, see AUTHORS.
111cb0ef41Sopenharmony_ci    :license: BSD, see LICENSE for details.
121cb0ef41Sopenharmony_ci"""
131cb0ef41Sopenharmony_ciimport sys
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciPY2 = sys.version_info[0] == 2
161cb0ef41Sopenharmony_ciPYPY = hasattr(sys, 'pypy_translation_info')
171cb0ef41Sopenharmony_ci_identity = lambda x: x
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciif not PY2:
211cb0ef41Sopenharmony_ci    unichr = chr
221cb0ef41Sopenharmony_ci    range_type = range
231cb0ef41Sopenharmony_ci    text_type = str
241cb0ef41Sopenharmony_ci    string_types = (str,)
251cb0ef41Sopenharmony_ci    integer_types = (int,)
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci    iterkeys = lambda d: iter(d.keys())
281cb0ef41Sopenharmony_ci    itervalues = lambda d: iter(d.values())
291cb0ef41Sopenharmony_ci    iteritems = lambda d: iter(d.items())
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci    import pickle
321cb0ef41Sopenharmony_ci    from io import BytesIO, StringIO
331cb0ef41Sopenharmony_ci    NativeStringIO = StringIO
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci    def reraise(tp, value, tb=None):
361cb0ef41Sopenharmony_ci        if value.__traceback__ is not tb:
371cb0ef41Sopenharmony_ci            raise value.with_traceback(tb)
381cb0ef41Sopenharmony_ci        raise value
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci    ifilter = filter
411cb0ef41Sopenharmony_ci    imap = map
421cb0ef41Sopenharmony_ci    izip = zip
431cb0ef41Sopenharmony_ci    intern = sys.intern
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci    implements_iterator = _identity
461cb0ef41Sopenharmony_ci    implements_to_string = _identity
471cb0ef41Sopenharmony_ci    encode_filename = _identity
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_cielse:
501cb0ef41Sopenharmony_ci    unichr = unichr
511cb0ef41Sopenharmony_ci    text_type = unicode
521cb0ef41Sopenharmony_ci    range_type = xrange
531cb0ef41Sopenharmony_ci    string_types = (str, unicode)
541cb0ef41Sopenharmony_ci    integer_types = (int, long)
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci    iterkeys = lambda d: d.iterkeys()
571cb0ef41Sopenharmony_ci    itervalues = lambda d: d.itervalues()
581cb0ef41Sopenharmony_ci    iteritems = lambda d: d.iteritems()
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci    import cPickle as pickle
611cb0ef41Sopenharmony_ci    from cStringIO import StringIO as BytesIO, StringIO
621cb0ef41Sopenharmony_ci    NativeStringIO = BytesIO
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci    exec('def reraise(tp, value, tb=None):\n raise tp, value, tb')
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci    from itertools import imap, izip, ifilter
671cb0ef41Sopenharmony_ci    intern = intern
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci    def implements_iterator(cls):
701cb0ef41Sopenharmony_ci        cls.next = cls.__next__
711cb0ef41Sopenharmony_ci        del cls.__next__
721cb0ef41Sopenharmony_ci        return cls
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci    def implements_to_string(cls):
751cb0ef41Sopenharmony_ci        cls.__unicode__ = cls.__str__
761cb0ef41Sopenharmony_ci        cls.__str__ = lambda x: x.__unicode__().encode('utf-8')
771cb0ef41Sopenharmony_ci        return cls
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci    def encode_filename(filename):
801cb0ef41Sopenharmony_ci        if isinstance(filename, unicode):
811cb0ef41Sopenharmony_ci            return filename.encode('utf-8')
821cb0ef41Sopenharmony_ci        return filename
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_cidef with_metaclass(meta, *bases):
861cb0ef41Sopenharmony_ci    """Create a base class with a metaclass."""
871cb0ef41Sopenharmony_ci    # This requires a bit of explanation: the basic idea is to make a
881cb0ef41Sopenharmony_ci    # dummy metaclass for one level of class instantiation that replaces
891cb0ef41Sopenharmony_ci    # itself with the actual metaclass.
901cb0ef41Sopenharmony_ci    class metaclass(type):
911cb0ef41Sopenharmony_ci        def __new__(cls, name, this_bases, d):
921cb0ef41Sopenharmony_ci            return meta(name, bases, d)
931cb0ef41Sopenharmony_ci    return type.__new__(metaclass, 'temporary_class', (), {})
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_citry:
971cb0ef41Sopenharmony_ci    from urllib.parse import quote_from_bytes as url_quote
981cb0ef41Sopenharmony_ciexcept ImportError:
991cb0ef41Sopenharmony_ci    from urllib import quote as url_quote
100