Author: Ronan Lamy <[email protected]>
Branch: can_cast
Changeset: r77183:4f3c9f03bae2
Date: 2015-05-07 19:15 +0100
http://bitbucket.org/pypy/pypy/changeset/4f3c9f03bae2/
Log: Add np.min_scalar_type()
diff --git a/pypy/module/micronumpy/__init__.py
b/pypy/module/micronumpy/__init__.py
--- a/pypy/module/micronumpy/__init__.py
+++ b/pypy/module/micronumpy/__init__.py
@@ -23,6 +23,7 @@
'where': 'arrayops.where',
'result_type': 'casting.result_type',
'can_cast': 'casting.can_cast',
+ 'min_scalar_type': 'casting.min_scalar_type',
'set_string_function': 'appbridge.set_string_function',
'typeinfo': 'descriptor.get_dtype_cache(space).w_typeinfo',
diff --git a/pypy/module/micronumpy/casting.py
b/pypy/module/micronumpy/casting.py
--- a/pypy/module/micronumpy/casting.py
+++ b/pypy/module/micronumpy/casting.py
@@ -4,11 +4,10 @@
from pypy.interpreter.gateway import unwrap_spec
from pypy.interpreter.error import oefmt
-from pypy.module.micronumpy.base import W_NDimArray
+from pypy.module.micronumpy.base import W_NDimArray, convert_to_array
from pypy.module.micronumpy import constants as NPY
from pypy.module.micronumpy.ufuncs import (
find_binop_result_dtype, find_dtype_for_scalar)
-from .boxes import W_GenericBox
from .types import (
Bool, ULong, Long, Float64, Complex64, UnicodeType, VoidType, ObjectType)
from .descriptor import get_dtype_cache, as_dtype, is_scalar_w
@@ -98,3 +97,12 @@
def as_scalar(space, w_obj):
dtype = find_dtype_for_scalar(space, w_obj)
return dtype.coerce(space, w_obj)
+
+def min_scalar_type(space, w_a):
+ w_array = convert_to_array(space, w_a)
+ dtype = w_array.get_dtype()
+ if w_array.is_scalar() and dtype.is_number():
+ num, alt_num = w_array.get_scalar_value().min_dtype()
+ return get_dtype_cache(space).dtypes_by_num[num]
+ else:
+ return dtype
diff --git a/pypy/module/micronumpy/test/test_casting.py
b/pypy/module/micronumpy/test/test_casting.py
--- a/pypy/module/micronumpy/test/test_casting.py
+++ b/pypy/module/micronumpy/test/test_casting.py
@@ -112,3 +112,10 @@
assert not np.can_cast(1 + 1e50j, np.complex64)
assert np.can_cast(1., np.complex64)
assert not np.can_cast(1e50, np.complex64)
+
+ def test_min_scalar_type(self):
+ import numpy as np
+ assert np.min_scalar_type(2**8 - 1) == np.dtype('uint8')
+ assert np.min_scalar_type(2**64 - 1) == np.dtype('uint64')
+ # XXX: np.asarray(2**64) fails with OverflowError
+ # assert np.min_scalar_type(2**64) == np.dtype('O')
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit