| OLD | NEW |
| 1 # -*- coding: utf-8 -*- | 1 # -*- coding: utf-8 -*- |
| 2 """ | 2 """ |
| 3 jinja2._compat | 3 jinja2._compat |
| 4 ~~~~~~~~~~~~~~ | 4 ~~~~~~~~~~~~~~ |
| 5 | 5 |
| 6 Some py2/py3 compatibility support based on a stripped down | 6 Some py2/py3 compatibility support based on a stripped down |
| 7 version of six so we don't have to depend on a specific version | 7 version of six so we don't have to depend on a specific version |
| 8 of it. | 8 of it. |
| 9 | 9 |
| 10 :copyright: Copyright 2013 by the Jinja team, see AUTHORS. | 10 :copyright: Copyright 2013 by the Jinja team, see AUTHORS. |
| 11 :license: BSD, see LICENSE for details. | 11 :license: BSD, see LICENSE for details. |
| 12 """ | 12 """ |
| 13 import sys | 13 import sys |
| 14 | 14 |
| 15 PY2 = sys.version_info[0] == 2 | 15 PY2 = sys.version_info[0] == 2 |
| 16 PYPY = hasattr(sys, 'pypy_translation_info') | 16 PYPY = hasattr(sys, 'pypy_translation_info') |
| 17 _identity = lambda x: x | 17 _identity = lambda x: x |
| 18 | 18 |
| 19 | 19 |
| 20 if not PY2: | 20 if not PY2: |
| 21 unichr = chr | 21 unichr = chr |
| 22 range_type = range | 22 range_type = range |
| 23 text_type = str | 23 text_type = str |
| 24 string_types = (str,) | 24 string_types = (str,) |
| 25 integer_types = (int,) |
| 25 | 26 |
| 26 iterkeys = lambda d: iter(d.keys()) | 27 iterkeys = lambda d: iter(d.keys()) |
| 27 itervalues = lambda d: iter(d.values()) | 28 itervalues = lambda d: iter(d.values()) |
| 28 iteritems = lambda d: iter(d.items()) | 29 iteritems = lambda d: iter(d.items()) |
| 29 | 30 |
| 30 import pickle | 31 import pickle |
| 31 from io import BytesIO, StringIO | 32 from io import BytesIO, StringIO |
| 32 NativeStringIO = StringIO | 33 NativeStringIO = StringIO |
| 33 | 34 |
| 34 def reraise(tp, value, tb=None): | 35 def reraise(tp, value, tb=None): |
| 35 if value.__traceback__ is not tb: | 36 if value.__traceback__ is not tb: |
| 36 raise value.with_traceback(tb) | 37 raise value.with_traceback(tb) |
| 37 raise value | 38 raise value |
| 38 | 39 |
| 39 ifilter = filter | 40 ifilter = filter |
| 40 imap = map | 41 imap = map |
| 41 izip = zip | 42 izip = zip |
| 42 intern = sys.intern | 43 intern = sys.intern |
| 43 | 44 |
| 44 implements_iterator = _identity | 45 implements_iterator = _identity |
| 45 implements_to_string = _identity | 46 implements_to_string = _identity |
| 46 encode_filename = _identity | 47 encode_filename = _identity |
| 47 get_next = lambda x: x.__next__ | 48 get_next = lambda x: x.__next__ |
| 48 | 49 |
| 49 else: | 50 else: |
| 50 unichr = unichr | 51 unichr = unichr |
| 51 text_type = unicode | 52 text_type = unicode |
| 52 range_type = xrange | 53 range_type = xrange |
| 53 string_types = (str, unicode) | 54 string_types = (str, unicode) |
| 55 integer_types = (int, long) |
| 54 | 56 |
| 55 iterkeys = lambda d: d.iterkeys() | 57 iterkeys = lambda d: d.iterkeys() |
| 56 itervalues = lambda d: d.itervalues() | 58 itervalues = lambda d: d.itervalues() |
| 57 iteritems = lambda d: d.iteritems() | 59 iteritems = lambda d: d.iteritems() |
| 58 | 60 |
| 59 import cPickle as pickle | 61 import cPickle as pickle |
| 60 from cStringIO import StringIO as BytesIO, StringIO | 62 from cStringIO import StringIO as BytesIO, StringIO |
| 61 NativeStringIO = BytesIO | 63 NativeStringIO = BytesIO |
| 62 | 64 |
| 63 exec('def reraise(tp, value, tb=None):\n raise tp, value, tb') | 65 exec('def reraise(tp, value, tb=None):\n raise tp, value, tb') |
| (...skipping 11 matching lines...) Expand all Loading... |
| 75 cls.__str__ = lambda x: x.__unicode__().encode('utf-8') | 77 cls.__str__ = lambda x: x.__unicode__().encode('utf-8') |
| 76 return cls | 78 return cls |
| 77 | 79 |
| 78 get_next = lambda x: x.next | 80 get_next = lambda x: x.next |
| 79 | 81 |
| 80 def encode_filename(filename): | 82 def encode_filename(filename): |
| 81 if isinstance(filename, unicode): | 83 if isinstance(filename, unicode): |
| 82 return filename.encode('utf-8') | 84 return filename.encode('utf-8') |
| 83 return filename | 85 return filename |
| 84 | 86 |
| 85 try: | |
| 86 next = next | |
| 87 except NameError: | |
| 88 def next(it): | |
| 89 return it.next() | |
| 90 | |
| 91 | 87 |
| 92 def with_metaclass(meta, *bases): | 88 def with_metaclass(meta, *bases): |
| 93 # This requires a bit of explanation: the basic idea is to make a | 89 # This requires a bit of explanation: the basic idea is to make a |
| 94 # dummy metaclass for one level of class instanciation that replaces | 90 # dummy metaclass for one level of class instanciation that replaces |
| 95 # itself with the actual metaclass. Because of internal type checks | 91 # itself with the actual metaclass. Because of internal type checks |
| 96 # we also need to make sure that we downgrade the custom metaclass | 92 # we also need to make sure that we downgrade the custom metaclass |
| 97 # for one level to something closer to type (that's why __call__ and | 93 # for one level to something closer to type (that's why __call__ and |
| 98 # __init__ comes back from type etc.). | 94 # __init__ comes back from type etc.). |
| 99 # | 95 # |
| 100 # This has the advantage over six.with_metaclass in that it does not | 96 # This has the advantage over six.with_metaclass in that it does not |
| 101 # introduce dummy classes into the final MRO. | 97 # introduce dummy classes into the final MRO. |
| 102 class metaclass(meta): | 98 class metaclass(meta): |
| 103 __call__ = type.__call__ | 99 __call__ = type.__call__ |
| 104 __init__ = type.__init__ | 100 __init__ = type.__init__ |
| 105 def __new__(cls, name, this_bases, d): | 101 def __new__(cls, name, this_bases, d): |
| 106 if this_bases is None: | 102 if this_bases is None: |
| 107 return type.__new__(cls, name, (), d) | 103 return type.__new__(cls, name, (), d) |
| 108 return meta(name, bases, d) | 104 return meta(name, bases, d) |
| 109 return metaclass('temporary_class', None, {}) | 105 return metaclass('temporary_class', None, {}) |
| 110 | 106 |
| 111 | 107 |
| 112 try: | 108 try: |
| 113 from collections import Mapping as mapping_types | |
| 114 except ImportError: | |
| 115 import UserDict | |
| 116 mapping_types = (UserDict.UserDict, UserDict.DictMixin, dict) | |
| 117 | |
| 118 | |
| 119 # common types. These do exist in the special types module too which however | |
| 120 # does not exist in IronPython out of the box. Also that way we don't have | |
| 121 # to deal with implementation specific stuff here | |
| 122 class _C(object): | |
| 123 def method(self): pass | |
| 124 def _func(): | |
| 125 yield None | |
| 126 function_type = type(_func) | |
| 127 generator_type = type(_func()) | |
| 128 method_type = type(_C().method) | |
| 129 code_type = type(_C.method.__code__) | |
| 130 try: | |
| 131 raise TypeError() | |
| 132 except TypeError: | |
| 133 _tb = sys.exc_info()[2] | |
| 134 traceback_type = type(_tb) | |
| 135 frame_type = type(_tb.tb_frame) | |
| 136 | |
| 137 | |
| 138 try: | |
| 139 from urllib.parse import quote_from_bytes as url_quote | 109 from urllib.parse import quote_from_bytes as url_quote |
| 140 except ImportError: | 110 except ImportError: |
| 141 from urllib import quote as url_quote | 111 from urllib import quote as url_quote |
| 142 | |
| 143 | |
| 144 try: | |
| 145 from thread import allocate_lock | |
| 146 except ImportError: | |
| 147 try: | |
| 148 from threading import Lock as allocate_lock | |
| 149 except ImportError: | |
| 150 from dummy_thread import allocate_lock | |
| OLD | NEW |