Skip to content

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

@mattip

Description

@mattip

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

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