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

Function PyArray_DescrNew

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

NUMPY_API * base cannot be NULL */

Source from the content-addressed store, hash-verified

1901 * base cannot be NULL
1902 */
1903NPY_NO_EXPORT PyArray_Descr *
1904PyArray_DescrNew(PyArray_Descr *base)
1905{
1906 PyArray_Descr *newdescr = PyObject_New(PyArray_Descr, Py_TYPE(base));
1907
1908 if (newdescr == NULL) {
1909 return NULL;
1910 }
1911 /* Don't copy PyObject_HEAD part */
1912 memcpy((char *)newdescr + sizeof(PyObject),
1913 (char *)base + sizeof(PyObject),
1914 sizeof(PyArray_Descr) - sizeof(PyObject));
1915
1916 /*
1917 * The c_metadata has a by-value ownership model, need to clone it
1918 * (basically a deep copy, but the auxdata clone function has some
1919 * flexibility still) so the new PyArray_Descr object owns
1920 * a copy of the data. Having both 'base' and 'newdescr' point to
1921 * the same auxdata pointer would cause a double-free of memory.
1922 */
1923 if (base->c_metadata != NULL) {
1924 newdescr->c_metadata = NPY_AUXDATA_CLONE(base->c_metadata);
1925 if (newdescr->c_metadata == NULL) {
1926 PyErr_NoMemory();
1927 /* TODO: This seems wrong, as the old fields get decref'd? */
1928 Py_DECREF(newdescr);
1929 return NULL;
1930 }
1931 }
1932
1933 if (newdescr->fields == Py_None) {
1934 newdescr->fields = NULL;
1935 }
1936 Py_XINCREF(newdescr->fields);
1937 Py_XINCREF(newdescr->names);
1938 if (newdescr->subarray) {
1939 newdescr->subarray = PyArray_malloc(sizeof(PyArray_ArrayDescr));
1940 if (newdescr->subarray == NULL) {
1941 Py_DECREF(newdescr);
1942 return (PyArray_Descr *)PyErr_NoMemory();
1943 }
1944 memcpy(newdescr->subarray, base->subarray, sizeof(PyArray_ArrayDescr));
1945 Py_INCREF(newdescr->subarray->shape);
1946 Py_INCREF(newdescr->subarray->base);
1947 }
1948 Py_XINCREF(newdescr->typeobj);
1949 Py_XINCREF(newdescr->metadata);
1950 newdescr->hash = -1;
1951
1952 return newdescr;
1953}
1954
1955/*
1956 * should never be called for builtin-types unless

Callers 14

_get_partFunction · 0.85
array_sortFunction · 0.85
array_partitionFunction · 0.85
array_argsortFunction · 0.85
array_argpartitionFunction · 0.85
array_setstateFunction · 0.85
PyArray_CheckFromAny_intFunction · 0.85
void_default_descrFunction · 0.85
void_ensure_canonicalFunction · 0.85
void_common_instanceFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected