Skip to content

Commit 0e4a5a7

Browse files
committed
Pull request #9: Add some HPy helpers
Merge ss/add-hpy-helpers to labs-hpy-port * commit '68e7a70c10eb0751424df3ce1fb43cf385e62d95': Cleanup some compiler warnings Change HPy helpers names in ndarraytypes.h to follow conventions Add hpy_abort_not_implemented helper function Add HPY helpers for array/dtype types
2 parents 859d40e + 68e7a70 commit 0e4a5a7

File tree

6 files changed

+58
-17
lines changed

6 files changed

+58
-17
lines changed

numpy/core/include/numpy/ndarrayobject.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ extern "C" {
2828

2929
#define PyArray_Check(op) PyObject_TypeCheck(op, &PyArray_Type)
3030
#define PyArray_CheckExact(op) (((PyObject*)(op))->ob_type == &PyArray_Type)
31+
#define HPyArray_Check(ctx, op) HPy_TypeCheck(ctx, op, HPyArray_Type)
32+
#define HPyArray_CheckExact(ctx, op) HPy_Is(ctx, op, HPyArray_Type)
3133

3234
#define PyArray_HasArrayInterfaceType(op, type, context, out) \
3335
((((out)=PyArray_FromStructInterface(op)) != Py_NotImplemented) || \

numpy/core/include/numpy/ndarraytypes.h

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ static NPY_INLINE
2424
void capi_warn(const char *where) {
2525
printf("DEBUG WARNING: leaving to CPython API in %s\n", where);
2626
}
27+
static __attribute__ ((noreturn)) NPY_INLINE
28+
void hpy_abort_not_implemented(const char *where) {
29+
printf("FATAL ERROR: not implemented in HPY: %s\n", where);
30+
abort();
31+
}
2732

2833
#ifdef __cplusplus
2934
}
@@ -673,6 +678,8 @@ typedef struct _PyArray_Descr {
673678
npy_hash_t hash;
674679
} PyArray_Descr;
675680

681+
HPyType_LEGACY_HELPERS(PyArray_Descr)
682+
676683
typedef struct _arr_descr {
677684
PyArray_Descr *base;
678685
PyObject *shape; /* a tuple */
@@ -775,10 +782,12 @@ typedef struct tagPyArrayObject {
775782
} PyArrayObject;
776783
#endif
777784

785+
HPyType_LEGACY_HELPERS(PyArrayObject)
786+
778787
static NPY_INLINE PyArrayObject_fields *
779788
HPyArray_AsFields(HPyContext *ctx, HPy h)
780789
{
781-
return (PyArrayObject_fields*)HPy_AsStructLegacy(ctx, h);
790+
return (PyArrayObject_fields*)PyArrayObject_AsStruct(ctx, h);
782791
}
783792

784793
/*
@@ -1552,6 +1561,12 @@ HPyArray_GetDescr(HPyContext *ctx, HPy arr)
15521561
return HPyField_Load(ctx, arr, HPyArray_AsFields(ctx, arr)->f_descr);
15531562
}
15541563

1564+
static NPY_INLINE HPy
1565+
HPyArray_DESCR(HPyContext *ctx, HPy arr, const PyArrayObject *arr_data)
1566+
{
1567+
return HPyField_Load(ctx, arr, ((PyArrayObject_fields *)arr_data)->f_descr);
1568+
}
1569+
15551570
static NPY_INLINE NPY_RETURNS_BORROWED_REF PyArray_Descr *
15561571
PyArray_DESCR(const PyArrayObject *arr)
15571572
{
@@ -1635,6 +1650,12 @@ PyArray_NDIM(const PyArrayObject *arr)
16351650
return ((PyArrayObject_fields *)arr)->nd;
16361651
}
16371652

1653+
static NPY_INLINE int
1654+
HPyArray_GetNDim(HPyContext *ctx, HPy arr)
1655+
{
1656+
return ((PyArrayObject_fields *) HPy_AsStructLegacy(ctx, arr))->nd;
1657+
}
1658+
16381659
static NPY_INLINE void *
16391660
PyArray_DATA(PyArrayObject *arr)
16401661
{
@@ -1647,6 +1668,12 @@ PyArray_BYTES(PyArrayObject *arr)
16471668
return ((PyArrayObject_fields *)arr)->data;
16481669
}
16491670

1671+
static NPY_INLINE char *
1672+
HPyArray_GetBytes(HPyContext *ctx, HPy h_arr)
1673+
{
1674+
return HPyArray_AsFields(ctx, h_arr)->data;
1675+
}
1676+
16501677
static NPY_INLINE npy_intp *
16511678
PyArray_DIMS(PyArrayObject *arr)
16521679
{
@@ -1684,6 +1711,24 @@ PyArray_ITEMSIZE(const PyArrayObject *arr)
16841711
return PyArray_DESCR(arr)->elsize;
16851712
}
16861713

1714+
static NPY_INLINE npy_intp
1715+
HPyArray_ITEMSIZE(HPyContext *ctx, HPy arr, const PyArrayObject *arr_data)
1716+
{
1717+
HPy descr = HPyArray_DESCR(ctx, arr, arr_data);
1718+
npy_intp res = PyArray_Descr_AsStruct(ctx, descr)->elsize;
1719+
HPy_Close(ctx, descr);
1720+
return res;
1721+
}
1722+
1723+
static NPY_INLINE npy_intp
1724+
HPyArray_GetItemsize(HPyContext *ctx, HPy arr)
1725+
{
1726+
HPy descr = HPyArray_GetDescr(ctx, arr);
1727+
npy_intp res = PyArray_Descr_AsStruct(ctx, descr)->elsize;
1728+
HPy_Close(ctx, descr);
1729+
return res;
1730+
}
1731+
16871732
static NPY_INLINE int
16881733
PyArray_TYPE(const PyArrayObject *arr)
16891734
{
@@ -1720,6 +1765,12 @@ PyArray_DTYPE(PyArrayObject *arr)
17201765
return PyArray_DESCR(arr);
17211766
}
17221767

1768+
static NPY_INLINE HPy
1769+
HPyArray_DTYPE(HPyContext *ctx, HPy arr)
1770+
{
1771+
return HPyArray_GetDescr(ctx, arr);
1772+
}
1773+
17231774
static NPY_INLINE npy_intp *
17241775
PyArray_SHAPE(PyArrayObject *arr)
17251776
{

numpy/core/src/multiarray/arrayobject.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ array_might_be_written(PyArrayObject *obj);
2828
static const int NPY_ARRAY_WARN_ON_WRITE = (1 << 31);
2929

3030
extern NPY_NO_EXPORT HPyType_Spec PyArray_Type_spec;
31+
extern NPY_NO_EXPORT HPy HPyArray_Type;
3132
extern NPY_NO_EXPORT HPyType_Spec PyArrayFlags_Type_Spec;
3233

3334
#endif /* NUMPY_CORE_SRC_MULTIARRAY_ARRAYOBJECT_H_ */

numpy/core/src/multiarray/conversion_utils.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,7 @@ HPyArray_IntpFromIndexSequence(HPyContext *ctx, HPy seq, npy_intp *vals, npy_int
11691169
{
11701170
HPy_ssize_t nd;
11711171
npy_intp i;
1172-
HPy op, err;
1172+
HPy op;
11731173

11741174
/*
11751175
* Check to see if sequence is a single integer first.

numpy/core/src/multiarray/multiarraymodule.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ set_legacy_print_mode(PyObject *NPY_UNUSED(self), PyObject *args)
111111

112112
NPY_NO_EXPORT PyTypeObject* _PyArray_Type_p = NULL;
113113
NPY_NO_EXPORT HPyContext *numpy_global_ctx = NULL;
114+
NPY_NO_EXPORT HPy HPyArray_Type;
114115
NPY_NO_EXPORT HPy HPyArrayDescr_Type;
115116

116117
/* Only here for API compatibility */
@@ -4870,6 +4871,7 @@ static HPy init__multiarray_umath_impl(HPyContext *ctx) {
48704871
goto err;
48714872
}
48724873
_PyArray_Type_p = (PyTypeObject*)HPy_AsPyObject(ctx, h_array_type);
4874+
HPyArray_Type = HPy_Dup(ctx, h_array_type);
48734875
PyArray_Type.tp_weaklistoffset = offsetof(PyArrayObject_fields, weakreflist);
48744876
HPy_Close(ctx, h_array_type);
48754877

numpy/core/src/multiarray/scalartypes.c.src

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3531,21 +3531,6 @@ object_arrtype_inplace_repeat(PyObjectScalarObject *self, Py_ssize_t count)
35313531
return PySequence_InPlaceRepeat(self->obval, count);
35323532
}
35333533

3534-
static PySequenceMethods object_arrtype_as_sequence = {
3535-
.sq_length = (lenfunc)object_arrtype_length,
3536-
.sq_concat = (binaryfunc)object_arrtype_concat,
3537-
.sq_repeat = (ssizeargfunc)object_arrtype_repeat,
3538-
.sq_contains = (objobjproc)object_arrtype_contains,
3539-
.sq_inplace_concat = (binaryfunc)object_arrtype_inplace_concat,
3540-
.sq_inplace_repeat = (ssizeargfunc)object_arrtype_inplace_repeat,
3541-
};
3542-
3543-
static PyMappingMethods object_arrtype_as_mapping = {
3544-
.mp_length = (lenfunc)object_arrtype_length,
3545-
.mp_subscript = (binaryfunc)object_arrtype_subscript,
3546-
.mp_ass_subscript = (objobjargproc)object_arrtype_ass_subscript,
3547-
};
3548-
35493534
static int
35503535
object_arrtype_getbuffer(PyObjectScalarObject *self, Py_buffer *view, int flags)
35513536
{

0 commit comments

Comments
 (0)