[pypy-commit] pypy default: clean up some stuff in micronumpy

bdkearns noreply at buildbot.pypy.org
Fri Oct 18 06:06:01 CEST 2013


Author: Brian Kearns <bdkearns at gmail.com>
Branch: 
Changeset: r67473:0ab4a0d7455f
Date: 2013-10-18 00:04 -0400
http://bitbucket.org/pypy/pypy/changeset/0ab4a0d7455f/

Log:	clean up some stuff in micronumpy

diff --git a/pypy/module/micronumpy/interp_boxes.py b/pypy/module/micronumpy/interp_boxes.py
--- a/pypy/module/micronumpy/interp_boxes.py
+++ b/pypy/module/micronumpy/interp_boxes.py
@@ -63,6 +63,7 @@
 
 class PrimitiveBox(Box):
     _mixin_ = True
+    _immutable_fields_ = ['value']
 
     def __init__(self, value):
         self.value = value
@@ -82,11 +83,11 @@
         ret = builder.build()
 
         lltype.free(value, flavor="raw")
-
         return ret
 
 class ComplexBox(Box):
     _mixin_ = True
+    _immutable_fields_ = ['real', 'imag']
 
     def __init__(self, real, imag=0.):
         self.real = real
@@ -111,12 +112,10 @@
         ret = builder.build()
 
         lltype.free(value, flavor="raw")
-
         return ret
 
+
 class W_GenericBox(W_Root):
