Skip to content

BUG: np.array([0, datetime.now()], dtype=object).sort() can raise on cmp(), but exception ignored #9404

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

Closed
mattip opened this issue Jul 12, 2017 · 1 comment · Fixed by #9412

Comments

@mattip
Copy link
Member

mattip commented Jul 12, 2017

the various functions in quicksort.c.src do not check PyErr_Occurred() before returning, when the tp_compare fails no exception is raised. The exception is thrown later by any random C-API call, but the error originated in the sort function

python -c "import numpy as np; from datetime import datetime as dt; \
                 a = np.array([1, 2, dt.now(), 0, 3], dtype=object); \
                 np.sort(a); print a"
numpy/core/fromnumeric.py:822: RuntimeWarning: tp_compare didn't return -1 or -2 for exception
  a.sort(axis=axis, kind=kind, order=order)
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "numpy/core/numeric.py", line 1942, in array_str
    return array2string(a, max_line_width, precision, suppress_small, ' ', "")
  File "numpy/core/arrayprint.py", line 397, in wrapper
    return f(self, *args, **kwargs)
  File "numpy/core/arrayprint.py", line 529, in array2string
    separator, prefix, formatter=formatter)
  File "numpy/core/arrayprint.py", line 373, in _array2string
    _summaryEdgeItems, summary_insert)[:-1]
  File "numpy/core/arrayprint.py", line 565, in _formatArray
    for i in range(leading_items):
TypeError: can't compare datetime.datetime to int

FWIW, on PyPy we detect that a C-API function raised an exception but returns a valid value, which
is how I found the problem

@eric-wieser
Copy link
Member

This only occurs on python 2.

The numpy code falsely assumes that comparisons cannot raise exceptions in python 2

eric-wieser added a commit that referenced this issue Jul 20, 2017
BUG: Check for exception in sort functions

Fixes gh-3879
Fixes gh-9404
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants