xref: /xnu-8796.121.2/tools/lldbmacros/core/compat.py (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions"""
2*c54f35caSApple OSS DistributionsComaptibility layer to support both Python 2 and Python 3 runtimes.
3*c54f35caSApple OSS Distributions"""
4*c54f35caSApple OSS Distributionsfrom __future__ import absolute_import
5*c54f35caSApple OSS Distributions
6*c54f35caSApple OSS Distributionsfrom future.builtins import int
7*c54f35caSApple OSS Distributionsfrom future.utils import with_metaclass, PY3
8*c54f35caSApple OSS Distributions
9*c54f35caSApple OSS Distributionsif PY3:
10*c54f35caSApple OSS Distributions    # Python 3 does not have long. Map it to int.
11*c54f35caSApple OSS Distributions    long = int
12*c54f35caSApple OSS Distributions
13*c54f35caSApple OSS Distributions
14*c54f35caSApple OSS Distributionsclass BaseValueInt(type):
15*c54f35caSApple OSS Distributions    """ Metaclass for valueint.
16*c54f35caSApple OSS Distributions
17*c54f35caSApple OSS Distributions        Allows to use valueint in places where long/int is expected.
18*c54f35caSApple OSS Distributions    """
19*c54f35caSApple OSS Distributions
20*c54f35caSApple OSS Distributions    def __instancecheck__(cls, instance):
21*c54f35caSApple OSS Distributions        if cls == valueint:
22*c54f35caSApple OSS Distributions            # Special case for Py2 short or long int
23*c54f35caSApple OSS Distributions            return isinstance(instance, (int, long))
24*c54f35caSApple OSS Distributions
25*c54f35caSApple OSS Distributions        return issubclass(instance.__class__, cls)
26*c54f35caSApple OSS Distributions
27*c54f35caSApple OSS Distributions
28*c54f35caSApple OSS Distributions# The class below inherits form int on Python 3 (see the long = int above).
29*c54f35caSApple OSS Distributions# In Python 2 mode it becames children of future's newint.
30*c54f35caSApple OSS Distributionsclass valueint(with_metaclass(BaseValueInt, long)):
31*c54f35caSApple OSS Distributions    """ Python 2/3 compatible integer that works with value class.
32*c54f35caSApple OSS Distributions
33*c54f35caSApple OSS Distributions        The newint from future mostly works but does not implement all
34*c54f35caSApple OSS Distributions        operators correctly so it breaks support for value class in Python 2.
35*c54f35caSApple OSS Distributions    """
36*c54f35caSApple OSS Distributions
37*c54f35caSApple OSS Distributions    def __floordiv__(self, other):
38*c54f35caSApple OSS Distributions        """ Fix up // operator.
39*c54f35caSApple OSS Distributions
40*c54f35caSApple OSS Distributions            newint class tries to construct newint even though the __floordiv__
41*c54f35caSApple OSS Distributions            has returned NotImplemented. It is required to catch the exception
42*c54f35caSApple OSS Distributions            and retry with __rfloordiv__.
43*c54f35caSApple OSS Distributions        """
44*c54f35caSApple OSS Distributions        try:
45*c54f35caSApple OSS Distributions            result = super(valueint, self).__floordiv__(other)
46*c54f35caSApple OSS Distributions        except TypeError:
47*c54f35caSApple OSS Distributions            return other.__rfloordiv__(self)
48*c54f35caSApple OSS Distributions        return result
49*c54f35caSApple OSS Distributions
50*c54f35caSApple OSS Distributions    # The __rfloordiv__ operator has similar problem as __floordiv__ because it
51*c54f35caSApple OSS Distributions    # does not forward to reverse operator. However it is not causing any extra
52*c54f35caSApple OSS Distributions    # problems because expressions in form of valueint // value are always
53*c54f35caSApple OSS Distributions    # handled by value class which converts second argument to int.
54