1# -*- coding: utf-8 -*-
2# Copyright (C) 2013  Christoph Reiter
3#
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 2 of the License, or
7# (at your option) any later version.
8
9import sys
10
11
12PY2 = sys.version_info[0] == 2
13PY3 = not PY2
14
15if PY2:
16    from StringIO import StringIO
17    BytesIO = StringIO
18    from cStringIO import StringIO as cBytesIO
19    from itertools import izip
20
21    long_ = long
22    integer_types = (int, long)
23    string_types = (str, unicode)
24    text_type = unicode
25
26    xrange = xrange
27    cmp = cmp
28    chr_ = chr
29
30    def endswith(text, end):
31        return text.endswith(end)
32
33    iteritems = lambda d: d.iteritems()
34    itervalues = lambda d: d.itervalues()
35    iterkeys = lambda d: d.iterkeys()
36
37    iterbytes = lambda b: iter(b)
38
39    exec("def reraise(tp, value, tb):\n raise tp, value, tb")
40
41    def swap_to_string(cls):
42        if "__str__" in cls.__dict__:
43            cls.__unicode__ = cls.__str__
44
45        if "__bytes__" in cls.__dict__:
46            cls.__str__ = cls.__bytes__
47
48        return cls
49
50    import __builtin__ as builtins
51    builtins
52
53elif PY3:
54    from io import StringIO
55    StringIO = StringIO
56    from io import BytesIO
57    cBytesIO = BytesIO
58
59    long_ = int
60    integer_types = (int,)
61    string_types = (str,)
62    text_type = str
63
64    izip = zip
65    xrange = range
66    cmp = lambda a, b: (a > b) - (a < b)
67    chr_ = lambda x: bytes([x])
68
69    def endswith(text, end):
70        # usefull for paths which can be both, str and bytes
71        if isinstance(text, str):
72            if not isinstance(end, str):
73                end = end.decode("ascii")
74        else:
75            if not isinstance(end, bytes):
76                end = end.encode("ascii")
77        return text.endswith(end)
78
79    iteritems = lambda d: iter(d.items())
80    itervalues = lambda d: iter(d.values())
81    iterkeys = lambda d: iter(d.keys())
82
83    iterbytes = lambda b: (bytes([v]) for v in b)
84
85    def reraise(tp, value, tb):
86        raise tp(value).with_traceback(tb)
87
88    def swap_to_string(cls):
89        return cls
90
91    import builtins
92    builtins
93