Skip to content

Commit cf02941

Browse files
committed
Pull request #11: Convert typeinfo_init_structsequences and set_typeinfo
Merge in ~STEPAN.SINDELAR_ORACLE.COM/numpy-hpy from tim/init_structsequences to labs-hpy-port * commit '2d4e8a58907d7f15d21323f0d748b57d5ffa582c': fix leaks of handles to global types close handles properly after building typeinfos fix generated code to build tuples of typeinfo switch to StructSequenceBuilder api convert typeinfo entirely to hpy begin using hpy structseq api start by moving structsequences to the stable (heap) API
2 parents e2d2aa2 + 2d4e8a5 commit cf02941

File tree

5 files changed

+179
-148
lines changed

5 files changed

+179
-148
lines changed

numpy/core/src/multiarray/arraytypes.c.src

Lines changed: 123 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -4590,9 +4590,9 @@ PyArray_DescrFromType(int type)
45904590
* and is used to initialize internal dtype tables.
45914591
*/
45924592
NPY_NO_EXPORT int
4593-
set_typeinfo(HPyContext *ctx, PyObject *dict)
4593+
set_typeinfo(HPyContext *ctx, HPy h_dict)
45944594
{
4595-
PyObject *infodict, *s;
4595+
HPy h_s, h_infodict, h_max, h_min, h_type;
45964596
int i;
45974597

45984598
PyArray_Descr *dtype;
@@ -4714,8 +4714,8 @@ set_typeinfo(HPyContext *ctx, PyObject *dict)
47144714
/**end repeat**/
47154715

47164716
/* Set a dictionary with type information */
4717-
infodict = PyDict_New();
4718-
if (infodict == NULL) return -1;
4717+
h_infodict = HPyDict_New(ctx);
4718+
if (HPy_IsNull(h_infodict)) return -1;
47194719

47204720
int ret;
47214721
/**begin repeat
@@ -4738,38 +4738,53 @@ set_typeinfo(HPyContext *ctx, PyObject *dict)
47384738
* npy_long, npy_ulong, npy_longlong, npy_ulonglong#
47394739
* #max= 1,
47404740
* NPY_MAX_BYTE, NPY_MAX_UBYTE, NPY_MAX_SHORT,
4741-
* NPY_MAX_USHORT, NPY_MAX_INT, PyLong_FromUnsignedLong(NPY_MAX_UINT),
4742-
* PyLong_FromLongLong((npy_longlong) NPY_MAX_INTP),
4743-
* PyLong_FromUnsignedLongLong((npy_ulonglong) NPY_MAX_UINTP),
4741+
* NPY_MAX_USHORT, NPY_MAX_INT, HPyLong_FromUnsignedLong(_CTX_COMMA_ NPY_MAX_UINT),
4742+
* HPyLong_FromLongLong(_CTX_COMMA_ (npy_longlong) NPY_MAX_INTP),
4743+
* HPyLong_FromUnsignedLongLong(_CTX_COMMA_ (npy_ulonglong) NPY_MAX_UINTP),
47444744
* NPY_MAX_LONG,
4745-
* PyLong_FromUnsignedLong((npy_ulong) NPY_MAX_ULONG),
4746-
* PyLong_FromLongLong((npy_longlong) NPY_MAX_LONGLONG),
4747-
* PyLong_FromUnsignedLongLong((npy_ulonglong) NPY_MAX_ULONGLONG)#
4745+
* HPyLong_FromUnsignedLong(_CTX_COMMA_ (npy_ulong) NPY_MAX_ULONG),
4746+
* HPyLong_FromLongLong(_CTX_COMMA_ (npy_longlong) NPY_MAX_LONGLONG),
4747+
* HPyLong_FromUnsignedLongLong(_CTX_COMMA_ (npy_ulonglong) NPY_MAX_ULONGLONG)#
47484748
* #min = 0, NPY_MIN_BYTE, 0, NPY_MIN_SHORT, 0, NPY_MIN_INT, 0,
4749-
* PyLong_FromLongLong((npy_longlong) NPY_MIN_INTP),
4749+
* HPyLong_FromLongLong(_CTX_COMMA_ (npy_longlong) NPY_MIN_INTP),
47504750
* 0, NPY_MIN_LONG, 0,
4751-
* PyLong_FromLongLong((npy_longlong) NPY_MIN_LONGLONG), 0#
4752-
* #cx = i*6, N, N, N, l, N, N, N#
4753-
* #cn = i*7, N, i, l, i, N, i#
4751+
* HPyLong_FromLongLong(_CTX_COMMA_ (npy_longlong) NPY_MIN_LONGLONG), 0#
4752+
* #cx = i*6, O, O, O, l, O, O, O#
4753+
* #cn = i*7, O, i, l, i, O, i#
4754+
* #maxType = int*6, HPy*3, long, HPy*3#
4755+
* #minType = int*7, HPy, int, long, int, HPy, int#
4756+
* #maxClose = //*6, HPy_Close(_CTX_COMMA_*3, //, HPy_Close(_CTX_COMMA_*3#
4757+
* #minClose = //*7, HPy_Close(_CTX_COMMA_, //*3, HPy_Close(_CTX_COMMA_, //#
47544758
*/
47554759

4756-
s = PyArray_typeinforanged(
4760+
#define _CTX_COMMA_ ctx,
4761+
@maxType@ max@Name@ = @max@;
4762+
@minType@ min@Name@ = @min@;
4763+
h_max = HPy_BuildValue(ctx, "@cx@", max@Name@);
4764+
h_min = HPy_BuildValue(ctx, "@cn@", min@Name@);
4765+
h_type = HPy_FromPyObject(ctx, (PyObject *)&Py@Name@ArrType_Type);
4766+
h_s = PyArray_typeinforanged(ctx,
47574767
NPY_@name@LTR, NPY_@name@, NPY_BITSOF_@uname@, _ALIGN(@type@),
4758-
Py_BuildValue("@cx@", @max@),
4759-
Py_BuildValue("@cn@", @min@),
4760-
&Py@Name@ArrType_Type
4768+
h_max,
4769+
h_min,
4770+
h_type
47614771
);
4762-
if (s == NULL) {
4763-
Py_DECREF(infodict);
4772+
HPy_Close(ctx, h_max);
4773+
HPy_Close(ctx, h_min);
4774+
HPy_Close(ctx, h_type);
4775+
@maxClose@ max@Name@);
4776+
@minClose@ min@Name@);
4777+
if (HPy_IsNull(h_s)) {
4778+
HPy_Close(ctx, h_infodict);
47644779
return -1;
47654780
}
4766-
ret = PyDict_SetItemString(infodict, "@name@", s);
4767-
Py_DECREF(s);
4781+
ret = HPy_SetItem_s(ctx, h_infodict, "@name@", h_s);
4782+
HPy_Close(ctx, h_s);
47684783
if (ret < 0) {
4769-
Py_DECREF(infodict);
4784+
HPy_Close(ctx, h_infodict);
47704785
return -1;
47714786
}
4772-
4787+
#undef _CTX_COMMA_
47734788

47744789
/**end repeat**/
47754790

@@ -4783,122 +4798,146 @@ set_typeinfo(HPyContext *ctx, PyObject *dict)
47834798
* #Name = Half, Float, Double, LongDouble,
47844799
* CFloat, CDouble, CLongDouble#
47854800
*/
4786-
s = PyArray_typeinfo(
4801+
h_type = HPy_FromPyObject(ctx, (PyObject *)_Py@Name@ArrType_Type_p);
4802+
h_s = PyArray_typeinfo(ctx,
47874803
NPY_@name@LTR, NPY_@name@, NPY_BITSOF_@name@,
4788-
_ALIGN(@type@), &Py@Name@ArrType_Type
4804+
_ALIGN(@type@), h_type
47894805
);
4790-
if (s == NULL) {
4791-
Py_DECREF(infodict);
4806+
HPy_Close(ctx, h_type);
4807+
if (HPy_IsNull(h_s)) {
4808+
HPy_Close(ctx, h_infodict);
47924809
return -1;
47934810
}
4794-
ret = PyDict_SetItemString(infodict, "@name@", s);
4795-
Py_DECREF(s);
4811+
ret = HPy_SetItem_s(ctx, h_infodict, "@name@", h_s);
4812+
HPy_Close(ctx, h_s);
47964813
if (ret < 0) {
4797-
Py_DECREF(infodict);
4814+
HPy_Close(ctx, h_infodict);
47984815
return -1;
47994816
}
48004817

48014818
/**end repeat**/
4802-
4803-
s = PyArray_typeinfo(
4819+
h_type = HPy_FromPyObject(ctx, (PyObject *)_PyObjectArrType_Type_p);
4820+
h_s = PyArray_typeinfo(ctx,
48044821
NPY_OBJECTLTR, NPY_OBJECT, sizeof(PyObject *) * CHAR_BIT,
48054822
_ALIGN(PyObject *),
4806-
&PyObjectArrType_Type
4823+
h_type
48074824
);
4808-
if (s == NULL) {
4809-
Py_DECREF(infodict);
4825+
HPy_Close(ctx, h_type);
4826+
if (HPy_IsNull(h_s)) {
4827+
HPy_Close(ctx, h_infodict);
48104828
return -1;
48114829
}
4812-
ret = PyDict_SetItemString(infodict, "OBJECT", s);
4813-
Py_DECREF(s);
4830+
ret = HPy_SetItem_s(ctx, h_infodict, "OBJECT", h_s);
4831+
HPy_Close(ctx, h_s);
48144832
if (ret < 0) {
4815-
Py_DECREF(infodict);
4833+
HPy_Close(ctx, h_infodict);
48164834
return -1;
48174835
}
4818-
s = PyArray_typeinfo(
4836+
h_type = HPy_FromPyObject(ctx, (PyObject *)_PyStringArrType_Type_p);
4837+
h_s = PyArray_typeinfo(ctx,
48194838
NPY_STRINGLTR, NPY_STRING, 0, _ALIGN(char),
4820-
&PyStringArrType_Type
4839+
h_type
48214840
);
4822-
if (s == NULL) {
4823-
Py_DECREF(infodict);
4841+
HPy_Close(ctx, h_type);
4842+
if (HPy_IsNull(h_s)) {
4843+
HPy_Close(ctx, h_infodict);
48244844
return -1;
48254845
}
4826-
ret = PyDict_SetItemString(infodict, "STRING", s);
4827-
Py_DECREF(s);
4846+
ret = HPy_SetItem_s(ctx, h_infodict, "STRING", h_s);
4847+
HPy_Close(ctx, h_s);
48284848
if (ret < 0) {
4829-
Py_DECREF(infodict);
4849+
HPy_Close(ctx, h_infodict);
48304850
return -1;
48314851
}
4832-
s = PyArray_typeinfo(
4852+
h_type = HPy_FromPyObject(ctx, (PyObject *)_PyUnicodeArrType_Type_p);
4853+
h_s = PyArray_typeinfo(ctx,
48334854
NPY_UNICODELTR, NPY_UNICODE, 0, _ALIGN(npy_ucs4),
4834-
&PyUnicodeArrType_Type
4855+
h_type
48354856
);
4836-
if (s == NULL) {
4837-
Py_DECREF(infodict);
4857+
HPy_Close(ctx, h_type);
4858+
if (HPy_IsNull(h_s)) {
4859+
HPy_Close(ctx, h_infodict);
48384860
return -1;
48394861
}
4840-
ret = PyDict_SetItemString(infodict, "UNICODE", s);
4841-
Py_DECREF(s);
4862+
ret = HPy_SetItem_s(ctx, h_infodict, "UNICODE", h_s);
4863+
HPy_Close(ctx, h_s);
48424864
if (ret < 0) {
4843-
Py_DECREF(infodict);
4865+
HPy_Close(ctx, h_infodict);
48444866
return -1;
48454867
}
4846-
s = PyArray_typeinfo(
4868+
h_type = HPy_FromPyObject(ctx, (PyObject *)_PyVoidArrType_Type_p);
4869+
h_s = PyArray_typeinfo(ctx,
48474870
NPY_VOIDLTR, NPY_VOID, 0, _ALIGN(char),
4848-
&PyVoidArrType_Type
4871+
h_type
48494872
);
4850-
if (s == NULL) {
4851-
Py_DECREF(infodict);
4873+
HPy_Close(ctx, h_type);
4874+
if (HPy_IsNull(h_s)) {
4875+
HPy_Close(ctx, h_infodict);
48524876
return -1;
48534877
}
4854-
ret = PyDict_SetItemString(infodict, "VOID", s);
4855-
Py_DECREF(s);
4878+
ret = HPy_SetItem_s(ctx, h_infodict, "VOID", h_s);
4879+
HPy_Close(ctx, h_s);
48564880
if (ret < 0) {
4857-
Py_DECREF(infodict);
4881+
HPy_Close(ctx, h_infodict);
48584882
return -1;
48594883
}
4860-
s = PyArray_typeinforanged(
4884+
h_max = HPyLong_FromLong(ctx, NPY_MAX_DATETIME);
4885+
h_min = HPyLong_FromLong(ctx, NPY_MIN_DATETIME);
4886+
h_type = HPy_FromPyObject(ctx, (PyObject *)_PyDatetimeArrType_Type_p);
4887+
h_s = PyArray_typeinforanged(ctx,
48614888
NPY_DATETIMELTR, NPY_DATETIME, NPY_BITSOF_DATETIME,
48624889
_ALIGN(npy_datetime),
4863-
MyPyLong_FromInt64(NPY_MAX_DATETIME),
4864-
MyPyLong_FromInt64(NPY_MIN_DATETIME),
4865-
&PyDatetimeArrType_Type
4890+
h_max,
4891+
h_min,
4892+
h_type
48664893
);
4867-
if (s == NULL) {
4868-
Py_DECREF(infodict);
4894+
HPy_Close(ctx, h_max);
4895+
HPy_Close(ctx, h_min);
4896+
HPy_Close(ctx, h_type);
4897+
if (HPy_IsNull(h_s)) {
4898+
HPy_Close(ctx, h_infodict);
48694899
return -1;
48704900
}
4871-
ret = PyDict_SetItemString(infodict, "DATETIME", s);
4872-
Py_DECREF(s);
4901+
ret = HPy_SetItem_s(ctx, h_infodict, "DATETIME", h_s);
4902+
HPy_Close(ctx, h_s);
48734903
if (ret < 0) {
4874-
Py_DECREF(infodict);
4904+
HPy_Close(ctx, h_infodict);
48754905
return -1;
48764906
}
4877-
s = PyArray_typeinforanged(
4907+
h_max = HPyLong_FromLong(ctx, NPY_MAX_TIMEDELTA);
4908+
h_min = HPyLong_FromLong(ctx, NPY_MIN_TIMEDELTA);
4909+
h_type = HPy_FromPyObject(ctx, (PyObject *)_PyTimedeltaArrType_Type_p);
4910+
h_s = PyArray_typeinforanged(ctx,
48784911
NPY_TIMEDELTALTR, NPY_TIMEDELTA, NPY_BITSOF_TIMEDELTA,
48794912
_ALIGN(npy_timedelta),
4880-
MyPyLong_FromInt64(NPY_MAX_TIMEDELTA),
4881-
MyPyLong_FromInt64(NPY_MIN_TIMEDELTA),
4882-
&PyTimedeltaArrType_Type
4913+
h_max,
4914+
h_min,
4915+
h_type
48834916
);
4884-
if (s == NULL) {
4885-
Py_DECREF(infodict);
4917+
HPy_Close(ctx, h_max);
4918+
HPy_Close(ctx, h_min);
4919+
HPy_Close(ctx, h_type);
4920+
if (HPy_IsNull(h_s)) {
4921+
HPy_Close(ctx, h_infodict);
48864922
return -1;
48874923
}
4888-
ret = PyDict_SetItemString(infodict, "TIMEDELTA", s);
4889-
Py_DECREF(s);
4924+
ret = HPy_SetItem_s(ctx, h_infodict, "TIMEDELTA", h_s);
4925+
HPy_Close(ctx, h_s);
48904926
if (ret < 0) {
4891-
Py_DECREF(infodict);
4927+
HPy_Close(ctx, h_infodict);
48924928
return -1;
48934929
}
48944930

48954931
#define SETTYPE(name) \
48964932
Py_INCREF(&Py##name##ArrType_Type); \
4897-
if (PyDict_SetItemString(infodict, #name, \
4898-
(PyObject *)&Py##name##ArrType_Type) < 0) { \
4899-
Py_DECREF(infodict); \
4933+
h_type = HPy_FromPyObject(ctx, (PyObject *)&Py##name##ArrType_Type); \
4934+
if (HPy_SetItem_s(ctx, h_infodict, #name, \
4935+
h_type) < 0) { \
4936+
HPy_Close(ctx, h_infodict); \
4937+
HPy_Close(ctx, h_type); \
49004938
return -1; \
4901-
}
4939+
} \
4940+
HPy_Close(ctx, h_type);
49024941

49034942
SETTYPE(Generic);
49044943
SETTYPE(Number);
@@ -4913,8 +4952,8 @@ set_typeinfo(HPyContext *ctx, PyObject *dict)
49134952

49144953
#undef SETTYPE
49154954

4916-
ret = PyDict_SetItemString(dict, "typeinfo", infodict);
4917-
Py_DECREF(infodict);
4955+
ret = HPy_SetItem_s(ctx, h_dict, "typeinfo", h_infodict);
4956+
HPy_Close(ctx, h_infodict);
49184957
if (ret < 0) {
49194958
return -1;
49204959
}

numpy/core/src/multiarray/arraytypes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "common.h"
55

66
NPY_NO_EXPORT int
7-
set_typeinfo(HPyContext *ctx, PyObject *dict);
7+
set_typeinfo(HPyContext *ctx, HPy dict);
88

99
/* needed for blasfuncs */
1010
NPY_NO_EXPORT void

numpy/core/src/multiarray/multiarraymodule.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5335,16 +5335,15 @@ static HPy init__multiarray_umath_impl(HPyContext *ctx) {
53355335
set_flaginfo(ctx, h_d);
53365336

53375337
/* Create the typeinfo types */
5338-
if (typeinfo_init_structsequences(d) < 0) {
5338+
if (typeinfo_init_structsequences(ctx, h_d) < 0) {
53395339
goto err;
53405340
}
53415341

53425342
if (intern_strings() < 0) {
53435343
goto err;
53445344
}
53455345

5346-
CAPI_WARN("startup: set_typeinfo");
5347-
if (set_typeinfo(ctx, d) != 0) {
5346+
if (set_typeinfo(ctx, h_d) != 0) {
53485347
goto err;
53495348
}
53505349
HPy h_array_method_type = HPyType_FromSpec(ctx, &PyArrayMethod_Type_Spec, NULL);

0 commit comments

Comments
 (0)