8000 Get rid of HPy_SetType · mattip/numpy@607adc7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 607adc7

Browse files
committed
Get rid of HPy_SetType
1 parent 1268bb1 commit 607adc7

File tree

5 files changed

+68
-76
lines changed

5 files changed

+68
-76
lines changed

numpy/core/src/multiarray/_multiarray_tests.c.src

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ create_custom_field_dtype_impl(HPyContext *ctx, HPy dummy, const HPy *args, size
731731
* Test that we reject this if the type is not set to something that
732732
* we are pretty sure can be safely replaced.
733733
*/
734-
HPy_SetType(ctx, h_dtype, h_scalar_type);
734+
// HPy_SetType(ctx, h_dtype, h_scalar_type);
735735
}
736736
else if (error_path != 0) {
737737
HPyErr_SetString(ctx, ctx->h_ValueError,
@@ -741,7 +741,7 @@ create_custom_field_dtype_impl(HPyContext *ctx, HPy dummy, const HPy *args, size
741741
}
742742
if (HPyArray_RegisterDataType(ctx, h_dtype) < 0) {
743743
/* Fix original type in the error_path == 2 case and delete it */
744-
HPy_SetType(ctx, h_dtype, h_original_type);
744+
// HPy_SetType(ctx, h_dtype, h_original_type);
745745
HPy_Close(ctx, h_original_type);
746746
return HPy_NULL;
747747
}

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

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4836,8 +4836,17 @@ static PyArray_ArrFuncs _Py@NAME@_ArrFuncs = {
48364836

48374837
static void init_@from@_Descr(HPyContext *ctx, HPyGlobal *global, HPy desc_type) {
48384838
PyArray_Descr *data;
4839-
HPy obj = HPy_New(ctx, desc_type, &data);
4839+
// HPy h_descr = HPy_New(ctx, desc_type, &data);
48404840

4841+
HPy typeobj = HPyGlobal_Load(ctx, HPy@NAME@ArrType_Type);
4842+
HPy h_descr = dtypemeta_init_legacy_descriptor(ctx, NPY_@from@, &_Py@NAME@_ArrFuncs, typeobj, desc_type);
4843+
if (HPy_IsNull(h_descr)) {
4844+
HPy_Close(ctx, typeobj);
4845+
// TODO HPY LABS PORT: error ?
4846+
return;
4847+
}
4848+
4849+
data = PyArray_Descr_AsStruct(ctx, h_descr);
48414850
data->typeobj = HPyField_NULL;
48424851
data->kind = NPY_@from@LTR;
48434852
data->type = NPY_@from@LTR;
@@ -4853,12 +4862,11 @@ static void init_@from@_Descr(HPyContext *ctx, HPyGlobal *global, HPy desc_type)
48534862
data->metadata = NULL;
48544863
data->c_metadata = NULL;
48554864
data->hash = -1;
4856-
HPy typeobj = HPyGlobal_Load(ctx, HPy@NAME@ArrType_Type);
4857-
HPyField_Store(ctx, obj, &data->typeobj, typeobj);
4865+
HPyField_Store(ctx, h_descr, &data->typeobj, typeobj);
48584866
HPy_Close(ctx, typeobj);
48594867

4860-
HPyGlobal_Store(ctx, global, obj);
4861-
HPy_Close(ctx, obj);
4868+
HPyGlobal_Store(ctx, global, h_descr);
4869+
HPy_Close(ctx, h_descr);
48624870
}
48634871

48644872
/**end repeat**/
@@ -4976,8 +4984,17 @@ static PyArray_ArrFuncs _Py@NAME@_ArrFuncs = {
49764984

49774985
static void init_@from@_Descr(HPyContext *ctx, HPyGlobal *global, HPy desc_type) {
49784986
PyArray_Descr *data;
4979-
HPy obj = HPy_New(ctx, desc_type, &data);
4987+
// HPy h_descr = HPy_New(ctx, desc_type, &data);
49804988

4989+
HPy typeobj = HPyGlobal_Load(ctx, HPy@NAME@ArrType_Type);
4990+
HPy h_descr = dtypemeta_init_legacy_descriptor(ctx, NPY_@from@, &_Py@NAME@_ArrFuncs, typeobj, desc_type);
4991+
if (HPy_IsNull(h_descr)) {
4992+
HPy_Close(ctx, typeobj);
4993+
// TODO HPY LABS PORT: error ?
4994+
return;
4995+
}
4996+
4997+
data = PyArray_Descr_AsStruct(ctx, h_descr);
49814998
data->typeobj = HPyField_NULL;
49824999
data->kind = NPY_@kind@LTR;
49835000
data->type = NPY_@from@LTR;
@@ -4993,12 +5010,11 @@ static void init_@from@_Descr(HPyContext *ctx, HPyGlobal *global, HPy desc_type)
49935010
data->metadata = NULL;
49945011
data->c_metadata = NULL;
49955012
data->hash = -1;
4996-
HPy typeobj = HPyGlobal_Load(ctx, HPy@NAME@ArrType_Type);
4997-
HPyField_Store(ctx, obj, &data->typeobj, typeobj);
5013+
HPyField_Store(ctx, h_descr, &data->typeobj, typeobj);
49985014
HPy_Close(ctx, typeobj);
49995015

5000-
HPyGlobal_Store(ctx, global, obj);
5001-
HPy_Close(ctx, obj);
5016+
HPyGlobal_Store(ctx, global, h_descr);
5017+
HPy_Close(ctx, h_descr);
50025018
}
50035019

50045020
/**end repeat**/
@@ -5175,6 +5191,7 @@ set_typeinfo(HPyContext *ctx, HPy h_dict)
51755191
* should be defined on the class and inherited to the scalar.
51765192
* (NPY_HALF is the largest builtin one.)
51775193
*/
5194+
/*
51785195
for (i = 0; i <= NPY_HALF; i++) {
51795196
h_descr = HPyGlobal_Load(ctx, _hpy_builtin_descrs[i]);
51805197
PyArray_Descr *descr = PyArray_Descr_AsStruct(ctx, h_descr);
@@ -5184,6 +5201,7 @@ set_typeinfo(HPyContext *ctx, HPy h_dict)
51845201
}
51855202
HPy_Close(ctx, h_descr);
51865203
}
5204+
*/
51875205

51885206
/*
51895207
* Add cast functions for the new types

numpy/core/src/multiarray/dtypemeta.c

Lines changed: 34 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -678,43 +678,14 @@ hdiscover_descr_from_pyobject_function_trampoline(HPyContext *ctx, HPy cls, HPy
678678
*
679679
* @returns 0 on success, -1 on failure.
680680
*/
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)
683683
{
684-
int result = -1;
685-
HPy h_typeobj = HPy_NULL;
686684
HPy h_PyArrayDescr_Type = HPy_NULL;
687685
HPy h_PyArrayDTypeMeta_Type = HPy_NULL;
688686
HPy h_new_dtype_type = HPy_NULL;
689687

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);
718689

719690
/*
720691
* 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
726697
* However, this function remains necessary for legacy user dtypes.
727698
*/
728699

729-
h_typeobj = HPyField_Load(ctx, h_descr, descr->typeobj);
730700
const char *scalar_name = HPyType_GetName(ctx, h_typeobj);
731701

732702
/*
@@ -739,20 +709,22 @@ dtypemeta_wrap_legacy_descriptor(HPyContext *ctx, HPy h_descr, PyArray_Descr *de
739709
if (dot) {
740710
scalar_name = dot + 1;
741711
}
742-
Py_ssize_t name_length = strlen(scalar_name) + 14;
712+
size_t name_length = strlen(scalar_name) + 14;
743713

744-
char *tp_name = PyMem_Malloc(name_length);
714+
// TODO HPY LABS PORT: PyMem_Malloc
715+
char *tp_name = MEM_MALLOC(name_length);
745716
if (tp_name == NULL) {
746-
PyErr_NoMemory();
747-
return -1;
717+
HPyErr_NoMemory(ctx);
718+
goto cleanup;
748719
}
749720

750721
snprintf(tp_name, name_length, "numpy.dtype[%s]", scalar_name);
751722

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));
753725
if (dt_slots == NULL) {
754726
PyMem_Free(tp_name);
755-
return -1;
727+
goto cleanup;
756728
}
757729
memset(dt_slots, '\0', sizeof(NPY_DType_Slots));
758730

@@ -766,7 +738,7 @@ dtypemeta_wrap_legacy_descriptor(HPyContext *ctx, HPy h_descr, PyArray_Descr *de
766738

767739
HPyType_SpecParam new_dtype_params[] = {
768740
{ 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?
770742
{ HPyType_SpecParam_Metaclass, h_PyArrayDTypeMeta_Type },
771743
{ 0 }
772744
};
@@ -790,11 +762,11 @@ dtypemeta_wrap_legacy_descriptor(HPyContext *ctx, HPy h_descr, PyArray_Descr *de
790762
* Fill DTypeMeta information that varies between DTypes, any variable
791763
* type information would need to be set before PyType_Ready().
792764
*/
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);
794766
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;
796768
new_dtype_data->flags = NPY_DT_LEGACY;
797-
dt_slots->f = *(descr->f);
769+
dt_slots->f = *f;
798770

799771
/* Set default functions (correct for most dtypes, override below) */
800772
dt_slots->default_descr = nonparametric_default_descr;
@@ -811,15 +783,15 @@ dtypemeta_wrap_legacy_descriptor(HPyContext *ctx, HPy h_descr, PyArray_Descr *de
811783
dt_slots->is_known_scalar_type = signed_integers_is_known_scalar_types;
812784
}
813785

814-
if (PyTypeNum_ISUSERDEF(descr->type_num)) {
786+
if (PyTypeNum_ISUSERDEF(type_num)) {
815787
dt_slots->common_dtype = legacy_userdtype_common_dtype_function;
816788
dt_slots->hpy_common_dtype = hpy_legacy_userdtype_common_dtype_function;
817789
}
818-
else if (descr->type_num == NPY_OBJECT) {
790+
else if (type_num == NPY_OBJECT) {
819791
dt_slots->common_dtype = object_common_dtype;
820792
dt_slots->hpy_common_dtype = hpy_object_common_dtype;
821793
}
822-
else if (PyTypeNum_ISDATETIME(descr->type_num)) {
794+
else if (PyTypeNum_ISDATETIME(type_num)) {
823795
/* Datetimes are flexible, but were not considered previously */
824796
new_dtype_data->flags |= NPY_DT_PARAMETRIC;
825797
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
831803
dt_slots->hpy_common_dtype = hpy_datetime_common_dtype;
832804
dt_slots->common_instance = datetime_type_promotion;
833805
dt_slots->hpy_common_instance = hpy_datetime_type_promotion;
834-
if (descr->type_num == NPY_DATETIME) {
806+
if (type_num == NPY_DATETIME) {
835807
dt_slots->is_known_scalar_type = datetime_known_scalar_types;
836808
}
837809
}
838-
else if (PyTypeNum_ISFLEXIBLE(descr->type_num)) {
810+
else if (PyTypeNum_ISFLEXIBLE(type_num)) {
839811
new_dtype_data->flags |= NPY_DT_PARAMETRIC;
840-
if (descr->type_num == NPY_VOID) {
812+
if (type_num == NPY_VOID) {
841813
dt_slots->default_descr = void_default_descr;
842814
dt_slots->discover_descr_from_pyobject = (
843815
void_discover_descr_from_pyobject);
@@ -860,31 +832,32 @@ dtypemeta_wrap_legacy_descriptor(HPyContext *ctx, HPy h_descr, PyArray_Descr *de
860832
}
861833
}
862834

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,
865842
PyTypeNum_ISUSERDEF(new_dtype_data->type_num)) < 0) {
866-
HPy_Close(ctx, descr_typeobj);
843+
// HPy_Close(ctx, descr_typeobj);
867844
goto cleanup;
868845
}
869-
HPy_Close(ctx, descr_typeobj);
846+
// HPy_Close(ctx, descr_typeobj);
870847

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;
878849

879850
cleanup:
880851
HPy_Close(ctx, h_typeobj);
881852
HPy_Close(ctx, h_PyArrayDescr_Type);
882853
HPy_Close(ctx, h_PyArrayDTypeMeta_Type);
883854
HPy_Close(ctx, h_new_dtype_type);
884-
return result;
855+
// HPy_Close(ctx, array_descr_type);
856+
return HPy_NULL;
885857
}
886858

887859

860+
888861
/*
889862
* Simple exposed information, defined for each DType (class).
890863
*/

numpy/core/src/multiarray/dtypemeta.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,8 @@ NPY_NO_EXPORT int
247247
python_builtins_are_known_scalar_types(HPyContext *ctx,
248248
HPy cls, HPy pytype);
249249

250-
NPY_NO_EXPORT int
251-
dtypemeta_wrap_legacy_descriptor(HPyContext *ctx, HPy h_descr, PyArray_Descr *dtypem);
250+
NPY_NO_EXPORT HPy
251+
dtypemeta_init_legacy_descriptor(HPyContext *ctx, int type_num, PyArray_ArrFuncs *f, HPy h_typeobj, HPy array_descr_type);
252252

253253
NPY_NO_EXPORT PyArray_Descr *
254254
default_descr_function_trampoline(PyArray_DTypeMeta *cls);

numpy/core/src/multiarray/multiarraymodule.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6445,7 +6445,7 @@ static int _multiarray_umath_impl(HPyContext *ctx, HPy h_mod) {
64456445
goto err;
64466446
}
64476447

6448-
init_arraytypes_hpy_global_state(ctx);
6448+
// init_arraytypes_hpy_global_state(ctx);
64496449
// HPY: TODO comment on this
64506450
if (init_scalartypes_basetypes(ctx) != 0) {
64516451
goto err;
@@ -6621,6 +6621,7 @@ static int _multiarray_umath_impl(HPyContext *ctx, HPy h_mod) {
66216621
goto err;
66226622
}
66236623

6624+
init_arraytypes_hpy_global_state(ctx);
66246625
if (set_typeinfo(ctx, h_d) != 0) {
66256626
goto err;
66266627
}

0 commit comments

Comments
 (0)
0