Skip to content

BUG: raise ValueError when reading into record array with references - #32227

Merged
ngoldbaum merged 2 commits into
numpy:mainfrom
MaanasArora:bug/valueerror-read-with-refs
Aug 10, 2026
Merged

ngoldbaum merged 2 commits into
numpy:mainfrom
MaanasArora:bug/valueerror-read-with-refs

Conversation

@MaanasArora

@MaanasArora MaanasArora commented Aug 8, 2026 •

Copy link
Copy Markdown
Contributor

Fixes a bug with numpy.rec.fromfile and numpy.rec.fromstring where attempting to read into dtypes with references, including object and structured dtypes, would be unsafely accepted and could lead to segfaults. We already guard against this in PyArray_FromFile and related functions:

if (PyDataType_REFCHK(dtype)) {
PyErr_SetString(PyExc_ValueError,
"Cannot read into object array");
Py_DECREF(dtype);
return NULL;
}

The test in this PR would lead to a segfault on main. This bug was reported privately, cc @rgommers

AI Disclosure

I used line completion with GitHub copilot. No other AI used.

@MaanasArora MaanasArora added the 09 - Backport-Candidate PRs tagged should be backported label Aug 8, 2026

@ikrommyd ikrommyd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good and trivial to me. I only wonder if you want to make the error the same as the C error "Cannot read into object array".

@MaanasArora

MaanasArora commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks @ikrommyd! I would, but it just seems unclear and maybe just wrong? Not sure if precedence is enough reason.

Comment thread numpy/_core/records.py Outdated
descr = format_parser(formats, names, titles, aligned, byteorder).dtype

if descr.hasobject:
raise ValueError("Cannot read into array with references")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

"Cannot read into array" would be a bit inscrutable to me if I saw this. Maybe something like "Cannot create record array for format string {formats}, arrays of references are not supported".

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also, does StringDType happen to work right now? Or could it also possibly segfault?

>>> np.dtype("T").hasobject
True

Maybe hasobject is a bad name for this...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

"Cannot read into array" would be a bit inscrutable to me if I saw this.

Thanks, agreed, it's different from the other one so might as well go the whole way! Does what I pushed work?

Also, does StringDType happen to work right now?

It couldn't because they can't be embedded anymore in structured arrays right? FWIW, the np counterparts seem to do PyDataType_REFCHK on the C level. As far as I could trace hasobject is basically REFCHK, and somehow sounds completely different :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah, it’s a synonym for REFCHK. Thanks for confirming this fixes a StringDType issue as well.

@ngoldbaum

Copy link
Copy Markdown
Member

Thanks @MaanasArora!

@ngoldbaum
ngoldbaum merged commit f58e879 into numpy:main Aug 10, 2026
91 checks passed
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Aug 14, 2026
charris added a commit that referenced this pull request Aug 14, 2026
BUG: raise ValueError when reading into record array with references (#32227)
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.

4 participants