@@ -4590,9 +4590,9 @@ PyArray_DescrFromType(int type)
4590
4590
* and is used to initialize internal dtype tables.
4591
4591
*/
4592
4592
NPY_NO_EXPORT int
4593
- set_typeinfo (HPyContext * ctx , PyObject * dict )
4593
+ set_typeinfo (HPyContext * ctx , HPy h_dict )
4594
4594
{
4595
- PyObject * infodict , * s ;
4595
+ HPy h_s , h_infodict , h_max , h_min , h_type ;
4596
4596
int i ;
4597
4597
4598
4598
PyArray_Descr * dtype ;
@@ -4714,8 +4714,8 @@ set_typeinfo(HPyContext *ctx, PyObject *dict)
4714
4714
/**end repeat**/
4715
4715
4716
4716
/* 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 ;
4719
4719
4720
4720
int ret ;
4721
4721
/**begin repeat
@@ -4738,38 +4738,53 @@ set_typeinfo(HPyContext *ctx, PyObject *dict)
4738
4738
* npy_long, npy_ulong, npy_longlong, npy_ulonglong#
4739
4739
* #max= 1,
4740
4740
* 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),
4744
4744
* 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)#
4748
4748
* #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),
4750
4750
* 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_, //#
4754
4758
*/
4755
4759
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 ,
4757
4767
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
4761
4771
);
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 );
4764
4779
return -1 ;
4765
4780
}
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 );
4768
4783
if (ret < 0 ) {
4769
- Py_DECREF ( infodict );
4784
+ HPy_Close ( ctx , h_infodict );
4770
4785
return -1 ;
4771
4786
}
4772
-
4787
+ #undef _CTX_COMMA_
4773
4788
4774
4789
/**end repeat**/
4775
4790
@@ -4783,122 +4798,146 @@ set_typeinfo(HPyContext *ctx, PyObject *dict)
4783
4798
* #Name = Half, Float, Double, LongDouble,
4784
4799
* CFloat, CDouble, CLongDouble#
4785
4800
*/
4786
- s = PyArray_typeinfo (
4801
+ h_type = HPy_FromPyObject (ctx , (PyObject * )_Py @Name @ArrType_Type_p );
4802
+ h_s = PyArray_typeinfo (ctx ,
4787
4803
NPY_ @name @LTR , NPY_ @name @, NPY_BITSOF_ @name @,
4788
- _ALIGN (@type @), & Py @ Name @ ArrType_Type
4804
+ _ALIGN (@type @), h_type
4789
4805
);
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 );
4792
4809
return -1 ;
4793
4810
}
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 );
4796
4813
if (ret < 0 ) {
4797
- Py_DECREF ( infodict );
4814
+ HPy_Close ( ctx , h_infodict );
4798
4815
return -1 ;
4799
4816
}
4800
4817
4801
4818
/**end repeat**/
4802
-
4803
- s = PyArray_typeinfo (
4819
+ h_type = HPy_FromPyObject ( ctx , ( PyObject * ) _PyObjectArrType_Type_p );
4820
+ h_s = PyArray_typeinfo (ctx ,
4804
4821
NPY_OBJECTLTR , NPY_OBJECT , sizeof (PyObject * ) * CHAR_BIT ,
4805
4822
_ALIGN (PyObject * ),
4806
- & PyObjectArrType_Type
4823
+ h_type
4807
4824
);
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 );
4810
4828
return -1 ;
4811
4829
}
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 );
4814
4832
if (ret < 0 ) {
4815
- Py_DECREF ( infodict );
4833
+ HPy_Close ( ctx , h_infodict );
4816
4834
return -1 ;
4817
4835
}
4818
- s = PyArray_typeinfo (
4836
+ h_type = HPy_FromPyObject (ctx , (PyObject * )_PyStringArrType_Type_p );
4837
+ h_s = PyArray_typeinfo (ctx ,
4819
4838
NPY_STRINGLTR , NPY_STRING , 0 , _ALIGN (char ),
4820
- & PyStringArrType_Type
4839
+ h_type
4821
4840
);
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 );
4824
4844
return -1 ;
4825
4845
}
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 );
4828
4848
if (ret < 0 ) {
4829
- Py_DECREF ( infodict );
4849
+ HPy_Close ( ctx , h_infodict );
4830
4850
return -1 ;
4831
4851
}
4832
- s = PyArray_typeinfo (
4852
+ h_type = HPy_FromPyObject (ctx , (PyObject * )_PyUnicodeArrType_Type_p );
4853
+ h_s = PyArray_typeinfo (ctx ,
4833
4854
NPY_UNICODELTR , NPY_UNICODE , 0 , _ALIGN (npy_ucs4 ),
4834
- & PyUnicodeArrType_Type
4855
+ h_type
4835
4856
);
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 );
4838
4860
return -1 ;
4839
4861
}
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 );
4842
4864
if (ret < 0 ) {
4843
- Py_DECREF ( infodict );
4865
+ HPy_Close ( ctx , h_infodict );
4844
4866
return -1 ;
4845
4867
}
4846
- s = PyArray_typeinfo (
4868
+ h_type = HPy_FromPyObject (ctx , (PyObject * )_PyVoidArrType_Type_p );
4869
+ h_s = PyArray_typeinfo (ctx ,
4847
4870
NPY_VOIDLTR , NPY_VOID , 0 , _ALIGN (char ),
4848
- & PyVoidArrType_Type
4871
+ h_type
4849
4872
);
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 );
4852
4876
return -1 ;
4853
4877
}
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 );
4856
4880
if (ret < 0 ) {
4857
- Py_DECREF ( infodict );
4881
+ HPy_Close ( ctx , h_infodict );
4858
4882
return -1 ;
4859
4883
}
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 ,
4861
4888
NPY_DATETIMELTR , NPY_DATETIME , NPY_BITSOF_DATETIME ,
4862
4889
_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
4866
4893
);
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 );
4869
4899
return -1 ;
4870
4900
}
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 );
4873
4903
if (ret < 0 ) {
4874
- Py_DECREF ( infodict );
4904
+ HPy_Close ( ctx , h_infodict );
4875
4905
return -1 ;
4876
4906
}
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 ,
4878
4911
NPY_TIMEDELTALTR , NPY_TIMEDELTA , NPY_BITSOF_TIMEDELTA ,
4879
4912
_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
4883
4916
);
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 );
4886
4922
return -1 ;
4887
4923
}
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 );
4890
4926
if (ret < 0 ) {
4891
- Py_DECREF ( infodict );
4927
+ HPy_Close ( ctx , h_infodict );
4892
4928
return -1 ;
4893
4929
}
4894
4930
4895
4931
#define SETTYPE (name ) \
4896
4932
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); \
4900
4938
return -1; \
4901
- }
4939
+ } \
4940
+ HPy_Close(ctx, h_type);
4902
4941
4903
4942
SETTYPE (Generic );
4904
4943
SETTYPE (Number );
@@ -4913,8 +4952,8 @@ set_typeinfo(HPyContext *ctx, PyObject *dict)
4913
4952
4914
4953
#undef SETTYPE
4915
4954
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 );
4918
4957
if (ret < 0 ) {
4919
4958
return -1 ;
4920
4959
}
0 commit comments