-    _attrs_ = ()
-
     def descr__new__(space, w_subtype, __args__):
         raise operationerrfmt(space.w_TypeError,
                               "cannot create '%N' instances",
@@ -125,12 +124,21 @@
     def get_dtype(self, space):
         return self._get_dtype(space)
 
+    def item(self, space):
+        return self.get_dtype(space).itemtype.to_builtin_type(space, self)
+
     def descr_str(self, space):
         return space.wrap(self.get_dtype(space).itemtype.str_format(self))
 
     def descr_format(self, space, w_spec):
         return space.format(self.item(space), w_spec)
 
+    def descr_hash(self, space):
+        return space.hash(self.item(space))
+
+    def descr_index(self, space):
+        return space.index(self.item(space))
+
     def descr_int(self, space):
         box = self.convert_to(W_LongBox._get_dtype(space))
         assert isinstance(box, W_LongBox)
@@ -222,12 +230,6 @@
         w_remainder = self.descr_rmod(space, w_other)
         return space.newtuple([w_quotient, w_remainder])
 
-    def descr_hash(self, space):
-        return space.hash(self.item(space))
-
-    def item(self, space):
-        return self.get_dtype(space).itemtype.to_builtin_type(space, self)
-
     def descr_any(self, space):
         value = space.is_true(self)
         return self.get_dtype(space).box(value)
@@ -260,7 +262,7 @@
     descr__new__, _get_dtype, descr_reduce = new_dtype_getter("bool")
 
 class W_NumberBox(W_GenericBox):
-    _attrs_ = ()
+    pass
 
 class W_IntegerBox(W_NumberBox):
     def int_w(self, space):
@@ -309,10 +311,10 @@
     descr__new__, _get_dtype, descr_reduce = new_dtype_getter('ulonglong')
 
 class W_InexactBox(W_NumberBox):
-    _attrs_ = ()
+    pass
 
 class W_FloatingBox(W_InexactBox):
-    _attrs_ = ()
+    pass
 
 class W_Float16Box(W_FloatingBox, PrimitiveBox):
     descr__new__, _get_dtype, descr_reduce = new_dtype_getter("float16")
@@ -323,9 +325,42 @@
 class W_Float64Box(W_FloatingBox, PrimitiveBox):
     descr__new__, _get_dtype, descr_reduce = new_dtype_getter("float64")
 
+class W_ComplexFloatingBox(W_InexactBox):
+    def descr_get_real(self, space):
+        dtype = self._COMPONENTS_BOX._get_dtype(space)
+        box = self.convert_real_to(dtype)
+        assert isinstance(box, self._COMPONENTS_BOX)
+        return space.wrap(box)
+
+    def descr_get_imag(self, space):
+        dtype = self._COMPONENTS_BOX._get_dtype(space)
+        box = self.convert_imag_to(dtype)
+        assert isinstance(box, self._COMPONENTS_BOX)
+        return space.wrap(box)
+
+class W_Complex64Box(ComplexBox, W_ComplexFloatingBox):
+    descr__new__, _get_dtype, descr_reduce = new_dtype_getter("complex64")
+    _COMPONENTS_BOX = W_Float32Box
+
+class W_Complex128Box(ComplexBox, W_ComplexFloatingBox):
+    descr__new__, _get_dtype, descr_reduce = new_dtype_getter("complex128")
+    _COMPONENTS_BOX = W_Float64Box
+
+if long_double_size == 8:
+    W_FloatLongBox = W_Float64Box
+    W_ComplexLongBox = W_Complex128Box
+
+elif long_double_size in (12, 16):
+    class W_FloatLongBox(W_FloatingBox, PrimitiveBox):
+        descr__new__, _get_dtype, descr_reduce = new_dtype_getter("float%d" % (long_double_size * 8))
+
+    class W_ComplexLongBox(ComplexBox, W_ComplexFloatingBox):
+        descr__new__, _get_dtype, descr_reduce = new_dtype_getter("complex%d" % (long_double_size * 16))
+        _COMPONENTS_BOX = W_FloatLongBox
+
 class W_FlexibleBox(W_GenericBox):
-    _attrs_ = ['ofs', 'dtype', 'arr']
-    _immutable_fields_ = ['ofs']
+    _immutable_fields_ = ['arr', 'ofs', 'dtype']
+
     def __init__(self, arr, ofs, dtype):
         self.arr = arr # we have to keep array alive
         self.ofs = ofs
@@ -334,11 +369,6 @@
     def get_dtype(self, space):
         return self.arr.dtype
 
- at unwrap_spec(self=W_GenericBox)
-def descr_index(space, self):
-    return space.index(self.item(space))
-
-
 class W_VoidBox(W_FlexibleBox):
     def descr_getitem(self, space, w_item):
         from pypy.module.micronumpy.types import VoidType
@@ -388,7 +418,6 @@
         # XXX assert dtype is str type
         return self
 
-
 class W_StringBox(W_CharacterBox):
     def descr__new__string_box(space, w_subtype, w_arg):
         from pypy.module.micronumpy.interp_dtype import new_string_dtype
@@ -398,7 +427,6 @@
             arr.storage[i] = arg[i]
         return W_StringBox(arr, 0, arr.dtype)
 
-
 class W_UnicodeBox(W_CharacterBox):
     def descr__new__unicode_box(space, w_subtype, w_arg):
         raise OperationError(space.w_NotImplementedError, space.wrap("Unicode is not supported yet"))
@@ -413,45 +441,6 @@
         #    arr.storage[i] = arg[i]
         return W_UnicodeBox(arr, 0, arr.dtype)
 
-
-class W_ComplexFloatingBox(W_InexactBox):
-    _attrs_ = ()
-    def descr_get_real(self, space):
-        dtype = self._COMPONENTS_BOX._get_dtype(space)
-        box = self.convert_real_to(dtype)
-        assert isinstance(box, self._COMPONENTS_BOX)
-        return space.wrap(box)
-
-    def descr_get_imag(self, space):
-        dtype = self._COMPONENTS_BOX._get_dtype(space)
-        box = self.convert_imag_to(dtype)
-        assert isinstance(box, self._COMPONENTS_BOX)
-        return space.wrap(box)
-
-
-class W_Complex64Box(ComplexBox, W_ComplexFloatingBox):
-    descr__new__, _get_dtype, descr_reduce = new_dtype_getter("complex64")
-    _COMPONENTS_BOX = W_Float32Box
-
-
-class W_Complex128Box(ComplexBox, W_ComplexFloatingBox):
-    descr__new__, _get_dtype, descr_reduce = new_dtype_getter("complex128")
-    _COMPONENTS_BOX = W_Float64Box
-
-
-if long_double_size == 8:
-    W_FloatLongBox = W_Float64Box
-    W_ComplexLongBox = W_Complex128Box
-
-elif long_double_size in (12, 16):
-    class W_FloatLongBox(W_FloatingBox, PrimitiveBox):
-        descr__new__, _get_dtype, descr_reduce = new_dtype_getter("float%d" % (long_double_size * 8))
-
-    class W_ComplexLongBox(ComplexBox, W_ComplexFloatingBox):
-        descr__new__, _get_dtype, descr_reduce = new_dtype_getter("complex%d" % (long_double_size * 16))
-        _COMPONENTS_BOX = W_FloatLongBox
-
-
 W_GenericBox.typedef = TypeDef("generic",
     __module__ = "numpypy",
 
@@ -521,7 +510,7 @@
 W_BoolBox.typedef = TypeDef("bool_", W_GenericBox.typedef,
     __module__ = "numpypy",
     __new__ = interp2app(W_BoolBox.descr__new__.im_func),
-    __index__ = interp2app(descr_index),
+    __index__ = interp2app(W_BoolBox.descr_index),
     __reduce__ = interp2app(W_BoolBox.descr_reduce),
 )
 
@@ -544,49 +533,49 @@
 W_Int8Box.typedef = TypeDef("int8", W_SignedIntegerBox.typedef,
     __module__ = "numpypy",
     __new__ = interp2app(W_Int8Box.descr__new__.im_func),
-    __index__ = interp2app(descr_index),
+    __index__ = interp2app(W_Int8Box.descr_index),
     __reduce__ = interp2app(W_Int8Box.descr_reduce),
 )
 
 W_UInt8Box.typedef = TypeDef("uint8", W_UnsignedIntegerBox.typedef,
     __module__ = "numpypy",
     __new__ = interp2app(W_UInt8Box.descr__new__.im_func),
-    __index__ = interp2app(descr_index),
+    __index__ = interp2app(W_UInt8Box.descr_index),
     __reduce__ = interp2app(W_UInt8Box.descr_reduce),
 )
 
 W_Int16Box.typedef = TypeDef("int16", W_SignedIntegerBox.typedef,
     __module__ = "numpypy",
     __new__ = interp2app(W_Int16Box.descr__new__.im_func),
-    __index__ = interp2app(descr_index),
+    __index__ = interp2app(W_Int16Box.descr_index),
     __reduce__ = interp2app(W_Int16Box.descr_reduce),
 )
 
 W_UInt16Box.typedef = TypeDef("uint16", W_UnsignedIntegerBox.typedef,
     __module__ = "numpypy",
     __new__ = interp2app(W_UInt16Box.descr__new__.im_func),
-    __index__ = interp2app(descr_index),
+    __index__ = interp2app(W_UInt16Box.descr_index),
     __reduce__ = interp2app(W_UInt16Box.descr_reduce),
 )
 
 W_Int32Box.typedef = TypeDef("int32", (W_SignedIntegerBox.typedef,) + MIXIN_32,
     __module__ = "numpypy",
     __new__ = interp2app(W_Int32Box.descr__new__.im_func),
-    __index__ = interp2app(descr_index),
+    __index__ = interp2app(W_Int32Box.descr_index),
     __reduce__ = interp2app(W_Int32Box.descr_reduce),
 )
 
 W_UInt32Box.typedef = TypeDef("uint32", W_UnsignedIntegerBox.typedef,
     __module__ = "numpypy",
     __new__ = interp2app(W_UInt32Box.descr__new__.im_func),
-    __index__ = interp2app(descr_index),
+    __index__ = interp2app(W_UInt32Box.descr_index),
     __reduce__ = interp2app(W_UInt32Box.descr_reduce),
 )
 
 W_Int64Box.typedef = TypeDef("int64", (W_SignedIntegerBox.typedef,) + MIXIN_64,
     __module__ = "numpypy",
     __new__ = interp2app(W_Int64Box.descr__new__.im_func),
-    __index__ = interp2app(descr_index),
+    __index__ = interp2app(W_Int64Box.descr_index),
     __reduce__ = interp2app(W_Int64Box.descr_reduce),
 )
 
@@ -600,7 +589,7 @@
 W_UInt64Box.typedef = TypeDef("uint64", W_UnsignedIntegerBox.typedef,
     __module__ = "numpypy",
     __new__ = interp2app(W_UInt64Box.descr__new__.im_func),
-    __index__ = interp2app(descr_index),
+    __index__ = interp2app(W_UInt64Box.descr_index),
     __reduce__ = interp2app(W_UInt64Box.descr_reduce),
 )
 
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
@@ -18,10 +18,8 @@
 
 
 class W_Ufunc(W_Root):
-    _attrs_ = ["name", "promote_to_float", "promote_bools", "identity",
-               "allow_bool", "allow_complex", "complex_to_float"]
-    _immutable_fields_ = ["promote_to_float", "promote_bools", "name",
-            "allow_bool", "allow_complex", "complex_to_float"]
+    _immutable_fields_ = ["name", "promote_to_float", "promote_bools", "identity",
+            "int_only", "allow_bool", "allow_complex", "complex_to_float"]
 
     def __init__(self, name, promote_to_float, promote_bools, identity,
                  int_only, allow_bool, allow_complex, complex_to_float):
@@ -254,14 +252,12 @@
         return res
 
 class W_Ufunc1(W_Ufunc):
+    _immutable_fields_ = ["func", "bool_result"]
     argcount = 1
 
-    _immutable_fields_ = ["func", "name"]
-
     def __init__(self, func, name, promote_to_float=False, promote_bools=False,
-        identity=None, bool_result=False, int_only=False,
-        allow_bool=True, allow_complex=True, complex_to_float=False):
-
+            identity=None, bool_result=False, int_only=False,
+            allow_bool=True, allow_complex=True, complex_to_float=False):
         W_Ufunc.__init__(self, name, promote_to_float, promote_bools, identity,
                          int_only, allow_bool, allow_complex, complex_to_float)
         self.func = func
@@ -322,13 +318,12 @@
 
 
 class W_Ufunc2(W_Ufunc):
-    _immutable_fields_ = ["comparison_func", "func", "name", "int_only"]
+    _immutable_fields_ = ["func", "comparison_func", "done_func"]
     argcount = 2
 
     def __init__(self, func, name, promote_to_float=False, promote_bools=False,
-        identity=None, comparison_func=False, int_only=False,
-        allow_bool=True, allow_complex=True, complex_to_float=False):
-
+            identity=None, comparison_func=False, int_only=False,
+            allow_bool=True, allow_complex=True, complex_to_float=False):
         W_Ufunc.__init__(self, name, promote_to_float, promote_bools, identity,
                          int_only, allow_bool, allow_complex, complex_to_float)
         self.func = func
diff --git a/pypy/module/micronumpy/types.py b/pypy/module/micronumpy/types.py
--- a/pypy/module/micronumpy/types.py
+++ b/pypy/module/micronumpy/types.py
@@ -115,8 +115,6 @@
     return dispatcher
 
 class BaseType(object):
-    _attrs_ = ()
-
     SortRepr = None # placeholders for sorting classes, overloaded in sort.py
     Sort = None
 
@@ -323,8 +321,6 @@
         raw_storage_setitem(storage, i + offset, value)
 
 class Bool(BaseType, Primitive):
-    _attrs_ = ()
-
     T = lltype.Bool
     BoxType = interp_boxes.W_BoolBox
     format_code = "?"
@@ -541,101 +537,75 @@
     _mixin_ = True
 
 class Int8(BaseType, Integer):
-    _attrs_ = ()
-
     T = rffi.SIGNEDCHAR
     BoxType = interp_boxes.W_Int8Box
     format_code = "b"
+
 NonNativeInt8 = Int8
 
 class UInt8(BaseType, Integer):
-    _attrs_ = ()
-
     T = rffi.UCHAR
     BoxType = interp_boxes.W_UInt8Box
     format_code = "B"
+
 NonNativeUInt8 = UInt8
 
 class Int16(BaseType, Integer):
-    _attrs_ = ()
-
     T = rffi.SHORT
     BoxType = interp_boxes.W_Int16Box
     format_code = "h"
 
 class NonNativeInt16(BaseType, NonNativeInteger):
-    _attrs_ = ()
-
     T = rffi.SHORT
     BoxType = interp_boxes.W_Int16Box
     format_code = "h"
 
 class UInt16(BaseType, Integer):
-    _attrs_ = ()
-
     T = rffi.USHORT
     BoxType = interp_boxes.W_UInt16Box
     format_code = "H"
 
 class NonNativeUInt16(BaseType, NonNativeInteger):
-    _attrs_ = ()
-
     T = rffi.USHORT
     BoxType = interp_boxes.W_UInt16Box
     format_code = "H"
 
 class Int32(BaseType, Integer):
-    _attrs_ = ()
-
     T = rffi.INT
     BoxType = interp_boxes.W_Int32Box
     format_code = "i"
 
 class NonNativeInt32(BaseType, NonNativeInteger):
-    _attrs_ = ()
-
     T = rffi.INT
     BoxType = interp_boxes.W_Int32Box
     format_code = "i"
 
 class UInt32(BaseType, Integer):
-    _attrs_ = ()
-
     T = rffi.UINT
     BoxType = interp_boxes.W_UInt32Box
     format_code = "I"
 
 class NonNativeUInt32(BaseType, NonNativeInteger):
-    _attrs_ = ()
-
     T = rffi.UINT
     BoxType = interp_boxes.W_UInt32Box
     format_code = "I"
 
 class Long(BaseType, Integer):
-    _attrs_ = ()
-
     T = rffi.LONG
     BoxType = interp_boxes.W_LongBox
     format_code = "l"
 
 class NonNativeLong(BaseType, NonNativeInteger):
-    _attrs_ = ()
-
     T = rffi.LONG
     BoxType = interp_boxes.W_LongBox
     format_code = "l"
 
 class ULong(BaseType, Integer):
-    _attrs_ = ()
-
     T = rffi.ULONG
     BoxType = interp_boxes.W_ULongBox
     format_code = "L"
 
 class NonNativeULong(BaseType, NonNativeInteger):
-    _attrs_ = ()
-
     T = rffi.ULONG
     BoxType = interp_boxes.W_ULongBox
     format_code = "L"
@@ -654,8 +624,6 @@
     return self.box(value)
 
 class Int64(BaseType, Integer):
-    _attrs_ = ()
-
     T = rffi.LONGLONG
     BoxType = interp_boxes.W_Int64Box
     format_code = "q"
@@ -663,8 +631,6 @@
     _coerce = func_with_new_name(_int64_coerce, '_coerce')
 
 class NonNativeInt64(BaseType, NonNativeInteger):
-    _attrs_ = ()
-
     T = rffi.LONGLONG
     BoxType = interp_boxes.W_Int64Box
     format_code = "q"
@@ -685,8 +651,6 @@
     return self.box(value)
 
 class UInt64(BaseType, Integer):
-    _attrs_ = ()
-
     T = rffi.ULONGLONG
     BoxType = interp_boxes.W_UInt64Box
     format_code = "Q"
@@ -694,8 +658,6 @@
     _coerce = func_with_new_name(_uint64_coerce, '_coerce')
 
 class NonNativeUInt64(BaseType, NonNativeInteger):
-    _attrs_ = ()
-
     T = rffi.ULONGLONG
     BoxType = interp_boxes.W_UInt64Box
     format_code = "Q"
@@ -1040,10 +1002,8 @@
 class BaseFloat16(Float):
     _mixin_ = True
 
-    _attrs_ = ()
     _STORAGE_T = rffi.USHORT
     T = rffi.SHORT
-
     BoxType = interp_boxes.W_Float16Box
 
     @specialize.argtype(1)
@@ -1085,15 +1045,11 @@
                 byteswap(rffi.cast(self._STORAGE_T, hbits)))
 
 class Float32(BaseType, Float):
-    _attrs_ = ()
-
     T = rffi.FLOAT
     BoxType = interp_boxes.W_Float32Box
     format_code = "f"
 
 class NonNativeFloat32(BaseType, NonNativeFloat):
-    _attrs_ = ()
-
     T = rffi.FLOAT
     BoxType = interp_boxes.W_Float32Box
     format_code = "f"
@@ -1107,22 +1063,17 @@
         return bool(v)
 
 class Float64(BaseType, Float):
-    _attrs_ = ()
-
     T = rffi.DOUBLE
     BoxType = interp_boxes.W_Float64Box
     format_code = "d"
 
 class NonNativeFloat64(BaseType, NonNativeFloat):
-    _attrs_ = ()
-
     T = rffi.DOUBLE
     BoxType = interp_boxes.W_Float64Box
     format_code = "d"
 
 class ComplexFloating(object):
     _mixin_ = True
-    _attrs_ = ()
 
     def _coerce(self, space, w_item):
         w_item = space.call_function(space.w_complex, w_item)
@@ -1668,8 +1619,6 @@
         return 0, 0
 
 class Complex64(ComplexFloating, BaseType):
-    _attrs_ = ()
-
     T = rffi.FLOAT
     BoxType = interp_boxes.W_Complex64Box
     ComponentBoxType = interp_boxes.W_Float32Box
@@ -1677,8 +1626,6 @@
 NonNativeComplex64 = Complex64
 
 class Complex128(ComplexFloating, BaseType):
-    _attrs_ = ()
-
     T = rffi.DOUBLE
     BoxType = interp_boxes.W_Complex128Box
     ComponentBoxType = interp_boxes.W_Float64Box
@@ -1688,13 +1635,12 @@
 if interp_boxes.long_double_size == 8:
     FloatLong = Float64
     NonNativeFloatLong = NonNativeFloat64
+
     ComplexLong = Complex128
     NonNativeComplexLong = NonNativeComplex128
 
 elif interp_boxes.long_double_size in (12, 16):
     class FloatLong(BaseType, Float):
-        _attrs_ = ()
-
         T = rffi.LONGDOUBLE
         BoxType = interp_boxes.W_FloatLongBox
 
@@ -1712,8 +1658,6 @@
     NonNativeFloatLong = FloatLong
 
     class ComplexLong(ComplexFloating, BaseType):
-        _attrs_ = ()
-
         T = rffi.LONGDOUBLE
         BoxType = interp_boxes.W_ComplexLongBox
         ComponentBoxType = interp_boxes.W_FloatLongBox


More information about the pypy-commit mailing list