@@ -1548,6 +1548,40 @@ should_use_min_scalar(npy_intp narrs, PyArrayObject **arr,
15481548}
15491549
15501550
1551+ /*
1552+ * Utility function used only in PyArray_ResultType for value-based logic.
1553+ * See that function for the meaning and contents of the parameters.
1554+ */
1555+ static PyArray_Descr *
1556+ get_descr_from_cast_or_value (
1557+ npy_intp i ,
1558+ PyArrayObject * arrs [],
1559+ npy_intp ndtypes ,
1560+ PyArray_Descr * descriptor ,
1561+ PyArray_DTypeMeta * common_dtype )
1562+ {
1563+ PyArray_Descr * curr ;
1564+ if (NPY_LIKELY (i < ndtypes ||
1565+ !(PyArray_FLAGS (arrs [i - ndtypes ]) & _NPY_ARRAY_WAS_PYSCALAR ))) {
1566+ curr = PyArray_CastDescrToDType (descriptor , common_dtype );
1567+ }
1568+ else {
1569+ /*
1570+ * Unlike `PyArray_CastToDTypeAndPromoteDescriptors`, deal with
1571+ * plain Python values "graciously". This recovers the original
1572+ * value the long route, but it should almost never happen...
1573+ */
1574+ PyObject * tmp = PyArray_GETITEM (arrs [i - ndtypes ],
1575+ PyArray_BYTES (arrs [i - ndtypes ]));
1576+ if (tmp == NULL ) {
1577+ return NULL ;
1578+ }
1579+ curr = NPY_DT_CALL_discover_descr_from_pyobject (common_dtype , tmp );
1580+ Py_DECREF (tmp );
1581+ }
1582+ return curr ;
1583+ }
1584+
15511585/*NUMPY_API
15521586 *
15531587 * Produces the result type of a bunch of inputs, using the same rules
@@ -1684,28 +1718,15 @@ PyArray_ResultType(
16841718 result = NPY_DT_CALL_default_descr (common_dtype );
16851719 }
16861720 else {
1687- result = PyArray_CastDescrToDType (all_descriptors [0 ], common_dtype );
1721+ result = get_descr_from_cast_or_value (
1722+ 0 , arrs , ndtypes , all_descriptors [0 ], common_dtype );
1723+ if (result == NULL ) {
1724+ goto error ;
1725+ }
16881726
16891727 for (npy_intp i = 1 ; i < ndtypes + narrs ; i ++ ) {
1690- PyArray_Descr * curr ;
1691- if (NPY_LIKELY (i < ndtypes ||
1692- !(PyArray_FLAGS (arrs [i - ndtypes ]) & _NPY_ARRAY_WAS_PYSCALAR ))) {
1693- curr = PyArray_CastDescrToDType (all_descriptors [i ], common_dtype );
1694- }
1695- else {
1696- /*
1697- * Unlike `PyArray_CastToDTypeAndPromoteDescriptors` deal with
1698- * plain Python values "graciously". This recovers the original
1699- * value the long route, but it should almost never happen...
1700- */
1701- PyObject * tmp = PyArray_GETITEM (
1702- arrs [i - ndtypes ], PyArray_BYTES (arrs [i - ndtypes ]));
1703- if (tmp == NULL ) {
1704- goto error ;
1705- }
1706- curr = NPY_DT_CALL_discover_descr_from_pyobject (common_dtype , tmp );
1707- Py_DECREF (tmp );
1708- }
1728+ PyArray_Descr * curr = get_descr_from_cast_or_value (
1729+ i , arrs , ndtypes , all_descriptors [i ], common_dtype );
17091730 if (curr == NULL ) {
17101731 goto error ;
17111732 }
0 commit comments