Skip to content

Conversation

devmt04
Copy link

@devmt04 devmt04 commented Aug 25, 2025

Previously, when creating a NumPy array with self-nested sequences, it causes a quick RAM exhaustion, as also described in #29620, all due to the cyclic recursions.

With these changes, when creating an array with sequences, a check for self-nesting is performed first, which prevents any further cyclic recursions.

Before:

>>> import numpy as np

>>> l = []
>>> l.append(l)
>>> l.append(l)
>>> np.array(l) # cause a hang here

Now:

>>> np.array(l)
Traceback (most recent call last):
  File "<python-input-4>", line 1, in <module>
    np.array(l)
    ~~~~~~~~^^^
ValueError: NumPy array cannot be created using self-nested sequences.

closes #29620

@jorenham
Copy link
Member

jorenham commented Aug 25, 2025

I believe that #29569 also addresses this, but I might be wrong

/* Check for self-nested sequences to prevent RAM exhaustion */
if (is_sequence) {
if (PySequence_Check(objects[0])) {
if (objects[0] == obj) {
Copy link
Member

@jorenham jorenham Aug 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this still leaves

l = [[]]
l[0].append(l)
l[0].append(l)
np.array(l)

and 62 other cases like these

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(not that that's necessarily a problem, it's just something to be aware of)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I got u, this addresses only one specific and most common case of self-nesting. Thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: np.array from self-referencing list fills RAM
2 participants