-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
API: Create Preliminary DTypeMeta class and np.dtype subclasses #15508
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
1809a0f
4a080dc
5ced904
63ed08c
c3c0786
e02cae8
6d951be
7c36544
e6fde0e
2ea745b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -147,13 +147,27 @@ dtypemeta_wrap_legacy_descriptor(PyArray_Descr *descr) | |
* In particular our own DTypes can be true static declarations. | ||
* However, this function remains necessary for legacy user dtypes. | ||
*/ | ||
char *tp_name = PyDataMem_NEW(100); | ||
|
||
const char *scalar_name = descr->typeobj->tp_name; | ||
/* | ||
* We have to take only the name, and ignore the module to get | ||
* a reasonable __name__, since static types are limited in this regard | ||
* (this is not ideal, but not a big issue in practice). | ||
* This is what Python does to print __name__ for static types. | ||
*/ | ||
const char *dot = strrchr(scalar_name, '.'); | ||
if (dot) { | ||
scalar_name = dot + 1; | ||
} | ||
ssize_t name_length = strlen(scalar_name) + 14; | ||
|
||
char *tp_name = malloc(name_length); | ||
if (tp_name == NULL) { | ||
PyErr_NoMemory(); | ||
return -1; | ||
} | ||
snprintf(tp_name, 100, "numpy.dtype[%s]", | ||
descr->typeobj->tp_name); | ||
|
||
snprintf(tp_name, name_length, "numpy.dtype[%s]", scalar_name); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Only downside i see is that from a python user perspective this is not a valid class name, and trying to create an object (in the obvious way) will fail. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True... I suppose the metaclass could probably have a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we do something like numpy.dtype_%s ? Although this may be a bit ugly and little less self-explanatory of it being a subclass of np.dtype. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could do various things I guess, but it will be a while before we should seriously consider exposing these in the API, and I think until then, it will be fine to modify the |
||
|
||
PyArray_DTypeMeta *dtype_class = malloc(sizeof(PyArray_DTypeMeta)); | ||
if (dtype_class == NULL) { | ||
|
@@ -176,7 +190,7 @@ dtypemeta_wrap_legacy_descriptor(PyArray_Descr *descr) | |
.tp_basicsize = sizeof(PyArray_Descr), | ||
.tp_flags = Py_TPFLAGS_DEFAULT, | ||
.tp_base = &PyArrayDescr_Type, | ||
.tp_new = (newfunc)legacy_dtype_default_new | ||
.tp_new = (newfunc)legacy_dtype_default_new, | ||
},}, | ||
.is_legacy = 1, | ||
.is_abstract = 0, /* this is a concrete DType */ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: a comment for this magic number would be great