Skip to content

BUG: fix np.fromiter corruption when reusing a StringDType instance (#32012) - #32117

Merged
charris merged 1 commit into
numpy:maintenance/2.5.xfrom
charris:backport-32012
Jul 27, 2026
Merged

BUG: fix np.fromiter corruption when reusing a StringDType instance (#32012)#32117
charris merged 1 commit into
numpy:maintenance/2.5.xfrom
charris:backport-32012

Conversation

@charris

@charris charris commented Jul 27, 2026

Copy link
Copy Markdown
Member

Backport of #32012.

PR summary

Fixes #32013

np.fromiter is buggy for StringDtype arrays. Specifically, when the dtype instance is already owned by someother array,
it creates corrupted structures since it re-uses the input dtype instance to create the output array.

This PR fixes this issue to avoid using the input dtype descriptor to write the output.

Reproducer which this PR fixes:

In [2]: sd = np.dtypes.StringDType()

In [3]: a = ["a"*18, "b"*18]*10_000

In [4]: np.fromiter(iter(a), dtype=sd, count=len(a))
Out[4]: 
array(['aaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbb', 'aaaaaaaaaaaaaaaaaa',
       ..., 'bbbbbbbbbbbbbbbbbb', 'aaaaaaaaaaaaaaaaaa',
       'bbbbbbbbbbbbbbbbbb'], shape=(20000,), dtype=StringDType())

In [5]: np.fromiter(iter(a), dtype=sd, count=len(a))
Out[5]: Fatal Python error: Segmentation fault

AI Disclosure

I’ve used Claude Opus to shepherd this along.

…ce (numpy#32012)

Co-authored-by: Gopu Yeshwanth Reddy <Gopu.Reddy@deshaw.com>
@charris charris added this to the 2.5.2 release milestone Jul 27, 2026
@charris charris added 00 - Bug 08 - Backport Used to tag backport PRs labels Jul 27, 2026
@charris
charris merged commit c40c6dc into numpy:maintenance/2.5.x Jul 27, 2026
85 checks passed
@charris
charris deleted the backport-32012 branch July 27, 2026 17:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

00 - Bug 08 - Backport Used to tag backport PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants