Skip to content

Should isinstance(newint(...), int) return True for Python 2 when value is inside int bounds? #187

Open
@posita

Description

@posita

Consider the following on PyPy (which uses a pure python version of datetime):

Python 2.7.10 (f3ad1e1e1d6215e20d34bb65ab85ff9188c9f559, Sep 04 2015, 05:13:03)
[PyPy 2.6.1 with GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>> import datetime
>>>> datetime.__file__
'/.../lib/pypy/lib_pypy/datetime.pyc'
>>>> from builtins import int as _int
>>>> i = int(1431216000)
>>>> j = long(i)
>>>> k = _int(i)
>>>> isinstance(i, int)
True
>>>> isinstance(i, long)
False
>>>> isinstance(j, int)
False
>>>> isinstance(j, long)
True
>>>> isinstance(k, int)
False
>>>> isinstance(k, long)
True
>>>> datetime.timedelta(seconds=i)
datetime.timedelta(16565)
>>>> datetime.timedelta(seconds=j)
datetime.timedelta(16565)
>>>> datetime.timedelta(seconds=k)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/.../lib/pypy/lib_pypy/datetime.py", line 491, in __new__
    assert isinstance(s, int)
AssertionError

This, of course, can be worked around using:

>>>> from future.utils import native
>>>> datetime.timedelta(seconds=native(k))
datetime.timedelta(16565)

This does not happen on cPython, which uses a C implementation of datetime:

Python 2.7.10 (default, Sep 24 2015, 10:13:45)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> datetime.__file__
'/.../lib/python2.7/lib-dynload/datetime.so'
>>> from builtins import int as _int
>>> i = int(1431216000)
>>> j = long(i)
>>> k = _int(i)
>>> isinstance(i, int)
True
>>> isinstance(i, long)
False
>>> isinstance(j, int)
False
>>> isinstance(j, long)
True
>>> isinstance(k, int)
False
>>> isinstance(k, long)
True
>>> datetime.timedelta(seconds=i)
datetime.timedelta(16565)
>>> datetime.timedelta(seconds=j)
datetime.timedelta(16565)
>>> datetime.timedelta(seconds=k)
datetime.timedelta(16565)

I'm not sure whether this is a problem with PyPy datetime.py, or future or something else, but it got me to thinking. What if isinstance(newint(x), int) evaluated to True iff x <= sys.maxint and x >= (-sys.maxint - 1)? Would that be absurd or break things? It is, after all, called int, but behaves exclusively like a long with respect to the above context in Python 2.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions