GeezerVison is showing github.com — BUG: `ufunc.at()` can cause segfaults if `nin=0` and `nout=1` · Issue #33053 · numpy/numpy
BUG: ufunc.at() can cause segfaults if nin=0 and nout=1 #33053DescriptionDescribe the issue:
The at() method of a ufunc with nin=0 and nout>1 causes a ValueError and the at() method of a gufunc with nin=0 and nout=1 causes a TypeError (it's not clear to me why the error types should differ, but this bug report is not about that). In contrast a ufunc with nin=0 and nout=1 causes a segmentation fault.
Reproduce the code example:
import numpy as np
ufunc_nin0_nout1 = np.frompyfunc(lambda: 0, nin=0, nout=1)
ufunc_nin0_nout1.at(np.empty((1,)), True)
Error message:
Program received signal SIGSEGV, Segmentation fault.
PyArray_CastDescrToDType (descr=0x7ffff71dcf20 <DOUBLE_Descr>, given_DType=0x0)
at ../numpy/_core/src/multiarray/dtypemeta.h:127
127 return (dtype->flags & NPY_DT_PARAMETRIC) != 0;
Python and NumPy Versions:
2.6.0.dev0+git20261015.8ecb569
3.12.3 (main, Aug 31 2026, 10:18:26) [GCC 13.3.0]
Runtime Environment:
No response
How does this issue affect you or how did you find it:
I am working on adding type annotations to custom ufuncs of a downstream library. When I was checking if the annotations for the at() methods match runtime behavior I triggered a segmentation fault instead of the error I was expecting. Activityjorenham commented on Oct 15, 2026 Haha, I didn't expect anyone to vectorize a function that takes no arguments. Anyway, segfaults shouldn't happen, so thanks for reporting :) eerovaher commented on Oct 15, 2026 ContributorAuthor More actions The library in question provides ufunc wrappers for a C library. The source code for the ufuncs is not written down, it is instead generated at build time by a Python script that takes the source code of the C library as an input. Apparently it is simpler if the Python script just generates ufuncs with nin=0. ikrommyd commented on Oct 15, 2026 We had a similar issue recently but for no output arguments ufuncs. I’ll take a look. Definitely a clean error instead of a seg fault is needed but theoretically this operation could work where it’s just like a setitem where the elements of the input are just set to what the ufunc outputs. But I wouldn’t want to enable this if it’s not trivial in code. ikrommyd commented on Oct 17, 2026 I thought about it a bit more and I think just erroring is the right fix. ufunc.at(a, idx, b) is a[idx] = ufunc(a[idx], b) and if we were to support nin=0 ufuncs it would mean this a[idx] = f(). Doing this is not trivial and needs reworking ufunc_at__fast_iter/ufunc_at__slow_iter which is something nobody asked for. So just erroring is the way to go IMO. I put a PR up.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|