Skip to content
Myr

_compat.py

This file

Path

src/jinja2/_compat.py

132 lines · 3.1 KiB · Python

Last changed by

Merge pull request #1133 from pallets/revert-renameDavid Lordbefore adoption15337f7

History of this file

Take it

1# -*- coding: utf-8 -*-2# flake8: noqa3import marshal4import sys56PY2 = sys.version_info[0] == 27PYPY = hasattr(sys, "pypy_translation_info")8_identity = lambda x: x910if not PY2:11    unichr = chr12    range_type = range13    text_type = str14    string_types = (str,)15    integer_types = (int,)1617    iterkeys = lambda d: iter(d.keys())18    itervalues = lambda d: iter(d.values())19    iteritems = lambda d: iter(d.items())2021    import pickle22    from io import BytesIO, StringIO2324    NativeStringIO = StringIO2526    def reraise(tp, value, tb=None):27        if value.__traceback__ is not tb:28            raise value.with_traceback(tb)29        raise value3031    ifilter = filter32    imap = map33    izip = zip34    intern = sys.intern3536    implements_iterator = _identity37    implements_to_string = _identity38    encode_filename = _identity3940    marshal_dump = marshal.dump41    marshal_load = marshal.load4243else:44    unichr = unichr45    text_type = unicode46    range_type = xrange47    string_types = (str, unicode)48    integer_types = (int, long)4950    iterkeys = lambda d: d.iterkeys()51    itervalues = lambda d: d.itervalues()52    iteritems = lambda d: d.iteritems()5354    import cPickle as pickle55    from cStringIO import StringIO as BytesIO, StringIO5657    NativeStringIO = BytesIO5859    exec("def reraise(tp, value, tb=None):\n raise tp, value, tb")6061    from itertools import imap, izip, ifilter6263    intern = intern6465    def implements_iterator(cls):66        cls.next = cls.__next__67        del cls.__next__68        return cls6970    def implements_to_string(cls):71        cls.__unicode__ = cls.__str__72        cls.__str__ = lambda x: x.__unicode__().encode("utf-8")73        return cls7475    def encode_filename(filename):76        if isinstance(filename, unicode):77            return filename.encode("utf-8")78        return filename7980    def marshal_dump(code, f):81        if isinstance(f, file):82            marshal.dump(code, f)83        else:84            f.write(marshal.dumps(code))8586    def marshal_load(f):87        if isinstance(f, file):88            return marshal.load(f)89        return marshal.loads(f.read())909192def with_metaclass(meta, *bases):93    """Create a base class with a metaclass."""94    # This requires a bit of explanation: the basic idea is to make a95    # dummy metaclass for one level of class instantiation that replaces96    # itself with the actual metaclass.97    class metaclass(type):98        def __new__(cls, name, this_bases, d):99            return meta(name, bases, d)100101    return type.__new__(metaclass, "temporary_class", (), {})102103104try:105    from urllib.parse import quote_from_bytes as url_quote106except ImportError:107    from urllib import quote as url_quote108109110try:111    from collections import abc112except ImportError:113    import collections as abc114115116try:117    from os import fspath118except ImportError:119    try:120        from pathlib import PurePath121    except ImportError:122        PurePath = None123124    def fspath(path):125        if hasattr(path, "__fspath__"):126            return path.__fspath__()127128        # Python 3.5 doesn't have __fspath__ yet, use str.129        if PurePath is not None and isinstance(path, PurePath):130            return str(path)131132        return path