🌐 AI搜索 & 代理 主页
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion numpy/_core/src/multiarray/number.c
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ static int
fast_scalar_power(PyObject *o1, PyObject *o2, int inplace, PyObject **result)
{
PyObject *fastop = NULL;

if (PyLong_CheckExact(o2)) {
int overflow = 0;
long exp = PyLong_AsLongAndOverflow(o2, &overflow);
Expand Down Expand Up @@ -363,7 +364,12 @@ fast_scalar_power(PyObject *o1, PyObject *o2, int inplace, PyObject **result)
}

PyArrayObject *a1 = (PyArrayObject *)o1;
if (!(PyArray_ISFLOAT(a1) || PyArray_ISCOMPLEX(a1))) {
if (PyArray_ISOBJECT(a1)) {
return 1;
}
if (fastop != n_ops.square && !PyArray_ISFLOAT(a1) && !PyArray_ISCOMPLEX(a1)) {
// we special-case squaring for any array type
// gh-29388
return 1;
}

Expand Down
7 changes: 7 additions & 0 deletions numpy/_core/tests/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -4211,6 +4211,13 @@ def pow_for(exp, arr):
assert_equal(obj_arr ** -1, pow_for(-1, obj_arr))
assert_equal(obj_arr ** 2, pow_for(2, obj_arr))

def test_pow_calls_square_structured_dtype(self):
# gh-29388
dt = np.dtype([('a', 'i4'), ('b', 'i4')])
a = np.array([(1, 2), (3, 4)], dtype=dt)
with pytest.raises(TypeError, match="ufunc 'square' not supported"):
a ** 2

def test_pos_array_ufunc_override(self):
class A(np.ndarray):
def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
Expand Down
Loading