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