1 # -*- coding: utf-8 -*-
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
10 :copyright: Copyright 2013 by the Jinja team, see AUTHORS.
11 :license: BSD, see LICENSE for details.
15 PY2
= sys
.version_info
[0] == 2
16 PYPY
= hasattr(sys
, 'pypy_translation_info')
17 _identity
= lambda x
: x
26 iterkeys
= lambda d
: iter(d
.keys())
27 itervalues
= lambda d
: iter(d
.values())
28 iteritems
= lambda d
: iter(d
.items())
31 from io
import BytesIO
, StringIO
32 NativeStringIO
= StringIO
34 def reraise(tp
, value
, tb
=None):
35 if value
.__traceback
__ is not tb
:
36 raise value
.with_traceback(tb
)
44 implements_iterator
= _identity
45 implements_to_string
= _identity
46 encode_filename
= _identity
47 get_next
= lambda x
: x
.__next
__
53 string_types
= (str, unicode)
55 iterkeys
= lambda d
: d
.iterkeys()
56 itervalues
= lambda d
: d
.itervalues()
57 iteritems
= lambda d
: d
.iteritems()
59 import cPickle
as pickle
60 from cStringIO
import StringIO
as BytesIO
, StringIO
61 NativeStringIO
= BytesIO
63 exec('def reraise(tp, value, tb=None):\n raise tp, value, tb')
65 from itertools
import imap
, izip
, ifilter
68 def implements_iterator(cls
):
69 cls
.next
= cls
.__next
__
73 def implements_to_string(cls
):
74 cls
.__unicode
__ = cls
.__str
__
75 cls
.__str
__ = lambda x
: x
.__unicode
__().encode('utf-8')
78 get_next
= lambda x
: x
.next
80 def encode_filename(filename
):
81 if isinstance(filename
, unicode):
82 return filename
.encode('utf-8')
92 def with_metaclass(meta
, *bases
):
93 # This requires a bit of explanation: the basic idea is to make a
94 # dummy metaclass for one level of class instanciation that replaces
95 # itself with the actual metaclass. Because of internal type checks
96 # 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
98 # __init__ comes back from type etc.).
100 # This has the advantage over six.with_metaclass in that it does not
101 # introduce dummy classes into the final MRO.
102 class metaclass(meta
):
103 __call__
= type.__call
__
104 __init__
= type.__init
__
105 def __new__(cls
, name
, this_bases
, d
):
106 if this_bases
is None:
107 return type.__new
__(cls
, name
, (), d
)
108 return meta(name
, bases
, d
)
109 return metaclass('temporary_class', None, {})
113 from collections
import Mapping
as mapping_types
116 mapping_types
= (UserDict
.UserDict
, UserDict
.DictMixin
, dict)
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
123 def method(self
): pass
126 function_type
= type(_func
)
127 generator_type
= type(_func())
128 method_type
= type(_C().method
)
129 code_type
= type(_C
.method
.__code
__)
133 _tb
= sys
.exc_info()[2]
134 traceback_type
= type(_tb
)
135 frame_type
= type(_tb
.tb_frame
)
139 from urllib
.parse
import quote_from_bytes
as url_quote
141 from urllib
import quote
as url_quote
145 from thread
import allocate_lock
148 from threading
import Lock
as allocate_lock
150 from dummy_thread
import allocate_lock