[pypy-commit] pypy default: clean up use of numpy constants and helper functions

bdkearns noreply at buildbot.pypy.org
Tue Oct 29 20:33:02 CET 2013


Author: Brian Kearns <bdkearns at gmail.com>
Branch: 
Changeset: r67691:72b4a905d844
Date: 2013-10-29 11:57 -0400
http://bitbucket.org/pypy/pypy/changeset/72b4a905d844/

Log:	clean up use of numpy constants and helper functions

diff --git a/pypy/module/micronumpy/constants.py b/pypy/module/micronumpy/constants.py
--- a/pypy/module/micronumpy/constants.py
+++ b/pypy/module/micronumpy/constants.py
@@ -1,21 +1,83 @@
-from pypy.interpreter.error import OperationError
+NPY_BOOL = 0
+NPY_BYTE = 1
+NPY_UBYTE = 2
+NPY_SHORT = 3
+NPY_USHORT = 4
+NPY_INT = 5
+NPY_UINT = 6
+NPY_LONG = 7
+NPY_ULONG = 8
+NPY_LONGLONG = 9
+NPY_ULONGLONG = 10
+NPY_FLOAT = 11
+NPY_DOUBLE = 12
+NPY_LONGDOUBLE = 13
+NPY_CFLOAT = 14
+NPY_CDOUBLE = 15
+NPY_CLONGDOUBLE = 16
+NPY_OBJECT = 17
+NPY_STRING = 18
+NPY_UNICODE = 19
+NPY_VOID = 20
+NPY_DATETIME = 21
+NPY_TIMEDELTA = 22
+NPY_HALF = 23
+NPY_NTYPES = 24
+NPY_NOTYPE = 25
+NPY_CHAR = 26
+NPY_USERDEF = 256
 
-MODE_CLIP, MODE_WRAP, MODE_RAISE = range(3)
+NPY_BOOLLTR = '?'
+NPY_BYTELTR = 'b'
+NPY_UBYTELTR = 'B'
+NPY_SHORTLTR = 'h'
+NPY_USHORTLTR = 'H'
+NPY_INTLTR = 'i'
+NPY_UINTLTR = 'I'
+NPY_LONGLTR = 'l'
+NPY_ULONGLTR = 'L'
+NPY_LONGLONGLTR = 'q'
+NPY_ULONGLONGLTR = 'Q'
+NPY_HALFLTR = 'e'
+NPY_FLOATLTR = 'f'
+NPY_DOUBLELTR = 'd'
+NPY_LONGDOUBLELTR = 'g'
+NPY_CFLOATLTR = 'F'
+NPY_CDOUBLELTR = 'D'
+NPY_CLONGDOUBLELTR = 'G'
+NPY_OBJECTLTR = 'O'
+NPY_STRINGLTR = 'S'
+NPY_STRINGLTR2 = 'a'
+NPY_UNICODELTR = 'U'
+NPY_VOIDLTR = 'V'
+NPY_DATETIMELTR = 'M'
+NPY_TIMEDELTALTR = 'm'
+NPY_CHARLTR = 'c'
 
-def clipmode_converter(space, w_mode):
-    if space.is_none(w_mode):
-        return MODE_RAISE
-    if space.isinstance_w(w_mode, space.w_str):
-        mode = space.str_w(w_mode)
-        if mode.startswith('C') or mode.startswith('c'):
-            return MODE_CLIP
-        if mode.startswith('W') or mode.startswith('w'):
-            return MODE_WRAP
-        if mode.startswith('R') or mode.startswith('r'):
-            return MODE_RAISE
-    elif space.isinstance_w(w_mode, space.w_int):
-        mode = space.int_w(w_mode)
-        if MODE_CLIP <= mode <= MODE_RAISE:
-            return mode
-    raise OperationError(space.w_TypeError,
-                         space.wrap("clipmode not understood"))
+NPY_INTPLTR = 'p'
+NPY_UINTPLTR = 'P'
+
+NPY_GENBOOLLTR ='b'
+NPY_SIGNEDLTR = 'i'
+NPY_UNSIGNEDLTR = 'u'
+NPY_FLOATINGLTR = 'f'
+NPY_COMPLEXLTR = 'c'
+
+NPY_CLIP = 0
+NPY_WRAP = 1
+NPY_RAISE = 2
+
+NPY_LITTLE = '<'
+NPY_BIG = '>'
+NPY_NATIVE = '='
+NPY_SWAP = 's'
+NPY_IGNORE = '|'
+
+import sys
+if sys.byteorder == 'big':
+    NPY_NATBYTE = NPY_BIG
+    NPY_OPPBYTE = NPY_LITTLE
+else:
+    NPY_NATBYTE = NPY_LITTLE
+    NPY_OPPBYTE = NPY_BIG
+del sys
diff --git a/pypy/module/micronumpy/interp_arrayops.py b/pypy/module/micronumpy/interp_arrayops.py
--- a/pypy/module/micronumpy/interp_arrayops.py
+++ b/pypy/module/micronumpy/interp_arrayops.py
@@ -1,11 +1,12 @@
 from pypy.module.micronumpy.base import convert_to_array, W_NDimArray
