@@ -1449,23 +1449,6 @@ PyArray_EquivTypes(PyArray_Descr *type1, PyArray_Descr *type2)
1449
1449
return 1 ;
1450
1450
}
1451
1451
1452
- if (Py_TYPE (Py_TYPE (type1 )) == & PyType_Type ) {
1453
- /*
1454
- * 2021-12-17: This case is nonsense and should be removed eventually!
1455
- *
1456
- * boost::python has/had a bug effectively using EquivTypes with
1457
- * `type(arbitrary_obj)`. That is clearly wrong as that cannot be a
1458
- * `PyArray_Descr *`. We assume that `type(type(type(arbitrary_obj))`
1459
- * is always in practice `type` (this is the type of the metaclass),
1460
- * but for our descriptors, `type(type(descr))` is DTypeMeta.
1461
- *
1462
- * In that case, we just return False. There is a possibility that
1463
- * this actually _worked_ effectively (returning 1 sometimes).
1464
- * We ignore that possibility for simplicity; it really is not our bug.
1465
- */
1466
- return 0 ;
1467
- }
1468
-
1469
1452
/*
1470
1453
* Do not use PyArray_CanCastTypeTo because it supports legacy flexible
1471
1454
* dtypes as input.
@@ -1482,45 +1465,29 @@ PyArray_EquivTypes(PyArray_Descr *type1, PyArray_Descr *type2)
1482
1465
1483
1466
1484
1467
/*
1485
- * This function returns true if a view can be safely created
1486
- * between the two types. This implies that PyArray_EquivTypes
1487
- * is true as well .
1468
+ * This function returns true if the two types can be safely cast at
1469
+ * *minimum_safety* casting level. Sets the view_offset if that is set
1470
+ * for the cast. If ignore_error is 1, errors in cast setup are ignored .
1488
1471
*/
1489
- NPY_NO_EXPORT unsigned char
1490
- PyArray_ViewableTypes (PyArray_Descr * type1 , PyArray_Descr * type2 )
1472
+ NPY_NO_EXPORT npy_intp
1473
+ PyArray_SafeCast (PyArray_Descr * type1 , PyArray_Descr * type2 ,
1474
+ npy_intp * view_offset , NPY_CASTING minimum_safety ,
1475
+ npy_intp ignore_error )
1491
1476
{
1492
1477
if (type1 == type2 ) {
1478
+ * view_offset = 0 ;
1493
1479
return 1 ;
1494
1480
}
1495
1481
1496
- if (Py_TYPE (Py_TYPE (type1 )) == & PyType_Type ) {
1497
- /*
1498
- * 2021-12-17: This case is nonsense and should be removed eventually!
1499
- *
1500
- * boost::python has/had a bug effectively using EquivTypes with
1501
- * `type(arbitrary_obj)`. That is clearly wrong as that cannot be a
1502
- * `PyArray_Descr *`. We assume that `type(type(type(arbitrary_obj))`
1503
- * is always in practice `type` (this is the type of the metaclass),
1504
- * but for our descriptors, `type(type(descr))` is DTypeMeta.
1505
- *
1506
- * In that case, we just return False. There is a possibility that
1507
- * this actually _worked_ effectively (returning 1 sometimes).
1508
- * We ignore that possibility for simplicity; it really is not our bug.
1509
- */
1510
- return 0 ;
1511
- }
1512
-
1513
- npy_intp view_offset ;
1514
- NPY_CASTING safety = PyArray_GetCastInfo (type1 , type2 , NULL , & view_offset );
1482
+ NPY_CASTING safety = PyArray_GetCastInfo (type1 , type2 , NULL , view_offset );
1515
1483
if (safety < 0 ) {
1516
- PyErr_Clear ();
1517
- return 0 ;
1518
- }
1519
- if ( view_offset != 0 ) {
1520
- return 0 ;
1484
+ if ( ignore_error ) {
1485
+ PyErr_Clear () ;
1486
+ return 0 ;
1487
+ }
1488
+ return -1 ;
1521
1489
}
1522
- /* If casting is "no casting" this dtypes are considered equivalent. */
1523
- return PyArray_MinCastSafety (safety , NPY_NO_CASTING ) == NPY_NO_CASTING ;
1490
+ return PyArray_MinCastSafety (safety , minimum_safety ) == minimum_safety ;
1524
1491
}
1525
1492
1526
1493
@@ -1652,11 +1619,10 @@ _array_fromobject_generic(
1652
1619
1653
1620
/* One more chance for faster exit if user specified the dtype. */
1654
1621
oldtype = PyArray_DESCR (oparr );
1655
- unsigned char viewable = PyArray_ViewableTypes (oldtype , dtype );
1656
- if (viewable < 0 ) {
1657
- goto finish ;
1658
- }
1659
- if (viewable ) {
1622
+ npy_intp view_offset ;
1623
+ npy_intp is_safe = PyArray_SafeCast (oldtype , dtype , & view_offset , NPY_NO_CASTING , 1 );
1624
+ npy_intp view_safe = (is_safe && (view_offset == 0 ));
1625
+ if (view_safe ) {
1660
1626
if (copy != NPY_COPY_ALWAYS && STRIDING_OK (oparr , order )) {
1661
1627
if (oldtype == dtype ) {
1662
1628
Py_INCREF (op );
0 commit comments