@@ -678,43 +678,14 @@ hdiscover_descr_from_pyobject_function_trampoline(HPyContext *ctx, HPy cls, HPy
678
678
*
679
679
* @returns 0 on success, -1 on failure.
680
680
*/
681
- NPY_NO_EXPORT int
682
- dtypemeta_wrap_legacy_descriptor (HPyContext * ctx , HPy h_descr , PyArray_Descr * descr )
681
+ NPY_NO_EXPORT HPy
682
+ dtypemeta_init_legacy_descriptor (HPyContext * ctx , int type_num , PyArray_ArrFuncs * f , HPy h_typeobj , HPy array_descr_type )
683
683
{
684
- int result = -1 ;
685
- HPy h_typeobj = HPy_NULL ;
686
684
HPy h_PyArrayDescr_Type = HPy_NULL ;
687
685
HPy h_PyArrayDTypeMeta_Type = HPy_NULL ;
688
686
HPy h_new_dtype_type = HPy_NULL ;
689
687
690
- HPy descr_type = HPy_Type (ctx , h_descr );
691
- HPy array_descr_type = HPyGlobal_Load (ctx , HPyArrayDescr_Type );
692
- int has_type_set = HPy_Is (ctx , descr_type , array_descr_type );
693
- HPy_Close (ctx , array_descr_type );
694
- if (!has_type_set ) {
695
- /* Accept if the type was filled in from an existing builtin dtype */
696
- for (int i = 0 ; i < NPY_NTYPES ; i ++ ) {
697
- HPy builtin = HPyArray_DescrFromType (ctx , i );
698
- has_type_set = HPy_Is (ctx , descr_type , HPy_Type (ctx , builtin ));
699
- HPy_Close (ctx , builtin );
700
- if (has_type_set ) {
701
- break ;
702
- }
703
- }
704
- }
705
- HPy_Close (ctx , descr_type );
706
-
707
- if (!has_type_set ) {
708
- HPyErr_SetString (ctx , ctx -> h_RuntimeError ,
709
- "During creation/wrapping of legacy DType, the original class "
710
- "was not of PyArrayDescr_Type (it is replaced in this step). "
711
- "The extension creating a custom DType for type %S must be "
712
- "modified to ensure `Py_TYPE(descr) == &PyArrayDescr_Type` or "
713
- "that of an existing dtype (with the assumption it is just "
714
- "copied over and can be replaced)."
715
- /*, descr->typeobj, Py_TYPE(descr)*/ );
716
- goto cleanup ;
717
- }
688
+ // HPy array_descr_type = HPyGlobal_Load(ctx, HPyArrayDescr_Type);
718
689
719
690
/*
720
691
* Note: we have no intention of freeing the memory again since this
@@ -726,7 +697,6 @@ dtypemeta_wrap_legacy_descriptor(HPyContext *ctx, HPy h_descr, PyArray_Descr *de
726
697
* However, this function remains necessary for legacy user dtypes.
727
698
*/
728
699
729
- h_typeobj = HPyField_Load (ctx , h_descr , descr -> typeobj );
730
700
const char * scalar_name = HPyType_GetName (ctx , h_typeobj );
731
701
732
702
/*
@@ -739,20 +709,22 @@ dtypemeta_wrap_legacy_descriptor(HPyContext *ctx, HPy h_descr, PyArray_Descr *de
739
709
if (dot ) {
740
710
scalar_name = dot + 1 ;
741
711
}
742
- Py_ssize_t name_length = strlen (scalar_name ) + 14 ;
712
+ size_t name_length = strlen (scalar_name ) + 14 ;
743
713
744
- char * tp_name = PyMem_Malloc (name_length );
714
+ // TODO HPY LABS PORT: PyMem_Malloc
715
+ char * tp_name = MEM_MALLOC (name_length );
745
716
if (tp_name == NULL ) {
746
- PyErr_NoMemory ( );
747
- return -1 ;
717
+ HPyErr_NoMemory ( ctx );
718
+ goto cleanup ;
748
719
}
749
720
750
721
snprintf (tp_name , name_length , "numpy.dtype[%s]" , scalar_name );
751
722
752
- NPY_DType_Slots * dt_slots = PyMem_Malloc (sizeof (NPY_DType_Slots ));
723
+ // TODO HPY LABS PORT: PyMem_Malloc
724
+ NPY_DType_Slots * dt_slots = MEM_MALLOC (sizeof (NPY_DType_Slots ));
753
725
if (dt_slots == NULL ) {
754
726
PyMem_Free (tp_name );
755
- return -1 ;
727
+ goto cleanup ;
756
728
}
757
729
memset (dt_slots , '\0' , sizeof (NPY_DType_Slots ));
758
730
@@ -766,7 +738,7 @@ dtypemeta_wrap_legacy_descriptor(HPyContext *ctx, HPy h_descr, PyArray_Descr *de
766
738
767
739
HPyType_SpecParam new_dtype_params [] = {
768
740
{ HPyType_SpecParam_Base , h_PyArrayDescr_Type },
769
- // HPY STEVE TODO: do I need to specify the metaclass if the base already has it as its metaclass?
741
+ // TODO HPY LABS PORT: is it necessary to specify the metaclass if the base already has it as its metaclass?
770
742
{ HPyType_SpecParam_Metaclass , h_PyArrayDTypeMeta_Type },
771
743
{ 0 }
772
744
};
@@ -790,11 +762,11 @@ dtypemeta_wrap_legacy_descriptor(HPyContext *ctx, HPy h_descr, PyArray_Descr *de
790
762
* Fill DTypeMeta information that varies between DTypes, any variable
791
763
* type information would need to be set before PyType_Ready().
792
764
*/
793
- HPyField_Store (ctx , h_new_dtype_type , & new_dtype_data -> singleton , h_descr );
765
+ // HPyField_Store(ctx, h_new_dtype_type, &new_dtype_data->singleton, h_descr);
794
766
HPyField_Store (ctx , h_new_dtype_type , & new_dtype_data -> scalar_type , h_typeobj );
795
- new_dtype_data -> type_num = descr -> type_num ;
767
+ new_dtype_data -> type_num = type_num ;
796
768
new_dtype_data -> flags = NPY_DT_LEGACY ;
797
- dt_slots -> f = * ( descr -> f ) ;
769
+ dt_slots -> f = * f ;
798
770
799
771
/* Set default functions (correct for most dtypes, override below) */
800
772
dt_slots -> default_descr = nonparametric_default_descr ;
@@ -811,15 +783,15 @@ dtypemeta_wrap_legacy_descriptor(HPyContext *ctx, HPy h_descr, PyArray_Descr *de
811
783
dt_slots -> is_known_scalar_type = signed_integers_is_known_scalar_types ;
812
784
}
813
785
814
- if (PyTypeNum_ISUSERDEF (descr -> type_num )) {
786
+ if (PyTypeNum_ISUSERDEF (type_num )) {
815
787
dt_slots -> common_dtype = legacy_userdtype_common_dtype_function ;
816
788
dt_slots -> hpy_common_dtype = hpy_legacy_userdtype_common_dtype_function ;
817
789
}
818
- else if (descr -> type_num == NPY_OBJECT ) {
790
+ else if (type_num == NPY_OBJECT ) {
819
791
dt_slots -> common_dtype = object_common_dtype ;
820
792
dt_slots -> hpy_common_dtype = hpy_object_common_dtype ;
821
793
}
822
- else if (PyTypeNum_ISDATETIME (descr -> type_num )) {
794
+ else if (PyTypeNum_ISDATETIME (type_num )) {
823
795
/* Datetimes are flexible, but were not considered previously */
824
796
new_dtype_data -> flags |= NPY_DT_PARAMETRIC ;
825
797
dt_slots -> default_descr = datetime_and_timedelta_default_descr ;
@@ -831,13 +803,13 @@ dtypemeta_wrap_legacy_descriptor(HPyContext *ctx, HPy h_descr, PyArray_Descr *de
831
803
dt_slots -> hpy_common_dtype = hpy_datetime_common_dtype ;
832
804
dt_slots -> common_instance = datetime_type_promotion ;
833
805
dt_slots -> hpy_common_instance = hpy_datetime_type_promotion ;
834
- if (descr -> type_num == NPY_DATETIME ) {
806
+ if (type_num == NPY_DATETIME ) {
835
807
dt_slots -> is_known_scalar_type = datetime_known_scalar_types ;
836
808
}
837
809
}
838
- else if (PyTypeNum_ISFLEXIBLE (descr -> type_num )) {
810
+ else if (PyTypeNum_ISFLEXIBLE (type_num )) {
839
811
new_dtype_data -> flags |= NPY_DT_PARAMETRIC ;
840
- if (descr -> type_num == NPY_VOID ) {
812
+ if (type_num == NPY_VOID ) {
841
813
dt_slots -> default_descr = void_default_descr ;
842
814
dt_slots -> discover_descr_from_pyobject = (
843
815
void_discover_descr_from_pyobject );
@@ -860,31 +832,32 @@ dtypemeta_wrap_legacy_descriptor(HPyContext *ctx, HPy h_descr, PyArray_Descr *de
860
832
}
861
833
}
862
834
863
- HPy descr_typeobj = HPyField_Load (ctx , h_descr , descr -> typeobj );
864
- if (_PyArray_MapPyTypeToDType (ctx , h_new_dtype_type , descr_typeobj ,
835
+ // create singleton
836
+ PyArray_Descr * data ;
837
+ HPy h_descr = HPy_New (ctx , h_new_dtype_type , & data );
838
+ HPyField_Store (ctx , h_new_dtype_type , & new_dtype_data -> singleton , h_descr );
839
+
840
+ // HPy descr_typeobj = HPyField_Load(ctx, h_descr, h_typeobj);
841
+ if (_PyArray_MapPyTypeToDType (ctx , h_new_dtype_type , /* descr_typeobj */ h_typeobj ,
865
842
PyTypeNum_ISUSERDEF (new_dtype_data -> type_num )) < 0 ) {
866
- HPy_Close (ctx , descr_typeobj );
843
+ // HPy_Close(ctx, descr_typeobj);
867
844
goto cleanup ;
868
845
}
869
- HPy_Close (ctx , descr_typeobj );
846
+ // HPy_Close(ctx, descr_typeobj);
870
847
871
- /* Finally, replace the current class of the descr */
872
- // Re HPy_SetType: in longer term, this can be refactored: it seems that
873
- // this whole function is here to support some legacy API, which we can keep
874
- // in C API, and to initialize singleton descriptors like BOOL_Descr, which
875
- // we can initialize with the right type already to avoid setting it ex-post
876
- HPy_SetType (ctx , h_descr , h_new_dtype_type );
877
- result = 0 ;
848
+ return h_descr ;
878
849
879
850
cleanup :
880
851
HPy_Close (ctx , h_typeobj );
881
852
HPy_Close (ctx , h_PyArrayDescr_Type );
882
853
HPy_Close (ctx , h_PyArrayDTypeMeta_Type );
883
854
HPy_Close (ctx , h_new_dtype_type );
884
- return result ;
855
+ // HPy_Close(ctx, array_descr_type);
856
+ return HPy_NULL ;
885
857
}
886
858
887
859
860
+
888
861
/*
889
862
* Simple exposed information, defined for each DType (class).
890
863
*/
0 commit comments