Commit f9e05e99 authored by Mat's avatar Mat
Browse files

Call PyArray_New instead of PyArray_SimpleNewFromData

When creating arrays where the element type is not trivial (for
instance strings), it is necessary to pass the element itemsize
(elsize) to numpy. Without it numpy has no way of knowing the full
size in memory of the array, and in fact it segfaults.
parent ebe5c4d1
Loading
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ struct array_meta {
	char	magic[16];
	size_t	size;
	int	typenum;
	int	itemsize;
	int	ndims;
	npy_intp dims[NPY_MAXDIMS];
} __attribute__ ((packed));
+3 −1
Original line number Diff line number Diff line
@@ -93,7 +93,9 @@ static PyObject *do_attach(const char *name)
	map_owner->name = strdup(name);

	/* Create the array object */
	array = PyArray_SimpleNewFromData(meta->ndims, meta->dims, meta->typenum, map_addr);
	array = PyArray_New(&PyArray_Type, meta->ndims, meta->dims,
	                    meta->typenum, NULL, map_addr, meta->itemsize,
	                    NPY_ARRAY_CARRAY, NULL);

	/* Attach MapOwner to the array */
	PyArray_SetBaseObject((PyArrayObject *) array, (PyObject *) map_owner);
+7 −4
Original line number Diff line number Diff line
@@ -91,6 +91,7 @@ static PyObject *do_create(const char *name, int ndims, npy_intp *dims, PyArray_
	strncpy(meta->magic, SHARED_ARRAY_MAGIC, sizeof (meta->magic));
	meta->size     = size;
	meta->typenum  = dtype->type_num;
	meta->itemsize = dtype->elsize;
	meta->ndims    = ndims;
	for (i = 0; i < ndims; i++)
		meta->dims[i] = dims[i];
@@ -103,7 +104,9 @@ static PyObject *do_create(const char *name, int ndims, npy_intp *dims, PyArray_
	map_owner->name = strdup(name);

	/* Create the array object */
	array = PyArray_SimpleNewFromData(ndims, dims, dtype->type_num, map_addr);
	array = PyArray_New(&PyArray_Type, meta->ndims, meta->dims,
	                    meta->typenum, NULL, map_addr, meta->itemsize,
	                    NPY_ARRAY_CARRAY, NULL);

	/* Attach MapOwner to the array */
	PyArray_SetBaseObject((PyArrayObject *) array, (PyObject *) map_owner);