Author: mattip <[email protected]>
Branch: numpy-flags
Changeset: r77378:d84dc304c027
Date: 2015-05-18 00:05 +0300
http://bitbucket.org/pypy/pypy/changeset/d84dc304c027/

Log:    pass tests in test_flagobj, add test for str(flags)

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
@@ -143,6 +143,10 @@
     def get_scalar_value(self):
         return self
 
+    def get_flags(self):
+        return (NPY.ARRAY_C_CONTIGUOUS | NPY.ARRAY_F_CONTIGUOUS | 
+                NPY.ARRAY_WRITEABLE | NPY.ARRAY_OWNDATA)
+
     def item(self, space):
         return self.get_dtype(space).itemtype.to_builtin_type(space, self)
 
diff --git a/pypy/module/micronumpy/flagsobj.py 
b/pypy/module/micronumpy/flagsobj.py
--- a/pypy/module/micronumpy/flagsobj.py
+++ b/pypy/module/micronumpy/flagsobj.py
@@ -1,4 +1,5 @@
 from rpython.rlib import jit
+from rpython.rlib.rstring import StringBuilder
 
 from pypy.interpreter.baseobjspace import W_Root
 from pypy.interpreter.error import OperationError
@@ -13,54 +14,50 @@
 def clear_flags(arr, flags):
     arr.flags &= ~flags
 
-def _update_contiguous_flags(arr):
-    is_c_contig = is_c_contiguous(arr)
-    if is_c_contig:
-        enable_flags(arr, NPY.ARRAY_C_CONTIGUOUS)
-    else:
-        clear_flags(arr, NPY.ARRAY_C_CONTIGUOUS)
-
-    is_f_contig = is_f_contiguous(arr)
-    if is_f_contig:
-        enable_flags(arr, NPY.ARRAY_F_CONTIGUOUS)
-    else:
-        clear_flags(arr, NPY.ARRAY_F_CONTIGUOUS)
-
+def get_tf_str(flags, key):
+    if flags & key:
+        return 'True'
+    return 'False'
 
 class W_FlagsObject(W_Root):
     def __init__(self, arr):
-        self.flags = 0
+        print 'initializing flag from',arr
+        if arr:
+            self.flags = arr.get_flags()
+        else:
+            self.flags = (NPY.ARRAY_C_CONTIGUOUS | NPY.ARRAY_F_CONTIGUOUS |
+                          NPY.ARRAY_OWNDATA | NPY.ARRAY_ALIGNED)
 
     def descr__new__(space, w_subtype):
         self = space.allocate_instance(W_FlagsObject, w_subtype)
         W_FlagsObject.__init__(self, None)
         return self
 
-    def descr_get_contiguous(self, space):
-        return space.w_True
+    def descr_c_contiguous(self, space):
+        return space.wrap(bool(self.flags & NPY.ARRAY_C_CONTIGUOUS))
 
-    def descr_get_fortran(self, space):
-        return space.w_False
+    def descr_f_contiguous(self, space):
+        return space.wrap(bool(self.flags & NPY.ARRAY_F_CONTIGUOUS))
 
     def descr_get_writeable(self, space):
-        return space.w_True
+        return space.wrap(bool(self.flags & NPY.ARRAY_WRITEABLE))
 
     def descr_get_fnc(self, space):
-        return space.wrap(
-            space.is_true(self.descr_get_fortran(space)) and not
-            space.is_true(self.descr_get_contiguous(space)))
+        return space.wrap(bool(
+            self.flags & NPY.ARRAY_F_CONTIGUOUS and not
+            self.flags & NPY.ARRAY_C_CONTIGUOUS ))
 
     def descr_get_forc(self, space):
-        return space.wrap(
-            space.is_true(self.descr_get_fortran(space)) or
-            space.is_true(self.descr_get_contiguous(space)))
+        return space.wrap(bool(
+            self.flags & NPY.ARRAY_F_CONTIGUOUS or
+            self.flags & NPY.ARRAY_C_CONTIGUOUS ))
 
     def descr_getitem(self, space, w_item):
         key = space.str_w(w_item)
         if key == "C" or key == "CONTIGUOUS" or key == "C_CONTIGUOUS":
-            return self.descr_get_contiguous(space)
+            return self.descr_c_contiguous(space)
         if key == "F" or key == "FORTRAN" or key == "F_CONTIGUOUS":
