NUMPY_API * Return descr object from array scalar. * * New reference */
| 529 | * New reference |
| 530 | */ |
| 531 | NPY_NO_EXPORT PyArray_Descr * |
| 532 | PyArray_DescrFromScalar(PyObject *sc) |
| 533 | { |
| 534 | int type_num; |
| 535 | PyArray_Descr *descr; |
| 536 | |
| 537 | if (PyArray_IsScalar(sc, Void)) { |
| 538 | descr = ((PyVoidScalarObject *)sc)->descr; |
| 539 | Py_INCREF(descr); |
| 540 | return descr; |
| 541 | } |
| 542 | |
| 543 | if (PyArray_IsScalar(sc, Datetime) || PyArray_IsScalar(sc, Timedelta)) { |
| 544 | PyArray_DatetimeMetaData *dt_data; |
| 545 | |
| 546 | if (PyArray_IsScalar(sc, Datetime)) { |
| 547 | descr = PyArray_DescrNewFromType(NPY_DATETIME); |
| 548 | } |
| 549 | else { |
| 550 | /* Timedelta */ |
| 551 | descr = PyArray_DescrNewFromType(NPY_TIMEDELTA); |
| 552 | } |
| 553 | if (descr == NULL) { |
| 554 | return NULL; |
| 555 | } |
| 556 | dt_data = &(((PyArray_DatetimeDTypeMetaData *)descr->c_metadata)->meta); |
| 557 | memcpy(dt_data, &((PyDatetimeScalarObject *)sc)->obmeta, |
| 558 | sizeof(PyArray_DatetimeMetaData)); |
| 559 | |
| 560 | return descr; |
| 561 | } |
| 562 | |
| 563 | descr = PyArray_DescrFromTypeObject((PyObject *)Py_TYPE(sc)); |
| 564 | if (descr == NULL) { |
| 565 | return NULL; |
| 566 | } |
| 567 | if (PyDataType_ISUNSIZED(descr)) { |
| 568 | PyArray_DESCR_REPLACE(descr); |
| 569 | if (descr == NULL) { |
| 570 | return NULL; |
| 571 | } |
| 572 | type_num = descr->type_num; |
| 573 | if (type_num == NPY_STRING) { |
| 574 | descr->elsize = PyBytes_GET_SIZE(sc); |
| 575 | } |
| 576 | else if (type_num == NPY_UNICODE) { |
| 577 | descr->elsize = PyUnicode_GET_LENGTH(sc) * 4; |
| 578 | } |
| 579 | else { |
| 580 | PyArray_Descr *dtype; |
| 581 | dtype = (PyArray_Descr *)PyObject_GetAttrString(sc, "dtype"); |
| 582 | if (dtype != NULL) { |
| 583 | descr->elsize = dtype->elsize; |
| 584 | descr->fields = dtype->fields; |
| 585 | Py_XINCREF(dtype->fields); |
| 586 | descr->names = dtype->names; |
| 587 | Py_XINCREF(dtype->names); |
| 588 | Py_DECREF(dtype); |
no test coverage detected