[pypy-commit] pypy fix-result-types: Add num2dtype() helper

rlamy noreply at buildbot.pypy.org
Thu May 21 21:01:03 CEST 2015


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: fix-result-types
Changeset: r77457:6756ec45542a
Date: 2015-05-21 17:13 +0100
http://bitbucket.org/pypy/pypy/changeset/6756ec45542a/

Log:	Add num2dtype() helper

diff --git a/pypy/module/micronumpy/boxes.py b/pypy/module/micronumpy/boxes.py
--- a/pypy/module/micronumpy/boxes.py
+++ b/pypy/module/micronumpy/boxes.py
@@ -35,8 +35,8 @@
 def new_dtype_getter(num):
     @specialize.memo()
     def _get_dtype(space):
-        from pypy.module.micronumpy.descriptor import get_dtype_cache
-        return get_dtype_cache(space).dtypes_by_num[num]
+        from pypy.module.micronumpy.descriptor import num2dtype
+        return num2dtype(space, num)
 
     def descr__new__(space, w_subtype, w_value=None):
         from pypy.module.micronumpy.ctors import array
@@ -144,7 +144,7 @@
         return self
 
     def get_flags(self):
-        return (NPY.ARRAY_C_CONTIGUOUS | NPY.ARRAY_F_CONTIGUOUS | 
+        return (NPY.ARRAY_C_CONTIGUOUS | NPY.ARRAY_F_CONTIGUOUS |
                 NPY.ARRAY_WRITEABLE | NPY.ARRAY_OWNDATA)
 
     def item(self, space):
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
@@ -12,7 +12,7 @@
     promotion_table)
 from .descriptor import (
     get_dtype_cache, as_dtype, is_scalar_w, variable_dtype, new_string_dtype,
-    new_unicode_dtype)
+    new_unicode_dtype, num2dtype)
 
 @jit.unroll_safe
 def result_type(space, __args__):
@@ -143,7 +143,7 @@
     dtypenum, altnum = value.min_dtype()
     if target.is_unsigned():
         dtypenum = altnum
-    dtype = get_dtype_cache(space).dtypes_by_num[dtypenum]
+    dtype = num2dtype(space, dtypenum)
     return can_cast_type(space, dtype, target, casting)
 
 def as_scalar(space, w_obj):
@@ -155,7 +155,7 @@
     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]
+        return num2dtype(space, num)
     else:
         return dtype
 
@@ -174,7 +174,7 @@
 def _promote_types(space, dt1, dt2):
     num = promotion_table[dt1.num][dt2.num]
     if num != -1:
-        return get_dtype_cache(space).dtypes_by_num[num]
+        return num2dtype(space, num)
 
     # dt1.num should be <= dt2.num
     if dt1.num > dt2.num:
diff --git a/pypy/module/micronumpy/descriptor.py b/pypy/module/micronumpy/descriptor.py
--- a/pypy/module/micronumpy/descriptor.py
+++ b/pypy/module/micronumpy/descriptor.py
@@ -1003,6 +1003,9 @@
 def get_dtype_cache(space):
     return space.fromcache(DtypeCache)
 
+def num2dtype(space, num):
+    return get_dtype_cache(space).dtypes_by_num[num]
+
 def as_dtype(space, w_arg, allow_None=True):
     from pypy.module.micronumpy.casting import find_dtype_for_scalar
     # roughly equivalent to CNumPy's PyArray_DescrConverter2
diff --git a/pypy/module/micronumpy/ufuncs.py b/pypy/module/micronumpy/ufuncs.py
--- a/pypy/module/micronumpy/ufuncs.py
+++ b/pypy/module/micronumpy/ufuncs.py
@@ -12,7 +12,8 @@
 from rpython.rlib.objectmodel import keepalive_until_here
 
 from pypy.module.micronumpy import loop, constants as NPY
-from pypy.module.micronumpy.descriptor import get_dtype_cache, decode_w_dtype
+from pypy.module.micronumpy.descriptor import (
+    get_dtype_cache, decode_w_dtype, num2dtype)
 from pypy.module.micronumpy.base import convert_to_array, W_NDimArray
 from pypy.module.micronumpy.ctors import numpify
 from pypy.module.micronumpy.nditer import W_NDIter, coalesce_iter
@@ -269,7 +270,7 @@
                         num = NPY.ULONG
                     else:
                         num = NPY.LONG
-            dtype = get_dtype_cache(space).dtypes_by_num[num]
+            dtype = num2dtype(space, num)
 
         if self.identity is None:
             for i in range(shapelen):


More information about the pypy-commit mailing list