-
-
Notifications
You must be signed in to change notification settings - Fork 5
[BUG]: Avoiding promoter overrides leading to global state corruption #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1ee4998
3412657
34e2117
107bec5
673d115
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -257,16 +257,17 @@ quad_reduce_comp_strided_loop_unaligned(PyArrayMethod_Context *context, char *co | |
|
|
||
|
|
||
| NPY_NO_EXPORT int | ||
| comparison_ufunc_promoter(PyUFuncObject *ufunc, PyArray_DTypeMeta *op_dtypes[], | ||
| PyArray_DTypeMeta *signature[], PyArray_DTypeMeta *new_op_dtypes[]) | ||
| comparison_ufunc_promoter(PyObject *ufunc_obj, PyArray_DTypeMeta *const op_dtypes[], | ||
| PyArray_DTypeMeta *const signature[], PyArray_DTypeMeta *new_op_dtypes[]) | ||
| { | ||
| PyArray_DTypeMeta *new_signature[NPY_MAXARGS]; | ||
| memcpy(new_signature, signature, 3 * sizeof(PyArray_DTypeMeta *)); | ||
| new_signature[2] = NULL; | ||
| int res = quad_ufunc_promoter(ufunc, op_dtypes, new_signature, new_op_dtypes); | ||
| int res = quad_ufunc_promoter(ufunc_obj, op_dtypes, new_signature, new_op_dtypes); | ||
| if (res < 0) { | ||
| return -1; | ||
| } | ||
| Py_INCREF(&PyArray_BoolDType); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch! |
||
| Py_XSETREF(new_op_dtypes[2], &PyArray_BoolDType); | ||
| return 0; | ||
| } | ||
|
|
@@ -301,6 +302,7 @@ create_quad_comparison_ufunc(PyObject *numpy, const char *ufunc_name) | |
| }; | ||
|
|
||
| if (PyUFunc_AddLoopFromSpec(ufunc, &Spec) < 0) { | ||
| Py_DECREF(ufunc); | ||
| return -1; | ||
| } | ||
|
|
||
|
|
@@ -326,25 +328,29 @@ create_quad_comparison_ufunc(PyObject *numpy, const char *ufunc_name) | |
| }; | ||
|
|
||
| if (PyUFunc_AddLoopFromSpec(ufunc, &Spec_reduce) < 0) { | ||
| Py_DECREF(ufunc); | ||
| return -1; | ||
| } | ||
|
|
||
| PyObject *promoter_capsule = | ||
| PyCapsule_New((void *)&comparison_ufunc_promoter, "numpy._ufunc_promoter", NULL); | ||
| if (promoter_capsule == NULL) { | ||
| Py_DECREF(ufunc); | ||
| return -1; | ||
| } | ||
|
|
||
| // Register promoter for (QuadPrecDType, Any, Bool) - needed for mixed-type comparisons | ||
| PyObject *DTypes = PyTuple_Pack(3, &QuadPrecDType, &PyArrayDescr_Type, &PyArray_BoolDType); | ||
| if (DTypes == 0) { | ||
| Py_DECREF(promoter_capsule); | ||
| Py_DECREF(ufunc); | ||
| return -1; | ||
| } | ||
|
|
||
| if (PyUFunc_AddPromoter(ufunc, DTypes, promoter_capsule) < 0) { | ||
| Py_DECREF(promoter_capsule); | ||
| Py_DECREF(DTypes); | ||
| Py_DECREF(ufunc); | ||
| return -1; | ||
| } | ||
| Py_DECREF(DTypes); | ||
|
|
@@ -353,16 +359,19 @@ create_quad_comparison_ufunc(PyObject *numpy, const char *ufunc_name) | |
| DTypes = PyTuple_Pack(3, &PyArrayDescr_Type, &QuadPrecDType, &PyArray_BoolDType); | ||
| if (DTypes == 0) { | ||
| Py_DECREF(promoter_capsule); | ||
| Py_DECREF(ufunc); | ||
| return -1; | ||
| } | ||
|
|
||
| if (PyUFunc_AddPromoter(ufunc, DTypes, promoter_capsule) < 0) { | ||
| Py_DECREF(promoter_capsule); | ||
| Py_DECREF(DTypes); | ||
| Py_DECREF(ufunc); | ||
| return -1; | ||
| } | ||
| Py_DECREF(promoter_capsule); | ||
| Py_DECREF(DTypes); | ||
| Py_DECREF(ufunc); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,9 +12,10 @@ | |
| #include "../dtype.h" | ||
|
|
||
| inline int | ||
| quad_ufunc_promoter(PyUFuncObject *ufunc, PyArray_DTypeMeta *op_dtypes[], | ||
| PyArray_DTypeMeta *signature[], PyArray_DTypeMeta *new_op_dtypes[]) | ||
| quad_ufunc_promoter(PyObject *ufunc_obj, PyArray_DTypeMeta *const op_dtypes[], | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we change the promoter registration to ensure one type remains quaddtype, we can very much simplify this current code ( no need to call |
||
| PyArray_DTypeMeta *const signature[], PyArray_DTypeMeta *new_op_dtypes[]) | ||
| { | ||
| PyUFuncObject *ufunc = (PyUFuncObject *)ufunc_obj; | ||
| int nin = ufunc->nin; | ||
| int nargs = ufunc->nargs; | ||
| PyArray_DTypeMeta *common = NULL; | ||
|
|
@@ -56,7 +57,7 @@ quad_ufunc_promoter(PyUFuncObject *ufunc, PyArray_DTypeMeta *op_dtypes[], | |
| } | ||
| // If no common output dtype, use standard promotion for inputs | ||
| if (common == NULL) { | ||
| common = PyArray_PromoteDTypeSequence(nin, op_dtypes); | ||
| common = PyArray_PromoteDTypeSequence(nin, const_cast<PyArray_DTypeMeta **>(op_dtypes)); | ||
| if (common == NULL) { | ||
| if (PyErr_ExceptionMatches(PyExc_TypeError)) { | ||
| PyErr_Clear(); // Do not propagate normal promotion errors | ||
|
|
@@ -87,4 +88,27 @@ quad_ufunc_promoter(PyUFuncObject *ufunc, PyArray_DTypeMeta *op_dtypes[], | |
| } | ||
|
|
||
|
|
||
| inline int | ||
| quad_ldexp_promoter(PyObject *ufunc_obj, PyArray_DTypeMeta *const op_dtypes[], | ||
| PyArray_DTypeMeta *const signature[], PyArray_DTypeMeta *new_op_dtypes[]) | ||
| { | ||
| Py_INCREF(&QuadPrecDType); | ||
| new_op_dtypes[0] = &QuadPrecDType; | ||
|
|
||
| // Promote the exponent to PyArray_IntpDType (unless signature specifies otherwise) | ||
| if (signature[1] != NULL) { | ||
| Py_INCREF(signature[1]); | ||
| new_op_dtypes[1] = signature[1]; | ||
| } | ||
| else { | ||
| Py_INCREF(&PyArray_IntpDType); | ||
| new_op_dtypes[1] = &PyArray_IntpDType; | ||
| } | ||
|
|
||
| Py_INCREF(&QuadPrecDType); | ||
| new_op_dtypes[2] = &QuadPrecDType; | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| #endif | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same simplification thing here as well + noticed a bug of dtype registration not matching with registered loop