1import sys
2
3from sentry_sdk._types import MYPY
4
5if MYPY:
6    from typing import Optional
7    from typing import Tuple
8    from typing import Any
9    from typing import Type
10    from typing import TypeVar
11
12    T = TypeVar("T")
13
14
15PY2 = sys.version_info[0] == 2
16
17if PY2:
18    import urlparse  # noqa
19
20    text_type = unicode  # noqa
21
22    string_types = (str, text_type)
23    number_types = (int, long, float)  # noqa
24    int_types = (int, long)  # noqa
25    iteritems = lambda x: x.iteritems()  # noqa: B301
26
27    def implements_str(cls):
28        # type: (T) -> T
29        cls.__unicode__ = cls.__str__
30        cls.__str__ = lambda x: unicode(x).encode("utf-8")  # noqa
31        return cls
32
33    exec("def reraise(tp, value, tb=None):\n raise tp, value, tb")
34
35
36else:
37    import urllib.parse as urlparse  # noqa
38
39    text_type = str
40    string_types = (text_type,)  # type: Tuple[type]
41    number_types = (int, float)  # type: Tuple[type, type]
42    int_types = (int,)  # noqa
43    iteritems = lambda x: x.items()
44
45    def implements_str(x):
46        # type: (T) -> T
47        return x
48
49    def reraise(tp, value, tb=None):
50        # type: (Optional[Type[BaseException]], Optional[BaseException], Optional[Any]) -> None
51        assert value is not None
52        if value.__traceback__ is not tb:
53            raise value.with_traceback(tb)
54        raise value
55
56
57def with_metaclass(meta, *bases):
58    # type: (Any, *Any) -> Any
59    class MetaClass(type):
60        def __new__(metacls, name, this_bases, d):
61            # type: (Any, Any, Any, Any) -> Any
62            return meta(name, bases, d)
63
64    return type.__new__(MetaClass, "temporary_class", (), {})
65
66
67def check_thread_support():
68    # type: () -> None
69    try:
70        from uwsgi import opt  # type: ignore
71    except ImportError:
72        return
73
74    # When `threads` is passed in as a uwsgi option,
75    # `enable-threads` is implied on.
76    if "threads" in opt:
77        return
78
79    if str(opt.get("enable-threads", "0")).lower() in ("false", "off", "no", "0"):
80        from warnings import warn
81
82        warn(
83            Warning(
84                "We detected the use of uwsgi with disabled threads.  "
85                "This will cause issues with the transport you are "
86                "trying to use.  Please enable threading for uwsgi.  "
87                '(Add the "enable-threads" flag).'
88            )
89        )
90