Skip to content

Stack overflow caused by clumsy array coercion #17785

@eric-wieser

Description

@eric-wieser

Reproducing code example:

Over at pygae/clifford#376, we've found that the adjustments to array coercion in 1.19.x seem to produce stack overflows where previously they were safe. A minimal repro is this clumsy coercion code, which mirrors what clifford.MVArray did in their 1.3.0 release.

import numpy as np

class MyArray(np.ndarray):
    def __new__(cls, input_array):
        obj = np.empty(len(input_array), dtype=object)
        obj[:] = input_array
        obj = obj.view(cls)
        return obj

class MyNastyClass:
    def __len__(self): return 32
    def __getitem__(self, item): return 1
    def __array__(self): return MyArray([self])

np.asarray(MyNastyClass())

Error message:

Windows fatal exception: stack overflow

Current thread 0x00006160 (most recent call first):
  File "<ipython-input-4-ac7cb36f8e78>", line 3 in __new__
  File "<ipython-input-4-ac7cb36f8e78>", line 12 in __array__
  File "<ipython-input-4-ac7cb36f8e78>", line 4 in __new__
  File "<ipython-input-4-ac7cb36f8e78>", line 12 in __array__
  File "<ipython-input-4-ac7cb36f8e78>", line 4 in __new__
  File "<ipython-input-4-ac7cb36f8e78>", line 12 in __array__

NumPy/Python version information:

1.19.4 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50) [MSC v.1916 64 bit (AMD64)]

Thankfully the code above was removed since it was bad for other reasons, but its unfortunate that users on an old release get a segfault.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions