* Assigns a scalar value specified by 'src_dtype' and 'src_data' * to elements of 'dst'. * * dst: The destination array. * src_dtype: The data type of the source scalar. * src_data: The memory element of the source scalar. * wheremask: If non-NULL, a boolean mask specifying where to copy. * casting: An exception is raised if the assignment violates this * casting rule. * * This
| 230 | * Returns 0 on success, -1 on failure. |
| 231 | */ |
| 232 | NPY_NO_EXPORT int |
| 233 | PyArray_AssignRawScalar(PyArrayObject *dst, |
| 234 | PyArray_Descr *src_dtype, char *src_data, |
| 235 | PyArrayObject *wheremask, |
| 236 | NPY_CASTING casting) |
| 237 | { |
| 238 | int allocated_src_data = 0; |
| 239 | npy_longlong scalarbuffer[4]; |
| 240 | |
| 241 | if (PyArray_FailUnlessWriteable(dst, "assignment destination") < 0) { |
| 242 | return -1; |
| 243 | } |
| 244 | |
| 245 | /* Check the casting rule */ |
| 246 | if (!can_cast_scalar_to(src_dtype, src_data, |
| 247 | PyArray_DESCR(dst), casting)) { |
| 248 | npy_set_invalid_cast_error( |
| 249 | src_dtype, PyArray_DESCR(dst), casting, NPY_TRUE); |
| 250 | return -1; |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * Make a copy of the src data if it's a different dtype than 'dst' |
| 255 | * or isn't aligned, and the destination we're copying to has |
| 256 | * more than one element. To avoid having to manage object lifetimes, |
| 257 | * we also skip this if 'dst' has an object dtype. |
| 258 | */ |
| 259 | if ((!PyArray_EquivTypes(PyArray_DESCR(dst), src_dtype) || |
| 260 | !(npy_is_aligned(src_data, npy_uint_alignment(src_dtype->elsize)) && |
| 261 | npy_is_aligned(src_data, src_dtype->alignment))) && |
| 262 | PyArray_SIZE(dst) > 1 && |
| 263 | !PyDataType_REFCHK(PyArray_DESCR(dst))) { |
| 264 | char *tmp_src_data; |
| 265 | |
| 266 | /* |
| 267 | * Use a static buffer to store the aligned/cast version, |
| 268 | * or allocate some memory if more space is needed. |
| 269 | */ |
| 270 | if ((int)sizeof(scalarbuffer) >= PyArray_DESCR(dst)->elsize) { |
| 271 | tmp_src_data = (char *)&scalarbuffer[0]; |
| 272 | } |
| 273 | else { |
| 274 | tmp_src_data = PyArray_malloc(PyArray_DESCR(dst)->elsize); |
| 275 | if (tmp_src_data == NULL) { |
| 276 | PyErr_NoMemory(); |
| 277 | goto fail; |
| 278 | } |
| 279 | allocated_src_data = 1; |
| 280 | } |
| 281 | |
| 282 | if (PyDataType_FLAGCHK(PyArray_DESCR(dst), NPY_NEEDS_INIT)) { |
| 283 | memset(tmp_src_data, 0, PyArray_DESCR(dst)->elsize); |
| 284 | } |
| 285 | |
| 286 | if (PyArray_CastRawArrays(1, src_data, tmp_src_data, 0, 0, |
| 287 | src_dtype, PyArray_DESCR(dst), 0) != NPY_SUCCEED) { |
| 288 | src_data = tmp_src_data; |
| 289 | goto fail; |
no test coverage detected