Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ee7fcb7

Browse files
authored
Merge pull request #15287 from eric-wieser/simplify-PyArray_DescrAlignConverter
MAINT: Express PyArray_DescrAlignConverter in terms of _convert_from_any
2 parents 48498b8 + b36db66 commit ee7fcb7

File tree

1 file changed

+33
-62
lines changed

1 file changed

+33
-62
lines changed

numpy/core/src/multiarray/descriptor.c

Lines changed: 33 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,29 @@
3939

4040
static PyObject *typeDict = NULL; /* Must be explicitly loaded */
4141

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+
4251
static PyArray_Descr *
4352
_use_inherit(PyArray_Descr *type, PyObject *newobj, int *errflag);
4453

54+
static PyArray_Descr *
55+
_convert_from_any(PyObject *obj, int align);
56+
4557
static PyArray_Descr *
4658
_arraydescr_run_converter(PyObject *arg, int align)
4759
{
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();
5864
}
59-
assert(type != NULL);
6065
return type;
6166
}
6267

@@ -1369,20 +1374,12 @@ _convert_from_type(PyObject *obj) {
13691374
}
13701375
}
13711376

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-
}
13801377

13811378
static PyArray_Descr *
1382-
_convert_from_bytes(PyObject *obj);
1379+
_convert_from_bytes(PyObject *obj, int align);
13831380

13841381
static PyArray_Descr *
1385-
_convert_from_any(PyObject *obj)
1382+
_convert_from_any(PyObject *obj, int align)
13861383
{
13871384
/* default */
13881385
if (obj == Py_None) {
@@ -1409,24 +1406,24 @@ _convert_from_any(PyObject *obj)
14091406
}
14101407
return NULL;
14111408
}
1412-
PyArray_Descr *ret = _convert_from_any(obj2);
1409+
PyArray_Descr *ret = _convert_from_any(obj2, align);
14131410
Py_DECREF(obj2);
14141411
return ret;
14151412
}
14161413
else if (PyBytes_Check(obj)) {
1417-
return _convert_from_bytes(obj);
1414+
return _convert_from_bytes(obj, align);
14181415
}
14191416
else if (PyTuple_Check(obj)) {
14201417
/* or a tuple */
1421-
return _convert_from_tuple(obj, 0);
1418+
return _convert_from_tuple(obj, align);
14221419
}
14231420
else if (PyList_Check(obj)) {
14241421
/* or a list */
1425-
return _convert_from_array_descr(obj, 0);
1422+
return _convert_from_array_descr(obj, align);
14261423
}
14271424
else if (PyDict_Check(obj) || PyDictProxy_Check(obj)) {
14281425
/* or a dictionary */
1429-
return _convert_from_dict(obj, 0);
1426+
return _convert_from_dict(obj, align);
14301427
}
14311428
else if (PyArray_Check(obj)) {
14321429
_report_generic_error();
@@ -1473,16 +1470,13 @@ _convert_from_any(PyObject *obj)
14731470
NPY_NO_EXPORT int
14741471
PyArray_DescrConverter(PyObject *obj, PyArray_Descr **at)
14751472
{
1476-
*at = _convert_from_any(obj);
1477-
if (*at == NULL && !PyErr_Occurred()) {
1478-
_report_generic_error();
1479-
}
1473+
*at = _arraydescr_run_converter(obj, 0);
14801474
return (*at) ? NPY_SUCCEED : NPY_FAIL;
14811475
}
14821476

14831477
/** Convert a bytestring specification into a dtype */
14841478
static PyArray_Descr *
1485-
_convert_from_bytes(PyObject *obj)
1479+
_convert_from_bytes(PyObject *obj, int align)
14861480
{
14871481
/* Check for a string typecode. */
14881482
char *type = NULL;
@@ -1498,7 +1492,7 @@ _convert_from_bytes(PyObject *obj)
14981492

14991493
/* check for commas present or first (or second) element a digit */
15001494
if (_check_for_commastring(type, len)) {
1501-
return _convert_from_commastring(obj, 0);
1495+
return _convert_from_commastring(obj, align);
15021496
}
15031497

15041498
/* Process the endian character. '|' is replaced by '='*/
@@ -1631,7 +1625,11 @@ _convert_from_bytes(PyObject *obj)
16311625
}
16321626
}
16331627
}
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);
16351633
}
16361634

16371635
if (PyDataType_ISUNSIZED(ret) && ret->elsize != elsize) {
@@ -2828,35 +2826,8 @@ arraydescr_setstate(PyArray_Descr *self, PyObject *args)
28282826
NPY_NO_EXPORT int
28292827
PyArray_DescrAlignConverter(PyObject *obj, PyArray_Descr **at)
28302828
{
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;
28602831
}
28612832

28622833
/*NUMPY_API

0 commit comments

Comments
 (0)