NPY_NO_EXPORT npy_bool _IsWriteable(PyArrayObject *ap) { PyObject *base=PyArray_BASE(ap); #if defined(NPY_PY3K) Py_buffer view; #else void *dummy; Py_ssize_t n; #endif /* If we own our own data, then no-problem */ if ((base == NULL) || (PyArray_FLAGS(ap) & NPY_ARRAY_OWNDATA)) { return NPY_TRUE; } /* * Get to the final base object * If it is a writeable array, then return TRUE * If we can find an array object * or a writeable buffer object as the final base object * or a string object (for pickling support memory savings). * - this last could be removed if a proper pickleable * buffer was added to Python. * * MW: I think it would better to disallow switching from READONLY * to WRITEABLE like this... */ while(PyArray_Check(base)) { if (PyArray_CHKFLAGS((PyArrayObject *)base, NPY_ARRAY_OWNDATA)) { return (npy_bool) (PyArray_ISWRITEABLE((PyArrayObject *)base)); } base = PyArray_BASE((PyArrayObject *)base); } /* * here so pickle support works seamlessly * and unpickled array can be set and reset writeable * -- could be abused -- */ if (PyString_Check(base)) { return NPY_TRUE; } #if defined(NPY_PY3K) if (PyObject_GetBuffer(base, &view, PyBUF_WRITABLE|PyBUF_SIMPLE) < 0) { PyErr_Clear(); return NPY_FALSE; } PyBuffer_Release(&view); #else if (PyObject_AsWriteBuffer(base, &dummy, &n) < 0) { PyErr_Clear(); return NPY_FALSE; } #endif return NPY_TRUE; }
/*@null@*/ static INLINE PyObject* _PyArrayProxy_New( /*@shared@*/ PyObject* self, int nd, const npy_intp* dims, int typenum, const void* data, const int flags) { PyArray_Descr* type_descr = NULL; PyObject* result = NULL; type_descr = (PyArray_Descr*)PyArray_DescrFromType(typenum); if (type_descr == NULL) { return NULL; } result = (PyObject*)PyArray_NewFromDescr( &PyArray_Type, type_descr, nd, (npy_intp*)dims, NULL, (void*)data, NPY_C_CONTIGUOUS | flags, NULL); if (result == NULL) { return NULL; } Py_INCREF(self); PyArray_BASE(result) = (PyObject*)self; return result; }
static PyObject *premalloced_npy_double_array(double *data, npy_intp len) { PyObject *premalloced = premalloced_new(data); if (!premalloced) return NULL; npy_intp dims[1] = {len}; PyArrayObject *out = (PyArrayObject *) PyArray_SimpleNewFromData(1, dims, NPY_DOUBLE, data); if (!out) { Py_DECREF(premalloced); return NULL; } #ifdef PyArray_BASE /* FIXME: PyArray_BASE changed from a macro to a getter function in * Numpy 1.7. When we drop Numpy 1.6 support, remove this #ifdef block. */ PyArray_BASE(out) = premalloced; #else if (PyArray_SetBaseObject(out, premalloced)) { Py_DECREF(out); return NULL; } #endif return (PyObject *)out; }
PyObject * wrap_double_array(double* d, int len) { init_numpy(); PyArray_Descr *type = PyArray_DescrFromType(NPY_DOUBLE); npy_intp dim[1] = {len}; npy_intp strides[1] = {sizeof(double)}; PyObject* arr = PyArray_NewFromDescr( &PyArray_Type, type, 1, dim, strides, d, NPY_CONTIGUOUS | NPY_WRITEABLE, NULL /*PyObject *obj*/ ); PyArray_BASE(arr) = make_deallocator_for(d, 0); return arr; }
PyObject * wrapDMat(DMat d) { init_numpy(); PyArray_Descr *type = PyArray_DescrFromType(NPY_DOUBLE); npy_intp dim[2] = {d->rows, d->cols}; npy_intp strides[2] = {d->cols*sizeof(double), sizeof(double)}; PyObject* arr = PyArray_NewFromDescr( &PyArray_Type, type, 2, dim, strides, d->value[0], NPY_CONTIGUOUS | NPY_WRITEABLE, NULL /*PyObject *obj*/ ); PyArray_BASE(arr) = make_deallocator_for(d->value, 1); return arr; }
static PyObject *pixbuf_get_pixels_array(PyObject *self, PyObject *args) { /* 1) read in Python pixbuf, get the underlying gdk_pixbuf */ PyGObject *py_pixbuf; GdkPixbuf *gdk_pixbuf; PyArrayObject *array; npy_intp dims[3] = { 0, 0, 3 }; npy_intp strides[3]; if (!PyArg_ParseTuple(args, "O!:pixbuf_get_pixels_array", &PyGdkPixbuf_Type, &py_pixbuf)) return NULL; gdk_pixbuf = GDK_PIXBUF(py_pixbuf->obj); /* 2) same as pygtk/gtk/gdk.c _wrap_gdk_pixbuf_get_pixels_array() * with 'self' changed to py_pixbuf */ dims[0] = gdk_pixbuf_get_height(gdk_pixbuf); dims[1] = gdk_pixbuf_get_width(gdk_pixbuf); if (gdk_pixbuf_get_has_alpha(gdk_pixbuf)) dims[2] = 4; strides[0] = gdk_pixbuf_get_rowstride(gdk_pixbuf); strides[1] = dims[2]; strides[2] = 1; array = (PyArrayObject*) PyArray_New(&PyArray_Type, 3, dims, NPY_UBYTE, strides, (void*)gdk_pixbuf_get_pixels(gdk_pixbuf), 1, NPY_ARRAY_WRITEABLE, NULL); if (array == NULL) return NULL; /* the array holds a ref to the pixbuf pixels through this wrapper*/ Py_INCREF(py_pixbuf); #if NPY_API_VERSION >= 0x00000007 if (PyArray_SetBaseObject(array, (PyObject *)py_pixbuf) == -1) { Py_DECREF(py_pixbuf); Py_DECREF(array); return NULL; } #else PyArray_BASE(array) = (PyObject *) py_pixbuf; #endif return PyArray_Return(array); }
py::handle<> host_pool_allocate( boost::shared_ptr<pycuda::memory_pool<host_allocator> > pool, py::object shape, py::object dtype, py::object order_py) { PyArray_Descr *tp_descr; if (PyArray_DescrConverter(dtype.ptr(), &tp_descr) != NPY_SUCCEED) throw py::error_already_set(); std::vector<npy_intp> dims; std::copy( py::stl_input_iterator<npy_intp>(shape), py::stl_input_iterator<npy_intp>(), back_inserter(dims)); std::auto_ptr<pooled_host_allocation> alloc( new pooled_host_allocation( pool, tp_descr->elsize*pycuda::size_from_dims(dims.size(), &dims.front()))); NPY_ORDER order = PyArray_CORDER; PyArray_OrderConverter(order_py.ptr(), &order); int flags = 0; if (order == PyArray_FORTRANORDER) flags |= NPY_FARRAY; else if (order == PyArray_CORDER) flags |= NPY_CARRAY; else throw std::runtime_error("unrecognized order specifier"); py::handle<> result = py::handle<>(PyArray_NewFromDescr( &PyArray_Type, tp_descr, int(dims.size()), &dims.front(), /*strides*/ NULL, alloc->ptr(), flags, /*obj*/NULL)); py::handle<> alloc_py(handle_from_new_ptr(alloc.release())); PyArray_BASE(result.get()) = alloc_py.get(); Py_INCREF(alloc_py.get()); return result; }
static PyObject *__getattro__(PyObject *self, PyObject *attr_name) { const char *name = PyString_AsString(attr_name); pylal_REAL8Window *obj = (pylal_REAL8Window *) self; if(!strcmp(name, "sumofsquares")) return PyFloat_FromDouble(obj->window->sumofsquares); if(!strcmp(name, "sum")) return PyFloat_FromDouble(obj->window->sum); if(!strcmp(name, "data")) { npy_intp dims[] = {obj->window->data->length}; PyObject *array = PyArray_SimpleNewFromData(1, dims, NPY_FLOAT64, obj->window->data->data); if(array) { /* incref self to prevent data from disappearing * while array is still in use, and tell numpy to * decref self when the array is deallocated */ Py_INCREF(self); PyArray_BASE(array) = self; } return array; } PyErr_SetString(PyExc_AttributeError, name); return NULL; }
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; }
static PyObject *SuperLU_getter(SuperLUObject *self, void *data) { char *name = (char*)data; if (strcmp(name, "shape") == 0) { return Py_BuildValue("(i,i)", self->m, self->n); } else if (strcmp(name, "nnz") == 0) return Py_BuildValue("i", ((SCformat *) self->L.Store)->nnz + ((SCformat *) self->U.Store)->nnz); else if (strcmp(name, "perm_r") == 0) { PyObject *perm_r; perm_r = PyArray_SimpleNewFromData( 1, (npy_intp *) (&self->n), NPY_INT, (void *) self->perm_r); if (perm_r == NULL) { return NULL; } /* For ref counting of the memory */ PyArray_BASE(perm_r) = (PyObject*)self; Py_INCREF(self); return perm_r; } else if (strcmp(name, "perm_c") == 0) { PyObject *perm_c; perm_c = PyArray_SimpleNewFromData( 1, (npy_intp *) (&self->n), NPY_INT, (void *) self->perm_c); if (perm_c == NULL) { return NULL; } /* For ref counting of the memory */ PyArray_BASE(perm_c) = (PyObject*)self; Py_INCREF(self); return perm_c; } else if (strcmp(name, "U") == 0 || strcmp(name, "L") == 0) { int ok; if (self->cached_U == NULL) { ok = LU_to_csc_matrix(&self->L, &self->U, &self->cached_L, &self->cached_U); if (ok != 0) { return NULL; } } if (strcmp(name, "U") == 0) { Py_INCREF(self->cached_U); return self->cached_U; } else { Py_INCREF(self->cached_L); return self->cached_L; } } else { PyErr_SetString(PyExc_RuntimeError, "internal error (this is a bug)"); return NULL; } }