8000 MAINT: fix thread-unsafe cache initialization in PyUFunc_TrueDivisionTypeResolver by ngoldbaum · Pull Request #26497 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

MAINT: fix thread-unsafe cache initialization in PyUFunc_TrueDivisionTypeResolver #26497

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions numpy/_core/src/umath/ufunc_type_resolution.c
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,25 @@ PyUFunc_RemainderTypeResolver(PyUFuncObject *ufunc,
}


static PyObject *default_truediv_type_tup = NULL;

NPY_NO_EXPORT int
init_ufunc_type_resolution_cache() {
/* Initialize default_truediv_type_tup global */
PyArray_Descr *tmp = PyArray_DescrFromType(NPY_DOUBLE);

if (tmp == NULL) {
return -1;
}
default_truediv_type_tup = PyTuple_Pack(3, tmp, tmp, tmp);
if (default_truediv_type_tup == NULL) {
Py_DECREF(tmp);
return -1;
}
Py_DECREF(tmp);
return 0;
}

/*
* True division should return float64 results when both inputs are integer
* types. The PyUFunc_DefaultTypeResolver promotes 8 bit integers to float16
Expand All @@ -1441,22 +1460,6 @@ PyUFunc_TrueDivisionTypeResolver(PyUFuncObject *ufunc,
PyArray_Descr **out_dtypes)
{
int type_num1, type_num2;
static PyObject *default_type_tup = NULL;

/* Set default type for integer inputs to NPY_DOUBLE */
if (default_type_tup == NULL) {
PyArray_Descr *tmp = PyArray_DescrFromType(NPY_DOUBLE);

if (tmp == NULL) {
return -1;
}
default_type_tup = PyTuple_Pack(3, tmp, tmp, tmp);
if (default_type_tup == NULL) {
Py_DECREF(tmp);
return -1;
}
Py_DECREF(tmp);
}

type_num1 = PyArray_DESCR(operands[0])->type_num;
type_num2 = PyArray_DESCR(operands[1])->type_num;
Expand All @@ -1465,7 +1468,7 @@ PyUFunc_TrueDivisionTypeResolver(PyUFuncObject *ufunc,
(PyTypeNum_ISINTEGER(type_num1) || PyTypeNum_ISBOOL(type_num1)) &&
(PyTypeNum_ISINTEGER(type_num2) || PyTypeNum_ISBOOL(type_num2))) {
return PyUFunc_DefaultTypeResolver(ufunc, casting, operands,
default_type_tup, out_dtypes);
default_truediv_type_tup, out_dtypes);
}
return PyUFunc_DivisionTypeResolver(ufunc, casting, operands,
type_tup, out_dtypes);
Expand Down
3 changes: 3 additions & 0 deletions numpy/_core/src/umath/ufunc_type_resolution.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ PyUFunc_MultiplicationTypeResolver(PyUFuncObject *ufunc,
PyObject *type_tup,
PyArray_Descr **out_dtypes);

NPY_NO_EXPORT int
init_ufunc_type_resolution_cache();

NPY_NO_EXPORT int
PyUFunc_TrueDivisionTypeResolver(PyUFuncObject *ufunc,
NPY_CASTING casting,
Expand Down
5 changes: 5 additions & 0 deletions numpy/_core/src/umath/umathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "stringdtype_ufuncs.h"
#include "special_integer_comparisons.h"
#include "extobj.h" /* for _extobject_contextvar exposure */
#include "ufunc_type_resolution.h"

/* Automatically generated code to define all ufuncs: */
#include "funcs.inc"
Expand Down Expand Up @@ -346,5 +347,9 @@ int initumath(PyObject *m)
return -1;
}

if (init_ufunc_type_resolution_cache() < 0) {
return -1;
}

return 0;
}
Loading
0