Skip to content

Python long raises OverflowError while same value as np.uint64 does not. #5612

Closed

Description

@johntyree

Trying to assign a value of 2**64 - 1 to an array of dtype uint64 raises an OverflowError, but only if the value is a Python long rather than a Numpy scalar. This seems like a bug to me because the value obviously doesn't overflow and they compare equal.

Python 3.4.2 (default, Jan 12 2015, 11:38:40) 
[GCC 4.9.2 20141224 (prerelease)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.version.version
'1.9.1'
>>> arr = np.empty((1,), dtype=np.uint64)
>>> arr.fill(np.uint64(2**64-1))
>>> arr.fill(2**64-1)                                                                                 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: int too big to convert
>>> np.uint64(2**64-1) == 2**64 - 1                                                                   
True
>>> arr == 2**64-1
array([ True], dtype=bool)

Activity

  1. jaimefrio commented on Feb 27, 2015

    @jaimefrio
    Member

    It only happens when using fill, not when doing a direct assignment:

    >>> a = np.empty(1, np.uint64)
    >>> a[0] = 2**64 - 1
    >>> a.fill(2**64-1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    OverflowError: int too big to convert
    

    This points to PyArray_FillWithScalar as the culprit, and I think it is indeed to blame: when converting integer types, it calls PyLong_AsLongLong, and doesn't try with PyLong_AsUnsignedLongLong if that fails, see here for the numpy code.

    PRs are welcome...

  2. johntyree commented on Feb 28, 2015

    @johntyree
    ContributorAuthor

    A real bug and not just pebkac! How exciting! I'll try to put together a PR this weekend.

  3. added 2 commits that reference this issue on Apr 17, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions