static void _set_circle_arc_dtypes(PyObject* inexact, PyObject* exact) { GEODE_ASSERT(PyArray_DescrCheck(inexact)); GEODE_ASSERT(PyArray_DescrCheck(exact)); GEODE_ASSERT(((PyArray_Descr*)inexact)->elsize==sizeof(CircleArc)); Py_INCREF(inexact); Py_INCREF( exact); NumpyDescr< CircleArc >::d = (PyArray_Descr*)inexact; }
/* * Walk inside the fields and add every item which will be used for hashing * into the list l * * Return 0 on success */ static int _array_descr_walk_fields(PyObject* fields, PyObject* l) { PyObject *key, *value, *foffset, *fdescr; Py_ssize_t pos = 0; int st; while (PyDict_Next(fields, &pos, &key, &value)) { /* * For each field, add the key + descr + offset to l */ /* XXX: are those checks necessary ? */ if (!PyUString_Check(key)) { PyErr_SetString(PyExc_SystemError, "(Hash) key of dtype dict not a string ???"); return -1; } if (!PyTuple_Check(value)) { PyErr_SetString(PyExc_SystemError, "(Hash) value of dtype dict not a dtype ???"); return -1; } if (PyTuple_Size(value) < 2) { PyErr_SetString(PyExc_SystemError, "(Hash) Less than 2 items in dtype dict ???"); return -1; } Py_INCREF(key); PyList_Append(l, key); fdescr = PyTuple_GetItem(value, 0); if (!PyArray_DescrCheck(fdescr)) { PyErr_SetString(PyExc_SystemError, "(Hash) First item in compound dtype tuple not a descr ???"); return -1; } else { Py_INCREF(fdescr); st = _array_descr_walk((PyArray_Descr*)fdescr, l); Py_DECREF(fdescr); if (st) { return -1; } } foffset = PyTuple_GetItem(value, 1); if (!PyInt_Check(foffset)) { PyErr_SetString(PyExc_SystemError, "(Hash) Second item in compound dtype tuple not an int ???"); return -1; } else { Py_INCREF(foffset); PyList_Append(l, foffset); } } return 0; }
NPY_NO_EXPORT PyArray_Descr* _descriptor_from_pep3118_format(char *s) { char *buf, *p; int in_name = 0; PyObject *descr; PyObject *str; PyObject *_numpy_internal; if (s == NULL) { return PyArray_DescrNewFromType(NPY_BYTE); } /* Strip whitespace, except from field names */ buf = (char*)malloc(strlen(s) + 1); p = buf; while (*s != '\0') { if (*s == ':') { in_name = !in_name; *p = *s; } else if (in_name || !NumPyOS_ascii_isspace(*s)) { *p = *s; } ++p; ++s; } *p = '\0'; str = PyUString_FromStringAndSize(buf, strlen(buf)); free(buf); if (str == NULL) { return NULL; } /* Convert */ _numpy_internal = PyImport_ImportModule("numpy.core._internal"); if (_numpy_internal == NULL) { Py_DECREF(str); return NULL; } descr = PyObject_CallMethod( _numpy_internal, "_dtype_from_pep3118", "O", str); Py_DECREF(str); Py_DECREF(_numpy_internal); if (descr == NULL) { PyErr_Format(PyExc_ValueError, "'%s' is not a valid PEP 3118 buffer format string", buf); return NULL; } if (!PyArray_DescrCheck(descr)) { PyErr_Format(PyExc_RuntimeError, "internal error: numpy.core._internal._dtype_from_pep3118 " "did not return a valid dtype, got %s", buf); return NULL; } return (PyArray_Descr*)descr; }
NPY_NO_EXPORT long PyArray_DescrHash(PyObject* odescr) { PyArray_Descr *descr; int st; long hash; if (!PyArray_DescrCheck(odescr)) { PyErr_SetString(PyExc_ValueError, "PyArray_DescrHash argument must be a type descriptor"); return -1; } descr = (PyArray_Descr*)odescr; st = _PyArray_DescrHashImp(descr, &hash); if (st) { return -1; } return hash; }
NPY_NO_EXPORT npy_hash_t PyArray_DescrHash(PyObject* odescr) { PyArray_Descr *descr; int st; if (!PyArray_DescrCheck(odescr)) { PyErr_SetString(PyExc_ValueError, "PyArray_DescrHash argument must be a type descriptor"); return -1; } descr = (PyArray_Descr*)odescr; if (descr->hash == -1) { st = _PyArray_DescrHashImp(descr, &descr->hash); if (st) { return -1; } } return descr->hash; }
NRT_adapt_ndarray_to_python(arystruct_t* arystruct, int ndim, int writeable, PyArray_Descr *descr) { PyArrayObject *array; MemInfoObject *miobj = NULL; PyObject *args; npy_intp *shape, *strides; int flags = 0; if (!PyArray_DescrCheck(descr)) { PyErr_Format(PyExc_TypeError, "expected dtype object, got '%.200s'", Py_TYPE(descr)->tp_name); return NULL; } if (arystruct->parent) { PyObject *obj = try_to_return_parent(arystruct, ndim, descr); if (obj) { /* Release NRT reference to the numpy array */ NRT_MemInfo_release(arystruct->meminfo); return obj; } } if (arystruct->meminfo) { /* wrap into MemInfoObject */ miobj = PyObject_New(MemInfoObject, &MemInfoType); args = PyTuple_New(1); /* SETITEM steals reference */ PyTuple_SET_ITEM(args, 0, PyLong_FromVoidPtr(arystruct->meminfo)); /* Note: MemInfo_init() does not incref. This function steals the * NRT reference. */ if (MemInfo_init(miobj, args, NULL)) { return NULL; } Py_DECREF(args); } shape = arystruct->shape_and_strides; strides = shape + ndim; Py_INCREF((PyObject *) descr); array = (PyArrayObject *) PyArray_NewFromDescr(&PyArray_Type, descr, ndim, shape, strides, arystruct->data, flags, (PyObject *) miobj); if (array == NULL) return NULL; /* Set writable */ #if NPY_API_VERSION >= 0x00000007 if (writeable) { PyArray_ENABLEFLAGS(array, NPY_ARRAY_WRITEABLE); } else { PyArray_CLEARFLAGS(array, NPY_ARRAY_WRITEABLE); } #else if (writeable) { array->flags |= NPY_WRITEABLE; } else { array->flags &= ~NPY_WRITEABLE; } #endif if (miobj) { /* Set the MemInfoObject as the base object */ #if NPY_API_VERSION >= 0x00000007 if (-1 == PyArray_SetBaseObject(array, (PyObject *) miobj)) { Py_DECREF(array); Py_DECREF(miobj); return NULL; } #else PyArray_BASE(array) = (PyObject *) miobj; #endif } return (PyObject *) array; }
/* * Walk inside the fields and add every item which will be used for hashing * into the list l * * Return 0 on success */ static int _array_descr_walk_fields(PyObject *names, PyObject* fields, PyObject* l) { PyObject *key, *value, *foffset, *fdescr, *ftitle; Py_ssize_t pos = 0; int st; if (!PyTuple_Check(names)) { PyErr_SetString(PyExc_SystemError, "(Hash) names is not a tuple ???"); return -1; } if (!PyDict_Check(fields)) { PyErr_SetString(PyExc_SystemError, "(Hash) fields is not a dict ???"); return -1; } for (pos = 0; pos < PyTuple_GET_SIZE(names); pos++) { /* * For each field, add the key + descr + offset to l */ key = PyTuple_GET_ITEM(names, pos); value = PyDict_GetItem(fields, key); /* XXX: are those checks necessary ? */ if (value == NULL) { PyErr_SetString(PyExc_SystemError, "(Hash) names and fields inconsistent ???"); return -1; } if (!PyUString_Check(key)) { PyErr_SetString(PyExc_SystemError, "(Hash) key of dtype dict not a string ???"); return -1; } if (!PyTuple_Check(value)) { PyErr_SetString(PyExc_SystemError, "(Hash) value of dtype dict not a dtype ???"); return -1; } if (PyTuple_GET_SIZE(value) < 2) { PyErr_SetString(PyExc_SystemError, "(Hash) Less than 2 items in dtype dict ???"); return -1; } PyList_Append(l, key); fdescr = PyTuple_GET_ITEM(value, 0); if (!PyArray_DescrCheck(fdescr)) { PyErr_SetString(PyExc_SystemError, "(Hash) First item in compound dtype tuple not a descr ???"); return -1; } else { Py_INCREF(fdescr); st = _array_descr_walk((PyArray_Descr*)fdescr, l); Py_DECREF(fdescr); if (st) { return -1; } } foffset = PyTuple_GET_ITEM(value, 1); if (!PyInt_Check(foffset)) { PyErr_SetString(PyExc_SystemError, "(Hash) Second item in compound dtype tuple not an int ???"); return -1; } else { PyList_Append(l, foffset); } if (PyTuple_GET_SIZE(value) > 2) { ftitle = PyTuple_GET_ITEM(value, 2); PyList_Append(l, ftitle); } } return 0; }