8000 MAINT: Move `np.dtype.name.__get__` to python by eric-wieser · Pull Request #11932 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

MAINT: Move np.dtype.name.__get__ to python #11932

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

Merged
merged 1 commit into from
Sep 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions numpy/core/_dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,14 @@ def _byte_order_str(dtype):


def _datetime_metadata_str(dtype):
# This is a hack since the data is not exposed to python in any other way
return dtype.name[dtype.name.rfind('['):]
# TODO: this duplicates the C append_metastr_to_string
unit, count = np.datetime_data(dtype)
if unit == 'generic':
return ''
elif count == 1:
return '[{}]'.format(unit)
else:
return '[{}{}]'.format(count, unit)


def _struct_dict_str(dtype, includealignedflag):
Expand Down Expand Up @@ -292,3 +298,26 @@ def _struct_repr(dtype):
return s


def _name_get(dtype):
# provides dtype.name.__get__

if dtype.isbuiltin == 2:
# user dtypes don't promise to do anything special
return dtype.type.__name__

# Builtin classes are documented as returning a "bit name"
name = dtype.type.__name__
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line blocks #10151, but so did the equivalent C code


# handle bool_, str_, etc
if name[-1] == '_':
name = name[:-1]

# append bit counts to str, unicode, and void
if np.issubdtype(dtype, np.flexible) and not _isunsized(dtype):
name += "{}".format(dtype.itemsize * 8)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@charris: As predicted, the assumption that a byte is 8 bits is pretty ingrained into our dtype string handling


# append metadata to datetimes
elif dtype.type in (np.datetime64, np.timedelta64):
name += _datetime_metadata_str(dtype)

return name
73 changes: 9 additions & 64 deletions numpy/core/src/multiarray/descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -1859,72 +1859,17 @@ arraydescr_protocol_typestr_get(PyArray_Descr *self)
}

static PyObject *
arraydescr_typename_get(PyArray_Descr *self)
arraydescr_name_get(PyArray_Descr *self)
{
static const char np_prefix[] = "numpy.";
const int np_prefix_len = sizeof(np_prefix) - 1;
PyTypeObject *typeobj = self->typeobj;
/* let python handle this */
PyObject *_numpy_dtype;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be static PyObject *_numpy_dtype = NULL; It needs to stay around and needs to be initialized.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it does need to stay around, and it's initialized at its first use below.

This matches dtype.__repr__, in that both do not bother to cache the module or the function

PyObject *res;
char *s;
int len;
int prefix_len;
int suffix_len;

if (PyTypeNum_ISUSERDEF(self->type_num)) {
s = strrchr(typeobj->tp_name, '.');
if (s == NULL) {
res = PyUString_FromString(typeobj->tp_name);
}
else {
res = PyUString_FromStringAndSize(s + 1, strlen(s) - 1);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This entire block is just typeobj.__name__

}
return res;
}
else {
/*
* NumPy type or subclass
*
* res is derived from typeobj->tp_name with the following rules:
* - if starts with "numpy.", that prefix is removed
* - if ends with "_", that suffix is removed
*/
len = strlen(typeobj->tp_name);

if (! strncmp(typeobj->tp_name, np_prefix, np_prefix_len)) {
prefix_len = np_prefix_len;
}
else {
prefix_len = 0;
}

if (typeobj->tp_name[len - 1] == '_') {
suffix_len = 1;
}
else {
suffix_len = 0;
}

len -= prefix_len;
len -= suffix_len;
res = PyUString_FromStringAndSize(typeobj->tp_name+prefix_len, len);
}
if (PyTypeNum_ISFLEXIBLE(self->type_num) && !PyDataType_ISUNSIZED(self)) {
PyObject *p;
p = PyUString_FromFormat("%d", self->elsize * 8);
PyUString_ConcatAndDel(&res, p);
}
if (PyDataType_ISDATETIME(self)) {
PyArray_DatetimeMetaData *meta;

meta = get_datetime_metadata_from_dtype(self);
if (meta == NULL) {
Py_DECREF(res);
return NULL;
}

res = append_metastr_to_string(meta, 0, res);
_numpy_dtype = PyImport_ImportModule("numpy.core._dtype");
if (_numpy_dtype == NULL) {
return NULL;
}

res = PyObject_CallMethod(_numpy_dtype, "_name_get", "O", self);
Py_DECREF(_numpy_dtype);
return res;
}

Expand Down 57B2 Expand Up @@ -2218,7 +2163,7 @@ static PyGetSetDef arraydescr_getsets[] = {
(getter)arraydescr_protocol_typestr_get,
NULL, NULL, NULL},
{"name",
(getter)arraydescr_typename_get,
(getter)arraydescr_name_get,
NULL, NULL, NULL},
{"base",
(getter)arraydescr_base_get,
Expand Down
0