Skip to content

BUG? np.ma.flatten_structured_array infinite recursion when field contains iterable object #29349

@Smith-cc

Description

@Smith-cc

When using np.ma.flatten_structured_array on a structured array that has a field of object type containing an iterable (like a list or str), it causes infinite recursion and crashes with RecursionError: maximum recursion depth exceeded.

I would like to confirm: Is this the expected behavior, or should flatten_structured_array guard against nested iterables like str to prevent infinite recursion?

# Example
import numpy as np

ndtype = [('col1', 'U1'), ('col2', object)]
arr = np.array([('A', ['oops'])], dtype=ndtype)

np.ma.flatten_structured_array(arr)

I think the root cause source code

def flatten_sequence(iterable):
    for elm in iter(iterable):
        if hasattr(elm, '__iter__'):
            yield from flatten_sequence(elm)
        else:
            yield elm

does not distinguish between strings and containers — so str are recursively iterated character-by-character, causing infinite recursion if nested in an object field.

The same problem also occurs below:

import numpy as np

mask = np.array([(0, 0), (0, '1')], dtype=[('a', bool), ('b', bool)]) # Ok
x = np.ma.flatten_mask(mask)

mask = np.array([(0, 0), (0, '1')], dtype=[('a', bool), ('b', object)])
x = np.ma.flatten_mask(mask) # Error

My concern is, should structured arrays with object fields containing containers be rejected or handled differently? Or is it the user’s responsibility to sanitize such fields before calling these functions?

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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy