-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Description
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?