int in python3 is a multi-head class of a class that can represent machine ints, as well as big-ints; which is superior to the difference between int and long in python2. On python3, the int(1 << n) construct never throws an error.
So, to solve this, six uses a neat trick that forces python to sew something in a machine the size of an int. The built-in len always tries to convert the return value of __len__ to a machine size value:
>>> class Lengthy(object): ... def __init__(self, x): ... self.x = x ... def __len__(self): ... return self.x ... >>> int(1<<100) 1267650600228229401496703205376L >>> type(int(1<<100)) <type 'long'> >>> len(Lengthy(1<<100)) Traceback (most recent call last): File "<ipython-input-6-6b1b77348950>", line 1, in <module> len(Lengthy(1<<100)) OverflowError: long int too large to convert to int >>>
or, in Python 3, the exception is slightly different:
>>> len(Lengthy(1<<100)) Traceback (most recent call last): File "<stdin>", line 1, in <module> OverflowError: cannot fit 'int' into an index-sized integer >>>
source share