|
1 | | -''' |
| 1 | +""" |
2 | 2 | Python 2.x/3.x Compatibility Layer |
3 | 3 | ------------------------------------------------- |
4 | 4 |
|
|
10 | 10 |
|
11 | 11 | :copyright: Copyright 2013 by the Jinja team, see AUTHORS. |
12 | 12 | :license: BSD, see LICENSE for details. |
13 | | -''' |
| 13 | +""" |
14 | 14 | import sys |
15 | | -import struct |
| 15 | +import six |
16 | 16 |
|
17 | | -#---------------------------------------------------------------------------# |
| 17 | +# --------------------------------------------------------------------------- # |
18 | 18 | # python version checks |
19 | | -#---------------------------------------------------------------------------# |
| 19 | +# --------------------------------------------------------------------------- # |
20 | 20 | PYTHON_VERSION = sys.version_info |
21 | | -IS_PYTHON2 = PYTHON_VERSION[0] == 2 |
22 | | -IS_PYTHON3 = PYTHON_VERSION[0] == 3 |
23 | | -IS_PYPY = hasattr(sys, 'pypy_translation_info') |
24 | | -IS_JYTHON = sys.platform.startswith('java') |
| 21 | +IS_PYTHON2 = six.PY2 |
| 22 | +IS_PYTHON3 = six.PY3 |
| 23 | +IS_PYPY = hasattr(sys, 'pypy_translation_info') |
| 24 | +IS_JYTHON = sys.platform.startswith('java') |
25 | 25 |
|
26 | | -#---------------------------------------------------------------------------# |
27 | | -# python > 3.3 compatability layer |
28 | | -#---------------------------------------------------------------------------# |
29 | | -if not IS_PYTHON2: |
30 | | - #-----------------------------------------------------------------------# |
31 | | - # portable builtins |
32 | | - #-----------------------------------------------------------------------# |
33 | | - int2byte = lambda b: struct.pack('B', b) |
34 | | - byte2int = lambda b: b |
35 | | - unichr = chr |
36 | | - range_type = range |
37 | | - text_type = str |
38 | | - string_types = (str,) |
39 | | - iterkeys = lambda d: iter(d.keys()) |
40 | | - itervalues = lambda d: iter(d.values()) |
41 | | - iteritems = lambda d: iter(d.items()) |
42 | | - get_next = lambda x: x.__next__() |
43 | | - |
44 | | - #-----------------------------------------------------------------------# |
45 | | - # module renames |
46 | | - #-----------------------------------------------------------------------# |
47 | | - from io import BytesIO, StringIO |
48 | | - NativeStringIO = StringIO |
| 26 | +# --------------------------------------------------------------------------- # |
| 27 | +# python > 3.3 compatibility layer |
| 28 | +# --------------------------------------------------------------------------- # |
| 29 | +# ----------------------------------------------------------------------- # |
| 30 | +# portable builtins |
| 31 | +# ----------------------------------------------------------------------- # |
| 32 | +int2byte = six.int2byte |
| 33 | +unichr = six.unichr |
| 34 | +range_type = six.moves.range |
| 35 | +text_type = six.string_types |
| 36 | +string_types = six.string_types |
| 37 | +iterkeys = six.iterkeys |
| 38 | +itervalues = six.itervalues |
| 39 | +iteritems = six.iteritems |
| 40 | +get_next = six.next |
| 41 | +unicode_string = six.u |
49 | 42 |
|
50 | | - ifilter = filter |
51 | | - imap = map |
52 | | - izip = zip |
53 | | - intern = sys.intern |
| 43 | +NativeStringIO = six.StringIO |
| 44 | +ifilter = six.moves.filter |
| 45 | +imap = six.moves.map |
| 46 | +izip = six.moves.zip |
| 47 | +intern = six.moves.intern |
54 | 48 |
|
| 49 | +if not IS_PYTHON2: |
| 50 | + # ----------------------------------------------------------------------- # |
| 51 | + # module renames |
| 52 | + # ----------------------------------------------------------------------- # |
55 | 53 | import socketserver |
56 | 54 |
|
57 | | - #-----------------------------------------------------------------------# |
| 55 | + # ----------------------------------------------------------------------- # |
58 | 56 | # decorators |
59 | | - #-----------------------------------------------------------------------# |
| 57 | + # ----------------------------------------------------------------------- # |
60 | 58 | implements_to_string = lambda x: x |
61 | 59 |
|
62 | | -#---------------------------------------------------------------------------# |
| 60 | + byte2int = lambda b: b |
| 61 | +# --------------------------------------------------------------------------- # |
63 | 62 | # python > 2.5 compatability layer |
64 | | -#---------------------------------------------------------------------------# |
| 63 | +# --------------------------------------------------------------------------- # |
65 | 64 | else: |
66 | | - #-----------------------------------------------------------------------# |
67 | | - # portable builtins |
68 | | - #-----------------------------------------------------------------------# |
69 | | - int2byte = chr |
70 | | - byte2int = ord |
71 | | - unichr = unichr |
72 | | - text_type = unicode |
73 | | - range_type = xrange |
74 | | - string_types = (str, unicode) |
75 | | - iterkeys = lambda d: d.iterkeys() |
76 | | - itervalues = lambda d: d.itervalues() |
77 | | - iteritems = lambda d: d.iteritems() |
78 | | - get_next = lambda x: x.next() |
79 | | - |
80 | | - #-----------------------------------------------------------------------# |
| 65 | + byte2int = six.byte2int |
| 66 | + # ----------------------------------------------------------------------- # |
81 | 67 | # module renames |
82 | | - #-----------------------------------------------------------------------# |
83 | | - from cStringIO import StringIO as BytesIO, StringIO |
84 | | - NativeStringIO = BytesIO |
85 | | - |
86 | | - from itertools import imap, izip, ifilter |
87 | | - intern = intern |
88 | 68 |
|
| 69 | + # ----------------------------------------------------------------------- # |
89 | 70 | import SocketServer as socketserver |
90 | 71 |
|
91 | | - #-----------------------------------------------------------------------# |
| 72 | + # ----------------------------------------------------------------------- # |
92 | 73 | # decorators |
93 | | - #-----------------------------------------------------------------------# |
| 74 | + # ----------------------------------------------------------------------- # |
94 | 75 | def implements_to_string(klass): |
95 | 76 | klass.__unicode__ = klass.__str__ |
96 | 77 | klass.__str__ = lambda x: x.__unicode__().encode('utf-8') |
|
0 commit comments