Skip to content

Commit 2d4e8a5

Browse files
committed
fix leaks of handles to global types
1 parent eb2b0db commit 2d4e8a5

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

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

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4592,7 +4592,7 @@ PyArray_DescrFromType(int type)
45924592
NPY_NO_EXPORT int
45934593
set_typeinfo(HPyContext *ctx, HPy h_dict)
45944594
{
4595-
HPy h_s, h_infodict, h_max, h_min;
4595+
HPy h_s, h_infodict, h_max, h_min, h_type;
45964596
int i;
45974597

45984598
PyArray_Descr *dtype;
@@ -4762,14 +4762,16 @@ set_typeinfo(HPyContext *ctx, HPy h_dict)
47624762
@minType@ min@Name@ = @min@;
47634763
h_max = HPy_BuildValue(ctx, "@cx@", max@Name@);
47644764
h_min = HPy_BuildValue(ctx, "@cn@", min@Name@);
4765+
h_type = HPy_FromPyObject(ctx, (PyObject *)&Py@Name@ArrType_Type);
47654766
h_s = PyArray_typeinforanged(ctx,
47664767
NPY_@name@LTR, NPY_@name@, NPY_BITSOF_@uname@, _ALIGN(@type@),
47674768
h_max,
47684769
h_min,
4769-
HPy_FromPyObject(ctx, (PyObject *)&Py@Name@ArrType_Type)
4770+
h_type
47704771
);
47714772
HPy_Close(ctx, h_max);
47724773
HPy_Close(ctx, h_min);
4774+
HPy_Close(ctx, h_type);
47734775
@maxClose@ max@Name@);
47744776
@minClose@ min@Name@);
47754777
if (HPy_IsNull(h_s)) {
@@ -4796,10 +4798,12 @@ set_typeinfo(HPyContext *ctx, HPy h_dict)
47964798
* #Name = Half, Float, Double, LongDouble,
47974799
* CFloat, CDouble, CLongDouble#
47984800
*/
4801+
h_type = HPy_FromPyObject(ctx, (PyObject *)_Py@Name@ArrType_Type_p);
47994802
h_s = PyArray_typeinfo(ctx,
48004803
NPY_@name@LTR, NPY_@name@, NPY_BITSOF_@name@,
4801-
_ALIGN(@type@), HPy_FromPyObject(ctx, (PyObject *)_Py@Name@ArrType_Type_p)
4804+
_ALIGN(@type@), h_type
48024805
);
4806+
HPy_Close(ctx, h_type);
48034807
if (HPy_IsNull(h_s)) {
48044808
HPy_Close(ctx, h_infodict);
48054809
return -1;
@@ -4812,12 +4816,13 @@ set_typeinfo(HPyContext *ctx, HPy h_dict)
48124816
}
48134817

48144818
/**end repeat**/
4815-
4819+
h_type = HPy_FromPyObject(ctx, (PyObject *)_PyObjectArrType_Type_p);
48164820
h_s = PyArray_typeinfo(ctx,
48174821
NPY_OBJECTLTR, NPY_OBJECT, sizeof(PyObject *) * CHAR_BIT,
48184822
_ALIGN(PyObject *),
4819-
HPy_FromPyObject(ctx, (PyObject *)_PyObjectArrType_Type_p)
4823+
h_type
48204824
);
4825+
HPy_Close(ctx, h_type);
48214826
if (HPy_IsNull(h_s)) {
48224827
HPy_Close(ctx, h_infodict);
48234828
return -1;
@@ -4828,10 +4833,12 @@ set_typeinfo(HPyContext *ctx, HPy h_dict)
48284833
HPy_Close(ctx, h_infodict);
48294834
return -1;
48304835
}
4836+
h_type = HPy_FromPyObject(ctx, (PyObject *)_PyStringArrType_Type_p);
48314837
h_s = PyArray_typeinfo(ctx,
48324838
NPY_STRINGLTR, NPY_STRING, 0, _ALIGN(char),
4833-
HPy_FromPyObject(ctx, (PyObject *)_PyStringArrType_Type_p)
4839+
h_type
48344840
);
4841+
HPy_Close(ctx, h_type);
48354842
if (HPy_IsNull(h_s)) {
48364843
HPy_Close(ctx, h_infodict);
48374844
return -1;
@@ -4842,10 +4849,12 @@ set_typeinfo(HPyContext *ctx, HPy h_dict)
48424849
HPy_Close(ctx, h_infodict);
48434850
return -1;
48444851
}
4852+
h_type = HPy_FromPyObject(ctx, (PyObject *)_PyUnicodeArrType_Type_p);
48454853
h_s = PyArray_typeinfo(ctx,
48464854
NPY_UNICODELTR, NPY_UNICODE, 0, _ALIGN(npy_ucs4),
4847-
HPy_FromPyObject(ctx, (PyObject *)_PyUnicodeArrType_Type_p)
4855+
h_type
48484856
);
4857+
HPy_Close(ctx, h_type);
48494858
if (HPy_IsNull(h_s)) {
48504859
HPy_Close(ctx, h_infodict);
48514860
return -1;
@@ -4856,10 +4865,12 @@ set_typeinfo(HPyContext *ctx, HPy h_dict)
48564865
HPy_Close(ctx, h_infodict);
48574866
return -1;
48584867
}
4868+
h_type = HPy_FromPyObject(ctx, (PyObject *)_PyVoidArrType_Type_p);
48594869
h_s = PyArray_typeinfo(ctx,
48604870
NPY_VOIDLTR, NPY_VOID, 0, _ALIGN(char),
4861-
HPy_FromPyObject(ctx, (PyObject *)_PyVoidArrType_Type_p)
4871+
h_type
48624872
);
4873+
HPy_Close(ctx, h_type);
48634874
if (HPy_IsNull(h_s)) {
48644875
HPy_Close(ctx, h_infodict);
48654876
return -1;
@@ -4872,15 +4883,17 @@ set_typeinfo(HPyContext *ctx, HPy h_dict)
48724883
}
48734884
h_max = HPyLong_FromLong(ctx, NPY_MAX_DATETIME);
48744885
h_min = HPyLong_FromLong(ctx, NPY_MIN_DATETIME);
4886+
h_type = HPy_FromPyObject(ctx, (PyObject *)_PyDatetimeArrType_Type_p);
48754887
h_s = PyArray_typeinforanged(ctx,
48764888
NPY_DATETIMELTR, NPY_DATETIME, NPY_BITSOF_DATETIME,
48774889
_ALIGN(npy_datetime),
48784890
h_max,
48794891
h_min,
4880-
HPy_FromPyObject(ctx, (PyObject *)_PyDatetimeArrType_Type_p)
4892+
h_type
48814893
);
48824894
HPy_Close(ctx, h_max);
48834895
HPy_Close(ctx, h_min);
4896+
HPy_Close(ctx, h_type);
48844897
if (HPy_IsNull(h_s)) {
48854898
HPy_Close(ctx, h_infodict);
48864899
return -1;
@@ -4893,15 +4906,17 @@ set_typeinfo(HPyContext *ctx, HPy h_dict)
48934906
}
48944907
h_max = HPyLong_FromLong(ctx, NPY_MAX_TIMEDELTA);
48954908
h_min = HPyLong_FromLong(ctx, NPY_MIN_TIMEDELTA);
4909+
h_type = HPy_FromPyObject(ctx, (PyObject *)_PyTimedeltaArrType_Type_p);
48964910
h_s = PyArray_typeinforanged(ctx,
48974911
NPY_TIMEDELTALTR, NPY_TIMEDELTA, NPY_BITSOF_TIMEDELTA,
48984912
_ALIGN(npy_timedelta),
48994913
h_max,
49004914
h_min,
4901-
HPy_FromPyObject(ctx, (PyObject *)_PyTimedeltaArrType_Type_p)
4915+
h_type
49024916
);
49034917
HPy_Close(ctx, h_max);
49044918
HPy_Close(ctx, h_min);
4919+
HPy_Close(ctx, h_type);
49054920
if (HPy_IsNull(h_s)) {
49064921
HPy_Close(ctx, h_infodict);
49074922
return -1;
@@ -4915,11 +4930,14 @@ set_typeinfo(HPyContext *ctx, HPy h_dict)
49154930

49164931
#define SETTYPE(name) \
49174932
Py_INCREF(&Py##name##ArrType_Type); \
4933+
h_type = HPy_FromPyObject(ctx, (PyObject *)&Py##name##ArrType_Type); \
49184934
if (HPy_SetItem_s(ctx, h_infodict, #name, \
4919-
HPy_FromPyObject(ctx, (PyObject *)&Py##name##ArrType_Type)) < 0) { \
4935+
h_type) < 0) { \
49204936
HPy_Close(ctx, h_infodict); \
4937+
HPy_Close(ctx, h_type); \
49214938
return -1; \
4922-
}
4939+
} \
4940+
HPy_Close(ctx, h_type);
49234941

49244942
SETTYPE(Generic);
49254943
SETTYPE(Number);

0 commit comments

Comments
 (0)