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