diff options
author | Eric Wieser <wieser.eric@gmail.com> | 2017-08-06 08:47:35 -0500 |
---|---|---|
committer | Eric Wieser <wieser.eric@gmail.com> | 2017-08-06 08:47:35 -0500 |
commit | 2489aded2dc7f779a66b5f93ea59ba4731c46b86 (patch) | |
tree | e26d04840a785faa1d76b506c766757e35ec37cf | |
parent | dfba08642ec76cee99e7d78e20865e2e63f89609 (diff) | |
download | numpy-2489aded2dc7f779a66b5f93ea59ba4731c46b86.tar.gz |
BUG: Don't duplicate the type->dtype conversion logic
dtype(t) already does this in descriptor.c
This means that `obj2sctype(long)` now returns `np.int64` rather than `None`,
because when you duplicate code, its easy to forget something in the second
place.
-rw-r--r-- | numpy/core/numerictypes.py | 38 |
1 files changed, 7 insertions, 31 deletions
diff --git a/numpy/core/numerictypes.py b/numpy/core/numerictypes.py index 275bb3fea..620b67cf6 100644 --- a/numpy/core/numerictypes.py +++ b/numpy/core/numerictypes.py @@ -528,27 +528,6 @@ def maximum_sctype(t): else: return sctypes[base][-1] -try: - buffer_type = _types.BufferType -except AttributeError: - # Py3K - buffer_type = memoryview - -_python_types = {int: 'int_', - float: 'float_', - complex: 'complex_', - bool: 'bool_', - bytes: 'bytes_', - unicode: 'unicode_', - buffer_type: 'void', - } - -def _python_type(t): - """ Get a numpy scalar type corresponding to a Python type or value """ - if not isinstance(t, type): - t = type(t) - return allTypes[_python_types.get(t, 'object_')] - def issctype(rep): """ @@ -633,22 +612,19 @@ def obj2sctype(rep, default=None): <type 'list'> """ - try: - if issubclass(rep, generic): - return rep - except TypeError: - pass - if isinstance(rep, dtype): - return rep.type - if isinstance(rep, type): - return _python_type(rep) + # prevent abtract classes being upcast + if isinstance(rep, type) and issubclass(rep, generic): + return rep + # extract dtype from arrays if isinstance(rep, ndarray): return rep.dtype.type + # fall back on dtype to convert try: res = dtype(rep) except Exception: return default - return res.type + else: + return res.type def issubclass_(arg1, arg2): |