1# -*- coding: utf-8 -*-
2
3"""
4    thriftpy2._compat
5    ~~~~~~~~~~~~~
6
7    py2/py3 compatibility support.
8"""
9
10from __future__ import absolute_import
11
12import platform
13import sys
14
15PY3 = sys.version_info[0] == 3
16PY35 = sys.version_info >= (3, 5)
17PYPY = "__pypy__" in sys.modules
18
19UNIX = platform.system() in ("Linux", "Darwin")
20CYTHON = UNIX and not PYPY  # Cython always disabled in pypy and windows
21
22# only Python 2.7.9 and Python 3.4 or above have true ssl context
23MODERN_SSL = sys.version_info >= (2, 7, 9)
24
25if PY3:
26    text_type = str
27    string_types = (str,)
28    from urllib.request import urlopen
29    from urllib.parse import urlparse
30
31    def u(s):
32        return s
33else:
34    text_type = unicode  # noqa
35    string_types = (str, unicode)  # noqa
36    from urllib2 import urlopen  # noqa
37    from urlparse import urlparse  # noqa
38
39    def u(s):
40        if not isinstance(s, text_type):
41            s = s.decode("utf-8")
42        return s
43
44
45def with_metaclass(meta, *bases):
46    """Create a base class with a metaclass for py2 & py3
47
48    This code snippet is copied from six."""
49    # This requires a bit of explanation: the basic idea is to make a
50    # dummy metaclass for one level of class instantiation that replaces
51    # itself with the actual metaclass.  Because of internal type checks
52    # we also need to make sure that we downgrade the custom metaclass
53    # for one level to something closer to type (that's why __call__ and
54    # __init__ comes back from type etc.).
55    class metaclass(meta):
56        __call__ = type.__call__
57        __init__ = type.__init__
58
59        def __new__(cls, name, this_bases, d):
60            if this_bases is None:
61                return type.__new__(cls, name, (), d)
62            return meta(name, bases, d)
63    return metaclass('temporary_class', None, {})
64