File: compat.py

package info (click to toggle)
pysdl2 0.9.7%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,824 kB
  • sloc: python: 17,244; makefile: 195; sh: 32
file content (109 lines) | stat: -rw-r--r-- 3,233 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""
Python compatibility helpers.
"""
import sys
import warnings
try:
	from collections.abc import Callable, Iterable
except ImportError:
	from collections import Callable, Iterable

__all__ = ["stringify", "byteify", "isiterable", "ISPYTHON2", "ISPYTHON3",
           "platform_is_64bit", "deprecated", "deprecation",
           "UnsupportedError", "ExperimentalWarning", "experimental",
           ]

ISPYTHON2 = False
ISPYTHON3 = False

if sys.version_info[0] < 3:
    # Wrapper around bytes() and decode() for Python 2.x
    byteify = lambda x, enc: x.encode(enc)
    # Wrapper around str() for Python 2.x
    stringify = lambda x, enc: str(x)
    ISPYTHON2 = True
else:
    __all__ += ["long", "unichr", "callable", "unicode"]
    byteify = bytes
    stringify = lambda x, enc: x.decode(enc)
    long = int
    unichr = chr
    callable = lambda x: isinstance(x, Callable)
    ISPYTHON3 = True
    unicode = str

isiterable = lambda x: isinstance(x, Iterable)


def platform_is_64bit():
    """Checks, if the platform is a 64-bit machine."""
    return sys.maxsize > 2 ** 32


def deprecated(func):
    """A simple decorator to mark functions and methods as deprecated."""
    def wrapper(*fargs, **kw):
        warnings.warn("%s is deprecated." % func.__name__,
                      category=DeprecationWarning, stacklevel=2)
        return func(*fargs, **kw)
    wrapper.__name__ = func.__name__
    wrapper.__doc__ = func.__doc__
    wrapper.__dict__.update(func.__dict__)
    return wrapper


def deprecation(message):
    """Prints a deprecation message."""
    warnings.warn(message, category=DeprecationWarning, stacklevel=2)


class UnsupportedError(Exception):
    """Indicates that a certain class, function or behaviour is not
    supported.
    """
    def __init__(self, obj, msg=None):
        """Creates an UnsupportedError for the specified obj.

        If a message is passed in msg, it will be printed instead of the
        default message.
        """
        super(UnsupportedError, self).__init__()
        self.obj = obj
        self.msg = msg

    def __str__(self):
        if self.msg is None:
            return "'%s' is not supported" % repr(self.obj)
        return repr(self.msg)


class ExperimentalWarning(Warning):
    """Indicates that a certain class, function or behaviour is in an
    experimental state.
    """
    def __init__(self, obj, msg=None):
        """Creates a ExperimentalWarning for the specified obj.

        If a message is passed in msg, it will be printed instead of the
        default message.
        """
        super(ExperimentalWarning, self).__init__()
        self.obj = obj
        self.msg = msg

    def __str__(self):
        if self.msg is None:
            return "%s is in an experimental state." % repr(self.obj)
        return repr(self.msg)


def experimental(func):
    """A simple decorator to mark functions and methods as experimental."""
    def wrapper(*fargs, **kw):
        warnings.warn("%s" % func.__name__, category=ExperimentalWarning,
                      stacklevel=2)
        return func(*fargs, **kw)
    wrapper.__name__ = func.__name__
    wrapper.__doc__ = func.__doc__
    wrapper.__dict__.update(func.__dict__)
    return wrapper