static PyObject * reorderdtype(PyObject * self, PyObject * args, PyObject * kwds) { PyObject *order = NULL; PyArray_Descr *newd, *dtype; static char *kwlist[] = {"dtype", "order", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!O", kwlist, &PyArrayDescr_Type, &dtype, &order)) { return NULL; } printf("compare, %p argsort %p %p %p\n", dtype->f->compare, dtype->f->argsort[0], dtype->f->argsort[1], dtype->f->argsort[2] ); PyObject *new_name; PyObject *_numpy_internal; if (!PyDataType_HASFIELDS(dtype)) { PyErr_SetString(PyExc_ValueError, "Cannot specify " "order when the array has no fields."); return NULL; } _numpy_internal = PyImport_ImportModule("numpy.core._internal"); if (_numpy_internal == NULL) { return NULL; } new_name = PyObject_CallMethod(_numpy_internal, "_newnames", "OO", dtype, order); Py_DECREF(_numpy_internal); if (new_name == NULL) { return NULL; } newd = PyArray_DescrNew(dtype); newd->names = new_name; return newd; }
int Object_npyArrayAddItem(void *prv, JSOBJ obj, JSOBJ value) { PyObject* type; PyArray_Descr* dtype; npy_intp i; char *new_data, *item; NpyArrContext* npyarr = (NpyArrContext*) obj; PRINTMARK(); if (!npyarr) { return 0; } i = npyarr->i; npyarr->shape.ptr[npyarr->dec->curdim-1]++; if (PyArray_Check((PyObject*)value)) { // multidimensional array, keep decoding values. return 1; } if (!npyarr->ret) { // Array not initialised yet. // We do it here so we can 'sniff' the data type if none was provided if (!npyarr->dec->dtype) { type = PyObject_Type(value); if(!PyArray_DescrConverter(type, &dtype)) { Py_DECREF(type); goto fail; } Py_INCREF(dtype); Py_DECREF(type); } else { dtype = PyArray_DescrNew(npyarr->dec->dtype); } // If it's an object or string then fill a Python list and subsequently // convert. Otherwise we would need to somehow mess about with // reference counts when renewing memory. npyarr->elsize = dtype->elsize; if (PyDataType_REFCHK(dtype) || npyarr->elsize == 0) { Py_XDECREF(dtype); if (npyarr->dec->curdim > 1) { PyErr_SetString(PyExc_ValueError, "Cannot decode multidimensional arrays with variable length elements to numpy"); goto fail; } npyarr->elcount = 0; npyarr->ret = PyList_New(0); if (!npyarr->ret) { goto fail; } ((JSONObjectDecoder*)npyarr->dec)->newArray = Object_npyNewArrayList; ((JSONObjectDecoder*)npyarr->dec)->arrayAddItem = Object_npyArrayListAddItem; ((JSONObjectDecoder*)npyarr->dec)->endArray = Object_npyEndArrayList; return Object_npyArrayListAddItem(prv, obj, value); } npyarr->ret = PyArray_NewFromDescr(&PyArray_Type, dtype, 1, &npyarr->elcount, NULL,NULL, 0, NULL); if (!npyarr->ret) { goto fail; } } if (i >= npyarr->elcount) { // Grow PyArray_DATA(ret): // this is similar for the strategy for PyListObject, but we use // 50% overallocation => 0, 4, 8, 14, 23, 36, 56, 86 ... if (npyarr->elsize == 0) { PyErr_SetString(PyExc_ValueError, "Cannot decode multidimensional arrays with variable length elements to numpy"); goto fail; } npyarr->elcount = (i >> 1) + (i < 4 ? 4 : 2) + i; if (npyarr->elcount <= NPY_MAX_INTP/npyarr->elsize) { new_data = PyDataMem_RENEW(PyArray_DATA(npyarr->ret), npyarr->elcount * npyarr->elsize); } else { PyErr_NoMemory(); goto fail; } ((PyArrayObject*) npyarr->ret)->data = (void*) new_data; // PyArray_BYTES(npyarr->ret) = new_data; } PyArray_DIMS(npyarr->ret)[0] = i + 1; if ((item = PyArray_GETPTR1(npyarr->ret, i)) == NULL || PyArray_SETITEM(npyarr->ret, item, value) == -1) { goto fail; } Py_DECREF( (PyObject *) value); npyarr->i++; return 1; fail: Npy_releaseContext(npyarr); return 0; }