BUG: raise ValueError when reading into record array with references - #32227
Conversation
…string and fromfile
ikrommyd
left a comment
There was a problem hiding this comment.
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".
|
Thanks @ikrommyd! I would, but it just seems unclear and maybe just wrong? Not sure if precedence is enough reason. |
| descr = format_parser(formats, names, titles, aligned, byteorder).dtype | ||
|
|
||
| if descr.hasobject: | ||
| raise ValueError("Cannot read into array with references") |
There was a problem hiding this comment.
"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".
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
"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
StringDTypehappen 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 :)
There was a problem hiding this comment.
Yeah, it’s a synonym for REFCHK. Thanks for confirming this fixes a StringDType issue as well.
|
Thanks @MaanasArora! |
BUG: raise ValueError when reading into record array with references (#32227)
Fixes a bug with
numpy.rec.fromfileandnumpy.rec.fromstringwhere 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 inPyArray_FromFileand related functions:numpy/numpy/_core/src/multiarray/ctors.c
Lines 3659 to 3664 in 73d1419
The test in this PR would lead to a segfault on
main. This bug was reported privately, cc @rgommersAI Disclosure
I used line completion with GitHub copilot. No other AI used.