Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bf_getbuffer slot #3

Merged
merged 4 commits into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions numpy/core/src/multiarray/arrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1822,9 +1822,15 @@ static PyType_Slot PyArray_Type_slots[] = {
{0, NULL},
};

static HPyDef *array_defines[] = {
&array_getbuffer,
NULL,
};

NPY_NO_EXPORT HPyType_Spec PyArray_Type_spec = {
.name = "numpy.ndarray",
.basicsize = sizeof(PyArrayObject_fields),
.flags = (HPy_TPFLAGS_DEFAULT | HPy_TPFLAGS_BASETYPE),
.defines = array_defines,
.legacy_slots = PyArray_Type_slots,
};
27 changes: 12 additions & 15 deletions numpy/core/src/multiarray/buffer.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "hpy.h"
#include "structmember.h"

#define NPY_NO_DEPRECATED_API NPY_API_VERSION
Expand Down Expand Up @@ -747,34 +748,36 @@ _buffer_get_info(void **buffer_info_cache_ptr, PyObject *obj, int flags)
/*
* Retrieving buffers for ndarray
*/
HPyDef_SLOT(array_getbuffer, array_getbuffer_impl, HPy_bf_getbuffer)
static int
array_getbuffer(PyObject *obj, Py_buffer *view, int flags)
array_getbuffer_impl(HPyContext ctx, HPy h_obj, HPy_buffer *view, int flags)
{
PyArrayObject *self;
_buffer_info_t *info = NULL;

PyObject *obj = HPy_AsPyObject(ctx, h_obj);
self = (PyArrayObject*)obj;

/* Check whether we can provide the wanted properties */
if ((flags & PyBUF_C_CONTIGUOUS) == PyBUF_C_CONTIGUOUS &&
!PyArray_CHKFLAGS(self, NPY_ARRAY_C_CONTIGUOUS)) {
PyErr_SetString(PyExc_ValueError, "ndarray is not C-contiguous");
HPyErr_SetString(ctx, ctx->h_ValueError, "ndarray is not C-contiguous");
goto fail;
}
if ((flags & PyBUF_F_CONTIGUOUS) == PyBUF_F_CONTIGUOUS &&
!PyArray_CHKFLAGS(self, NPY_ARRAY_F_CONTIGUOUS)) {
PyErr_SetString(PyExc_ValueError, "ndarray is not Fortran contiguous");
HPyErr_SetString(ctx, ctx->h_ValueError, "ndarray is not Fortran contiguous");
goto fail;
}
if ((flags & PyBUF_ANY_CONTIGUOUS) == PyBUF_ANY_CONTIGUOUS
&& !PyArray_ISONESEGMENT(self)) {
PyErr_SetString(PyExc_ValueError, "ndarray is not contiguous");
HPyErr_SetString(ctx, ctx->h_ValueError, "ndarray is not contiguous");
goto fail;
}
if ((flags & PyBUF_STRIDES) != PyBUF_STRIDES &&
!PyArray_CHKFLAGS(self, NPY_ARRAY_C_CONTIGUOUS)) {
/* Non-strided N-dim buffers must be C-contiguous */
PyErr_SetString(PyExc_ValueError, "ndarray is not C-contiguous");
HPyErr_SetString(ctx, ctx->h_ValueError, "ndarray is not C-contiguous");
goto fail;
}
if ((flags & PyBUF_WRITEABLE) == PyBUF_WRITEABLE) {
Expand All @@ -784,7 +787,7 @@ array_getbuffer(PyObject *obj, Py_buffer *view, int flags)
}

if (view == NULL) {
PyErr_SetString(PyExc_ValueError, "NULL view in getbuffer");
HPyErr_SetString(ctx, ctx->h_ValueError, "NULL view in getbuffer");
goto fail;
}

Expand Down Expand Up @@ -830,12 +833,13 @@ array_getbuffer(PyObject *obj, Py_buffer *view, int flags)
else {
view->strides = NULL;
}
view->obj = (PyObject*)self;
view->obj = HPy_Dup(ctx, h_obj);
Py_DECREF(obj);

Py_INCREF(self);
return 0;

fail:
Py_DECREF(obj);
return -1;
}

Expand Down Expand Up @@ -885,13 +889,6 @@ void_getbuffer(PyObject *self, Py_buffer *view, int flags)
}


/*************************************************************************/

NPY_NO_EXPORT PyBufferProcs array_as_buffer = {
(getbufferproc)array_getbuffer,
(releasebufferproc)0,
};


/*************************************************************************
* Convert PEP 3118 format string to PyArray_Descr
Expand Down
1 change: 0 additions & 1 deletion numpy/core/src/multiarray/multiarraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -4598,7 +4598,6 @@ static HPy init__multiarray_umath_impl(HPyContext ctx) {
goto err;
}
_PyArray_Type_p = (PyTypeObject*)HPy_AsPyObject(ctx, h_array_type);
PyArray_Type.tp_as_buffer = &array_as_buffer;
PyArray_Type.tp_weaklistoffset = offsetof(PyArrayObject_fields, weakreflist);
HPy_Close(ctx, h_array_type);

Expand Down
3 changes: 2 additions & 1 deletion numpy/core/src/multiarray/npy_buffer.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#ifndef _NPY_PRIVATE_BUFFER_H_
#define _NPY_PRIVATE_BUFFER_H_
#include "hpy.h"

extern NPY_NO_EXPORT PyBufferProcs array_as_buffer;
extern NPY_NO_EXPORT HPyDef array_getbuffer;

NPY_NO_EXPORT int
_buffer_info_free(void *buffer_info, PyObject *obj);
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ requires = [
"setuptools<49.2.0",
"wheel<=0.35.1",
"Cython>=0.29.21,<3.0", # Note: keep in sync with tools/cythonize.py
"hpy.devel @ git+https://github.com/hpyproject/hpy.git@8e20b89116c2993188157c09a6070a64f8efbd82#egg=hpy.devel"
"hpy.devel @ git+https://github.com/hpyproject/hpy.git@5953dc7d4d8fd8c5179c75bded3ff514ffde4323#egg=hpy.devel"
]


Expand Down
2 changes: 1 addition & 1 deletion test_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ cffi
mypy==0.790; platform_python_implementation != "PyPy"
typing_extensions
# HPy
git+https://github.com/hpyproject/hpy.git@8e20b89116c2993188157c09a6070a64f8efbd82#egg=hpy.devel
git+https://github.com/hpyproject/hpy.git@5953dc7d4d8fd8c5179c75bded3ff514ffde4323#egg=hpy.devel