MCPcopy Create free account
hub / github.com/numpy/numpy / _try_convert_from_inherit_tuple

Function _try_convert_from_inherit_tuple

numpy/core/src/multiarray/descriptor.c:842–906  ·  view source on GitHub ↗

* A tuple type would be either (generic typeobject, typesize) * or (fixed-length data-type, shape) * * or (inheriting data-type, new-data-type) * The new data-type must have the same itemsize as the inheriting data-type * unless the latter is 0 * * Thus (int32, {'real':(int16,0),'imag',(int16,2)}) * * is one way to specify a descriptor that will give * a['real'] and a['imag'] to an int32

Source from the content-addressed store, hash-verified

840 * appropriate.
841 */
842static PyArray_Descr *
843_try_convert_from_inherit_tuple(PyArray_Descr *type, PyObject *newobj)
844{
845 if (PyArray_IsScalar(newobj, Integer) || _is_tuple_of_integers(newobj)) {
846 /* It's a subarray or flexible type instead */
847 Py_INCREF(Py_NotImplemented);
848 return (PyArray_Descr *)Py_NotImplemented;
849 }
850 PyArray_Descr *conv = _convert_from_any(newobj, 0);
851 if (conv == NULL) {
852 /* Let someone else try to convert this */
853 PyErr_Clear();
854 Py_INCREF(Py_NotImplemented);
855 return (PyArray_Descr *)Py_NotImplemented;
856 }
857 PyArray_Descr *new = PyArray_DescrNew(type);
858 if (new == NULL) {
859 goto fail;
860 }
861 if (PyDataType_ISUNSIZED(new)) {
862 new->elsize = conv->elsize;
863 }
864 else if (new->elsize != conv->elsize) {
865 PyErr_SetString(PyExc_ValueError,
866 "mismatch in size of old and new data-descriptor");
867 Py_DECREF(new);
868 goto fail;
869 }
870 else if (_validate_union_object_dtype(new, conv) < 0) {
871 Py_DECREF(new);
872 goto fail;
873 }
874
875 if (PyDataType_HASFIELDS(conv)) {
876 Py_XDECREF(new->fields);
877 new->fields = conv->fields;
878 Py_XINCREF(new->fields);
879
880 Py_XDECREF(new->names);
881 new->names = conv->names;
882 Py_XINCREF(new->names);
883 }
884 if (conv->metadata != NULL) {
885 Py_XDECREF(new->metadata);
886 new->metadata = conv->metadata;
887 Py_XINCREF(new->metadata);
888 }
889 /*
890 * Certain flags must be inherited from the fields. This is needed
891 * only for void dtypes (or subclasses of it such as a record dtype).
892 * For other dtypes, the field part will only be used for direct field
893 * access and thus flag inheritance should not be necessary.
894 * (We only allow object fields if the dtype is object as well.)
895 * This ensures copying over of the NPY_FROM_FIELDS "inherited" flags.
896 */
897 if (new->type_num == NPY_VOID) {
898 new->flags = conv->flags;
899 }

Callers 1

_convert_from_tupleFunction · 0.85

Calls 4

_is_tuple_of_integersFunction · 0.85
_convert_from_anyFunction · 0.85
PyArray_DescrNewFunction · 0.85

Tested by

no test coverage detected