Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
posita opened this issue Nov 22, 2015 · 1 comment

Comments

@posita
Copy link
Contributor

posita commented Nov 22, 2015

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.

@posita
Copy link
Contributor Author

posita commented Nov 22, 2015

See also:

However, even if these are fixed, having isinstance(newint(...), int) return True when in range may avoid similar problems elsewhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants