8000 small fixups/name changes · numpy/numpy@2ea745b · GitHub
[go: up one dir, main page]

Skip to content

Commit 2ea745b

Browse files
committed
small fixups/name changes
1 parent e6fde0e commit 2ea745b

File tree

3 files changed

+52
-56
lines changed

3 files changed

+52
-56
lines changed

numpy/core/include/numpy/ndarraytypes.h

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1839,18 +1839,20 @@ typedef void (PyDataMem_EventHookFunc)(void *inp, void *outp, size_t size,
18391839
PyHeapTypeObject super;
18401840

18411841
/*
1842-
* TODO: Update above comment as necessary.
18431842
* Most DTypes will have a singleton default instance, for the
18441843
* parametric legacy DTypes (bytes, string, void, datetime) this
18451844
* may be a pointer to the *prototype* instance?
18461845
*/
18471846
PyArray_Descr *singleton;
18481847
/*
1849-
* Is this DType created using the old API?
1850-
* TODO: For now this mostly exists for possible assertions,
1851-
* we may not need it.
1848+
* Is this DType created using the old API? This exists mainly to
1849+
* allow for assertions in paths specific to wrapping legacy types.
18521850
*/
1853-
npy_bool is_legacy;
1851+
npy_bool legacy;
1852+
/* The values stored by a parametric datatype depend on its instance */
1853+
npy_bool parametric;
1854+
/* whether the DType can be instantiated (i.e. np.dtype cannot) */
1855+
npy_bool abstract;
18541856

18551857
/*
18561858
* The following fields replicate the most important dtype information.
@@ -1865,20 +1867,11 @@ typedef void (PyDataMem_EventHookFunc)(void *inp, void *outp, size_t size,
18651867
char type;
18661868
/* flags describing data type */
18671869
char flags;
1868-
npy_bool is_parametric;
1869-
/* whether the DType can be instantiated (i.e. np.dtype cannot) */
1870-
npy_bool is_abstract;
18711870
/* number representing this type */
18721871
int type_num;
1873-
/*
1874-
* itemsize of this type, -1 if variable. Use npy_intp, even though it
1875-
* is effectively limited to int due to other public API.
1876-
*/
1877-
npy_intp itemsize;
18781872
8000 /*
18791873
* Point to the original ArrFuncs.
1880-
* TODO: If we wanted to do, we could make a copy to detect changes.
1881-
* However, I doubt that is necessary.
1874+
* NOTE: We could make a copy to detect changes to `f`.
18821875
*/
18831876
PyArray_ArrFuncs *f;
18841877
} PyArray_DTypeMeta;

numpy/core/src/multiarray/descriptor.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3490,6 +3490,8 @@ NPY_NO_EXPORT PyArray_DTypeMeta PyArrayDescr_TypeFull = {
34903490
},},
34913491
.type_num = -1,
34923492
.kind = '\0',
3493-
.is_abstract = 1,
3494-
.is_parametric = 0,
3493+
.abstract = 1,
3494+
.parametric = 0,
3495+
.singleton = 0,
3496+
.scalar_type = NULL,
34953497
};

numpy/core/src/multiarray/dtypemeta.c

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,29 @@
1515

1616
static void
1717
dtypemeta_dealloc(PyArray_DTypeMeta *self) {
18-
/*
19-
* PyType_Type asserts Py_TPFLAGS_HEAPTYPE as well. Do not rely on
20-
* a python debug build though.
21-
*/
18+
/* Do not accidentally delete a statically defined DType: */
2219
assert(((PyTypeObject *)self)->tp_flags & Py_TPFLAGS_HEAPTYPE);
20+
2321
Py_XDECREF(self->scalar_type);
22+
Py_XDECREF(self->singleton);
2423
PyType_Type.tp_dealloc((PyObject *) self);
2524
}
2625

2726
static PyObject *
28-
dtypemeta_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
27+
dtypemeta_new(PyTypeObject *NPY_UNUSED(type),
28+
PyObject *NPY_UNUSED(args), PyObject *NPY_UNUSED(kwds))
2929
{
3030
PyErr_SetString(PyExc_TypeError,
3131
"Preliminary-API: Cannot subclass DType.");
3232
return NULL;
3333
}
3434

3535
static int
36-
dtypemeta_init(PyTypeObject *type, PyObject *args, PyObject *kwds)
36+
dtypemeta_init(PyTypeObject *NPY_UNUSED(type),
37+
PyObject *NPY_UNUSED(args), PyObject *NPY_UNUSED(kwds))
3738
{
3839
PyErr_SetString(PyExc_TypeError,
39-
"Preliminary-API: Cannot initialize DType class.");
40+
"Preliminary-API: Cannot __init__ DType class.");
4041
return -1;
4142
}
4243

@@ -46,8 +47,8 @@ dtypemeta_init(PyTypeObject *type, PyObject *args, PyObject *kwds)
4647
*
4748
* Python Type objects are either statically created (typical C-Extension type)
4849
* or HeapTypes (typically created in Python).
49-
* HeapTypes have the Py_TPFLAGS_HEAPTYPE flag, and are garbage collected, our
50-
* DTypeMeta instances (`np.dtype` and its subclasses) *may* be HeapTypes
50+
* HeapTypes have the Py_TPFLAGS_HEAPTYPE flag and are garbage collected.
51+
* Our DTypeMeta instances (`np.dtype` and its subclasses) *may* be HeapTypes
5152
* if the Py_TPFLAGS_HEAPTYPE flag is set (they are created from Python).
5253
* They are not for legacy DTypes or np.dtype itself.
5354
*
@@ -67,10 +68,12 @@ dtypemeta_traverse(PyArray_DTypeMeta *type, visitproc visit, void *arg)
6768
/*
6869
* We have to traverse the base class (if it is a HeapType).
6970
* PyType_Type will handle this logic for us.
70-
* This function is currently not used, but may be necessary in the future
71-
* when we implement HeapTypes (python/dynamically defined types).
71+
* This function is currently not used, but will probably be necessary
72+
* in the future when we implement HeapTypes (python/dynamically
73+
* defined types). It should be revised at that time.
7274
*/
73-
assert(!type->is_legacy && (PyTypeObject *)type != &PyArrayDescr_Type);
75+
assert(0);
76+
assert(!type->legacy && (PyTypeObject *)type != &PyArrayDescr_Type);
7477
Py_VISIT(type->singleton);
7578
Py_VISIT(type->scalar_type);
7679
return PyType_Type.tp_traverse((PyObject *)type, visit, arg);
@@ -82,7 +85,7 @@ legacy_dtype_default_new(PyArray_DTypeMeta *self,
8285
PyObject *args, PyObject *kwargs)
8386
{
8487
/* TODO: This should allow endianess and possibly metadata */
85-
if (self->is_parametric) {
88+
if (self->parametric) {
8689
/* reject parametric ones since we would need to get unit, etc. info */
8790
PyErr_Format(PyExc_TypeError,
8891
"Preliminary-API: Flexible/Parametric legacy DType '%S' can "
@@ -193,8 +196,8 @@ dtypemeta_wrap_legacy_descriptor(PyArray_Descr *descr)
193196
.tp_base = &PyArrayDescr_Type,
194197
.tp_new = (newfunc)legacy_dtype_default_new,
195198
},},
196-
.is_legacy = 1,
197-
.is_abstract = 0, /* this is a concrete DType */
199+
.legacy = 1,
200+
.abstract = 0, /* this is a concrete DType */
198201
/* Further fields are not common between DTypes */
199202
};
200203
memcpy(dtype_class, &prototype, sizeof(PyArray_DTypeMeta));
@@ -217,15 +220,13 @@ dtypemeta_wrap_legacy_descriptor(PyArray_Descr *descr)
217220
dtype_class->type = descr->type;
218221
dtype_class->f = descr->f;
219222
dtype_class->kind = descr->kind;
220-
dtype_class->itemsize = descr->elsize;
221223

222224
if (PyTypeNum_ISDATETIME(descr->type_num)) {
223225
/* Datetimes are flexible, but were not considered previously */
224-
dtype_class->is_parametric = NPY_TRUE;
226+
dtype_class->parametric = NPY_TRUE;
225227
}
226228
else if (PyTypeNum_ISFLEXIBLE(descr->type_num)) {
227-
dtype_class->is_parametric = NPY_TRUE;
228-
dtype_class->itemsize = -1; /* itemsize is not fixed */
229+
dtype_class->parametric = NPY_TRUE;
229230
}
230231

231232
/* Finally, replace the current class of the descr */
@@ -240,29 +241,29 @@ dtypemeta_wrap_legacy_descriptor(PyArray_Descr *descr)
240241
* preliminary (the flags should also return bools).
241242
*/
242243
static PyMemberDef dtypemeta_members[] = {
243-
{"_abstract",
244-
T_BYTE, offsetof(PyArray_DTypeMeta, is_abstract), READONLY, NULL},
245-
{"type",
246-
T_OBJECT, offsetof(PyArray_DTypeMeta, scalar_type), READONLY, NULL},
247-
{"_parametric",
248-
T_BYTE, offsetof(PyArray_DTypeMeta, is_parametric), READONLY, NULL},
249-
{NULL, 0, 0, 0, NULL},
244+
{"_abstract",
245+
T_BYTE, offsetof(PyArray_DTypeMeta, abstract), READONLY, NULL},
246+
{"type",
247+
T_OBJECT, offsetof(PyArray_DTypeMeta, scalar_type), READONLY, NULL},
248+
{"_parametric",
249+
T_BYTE, offsetof(PyArray_DTypeMeta, parametric), READONLY, NULL},
250+
{NULL, 0, 0, 0, NULL},
250251
};
251252

252253

253254
NPY_NO_EXPORT PyTypeObject PyArrayDTypeMeta_Type = {
254-
PyVarObject_HEAD_INIT(NULL, 0)
255-
.tp_name = "numpy._DTypeMeta",
256-
.tp_basicsize = sizeof(PyArray_DTypeMeta),
257-
.tp_dealloc = (destructor)dtypemeta_dealloc,
258-
/* Types are garbage collected (see dtypemeta_is_gc documentation) */
259-
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
260-
.tp_doc = "Preliminary NumPy API: The Type of NumPy DTypes (metaclass)",
261-
.tp_members = dtypemeta_members,
262-
.tp_base = NULL, /* set to PyType_Type at import time */
263-
.tp_init = (initproc)dtypemeta_init,
264-
.tp_new = dtypemeta_new,
265-
.tp_is_gc = dtypemeta_is_gc,
266-
.tp_traverse = (traverseproc)dtypemeta_traverse,
255+
PyVarObject_HEAD_INIT(NULL, 0)
256+
.tp_name = "numpy._DTypeMeta",
257+
.tp_basicsize = sizeof(PyArray_DTypeMeta),
258+
.tp_dealloc = (destructor)dtypemeta_dealloc,
259+
/* Types are garbage collected (see dtypemeta_is_gc documentation) */
260+
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
261+
.tp_doc = "Preliminary NumPy API: The Type of NumPy DTypes (metaclass)",
262+
.tp_members = dtypemeta_members,
263+
.tp_base = NULL, /* set to PyType_Type at import time */
264+
.tp_init = (initproc)dtypemeta_init,
265+
.tp_new = dtypemeta_new,
266+
.tp_is_gc = dtypemeta_is_gc,
267+
.tp_traverse = (traverseproc)dtypemeta_traverse,
267268
};
268269

0 commit comments

Comments
 (0)
0