GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — BUG: fix loadtxt crash with existing StringDType dtype by ngoldbaum · Pull Request #33266 · numpy/numpy

Conversation

@ngoldbaum

@ngoldbaum ngoldbaum commented Nov 9, 2026

Copy link
Copy Markdown
Member

PR summary

Use the output array’s finalized descriptor when packing values in loadtxt. Reusing an array-owned StringDType otherwise writes long strings into the wrong allocator.

On main:

In [1]: import numpy as np

In [2]: owner = np.array(["x"], dtype="T")

In [3]: result = np.loadtxt(["a-string-longer-than-15-bytes"], dtype=owner.dtype)

In [4]: result.item()
Traceback (most recent call last):
  Cell In[4], line 1
    result.item()
MemoryError: Failed to load string in StringDType getitem

Add a regression test for reusing an array’s StringDType and extend existing loadtxt tests to cover StringDType with Unicode, converters, quoting, and max_rows.

AI Disclosure

I worked on this with an AI model

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
ngoldbaum added 2 commits November 9, 2026 16:10
@ngoldbaum
BUG: fix loadtxt crash with existing StringDType dtype
0917121
@ngoldbaum
TST: add StringDType parameters for loadtxt tests
50e261d
@ngoldbaum ngoldbaum added this to the 2.5.5 release milestone Nov 9, 2026
@ngoldbaum ngoldbaum added 00 - Bug 09 - Backport-Candidate PRs tagged should be backported component: numpy.strings String dtypes and functions labels Nov 9, 2026
@ngoldbaum ngoldbaum changed the title Loadtxt crash tests BUG: fix loadtxt crash with existing StringDType dtype Nov 9, 2026
@ngoldbaum ngoldbaum added the triage review Issue/PR to be discussed at the next triage meeting label Nov 11, 2026
ngoldbaum
ngoldbaum commented Nov 11, 2026
View reviewed changes
Comment thread numpy/_core/src/multiarray/textreading/rows.c
// finalization might replace the descriptor
Py_INCREF(PyArray_DESCR(data_array));
Py_SETREF(field_types[0].descr, PyArray_DESCR(data_array));
}

@ngoldbaum ngoldbaum Nov 11, 2026

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Might want to check if we can do this unconditionally but structured dtypes with subarrays might not work correctly with this.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
seberg
seberg approved these changes Nov 11, 2026
View reviewed changes

@seberg seberg left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This seems right (and I assume the models thought it through as well).

Mainly that one question: the dtype can be replaced mainly for subarray dtypes where the subarray dtype get's absorbed into the array dimensions (the other thing is S0 -> S1).

If that isn't a problem here, then I would be tempted to not have a branch that feels a little unnecessary.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ngoldbaum
MAINT: only update descriptor if replacement happened
844be53
@ngoldbaum

ngoldbaum commented Nov 11, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

@seberg for structured dtypes, field_types[0].descr describes just the first field rather than the whole dtype, so stuff like this has different output if we do this unconditionally:

dt = np.dtype([
    ("a", np.dtype("i2").newbyteorder()),
    ("b", "i2")
])
np.loadtxt(["1 2"]).item()

Doing the replacement unconditionally means this outputs (256, 2).

However if I make the condition if (out_descr != PyArray_DESCR(data_array)) instead of the has_finalize check, then I think this is fully general and avoids any problematic cases?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ngoldbaum
MAINT: remove now-unused header
eec7513
@ngoldbaum ngoldbaum added triaged Issue/PR that was discussed in a triage meeting and removed triage review Issue/PR to be discussed at the next triage meeting labels Nov 11, 2026
@ngoldbaum
ngoldbaum merged commit 8a3b8b4 into numpy:main Nov 11, 2026
87 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@seberg

seberg commented Nov 12, 2026

Copy link
Copy Markdown
Member

I was actually worried about this: np.loadtxt(["1", "2"], dtype="2i") but it's just a crash (which is all good, although a clean error is clearly better ;)).

So this seems OK, although, long term this really should walk the fields or probably finalize the descriptor first (and then create the array forcing identical descriptor which we can).
That would become relevant if we start allowing nested finalization so that you can package a StringDType into a structured one.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@charris charris mentioned this pull request Nov 12, 2026
BUG: fix loadtxt crash with existing StringDType dtype (#33266) #33313
Merged
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Nov 12, 2026
charris pushed a commit that referenced this pull request Nov 12, 2026
@ngoldbaum @charris
BUG: fix loadtxt crash with existing StringDType dtype (#33266)
cafa2ae
charris added a commit that referenced this pull request Nov 12, 2026
@charris
Merge pull request #33313 from charris/backport-33266
f531892
BUG: fix loadtxt crash with existing StringDType dtype (#33266)
MaanasArora pushed a commit to MaanasArora/numpy that referenced this pull request Nov 15, 2026
@ngoldbaum @MaanasArora
BUG: fix loadtxt crash with existing StringDType dtype (numpy#33266)
535077d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@seberg seberg seberg approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

00 - Bug component: numpy.strings String dtypes and functions triaged Issue/PR that was discussed in a triage meeting

Projects

None yet

Milestone

2.5.5 release

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@ngoldbaum @seberg @charris
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.