8000 Pull request #16: Migrate PyArray_DTypeMeta fields and PyBoundArrayMe… · mattip/numpy@b54ca0f · GitHub
[go: up one dir, main page]

Skip to content

Commit b54ca0f

Browse files
committed
Pull request #16: Migrate PyArray_DTypeMeta fields and PyBoundArrayMethod_Type
Merge in ~STEPAN.SINDELAR_ORACLE.COM/numpy-hpy from fa/dtype_meta to labs-hpy-port * commit 'd8ece4384f1ed6e87bca9abd07061f1859b874f0': Remove PyObject_HEAD from PyBoundArrayMethodObject Fix: missing var init Fix wrong *_AsStruct call Migrate initialization of PyArrayMethod_Spec Migrate PyBoundArrayMethod_Type to pure HPy type Convert PyArray_DTypeMeta.singleton/scalar_type to HPyField add missing prototype
2 parents 4acf78a + d8ece43 commit b54ca0f

22 files changed

+1092
-759
lines changed

numpy/core/include/numpy/experimental_dtype_api.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@
160160
*/
161161
typedef struct {
162162
PyHeapTypeObject super;
163-
PyArray_Descr *singleton;
163+
HPyField singleton;
164164
int type_num;
165-
PyTypeObject *scalar_type;
165+
HPyField scalar_type;
166166
npy_uint64 flags;
167167
void *dt_slots;
168168
void *reserved[3];
@@ -396,9 +396,17 @@ typedef PyArray_Descr *__get_default_descr(
396396
static NPY_INLINE PyArray_Descr *
397397
PyArray_GetDefaultDescr(PyArray_DTypeMeta *DType)
398398
{
399-
if (DType->singleton != NULL) {
400-
Py_INCREF(DType->singleton);
401-
return DType->singleton;
399+
HPyContext *ctx = npy_get_context();
400+
PyArray_Descr *res;
401+
HPy h_dtype = HPy_FromPyObject(ctx, (PyObject *)DType);
402+
PyArray_DTypeMeta *h_dtype_data = PyArray_DTypeMeta_AsStruct(ctx, h_dtype);
403+
HPy h_singleton = HPyField_Load(ctx, h_dtype, h_dtype_data->singleton);
404+
HPy_Close(ctx, h_dtype);
405+
406+
if (!HPy_IsNull(h_singleton)) {
407+
res = (PyArray_Descr *) HPy_AsPyObject(ctx, h_singleton);
408+
HPy_Close(ctx, h_singleton);
409+
return res;
402410
}
403411
return _PyArray_GetDefaultDescr(DType);
404412
}

numpy/core/include/numpy/ndarraytypes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2070,12 +2070,12 @@ typedef void (PyDataMem_EventHookFunc)(void *inp, void *outp, size_t size,
20702070
* parametric legacy DTypes (bytes, string, void, datetime) this
20712071
* may be a pointer to the *prototype* instance?
20722072
*/
2073-
PyArray_Descr *singleton;
2073+
HPyField singleton; /* PyArray_Descr *singleton */
20742074
/* Copy of the legacy DTypes type number, usually invalid. */
20752075
int type_num;
20762076

20772077
/* The type object of the scalar instances (may be NULL?) */
2078-
PyTypeObject *scalar_type;
2078+
HPyField scalar_type; /* PyTypeObject *scalar_type */
20792079
/*
20802080
* DType flags to signal legacy, parametric, or
20812081
* abstract. But plenty of space for additional information/flags.

numpy/core/src/common/mem_overlap.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ NPY_VISIBILITY_HIDDEN mem_overlap_t
3838
solve_may_share_memory(PyArrayObject *a, PyArrayObject *b,
3939
Py_ssize_t max_work);
4040

41+
NPY_VISIBILITY_HIDDEN mem_overlap_t
42+
hpy_solve_may_share_memory(HPyContext *ctx, HPy a, HPy b,
43+
HPy_ssize_t max_work);
44+
4145
NPY_VISIBILITY_HIDDEN mem_overlap_t
4246
solve_may_have_internal_overlap(PyArrayObject *a, Py_ssize_t max_work);
4347

numpy/core/src/multiarray/abstractdtypes.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@
1111
#include "abstractdtypes.h"
1212
#include "array_coercion.h"
1313
#include "common.h"
14+
#include "arraytypes.h"
1415

1516

16-
static NPY_INLINE PyArray_Descr *
17-
int_default_descriptor(PyArray_DTypeMeta* NPY_UNUSED(cls))
17+
//static NPY_INLINE PyArray_Descr *
18+
//int_default_descriptor(PyArray_DTypeMeta* NPY_UNUSED(cls))
19+
static NPY_INLINE HPy
20+
int_default_descriptor(HPyContext *ctx, HPy NPY_UNUSED(cls))
1821
{
19-
return PyArray_DescrFromType(NPY_LONG);
22+
return HPyArray_DescrFromType(ctx, NPY_LONG);
2023
}
2124

2225
static PyArray_Descr *
@@ -51,10 +54,10 @@ discover_descriptor_from_pyint(
5154
}
5255

5356

54-
static NPY_INLINE PyArray_Descr *
55-
float_default_descriptor(PyArray_DTypeMeta* NPY_UNUSED(cls))
57+
static NPY_INLINE HPy
58+
float_default_descriptor(HPyContext *ctx, HPy NPY_UNUSED(cls))
5659
{
57-
return PyArray_DescrFromType(NPY_DOUBLE);
60+
return HPyArray_DescrFromType(ctx, NPY_DOUBLE);
5861
}
5962

6063

@@ -66,10 +69,10 @@ discover_descriptor_from_pyfloat(
6669
return PyArray_DescrFromType(NPY_DOUBLE);
6770
}
6871

69-
static NPY_INLINE PyArray_Descr *
70-
complex_default_descriptor(PyArray_DTypeMeta* NPY_UNUSED(cls))
72+
static NPY_INLINE HPy
73+
complex_default_descriptor(HPyContext *ctx, HPy NPY_UNUSED(cls))
7174
{
72-
return PyArray_DescrFromType(NPY_CDOUBLE);
75+
return HPyArray_DescrFromType(ctx, NPY_CDOUBLE);
7376
}
7477

7578
static PyArray_Descr*
@@ -128,7 +131,7 @@ initialize_and_map_pytypes_to_dtypes(HPyContext *ctx)
128131
PyArray_DTypeMeta *int_abstract_dtype_data = PyArray_DTypeMeta_AsStruct(ctx, h_PyArray_PyIntAbstractDType);
129132
int_abstract_dtype_data->dt_slots = &pyintabstractdtype_slots;
130133
int_abstract_dtype_data->flags = NPY_DT_ABSTRACT;
131-
int_abstract_dtype_data->scalar_type = &PyLong_Type;
134+
HPyField_Store(ctx, h_PyArray_PyIntAbstractDType, &int_abstract_dtype_data->scalar_type, ctx->h_LongType);
132135
PyArray_PyIntAbstractDType = (PyArray_DTypeMeta *) HPy_AsPyObject(ctx, h_PyArray_PyIntAbstractDType);
133136
HPy_Close(ctx, h_PyArray_PyIntAbstractDType);
134137

@@ -140,7 +143,7 @@ initialize_and_map_pytypes_to_dtypes(HPyContext *ctx)
140143
PyArray_DTypeMeta *float_abstract_dtype_data = PyArray_DTypeMeta_AsStruct(ctx, h_PyArray_PyFloatAbstractDType);
141144
float_abstract_dtype_data->dt_slots = &pyfloatabstractdtype_slots;
142145
float_abstract_dtype_data->flags = NPY_DT_ABSTRACT;
143-
float_abstract_dtype_data->scalar_type = &PyFloat_Type;
146+
HPyField_Store(ctx, h_PyArray_PyFloatAbstractDType, &float_abstract_dtype_data->scalar_type, ctx->h_FloatType);
144147
PyArray_PyFloatAbstractDType = (PyArray_DTypeMeta *) HPy_AsPyObject(ctx, h_PyArray_PyFloatAbstractDType);
145148
HPy_Close(ctx, h_PyArray_PyFloatAbstractDType);
146149

@@ -152,7 +155,7 @@ initialize_and_map_pytypes_to_dtypes(HPyContext *ctx)
152155
PyArray_DTypeMeta *complex_abstract_dtype_data = PyArray_DTypeMeta_AsStruct(ctx, h_PyArray_PyComplexAbstractDType);
153156
complex_abstract_dtype_data->dt_slots = &pycomplexabstractdtype_slots;
154157
complex_abstract_dtype_data->flags = NPY_DT_ABSTRACT;
155-
complex_abstract_dtype_data->scalar_type = &PyComplex_Type;
158+
HPyField_Store(ctx, h_PyArray_PyComplexAbstractDType, &complex_abstract_dtype_data->scalar_type, ctx->h_ComplexType);
156159
PyArray_PyComplexAbstractDType = (PyArray_DTypeMeta *) HPy_AsPyObject(ctx, h_PyArray_PyComplexAbstractDType);
157160
HPy_Close(ctx, h_PyArray_PyComplexAbstractDType);
158161

@@ -327,4 +330,4 @@ NPY_DType_Slots pycomplexabstractdtype_slots = {
327330
.default_descr = complex_default_descriptor,
328331
.discover_descr_from_pyobject = discover_descriptor_from_pycomplex,
329332
.common_dtype = complex_common_dtype,
330-
};
333+
};

numpy/core/src/multiarray/array_coercion.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,15 @@ discover_dtype_from_pyobject(
257257
* handle the scalar-type. (Even if it cannot handle here it may be
258258
* asked to attempt to do so later, if no other matching DType exists.)
259259
*/
260-
if ((Py_TYPE(obj) == fixed_DType->scalar_type) ||
260+
HPyContext *ctx = npy_get_context();
261+
HPy h_fixed_DType = HPy_FromPyObject(ctx, (PyObject *)fixed_DType);
262+
HPy h_scalar_type = HPyField_Load(ctx, h_fixed_DType, PyArray_DTypeMeta_AsStruct(ctx, h_fixed_DType)->scalar_type);
263+
PyTypeObject *scalar_type = (PyTypeObject *)HPy_AsPyObject(ctx, h_scalar_type);
264+
int is_scalar_type = Py_TYPE(obj) == scalar_type;
265+
Py_DECREF(scalar_type);
266+
HPy_Close(ctx, h_scalar_type);
267+
HPy_Close(ctx, h_fixed_DType);
268+
if (is_scalar_type ||
261269
NPY_DT_CALL_is_known_scalar_type(fixed_DType, Py_TYPE(obj))) {
262270
Py_INCREF(fixed_DType);
263271
return fixed_DType;

0 commit comments

Comments
 (0)
0