39
39
40
40
static PyObject * typeDict = NULL ; /* Must be explicitly loaded */
41
41
42
+ /*
43
+ * Generate a vague error message when a function returned NULL but forgot
44
+ * to set an exception. We should aim to remove this eventually.
45
+ */
46
+ static void
47
+ _report_generic_error (void ) {
48
+ PyErr_SetString (PyExc_TypeError , "data type not understood" );
49
+ }
50
+
42
51
static PyArray_Descr *
43
52
_use_inherit (PyArray_Descr * type , PyObject * newobj , int * errflag );
44
53
54
+ static PyArray_Descr *
55
+ _convert_from_any (PyObject * obj , int align );
56
+
45
57
static PyArray_Descr *
46
58
_arraydescr_run_converter (PyObject * arg , int align )
47
59
{
48
- PyArray_Descr * type = NULL ;
49
- if (align ) {
50
- if (PyArray_DescrAlignConverter (arg , & type ) == NPY_FAIL ) {
51
- return NULL ;
52
- }
53
- }
54
- else {
55
- if (PyArray_DescrConverter (arg , & type ) == NPY_FAIL ) {
56
- return NULL ;
57
- }
60
+ PyArray_Descr * type = _convert_from_any (arg , align );
61
+ /* TODO: fix the `_convert` functions so that this can never happen */
62
+ if (type == NULL && !PyErr_Occurred ()) {
63
+ _report_generic_error ();
58
64
}
59
- assert (type != NULL );
60
65
return type ;
61
66
}
62
67
@@ -1369,20 +1374,12 @@ _convert_from_type(PyObject *obj) {
1369
1374
}
1370
1375
}
1371
1376
1372
- /*
1373
- * Generate a vague error message when a function returned NULL but forgot
1374
- * to set an exception. We should aim to remove this eventually.
1375
- */
1376
- static void
1377
- _report_generic_error (void ) {
1378
- PyErr_SetString (PyExc_TypeError , "data type not understood" );
1379
- }
1380
1377
1381
1378
static PyArray_Descr *
1382
- _convert_from_bytes (PyObject * obj );
1379
+ _convert_from_bytes (PyObject * obj , int align );
1383
1380
1384
1381
static PyArray_Descr *
1385
- _convert_from_any (PyObject * obj )
1382
+ _convert_from_any (PyObject * obj , int align )
1386
1383
{
1387
1384
/* default */
1388
1385
if (obj == Py_None ) {
@@ -1409,24 +1406,24 @@ _convert_from_any(PyObject *obj)
1409
1406
}
1410
1407
return NULL ;
1411
1408
}
1412
- PyArray_Descr * ret = _convert_from_any (obj2 );
1409
+ PyArray_Descr * ret = _convert_from_any (obj2 , align );
1413
1410
Py_DECREF (obj2 );
1414
1411
return ret ;
1415
1412
}
1416
1413
else if (PyBytes_Check (obj )) {
1417
- return _convert_from_bytes (obj );
1414
+ return _convert_from_bytes (obj , align );
1418
1415
}
1419
1416
else if (PyTuple_Check (obj )) {
1420
1417
/* or a tuple */
1421
- return _convert_from_tuple (obj , 0 );
1418
+ return _convert_from_tuple (obj , align );
1422
1419
}
1423
1420
else if (PyList_Check (obj )) {
1424
1421
/* or a list */
1425
- return _convert_from_array_descr (obj , 0 );
1422
+ return _convert_from_array_descr (obj , align );
1426
1423
}
1427
1424
else if (PyDict_Check (obj ) || PyDictProxy_Check (obj )) {
1428
1425
/* or a dictionary */
1429
- return _convert_from_dict (obj , 0 );
1426
+ return _convert_from_dict (obj , align );
1430
1427
}
1431
1428
else if (PyArray_Check (obj )) {
1432
1429
_report_generic_error ();
@@ -1473,16 +1470,13 @@ _convert_from_any(PyObject *obj)
1473
1470
NPY_NO_EXPORT int
1474
1471
PyArray_DescrConverter (PyObject * obj , PyArray_Descr * * at )
1475
1472
{
1476
- * at = _convert_from_any (obj );
1477
- if (* at == NULL && !PyErr_Occurred ()) {
1478
- _report_generic_error ();
1479
- }
1473
+ * at = _arraydescr_run_converter (obj , 0 );
1480
1474
return (* at ) ? NPY_SUCCEED : NPY_FAIL ;
1481
1475
}
1482
1476
1483
1477
/** Convert a bytestring specification into a dtype */
1484
1478
static PyArray_Descr *
1485
- _convert_from_bytes (PyObject * obj )
1479
+ _convert_from_bytes (PyObject * obj , int align )
1486
1480
{
1487
1481
/* Check for a string typecode. */
1488
1482
char * type = NULL ;
@@ -1498,7 +1492,7 @@ _convert_from_bytes(PyObject *obj)
1498
1492
1499
1493
/* check for commas present or first (or second) element a digit */
1500
1494
if (_check_for_commastring (type , len )) {
1501
- return _convert_from_commastring (obj , 0 );
1495
+ return _convert_from_commastring (obj , align );
1502
1496
}
1503
1497
1504
1498
/* Process the endian character. '|' is replaced by '='*/
@@ -1631,7 +1625,11 @@ _convert_from_bytes(PyObject *obj)
1631
1625
}
1632
1626
}
1633
1627
}
1634
- return _convert_from_any (item );
1628
+ /*
1629
+ * Probably only ever dispatches to `_convert_from_type`, but who
1630
+ * knows what users are injecting into `np.typeDict`.
1631
+ */
1632
+ return _convert_from_any (item , align );
1635
1633
}
1636
1634
1637
1635
if (PyDataType_ISUNSIZED (ret ) && ret -> elsize != elsize ) {
@@ -2828,35 +2826,8 @@ arraydescr_setstate(PyArray_Descr *self, PyObject *args)
2828
2826
NPY_NO_EXPORT int
2829
2827
PyArray_DescrAlignConverter (PyObject * obj , PyArray_Descr * * at )
2830
2828
{
2831
- if (PyDict_Check (obj ) || PyDictProxy_Check (obj )) {
2832
- * at = _convert_from_dict (obj , 1 );
2833
- }
2834
- else if (PyBytes_Check (obj )) {
2835
- * at = _convert_from_commastring (obj , 1 );
2836
- }
2837
- else if (PyUnicode_Check (obj )) {
2838
- PyObject * tmp ;
2839
- tmp = PyUnicode_AsASCIIString (obj );
2840
- * at = _convert_from_commastring (tmp , 1 );
2841
- Py_DECREF (tmp );
2842
- }
2843
- else if (PyTuple_Check (obj )) {
2844
- * at = _convert_from_tuple (obj , 1 );
2845
- }
2846
- else if (PyList_Check (obj )) {
2847
- * at = _convert_from_array_descr (obj , 1 );
2848
- }
2849
- else {
2850
- return PyArray_DescrConverter (obj , at );
2851
- }
2852
- if (* at == NULL ) {
2853
- if (!PyErr_Occurred ()) {
2854
- PyErr_SetString (PyExc_ValueError ,
2855
- "data-type-descriptor not understood" );
2856
- }
2857
- return NPY_FAIL ;
2858
- }
2859
- return NPY_SUCCEED ;
2829
+ * at = _arraydescr_run_converter (obj , 1 );
2830
+ return (* at ) ? NPY_SUCCEED : NPY_FAIL ;
2860
2831
}
2861
2832
2862
2833
/*NUMPY_API
0 commit comments