Skip to content

BUG subclass with __numpy_ufunc__ segfaults with recarray comparison #4855

@mhvk

Description

@mhvk

If I define a ndarray subclass that implements __numpy_ufunc__, it segfaults whenever it contains a record array and a comparison is done (see below). Note that any direct calls to ufunc give the usual TypeError, and the segfault can be avoided by defining one's own __eq__, __ne__.

import numpy as np
class MyA(np.ndarray):
    def __numpy_ufunc__(self, ufunc, method, i, inputs, **kwargs):
        return getattr(ufunc, method)(*(input.view(np.ndarray)
                                        for input in inputs), **kwargs)

a = np.arange(12.).reshape(4,3)
ma = a.view(MyA)
ma == ma[0]
# works as expected
ra = a.view(dtype=('f8,f8,f8')).squeeze()
ra == ra[0]
# works as expected: array([ True, False, False, False], dtype=bool)
mra = ra.view(MyA)
mra == ra[0]
# Segmentation fault

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