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