[pypy-issue] Issue #2048: datetime.utcfromtimestamp barfs when calling divmod(<newint>, <float>) (pypy/pypy)

posita issues-reply at bitbucket.org
Thu May 14 05:32:16 CEST 2015


New issue 2048: datetime.utcfromtimestamp barfs when calling divmod(<newint>, <float>)
https://bitbucket.org/pypy/pypy/issue/2048/datetimeutcfromtimestamp-barfs-when

posita:

The effect of this is hidden by the fact that the native `int`'s `__divmod__` method accepts `float`s:

```py
>>>> from datetime import datetime
>>>> i = int(1431216000)
>>>> type(i)
<type 'int'>
>>>> divmod(i, 1.0)
(1431216000.0, 0.0)
>>>> datetime.utcfromtimestamp(i)
datetime.datetime(2015, 5, 10, 0, 0)
```

However, [`future`](http://python-future.org/)'s `int` implementation does not:

```py
>>>> from builtins import int as _int
>>>> import future
>>>> future.__version__
'0.14.3'
>>>> j = _int(i)
>>>> type(j)
<class 'future.types.newint.newint'>
>>>> i == j
True
>>>> datetime.utcfromtimestamp(j)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/matt/.site/lib/pypy/lib_pypy/datetime.py", line 1530, in utcfromtimestamp
    t, frac = divmod(t, 1.0)
  File ".../site-packages/future-0.14.3-py2.7.egg/future/types/newint.py", line 204, in __divmod__
    return (newint(value[0]), newint(value[1]))
TypeError: 'NotImplemented' object is not subscriptable
```

Now, before everybody jumps up and [points figures at `future`](https://github.com/PythonCharmers/python-future/issues/144), cPython *seems* to promote `future`'s `newint` to a native `int` or `float` or something, because calling [*its* `datetime.utcfromtimestamp`](https://github.com/python/cpython/blob/aa8ea3a6be22c92e774df90c6a6ee697915ca8ec/Lib/datetime.py#L1398) *works* with `future`'s `newint`s (despite PyPy's [very similar implementation](/pypy/pypy/src/bdf0f94b1bd21c29a5996d536af123e1e5cadbad/lib_pypy/datetime.py?at=default#cl-1530)):

```py
>>> from datetime import datetime
>>> i = int(1431216000)
>>> from builtins import int as _int
>>> import future
>>> future.__version__
'0.14.3'
>>> type(i)
>>> j = _int(i)
>>> type(j)
<class 'future.types.newint.newint'>
>>> i == j
True
>>> divmod(i, 1.0)
(1431216000.0, 0.0)
>>> divmod(j, 1.0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../site-packages/future/types/newint.py", line 204, in __divmod__
    return (newint(value[0]), newint(value[1]))
TypeError: 'NotImplementedType' object has no attribute '__getitem__'
>>> datetime.utcfromtimestamp(i)
datetime.datetime(2015, 5, 10, 0, 0)
>>> datetime.utcfromtimestamp(j)
datetime.datetime(2015, 5, 10, 0, 0)
```

[![Whoa, WHAT?!](http://matt.bogosian.net/img/doris.png)](http://b1nd1.deviantart.com/art/Doris-156093096)




More information about the pypy-issue mailing list