-            return self.descr_get_fortran(space)
+            return self.descr_f_contiguous(space)
         if key == "W" or key == "WRITEABLE":
             return self.descr_get_writeable(space)
         if key == "FNC":
@@ -85,6 +82,22 @@
     def descr_ne(self, space, w_other):
         return space.wrap(not self.eq(space, w_other))
 
+    def descr___str__(self, space):
+        s = StringBuilder()
+        s.append('  C_CONTIGUOUS : ')
+        s.append(get_tf_str(self.flags, NPY.ARRAY_C_CONTIGUOUS))
+        s.append('\n  F_CONTIGUOUS : ')
+        s.append(get_tf_str(self.flags, NPY.ARRAY_F_CONTIGUOUS))
+        s.append('\n  OWNDATA : ')
+        s.append(get_tf_str(self.flags, NPY.ARRAY_OWNDATA))
+        s.append('\n  WRITEABLE : ')
+        s.append(get_tf_str(self.flags, NPY.ARRAY_WRITEABLE))
+        s.append('\n  ALIGNED : ')
+        s.append(get_tf_str(self.flags, NPY.ARRAY_ALIGNED))
+        s.append('\n  UPDATEIFCOPY : ')
+        s.append(get_tf_str(self.flags, NPY.ARRAY_UPDATEIFCOPY))
+        return space.wrap(s.build())
+
 W_FlagsObject.typedef = TypeDef("numpy.flagsobj",
     __new__ = interp2app(W_FlagsObject.descr__new__.im_func),
 
@@ -92,11 +105,12 @@
     __setitem__ = interp2app(W_FlagsObject.descr_setitem),
     __eq__ = interp2app(W_FlagsObject.descr_eq),
     __ne__ = interp2app(W_FlagsObject.descr_ne),
+    __str__ = interp2app(W_FlagsObject.descr___str__),
 
-    contiguous = GetSetProperty(W_FlagsObject.descr_get_contiguous),
-    c_contiguous = GetSetProperty(W_FlagsObject.descr_get_contiguous),
-    f_contiguous = GetSetProperty(W_FlagsObject.descr_get_fortran),
-    fortran = GetSetProperty(W_FlagsObject.descr_get_fortran),
+    contiguous = GetSetProperty(W_FlagsObject.descr_c_contiguous),
+    c_contiguous = GetSetProperty(W_FlagsObject.descr_c_contiguous),
+    f_contiguous = GetSetProperty(W_FlagsObject.descr_f_contiguous),
+    fortran = GetSetProperty(W_FlagsObject.descr_f_contiguous),
     writeable = GetSetProperty(W_FlagsObject.descr_get_writeable),
     fnc = GetSetProperty(W_FlagsObject.descr_get_fnc),
     forc = GetSetProperty(W_FlagsObject.descr_get_forc),
diff --git a/pypy/module/micronumpy/iterators.py 
b/pypy/module/micronumpy/iterators.py
--- a/pypy/module/micronumpy/iterators.py
+++ b/pypy/module/micronumpy/iterators.py
@@ -39,8 +39,6 @@
 from rpython.rlib import jit
 from pypy.module.micronumpy import support, constants as NPY
 from pypy.module.micronumpy.base import W_NDimArray
-from pypy.module.micronumpy.flagsobj import _update_contiguous_flags
-
 
 class PureShapeIter(object):
     def __init__(self, shape, idx_w):
@@ -96,7 +94,6 @@
     @jit.unroll_safe
     def __init__(self, array, size, shape, strides, backstrides):
         assert len(shape) == len(strides) == len(backstrides)
-        _update_contiguous_flags(array)
         self.contiguous = (array.flags & NPY.ARRAY_C_CONTIGUOUS and
                            array.shape == shape and array.strides == strides)
 
diff --git a/pypy/module/micronumpy/test/test_flagsobj.py 
b/pypy/module/micronumpy/test/test_flagsobj.py
--- a/pypy/module/micronumpy/test/test_flagsobj.py
+++ b/pypy/module/micronumpy/test/test_flagsobj.py
@@ -9,6 +9,10 @@
         b = type(a.flags)()
         assert b is not a.flags
         assert b['C'] is True
+        s = str(b)
+        assert s == '%s' %('  C_CONTIGUOUS : True\n  F_CONTIGUOUS : True'
+                         '\n  OWNDATA : True\n  WRITEABLE : False'
+                         '\n  ALIGNED : True\n  UPDATEIFCOPY : False')
 
     def test_repr(self):
         import numpy as np
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to