* This function allows the user to register a 1-d loop with an already * created ufunc. This function is similar to RegisterLoopForType except * that it allows a 1-d loop to be registered with PyArray_Descr objects * instead of dtype type num values. This allows a 1-d loop to be registered * for a structured array dtype or a custom dtype. The ufunc is called * whenever any of it's input argum
| 5421 | */ |
| 5422 | /*UFUNC_API*/ |
| 5423 | NPY_NO_EXPORT int |
| 5424 | PyUFunc_RegisterLoopForDescr(PyUFuncObject *ufunc, |
| 5425 | PyArray_Descr *user_dtype, |
| 5426 | PyUFuncGenericFunction function, |
| 5427 | PyArray_Descr **arg_dtypes, |
| 5428 | void *data) |
| 5429 | { |
| 5430 | int i; |
| 5431 | int result = 0; |
| 5432 | int *arg_typenums; |
| 5433 | PyObject *key, *cobj; |
| 5434 | |
| 5435 | if (user_dtype == NULL) { |
| 5436 | PyErr_SetString(PyExc_TypeError, |
| 5437 | "unknown user defined struct dtype"); |
| 5438 | return -1; |
| 5439 | } |
| 5440 | |
| 5441 | key = PyLong_FromLong((long) user_dtype->type_num); |
| 5442 | if (key == NULL) { |
| 5443 | return -1; |
| 5444 | } |
| 5445 | |
| 5446 | arg_typenums = PyArray_malloc(ufunc->nargs * sizeof(int)); |
| 5447 | if (arg_typenums == NULL) { |
| 5448 | Py_DECREF(key); |
| 5449 | PyErr_NoMemory(); |
| 5450 | return -1; |
| 5451 | } |
| 5452 | if (arg_dtypes != NULL) { |
| 5453 | for (i = 0; i < ufunc->nargs; i++) { |
| 5454 | arg_typenums[i] = arg_dtypes[i]->type_num; |
| 5455 | } |
| 5456 | } |
| 5457 | else { |
| 5458 | for (i = 0; i < ufunc->nargs; i++) { |
| 5459 | arg_typenums[i] = user_dtype->type_num; |
| 5460 | } |
| 5461 | } |
| 5462 | |
| 5463 | result = PyUFunc_RegisterLoopForType(ufunc, user_dtype->type_num, |
| 5464 | function, arg_typenums, data); |
| 5465 | |
| 5466 | if (result == 0) { |
| 5467 | cobj = PyDict_GetItemWithError(ufunc->userloops, key); |
| 5468 | if (cobj == NULL && PyErr_Occurred()) { |
| 5469 | result = -1; |
| 5470 | } |
| 5471 | else if (cobj == NULL) { |
| 5472 | PyErr_SetString(PyExc_KeyError, |
| 5473 | "userloop for user dtype not found"); |
| 5474 | result = -1; |
| 5475 | } |
| 5476 | else { |
| 5477 | int cmp = 1; |
| 5478 | PyUFunc_Loop1d *current = PyCapsule_GetPointer(cobj, NULL); |
| 5479 | if (current == NULL) { |
| 5480 | result = -1; |