Skip to content

Commit 32d7bd4

Browse files
authored
Merge pull request #2 from hpyproject/feature/update-to-hpy-d5c8442b
Update piconumpy to hpy@d5c8442b.
2 parents 429d28f + d016d13 commit 32d7bd4

1 file changed

Lines changed: 28 additions & 30 deletions

File tree

piconumpy/_piconumpy_hpy.c

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
#include "hpy.h"
22

33
typedef struct {
4-
HPyObject_HEAD
54
/* Type-specific fields go here. */
65
double *data;
76
int size;
87
} ArrayObject;
98

9+
HPyType_HELPERS(ArrayObject)
10+
1011
HPyDef_SLOT(Array_destroy, Array_destroy_impl, HPy_tp_destroy)
1112
static void Array_destroy_impl(void *obj) {
1213
ArrayObject *self = (ArrayObject *)obj;
1314
free(self->data);
1415
}
1516

1617
HPyDef_SLOT(Array_init, Array_init_impl, HPy_tp_init)
17-
static int Array_init_impl(HPyContext ctx, HPy h_self, HPy *args,
18+
static int Array_init_impl(HPyContext *ctx, HPy h_self, HPy *args,
1819
HPy_ssize_t nargs, HPy kw) {
1920
static const char *kwlist[] = {"data", NULL};
20-
ArrayObject *self = HPy_CAST(ctx, ArrayObject, h_self);
21+
ArrayObject *self = ArrayObject_AsStruct(ctx, h_self);
2122
int index;
2223
HPy h_data = HPy_NULL;
2324
HPyTracker ht;
@@ -58,8 +59,8 @@ HPyDef_MEMBER(Array_size, "size", HPyMember_INT, offsetof(ArrayObject, size),
5859

5960
HPyDef_METH(Array_tolist, "tolist", Array_tolist_impl, HPyFunc_NOARGS,
6061
.doc = "Return the data as a list")
61-
static HPy Array_tolist_impl(HPyContext ctx, HPy h_self) {
62-
ArrayObject *self = HPy_CAST(ctx, ArrayObject, h_self);
62+
static HPy Array_tolist_impl(HPyContext *ctx, HPy h_self) {
63+
ArrayObject *self = ArrayObject_AsStruct(ctx, h_self);
6364
int index;
6465
HPyListBuilder builder = HPyListBuilder_New(ctx, self->size);
6566
for (index = 0; index < self->size; index++) {
@@ -70,10 +71,10 @@ static HPy Array_tolist_impl(HPyContext ctx, HPy h_self) {
7071
return HPyListBuilder_Build(ctx, builder);
7172
};
7273

73-
static HPy Array_empty(HPyContext ctx, int size, ArrayObject **result);
74+
static HPy Array_empty(HPyContext *ctx, int size, ArrayObject **result);
7475

7576
HPyDef_SLOT(Array_multiply, Array_multiply_impl, HPy_nb_multiply)
76-
static HPy Array_multiply_impl(HPyContext ctx, HPy h1, HPy h2) {
77+
static HPy Array_multiply_impl(HPyContext *ctx, HPy h1, HPy h2) {
7778
int index;
7879
double number;
7980
HPy h_number = HPy_NULL;
@@ -82,10 +83,10 @@ static HPy Array_multiply_impl(HPyContext ctx, HPy h1, HPy h2) {
8283

8384
if (HPyNumber_Check(ctx, h2)) {
8485
h_number = h2;
85-
arr = HPy_CAST(ctx, ArrayObject, h1);
86+
arr = ArrayObject_AsStruct(ctx, h1);
8687
} else if (HPyNumber_Check(ctx, h1)) {
8788
h_number = h1;
88-
arr = HPy_CAST(ctx, ArrayObject, h2);
89+
arr = ArrayObject_AsStruct(ctx, h2);
8990
}
9091

9192
if (HPyNumber_Check(ctx, h1) || HPyNumber_Check(ctx, h2)) {
@@ -100,12 +101,12 @@ static HPy Array_multiply_impl(HPyContext ctx, HPy h1, HPy h2) {
100101
};
101102

102103
HPyDef_SLOT(Array_add, Array_add_impl, HPy_nb_add)
103-
static HPy Array_add_impl(HPyContext ctx, HPy h1, HPy h2) {
104+
static HPy Array_add_impl(HPyContext *ctx, HPy h1, HPy h2) {
104105
int index;
105106
ArrayObject *result = NULL, *a1, *a2;
106107
HPy h_result = HPy_NULL;
107-
a1 = HPy_CAST(ctx, ArrayObject, h1);
108-
a2 = HPy_CAST(ctx, ArrayObject, h2);
108+
a1 = ArrayObject_AsStruct(ctx, h1);
109+
a2 = ArrayObject_AsStruct(ctx, h2);
109110

110111
if (a1->size != a2->size)
111112
return HPy_NULL; /* XXX should raise an exception */
@@ -118,7 +119,7 @@ static HPy Array_add_impl(HPyContext ctx, HPy h1, HPy h2) {
118119
};
119120

120121
HPyDef_SLOT(Array_divide, Array_divide_impl, HPy_nb_true_divide)
121-
static HPy Array_divide_impl(HPyContext ctx, HPy h1, HPy h2) {
122+
static HPy Array_divide_impl(HPyContext *ctx, HPy h1, HPy h2) {
122123
int index;
123124
double number;
124125
ArrayObject *result = NULL, *a1;
@@ -127,7 +128,7 @@ static HPy Array_divide_impl(HPyContext ctx, HPy h1, HPy h2) {
127128
if (!HPyNumber_Check(ctx, h2)) {
128129
return HPy_NULL;
129130
}
130-
a1 = HPy_CAST(ctx, ArrayObject, h1);
131+
a1 = ArrayObject_AsStruct(ctx, h1);
131132
number = HPyFloat_AsDouble(ctx, h2);
132133
h_result = Array_empty(ctx, a1->size, &result);
133134
for (index = 0; index < a1->size; index++) {
@@ -138,16 +139,16 @@ static HPy Array_divide_impl(HPyContext ctx, HPy h1, HPy h2) {
138139

139140

140141
HPyDef_SLOT(Array_length, Array_length_impl, HPy_sq_length)
141-
HPy_ssize_t Array_length_impl(HPyContext ctx, HPy h_arr) {
142-
ArrayObject *arr = HPy_CAST(ctx, ArrayObject, h_arr);
142+
HPy_ssize_t Array_length_impl(HPyContext *ctx, HPy h_arr) {
143+
ArrayObject *arr = ArrayObject_AsStruct(ctx, h_arr);
143144
HPy_ssize_t result = (HPy_ssize_t)arr->size;
144145
return result;
145146
};
146147

147148

148149
HPyDef_SLOT(Array_item, Array_item_impl, HPy_sq_item)
149-
HPy Array_item_impl(HPyContext ctx, HPy h_arr, HPy_ssize_t index) {
150-
ArrayObject *arr = HPy_CAST(ctx, ArrayObject, h_arr);
150+
HPy Array_item_impl(HPyContext *ctx, HPy h_arr, HPy_ssize_t index) {
151+
ArrayObject *arr = ArrayObject_AsStruct(ctx, h_arr);
151152
if (index < 0 || index >= arr->size) {
152153
HPyErr_SetString(ctx, ctx->h_IndexError, "index out of range");
153154
return HPy_NULL;
@@ -157,8 +158,8 @@ HPy Array_item_impl(HPyContext ctx, HPy h_arr, HPy_ssize_t index) {
157158
};
158159

159160
HPyDef_SLOT(Array_setitem, Array_setitem_impl, HPy_sq_ass_item)
160-
int Array_setitem_impl(HPyContext ctx, HPy h_arr, HPy_ssize_t index, HPy h_item) {
161-
ArrayObject *arr = HPy_CAST(ctx, ArrayObject, h_arr);
161+
int Array_setitem_impl(HPyContext *ctx, HPy h_arr, HPy_ssize_t index, HPy h_item) {
162+
ArrayObject *arr = ArrayObject_AsStruct(ctx, h_arr);
162163
if (index < 0 || index >= arr->size) {
163164
HPyErr_SetString(ctx, ctx->h_IndexError, "index out of range");
164165
return -1;
@@ -201,7 +202,7 @@ static HPyType_Spec Array_type_spec = {
201202

202203
HPy h_ArrayType;
203204

204-
static HPy Array_empty(HPyContext ctx, int size, ArrayObject **result) {
205+
static HPy Array_empty(HPyContext *ctx, int size, ArrayObject **result) {
205206
ArrayObject *new_array;
206207
HPy h_new_array = HPy_New(ctx, h_ArrayType, &new_array);
207208
new_array->size = size;
@@ -214,15 +215,15 @@ static HPy Array_empty(HPyContext ctx, int size, ArrayObject **result) {
214215
};
215216

216217
HPyDef_METH(empty, "empty", empty_impl, HPyFunc_O, .doc = "Create an empty array")
217-
static HPy empty_impl(HPyContext ctx, HPy module, HPy arg) {
218+
static HPy empty_impl(HPyContext *ctx, HPy module, HPy arg) {
218219
int size;
219220
ArrayObject *result;
220221
size = (int)HPyLong_AsLong(ctx, arg);
221222
return Array_empty(ctx, size, &result);
222223
};
223224

224225
HPyDef_METH(zeros, "zeros", zeros_impl, HPyFunc_O, .doc = "Create a zero-filled array")
225-
static HPy zeros_impl(HPyContext ctx, HPy module, HPy arg) {
226+
static HPy zeros_impl(HPyContext *ctx, HPy module, HPy arg) {
226227
int size;
227228
ArrayObject *result;
228229
size = (int)HPyLong_AsLong(ctx, arg);
@@ -250,20 +251,17 @@ static HPyModuleDef piconumpymodule = {
250251
};
251252

252253
HPy_MODINIT(_piconumpy_hpy)
253-
static HPy init__piconumpy_hpy_impl(HPyContext ctx) {
254+
static HPy init__piconumpy_hpy_impl(HPyContext *ctx) {
254255
HPy hm = HPyModule_Create(ctx, &piconumpymodule);
255256
if (HPy_IsNull(hm))
256257
return HPy_NULL;
257258

258-
h_ArrayType = HPyType_FromSpec(ctx, &Array_type_spec, NULL);
259-
if (HPy_IsNull(h_ArrayType))
260-
return HPy_NULL;
261-
262-
if (HPy_SetAttr_s(ctx, hm, "array", h_ArrayType) < 0) {
263-
HPy_Close(ctx, h_ArrayType);
259+
if (!HPyHelpers_AddType(ctx, hm, "array", &Array_type_spec, NULL)) {
264260
HPy_Close(ctx, hm);
265261
return HPy_NULL;
266262
}
267263

264+
h_ArrayType = HPy_GetAttr_s(ctx, hm, "array");
265+
268266
return hm;
269267
}

0 commit comments

Comments
 (0)