-from pypy.module.micronumpy import loop, interp_dtype, interp_ufuncs, constants
+from pypy.module.micronumpy import loop, interp_dtype, interp_ufuncs
 from pypy.module.micronumpy.iter import Chunk, Chunks
 from pypy.module.micronumpy.strides import shape_agreement,\
      shape_agreement_multiple
-from pypy.module.micronumpy.constants import clipmode_converter
 from pypy.interpreter.error import OperationError, operationerrfmt
 from pypy.interpreter.gateway import unwrap_spec
+from pypy.module.micronumpy.conversion_utils import clipmode_converter
+from pypy.module.micronumpy.constants import *
 
 def where(space, w_arr, w_x=None, w_y=None):
     """where(condition, [x, y])
@@ -219,17 +220,18 @@
         index = int_w(space, idx)
 
         if index < 0 or index >= arr.get_size():
-            if mode == constants.MODE_RAISE:
+            if mode == NPY_RAISE:
                 raise OperationError(space.w_IndexError, space.wrap(
                     "index %d is out of bounds for axis 0 with size %d" % (index, arr.get_size())))
-            elif mode == constants.MODE_WRAP:
+            elif mode == NPY_WRAP:
                 index = index % arr.get_size()
-            else:
-                assert mode == constants.MODE_CLIP
+            elif mode == NPY_CLIP:
                 if index < 0:
                     index = 0
                 else:
                     index = arr.get_size() - 1
+            else:
+                assert False
 
         value = values[v_idx]
 
diff --git a/pypy/module/micronumpy/interp_dtype.py b/pypy/module/micronumpy/interp_dtype.py
--- a/pypy/module/micronumpy/interp_dtype.py
+++ b/pypy/module/micronumpy/interp_dtype.py
@@ -9,24 +9,8 @@
 from rpython.rlib.rarithmetic import LONG_BIT, r_longlong, r_ulonglong
 from rpython.rtyper.lltypesystem import rffi
 from rpython.rlib import jit
+from pypy.module.micronumpy.constants import *
 
-if sys.byteorder == 'little':
-    NATBYTE = '<'
-    OPPBYTE = '>'
-else:
-    NATBYTE = '>'
-    OPPBYTE = '<'
-
-UNSIGNEDLTR = "u"
-SIGNEDLTR = "i"
-BOOLLTR = "b"
-FLOATINGLTR = "f"
-COMPLEXLTR = "c"
-VOIDLTR = 'V'
-STRINGLTR = 'S'
-UNICODELTR = 'U'
-INTPLTR = 'p'
-UINTPLTR = 'P'
 
 def decode_w_dtype(space, w_dtype):
     if space.is_none(w_dtype):
@@ -52,7 +36,7 @@
 class W_Dtype(W_Root):
     _immutable_fields_ = ["itemtype?", "num", "kind", "name?", "char", "w_box_type", "byteorder", "float_type"]
 
-    def __init__(self, itemtype, num, kind, name, char, w_box_type, byteorder='=',
+    def __init__(self, itemtype, num, kind, name, char, w_box_type, byteorder=NPY_NATIVE,
                  alternate_constructors=[], aliases=[], float_type=None,
                  fields=None, fieldnames=None, shape=[], subdtype=None):
         self.itemtype = itemtype
@@ -107,35 +91,35 @@
         self.itemtype.fill(storage, self.get_size(), box, start, stop, 0)
 
     def is_int_type(self):
-        return (self.kind == SIGNEDLTR or self.kind == UNSIGNEDLTR or
-                self.kind == BOOLLTR)
+        return (self.kind == NPY_SIGNEDLTR or self.kind == NPY_UNSIGNEDLTR or
+                self.kind == NPY_GENBOOLLTR)
 
     def is_signed(self):
-        return self.kind == SIGNEDLTR
+        return self.kind == NPY_SIGNEDLTR
 
     def is_complex_type(self):
-        return self.kind == COMPLEXLTR
+        return self.kind == NPY_COMPLEXLTR
 
     def is_float_type(self):
-        return (self.kind == FLOATINGLTR or self.float_type is not None)
+        return (self.kind == NPY_FLOATINGLTR or self.float_type is not None)
 
     def is_bool_type(self):
-        return self.kind == BOOLLTR
+        return self.kind == NPY_GENBOOLLTR
 
     def is_record_type(self):
         return self.fields is not None
 
     def is_str_type(self):
-        return self.num == 18
+        return self.num == NPY_STRING
 
     def is_str_or_unicode(self):
-        return (self.num == 18 or self.num == 19)
+        return (self.num == NPY_STRING or self.num == NPY_UNICODE)
 
     def is_flexible_type(self):
         return (self.is_str_or_unicode() or self.is_record_type())
 
     def is_native(self):
-        return self.byteorder in ('=', NATBYTE)
+        return self.byteorder in (NPY_NATIVE, NPY_NATBYTE)
 
     def get_size(self):
         return self.itemtype.get_element_size()
@@ -163,15 +147,15 @@
     def descr_get_str(self, space):
         size = self.get_size()
         basic = self.kind
-        if basic == UNICODELTR:
+        if basic == NPY_UNICODELTR:
             size >>= 2
-            endian = NATBYTE
+            endian = NPY_NATBYTE
         elif size <= 1:
-            endian = '|'  # ignore
+            endian = NPY_IGNORE
         else:
             endian = self.byteorder
-            if endian == '=':
-                endian = NATBYTE
+            if endian == NPY_NATIVE:
+                endian = NPY_NATBYTE
         return space.wrap("%s%s%s" % (endian, basic, size))
 
     def descr_get_base(self, space):
@@ -268,7 +252,7 @@
         names = self.descr_get_names(space)
         values = self.descr_get_fields(space)
         if self.fields:
-            endian = '|'
+            endian = NPY_IGNORE
             #TODO: Implement this when subarrays are implemented
             subdescr = space.w_None
             size = 0
@@ -281,8 +265,8 @@
             alignment = space.wrap(1)
         else:
             endian = self.byteorder
-            if endian == '=':
-                endian = NATBYTE
+            if endian == NPY_NATIVE:
+                endian = NPY_NATBYTE
             subdescr = space.w_None
             w_size = space.wrap(-1)
             alignment = space.wrap(-1)
@@ -296,8 +280,8 @@
             raise OperationError(space.w_NotImplementedError, space.wrap("Pickling protocol version not supported"))
 
         endian = space.str_w(space.getitem(w_data, space.wrap(1)))
-        if endian == NATBYTE:
-            endian = '='
+        if endian == NPY_NATBYTE:
+            endian = NPY_NATIVE
         self.byteorder = endian
 
         fieldnames = space.getitem(w_data, space.wrap(3))
@@ -331,8 +315,8 @@
         offset += subdtype.itemtype.get_element_size() * size
         fieldnames.append(fldname)
     itemtype = types.RecordType(ofs_and_items, offset)
-    return W_Dtype(itemtype, 20, VOIDLTR, "void" + str(8 * itemtype.get_element_size()),
-                   "V", space.gettypefor(interp_boxes.W_VoidBox), fields=fields,
+    return W_Dtype(itemtype, NPY_VOID, NPY_VOIDLTR, "void" + str(8 * itemtype.get_element_size()),
+                   NPY_VOIDLTR, space.gettypefor(interp_boxes.W_VoidBox), fields=fields,
                    fieldnames=fieldnames)
 
 def dtype_from_dict(space, w_dict):
@@ -358,8 +342,8 @@
             dim = space.int_w(w_dim)
             shape.append(dim)
             size *= dim
-        return W_Dtype(types.VoidType(subdtype.itemtype.get_element_size() * size), 20, VOIDLTR, "void" + str(8 * subdtype.itemtype.get_element_size() * size),
-                    "V", space.gettypefor(interp_boxes.W_VoidBox), shape=shape, subdtype=subdtype)
+        return W_Dtype(types.VoidType(subdtype.itemtype.get_element_size() * size), NPY_VOID, NPY_VOIDLTR, "void" + str(8 * subdtype.itemtype.get_element_size() * size),
+                    NPY_VOIDLTR, space.gettypefor(interp_boxes.W_VoidBox), shape=shape, subdtype=subdtype)
 
     if space.is_none(w_dtype):
         return cache.w_float64dtype
@@ -435,26 +419,28 @@
             size = int(name[1:])
         except ValueError:
             raise OperationError(space.w_TypeError, space.wrap("data type not understood"))
-    if char == 'c':
-        char = 'S'
+    if char == NPY_CHARLTR:
+        char = NPY_STRINGLTR
         size = 1
-    if char == 'S':
+
+    if char == NPY_STRINGLTR:
         itemtype = types.StringType(size)
         basename = 'string'
-        num = 18
+        num = NPY_STRING
         w_box_type = space.gettypefor(interp_boxes.W_StringBox)
-    elif char == 'V':
-        num = 20
+    elif char == NPY_VOIDLTR:
+        itemtype = types.VoidType(size)
         basename = 'void'
-        itemtype = types.VoidType(size)
-        return W_Dtype(itemtype, 20, VOIDLTR, "void" + str(size),
-                    "V", space.gettypefor(interp_boxes.W_VoidBox))
+        num = NPY_VOID
+        w_box_type = space.gettypefor(interp_boxes.W_VoidBox)
+    elif char == NPY_UNICODELTR:
+        itemtype = types.UnicodeType(size)
+        basename = 'unicode'
+        num = NPY_UNICODE
+        w_box_type = space.gettypefor(interp_boxes.W_UnicodeBox)
     else:
-        assert char == 'U'
-        basename = 'unicode'
-        itemtype = types.UnicodeType(size)
-        num = 19
-        w_box_type = space.gettypefor(interp_boxes.W_UnicodeBox)
+        assert False
+
     return W_Dtype(itemtype, num, char,
                    basename + str(8 * itemtype.get_element_size()),
                    char, w_box_type)
@@ -463,10 +449,10 @@
     itemtype = types.StringType(size)
     return W_Dtype(
         itemtype,
-        num=18,
-        kind=STRINGLTR,
+        num=NPY_STRING,
+        kind=NPY_STRINGLTR,
         name='string' + str(8 * itemtype.get_element_size()),
-        char='S',
+        char=NPY_STRINGLTR,
         w_box_type = space.gettypefor(interp_boxes.W_StringBox),
     )
 
@@ -474,10 +460,10 @@
     itemtype = types.UnicodeType(size)
     return W_Dtype(
         itemtype,
-        num=19,
-        kind=UNICODELTR,
+        num=NPY_UNICODE,
+        kind=NPY_UNICODELTR,
         name='unicode' + str(8 * itemtype.get_element_size()),
-        char='U',
+        char=NPY_UNICODELTR,
         w_box_type = space.gettypefor(interp_boxes.W_UnicodeBox),
     )
 
@@ -486,67 +472,67 @@
     def __init__(self, space):
         self.w_booldtype = W_Dtype(
             types.Bool(),
-            num=0,
-            kind=BOOLLTR,
+            num=NPY_BOOL,
+            kind=NPY_GENBOOLLTR,
             name="bool",
-            char="?",
+            char=NPY_BOOLLTR,
             w_box_type=space.gettypefor(interp_boxes.W_BoolBox),
             alternate_constructors=[space.w_bool],
         )
         self.w_int8dtype = W_Dtype(
             types.Int8(),
-            num=1,
-            kind=SIGNEDLTR,
+            num=NPY_BYTE,
+            kind=NPY_SIGNEDLTR,
             name="int8",
-            char="b",
+            char=NPY_BYTELTR,
             w_box_type=space.gettypefor(interp_boxes.W_Int8Box)
         )
         self.w_uint8dtype = W_Dtype(
             types.UInt8(),
-            num=2,
-            kind=UNSIGNEDLTR,
+            num=NPY_UBYTE,
+            kind=NPY_UNSIGNEDLTR,
             name="uint8",
-            char="B",
+            char=NPY_UBYTELTR,
             w_box_type=space.gettypefor(interp_boxes.W_UInt8Box),
         )
         self.w_int16dtype = W_Dtype(
             types.Int16(),
-            num=3,
-            kind=SIGNEDLTR,
+            num=NPY_SHORT,
+            kind=NPY_SIGNEDLTR,
             name="int16",
-            char="h",
+            char=NPY_SHORTLTR,
             w_box_type=space.gettypefor(interp_boxes.W_Int16Box),
         )
         self.w_uint16dtype = W_Dtype(
             types.UInt16(),
-            num=4,
-            kind=UNSIGNEDLTR,
+            num=NPY_USHORT,
+            kind=NPY_UNSIGNEDLTR,
             name="uint16",
-            char="H",
+            char=NPY_USHORTLTR,
             w_box_type=space.gettypefor(interp_boxes.W_UInt16Box),
         )
         self.w_int32dtype = W_Dtype(
             types.Int32(),
-            num=5,
-            kind=SIGNEDLTR,
+            num=NPY_INT,
+            kind=NPY_SIGNEDLTR,
             name="int32",
-            char="i",
+            char=NPY_INTLTR,
             w_box_type=space.gettypefor(interp_boxes.W_Int32Box),
        )
         self.w_uint32dtype = W_Dtype(
             types.UInt32(),
-            num=6,
-            kind=UNSIGNEDLTR,
+            num=NPY_UINT,
+            kind=NPY_UNSIGNEDLTR,
             name="uint32",
-            char="I",
+            char=NPY_UINTLTR,
             w_box_type=space.gettypefor(interp_boxes.W_UInt32Box),
         )
         self.w_longdtype = W_Dtype(
             types.Long(),
-            num=7,
-            kind=SIGNEDLTR,
+            num=NPY_LONG,
+            kind=NPY_SIGNEDLTR,
             name="int%d" % LONG_BIT,
-            char="l",
+            char=NPY_LONGLTR,
             w_box_type=space.gettypefor(interp_boxes.W_LongBox),
             alternate_constructors=[space.w_int,
                                     space.gettypefor(interp_boxes.W_IntegerBox),
@@ -556,10 +542,10 @@
         )
         self.w_ulongdtype = W_Dtype(
             types.ULong(),
-            num=8,
-            kind=UNSIGNEDLTR,
+            num=NPY_ULONG,
+            kind=NPY_UNSIGNEDLTR,
             name="uint%d" % LONG_BIT,
-            char="L",
+            char=NPY_ULONGLTR,
             w_box_type=space.gettypefor(interp_boxes.W_ULongBox),
             alternate_constructors=[ space.gettypefor(interp_boxes.W_UnsignedIntegerBox),
                                    ],
@@ -567,35 +553,35 @@
         )
         self.w_int64dtype = W_Dtype(
             types.Int64(),
-            num=9,
-            kind=SIGNEDLTR,
+            num=NPY_LONGLONG,
+            kind=NPY_SIGNEDLTR,
             name="int64",
-            char="q",
+            char=NPY_LONGLONGLTR,
             w_box_type=space.gettypefor(interp_boxes.W_Int64Box),
             alternate_constructors=[space.w_long],
         )
         self.w_uint64dtype = W_Dtype(
             types.UInt64(),
-            num=10,
-            kind=UNSIGNEDLTR,
+            num=NPY_ULONGLONG,
+            kind=NPY_UNSIGNEDLTR,
             name="uint64",
-            char="Q",
+            char=NPY_ULONGLONGLTR,
             w_box_type=space.gettypefor(interp_boxes.W_UInt64Box),
         )
         self.w_float32dtype = W_Dtype(
             types.Float32(),
-            num=11,
-            kind=FLOATINGLTR,
+            num=NPY_FLOAT,
+            kind=NPY_FLOATINGLTR,
             name="float32",
-            char="f",
+            char=NPY_FLOATLTR,
             w_box_type=space.gettypefor(interp_boxes.W_Float32Box),
         )
         self.w_float64dtype = W_Dtype(
             types.Float64(),
-            num=12,
-            kind=FLOATINGLTR,
+            num=NPY_DOUBLE,
+            kind=NPY_FLOATINGLTR,
             name="float64",
-            char="d",
+            char=NPY_DOUBLELTR,
             w_box_type = space.gettypefor(interp_boxes.W_Float64Box),
             alternate_constructors=[space.w_float,
                                     space.gettypefor(interp_boxes.W_NumberBox),
@@ -604,28 +590,28 @@
         )
         self.w_floatlongdtype = W_Dtype(
             types.FloatLong(),
-            num=13,
-            kind=FLOATINGLTR,
+            num=NPY_LONGDOUBLE,
+            kind=NPY_FLOATINGLTR,
             name="float%d" % (interp_boxes.long_double_size * 8),
-            char="g",
+            char=NPY_LONGDOUBLELTR,
             w_box_type=space.gettypefor(interp_boxes.W_FloatLongBox),
             aliases=["longdouble", "longfloat"],
         )
         self.w_complex64dtype = W_Dtype(
             types.Complex64(),
-            num=14,
-            kind=COMPLEXLTR,
+            num=NPY_CFLOAT,
+            kind=NPY_COMPLEXLTR,
             name="complex64",
-            char="F",
+            char=NPY_CFLOATLTR,
             w_box_type = space.gettypefor(interp_boxes.W_Complex64Box),
             float_type = self.w_float32dtype,
         )
         self.w_complex128dtype = W_Dtype(
             types.Complex128(),
-            num=15,
-            kind=COMPLEXLTR,
+            num=NPY_CDOUBLE,
+            kind=NPY_COMPLEXLTR,
             name="complex128",
-            char="D",
+            char=NPY_CDOUBLELTR,
             w_box_type = space.gettypefor(interp_boxes.W_Complex128Box),
             alternate_constructors=[space.w_complex],
             aliases=["complex"],
@@ -633,39 +619,39 @@
         )
         self.w_complexlongdtype = W_Dtype(
             types.ComplexLong(),
-            num=16,
-            kind=COMPLEXLTR,
+            num=NPY_CLONGDOUBLE,
+            kind=NPY_COMPLEXLTR,
             name="complex%d" % (interp_boxes.long_double_size * 16),
-            char="G",
+            char=NPY_CLONGDOUBLELTR,
             w_box_type = space.gettypefor(interp_boxes.W_ComplexLongBox),
             aliases=["clongdouble", "clongfloat"],
             float_type = self.w_floatlongdtype,
         )
         self.w_stringdtype = W_Dtype(
             types.StringType(0),
-            num=18,
-            kind=STRINGLTR,
+            num=NPY_STRING,
+            kind=NPY_STRINGLTR,
             name='string',
-            char='S',
+            char=NPY_STRINGLTR,
             w_box_type = space.gettypefor(interp_boxes.W_StringBox),
             alternate_constructors=[space.w_str, space.gettypefor(interp_boxes.W_CharacterBox)],
             aliases=["str"],
         )
         self.w_unicodedtype = W_Dtype(
             types.UnicodeType(0),
-            num=19,
-            kind=UNICODELTR,
+            num=NPY_UNICODE,
+            kind=NPY_UNICODELTR,
             name='unicode',
-            char='U',
+            char=NPY_UNICODELTR,
             w_box_type = space.gettypefor(interp_boxes.W_UnicodeBox),
             alternate_constructors=[space.w_unicode],
         )
         self.w_voiddtype = W_Dtype(
             types.VoidType(0),
-            num=20,
-            kind=VOIDLTR,
+            num=NPY_VOID,
+            kind=NPY_VOIDLTR,
             name='void',
-            char='V',
+            char=NPY_VOIDLTR,
             w_box_type = space.gettypefor(interp_boxes.W_VoidBox),
             #alternate_constructors=[space.w_buffer],
             # XXX no buffer in space
@@ -674,43 +660,43 @@
         )
         self.w_float16dtype = W_Dtype(
             types.Float16(),
-            num=23,
-            kind=FLOATINGLTR,
+            num=NPY_HALF,
+            kind=NPY_FLOATINGLTR,
             name="float16",
-            char="e",
+            char=NPY_HALFLTR,
             w_box_type=space.gettypefor(interp_boxes.W_Float16Box),
         )
         ptr_size = rffi.sizeof(rffi.CCHARP)
         if ptr_size == 4:
             intp_box = interp_boxes.W_Int32Box
             intp_type = types.Int32()
-            intp_num = 5
+            intp_num = NPY_INT
             uintp_box = interp_boxes.W_UInt32Box
             uintp_type = types.UInt32()
-            uintp_num = 6
+            uintp_num = NPY_UINT
         elif ptr_size == 8:
             intp_box = interp_boxes.W_Int64Box
             intp_type = types.Int64()
-            intp_num = 7
+            intp_num = NPY_LONG
             uintp_box = interp_boxes.W_UInt64Box
             uintp_type = types.UInt64()
-            uintp_num = 8
+            uintp_num = NPY_ULONG
         else:
             raise ValueError('unknown point size %d' % ptr_size)
         self.w_intpdtype = W_Dtype(
             intp_type,
             num=intp_num,
-            kind=INTPLTR,
+            kind=NPY_INTPLTR,
             name='intp',
-            char=INTPLTR,
+            char=NPY_INTPLTR,
             w_box_type = space.gettypefor(intp_box),
         )
         self.w_uintpdtype = W_Dtype(
             uintp_type,
             num=uintp_num,
-            kind=UINTPLTR,
+            kind=NPY_UINTPLTR,
             name='uintp',
-            char=UINTPLTR,
+            char=NPY_UINTPLTR,
             w_box_type = space.gettypefor(uintp_box),
         )
         float_dtypes = [self.w_float16dtype, self.w_float32dtype,
@@ -741,24 +727,24 @@
             self.dtypes_by_name[dtype.name] = dtype
             can_name = dtype.kind + str(dtype.itemtype.get_element_size())
             self.dtypes_by_name[can_name] = dtype
-            self.dtypes_by_name[NATBYTE + can_name] = dtype
-            self.dtypes_by_name['=' + can_name] = dtype
-            new_name = OPPBYTE + can_name
+            self.dtypes_by_name[NPY_NATBYTE + can_name] = dtype
+            self.dtypes_by_name[NPY_NATIVE + can_name] = dtype
+            new_name = NPY_OPPBYTE + can_name
             itemtypename = dtype.itemtype.__class__.__name__
             itemtype = getattr(types, 'NonNative' + itemtypename)()
             self.dtypes_by_name[new_name] = W_Dtype(
                 itemtype,
                 dtype.num, dtype.kind, new_name, dtype.char, dtype.w_box_type,
-                byteorder=OPPBYTE, float_type=dtype.float_type)
+                byteorder=NPY_OPPBYTE, float_type=dtype.float_type)
             if dtype.kind != dtype.char:
                 can_name = dtype.char
-                self.dtypes_by_name[NATBYTE + can_name] = dtype
-                self.dtypes_by_name['=' + can_name] = dtype
-                new_name = OPPBYTE + can_name
+                self.dtypes_by_name[NPY_NATBYTE + can_name] = dtype
+                self.dtypes_by_name[NPY_NATIVE + can_name] = dtype
+                new_name = NPY_OPPBYTE + can_name
                 self.dtypes_by_name[new_name] = W_Dtype(
                     itemtype,
                     dtype.num, dtype.kind, new_name, dtype.char, dtype.w_box_type,
-                    byteorder=OPPBYTE, float_type=dtype.float_type)
+                    byteorder=NPY_OPPBYTE, float_type=dtype.float_type)
 
             for alias in dtype.aliases:
                 self.dtypes_by_name[alias] = dtype
@@ -815,9 +801,9 @@
                        space.wrap(dtype.num),
                        space.wrap(itemsize * 8), # in case of changing
                        # number of bits per byte in the future
-                       space.wrap(itemsize / (2 if dtype.kind == COMPLEXLTR else 1) or 1)]
+                       space.wrap(itemsize / (2 if dtype.kind == NPY_COMPLEXLTR else 1) or 1)]
             if dtype.is_int_type():
-                if dtype.kind == BOOLLTR:
+                if dtype.kind == NPY_GENBOOLLTR:
                     w_maxobj = space.wrap(1)
                     w_minobj = space.wrap(0)
                 elif dtype.is_signed():
diff --git a/pypy/module/micronumpy/interp_ufuncs.py b/pypy/module/micronumpy/interp_ufuncs.py
--- a/pypy/module/micronumpy/interp_ufuncs.py
+++ b/pypy/module/micronumpy/interp_ufuncs.py
@@ -9,6 +9,7 @@
 from pypy.module.micronumpy.interp_support import unwrap_axis_arg
 from pypy.module.micronumpy.strides import shape_agreement
 from pypy.module.micronumpy.base import convert_to_array, W_NDimArray
+from pypy.module.micronumpy.constants import *
 
 def done_if_true(dtype, val):
     return dtype.itemtype.bool(val)
@@ -431,16 +432,16 @@
     if dt1.num > dt2.num:
         dt1, dt2 = dt2, dt1
     # Some operations promote op(bool, bool) to return int8, rather than bool
-    if promote_bools and (dt1.kind == dt2.kind == interp_dtype.BOOLLTR):
+    if promote_bools and (dt1.kind == dt2.kind == NPY_GENBOOLLTR):
         return interp_dtype.get_dtype_cache(space).w_int8dtype
 
     # Everything numeric promotes to complex
     if dt2.is_complex_type() or dt1.is_complex_type():
-        if dt2.num == 14:
+        if dt2.num == NPY_CFLOAT:
             return interp_dtype.get_dtype_cache(space).w_complex64dtype
-        elif dt2.num == 15:
+        elif dt2.num == NPY_CDOUBLE:
             return interp_dtype.get_dtype_cache(space).w_complex128dtype
-        elif dt2.num == 16:
+        elif dt2.num == NPY_CLONGDOUBLE:
             return interp_dtype.get_dtype_cache(space).w_complexlongdtype
         else:
             raise OperationError(space.w_TypeError, space.wrap("Unsupported types"))
@@ -452,35 +453,30 @@
         return dt2
 
     # Everything promotes to float, and bool promotes to everything.
-    if dt2.kind == interp_dtype.FLOATINGLTR or dt1.kind == interp_dtype.BOOLLTR:
+    if dt2.kind == NPY_FLOATINGLTR or dt1.kind == NPY_GENBOOLLTR:
         # Float32 + 8-bit int = Float64
-        if dt2.num == 11 and dt1.itemtype.get_element_size() >= 4:
+        if dt2.num == NPY_FLOAT and dt1.itemtype.get_element_size() >= 4:
             return interp_dtype.get_dtype_cache(space).w_float64dtype
         return dt2
 
     # for now this means mixing signed and unsigned
-    if dt2.kind == interp_dtype.SIGNEDLTR:
+    if dt2.kind == NPY_SIGNEDLTR:
         # if dt2 has a greater number of bytes, then just go with it
         if dt1.itemtype.get_element_size() < dt2.itemtype.get_element_size():
             return dt2
         # we need to promote both dtypes
         dtypenum = dt2.num + 2
-    elif dt2.num == 10 or (LONG_BIT == 64 and dt2.num == 8):
+    elif dt2.num == NPY_ULONGLONG or (LONG_BIT == 64 and dt2.num == NPY_ULONG):
         # UInt64 + signed = Float64
-        dtypenum = 12
+        dtypenum = NPY_DOUBLE
     elif dt2.is_flexible_type():
         # For those operations that get here (concatenate, stack),
         # flexible types take precedence over numeric type
         if dt2.is_record_type():
             return dt2
         if dt1.is_str_or_unicode():
-            if dt2.num == 18:
-                if dt2.itemtype.get_element_size() >= \
-                           dt1.itemtype.get_element_size():
-                    return dt2
-                return dt1
             if dt2.itemtype.get_element_size() >= \
-                       dt1.itemtype.get_element_size():
+                    dt1.itemtype.get_element_size():
                 return dt2
             return dt1
         return dt2
@@ -490,7 +486,7 @@
     newdtype = interp_dtype.get_dtype_cache(space).dtypes_by_num[dtypenum]
 
     if (newdtype.itemtype.get_element_size() > dt2.itemtype.get_element_size() or
-        newdtype.kind == interp_dtype.FLOATINGLTR):
+        newdtype.kind == NPY_FLOATINGLTR):
         return newdtype
     else:
         # we only promoted to long on 32-bit or to longlong on 64-bit
@@ -501,23 +497,23 @@
 @jit.unroll_safe
 def find_unaryop_result_dtype(space, dt, promote_to_float=False,
         promote_bools=False, promote_to_largest=False):
-    if promote_bools and (dt.kind == interp_dtype.BOOLLTR):
+    if promote_bools and (dt.kind == NPY_GENBOOLLTR):
         return interp_dtype.get_dtype_cache(space).w_int8dtype
     if promote_to_float:
-        if dt.kind == interp_dtype.FLOATINGLTR or dt.kind==interp_dtype.COMPLEXLTR:
+        if dt.kind == NPY_FLOATINGLTR or dt.kind == NPY_COMPLEXLTR:
             return dt
-        if dt.num >= 5:
+        if dt.num >= NPY_INT:
             return interp_dtype.get_dtype_cache(space).w_float64dtype
         for bytes, dtype in interp_dtype.get_dtype_cache(space).float_dtypes_by_num_bytes:
-            if (dtype.kind == interp_dtype.FLOATINGLTR and
+            if (dtype.kind == NPY_FLOATINGLTR and
                 dtype.itemtype.get_element_size() > dt.itemtype.get_element_size()):
                 return dtype
     if promote_to_largest:
-        if dt.kind == interp_dtype.BOOLLTR or dt.kind == interp_dtype.SIGNEDLTR:
+        if dt.kind == NPY_GENBOOLLTR or dt.kind == NPY_SIGNEDLTR:
             return interp_dtype.get_dtype_cache(space).w_float64dtype
-        elif dt.kind == interp_dtype.FLOATINGLTR:
+        elif dt.kind == NPY_FLOATINGLTR:
             return interp_dtype.get_dtype_cache(space).w_float64dtype
-        elif dt.kind == interp_dtype.UNSIGNEDLTR:
+        elif dt.kind == NPY_UNSIGNEDLTR:
             return interp_dtype.get_dtype_cache(space).w_uint64dtype
         else:
             assert False
@@ -559,8 +555,8 @@
         if (current_guess is None):
             return interp_dtype.variable_dtype(space,
                                                'S%d' % space.len_w(w_obj))
-        elif current_guess.num ==18:
-            if  current_guess.itemtype.get_size() < space.len_w(w_obj):
+        elif current_guess.num == NPY_STRING:
+            if current_guess.itemtype.get_size() < space.len_w(w_obj):
                 return interp_dtype.variable_dtype(space,
                                                    'S%d' % space.len_w(w_obj))
         return current_guess
diff --git a/pypy/module/micronumpy/loop.py b/pypy/module/micronumpy/loop.py
--- a/pypy/module/micronumpy/loop.py
+++ b/pypy/module/micronumpy/loop.py
@@ -10,7 +10,7 @@
 from rpython.rtyper.lltypesystem import lltype, rffi
 from pypy.module.micronumpy.base import W_NDimArray
 from pypy.module.micronumpy.iter import PureShapeIterator
-from pypy.module.micronumpy import constants
+from pypy.module.micronumpy.constants import *
 from pypy.module.micronumpy.support import int_w
 
 call2_driver = jit.JitDriver(name='numpy_call2',
@@ -583,13 +583,13 @@
                                       mode=mode)
         index = int_w(space, arr_iter.getitem())
         if index < 0 or index >= len(iterators):
-            if mode == constants.MODE_RAISE:
+            if mode == NPY_RAISE:
                 raise OperationError(space.w_ValueError, space.wrap(
                     "invalid entry in choice array"))
-            elif mode == constants.MODE_WRAP:
+            elif mode == NPY_WRAP:
                 index = index % (len(iterators))
             else:
-                assert mode == constants.MODE_CLIP
+                assert mode == NPY_CLIP
                 if index < 0:
                     index = 0
                 else:
diff --git a/pypy/module/micronumpy/test/test_base.py b/pypy/module/micronumpy/test/test_base.py
--- a/pypy/module/micronumpy/test/test_base.py
+++ b/pypy/module/micronumpy/test/test_base.py
@@ -1,4 +1,4 @@
-from pypy.module.micronumpy.interp_dtype import NATBYTE, OPPBYTE
+from pypy.module.micronumpy.interp_dtype import NPY_NATBYTE, NPY_OPPBYTE
 from pypy.conftest import option
 import sys
 
@@ -11,5 +11,5 @@
             if '__pypy__' not in sys.builtin_module_names:
                 import numpy
                 sys.modules['numpypy'] = numpy
-        cls.w_non_native_prefix = cls.space.wrap(OPPBYTE)
-        cls.w_native_prefix = cls.space.wrap(NATBYTE)
+        cls.w_non_native_prefix = cls.space.wrap(NPY_OPPBYTE)
+        cls.w_native_prefix = cls.space.wrap(NPY_NATBYTE)


More information about the pypy-commit mailing list