Commits

Brian Kearns committed 49ca298

add basic ndarray.flags object

Comments (0)

Files changed (3)

pypy/module/micronumpy/interp_flagsobj.py

+from pypy.interpreter.baseobjspace import W_Root
+from pypy.interpreter.typedef import TypeDef, GetSetProperty
+from pypy.interpreter.gateway import interp2app
+from pypy.interpreter.error import OperationError
+
+
+class W_FlagsObject(W_Root):
+    def __init__(self, arr):
+        self.arr = arr
+
+    def descr_get_contiguous(self, space):
+        return space.w_True
+
+    def descr_get_fortran(self, space):
+        return space.w_False
+
+    def descr_get_writeable(self, space):
+        return space.w_True
+
+    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)
+        if key == "F" or key == "FORTRAN" or key == "F_CONTIGUOUS":
+            return self.descr_get_fortran(space)
+        if key == "W" or key == "WRITEABLE":
+            return self.descr_get_writeable(space)
+        raise OperationError(space.w_KeyError, space.wrap(
+            "Unknown flag"))
+
+    def descr_setitem(self, space, w_item, w_value):
+        raise OperationError(space.w_KeyError, space.wrap(
+            "Unknown flag"))
+
+W_FlagsObject.typedef = TypeDef("flagsobj",
+    __module__ = "numpy",
+    __getitem__ = interp2app(W_FlagsObject.descr_getitem),
+    __setitem__ = interp2app(W_FlagsObject.descr_setitem),
+
+    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),
+    writeable = GetSetProperty(W_FlagsObject.descr_get_writeable),
+)

pypy/module/micronumpy/interp_numarray.py

 from pypy.module.micronumpy.strides import find_shape_and_elems,\
      get_shape_from_iterable, to_coords, shape_agreement, \
      shape_agreement_multiple
+from pypy.module.micronumpy.interp_flagsobj import W_FlagsObject
 from pypy.module.micronumpy.interp_flatiter import W_FlatIterator
 from pypy.module.micronumpy.appbridge import get_appbridge_cache
 from pypy.module.micronumpy import loop
         raise OperationError(space.w_NotImplementedError, space.wrap(
             "dumps not implemented yet"))
 
+    w_flags = None
     def descr_get_flags(self, space):
-        raise OperationError(space.w_NotImplementedError, space.wrap(
-            "getting flags not implemented yet"))
-
-    def descr_set_flags(self, space, w_args):
-        raise OperationError(space.w_NotImplementedError, space.wrap(
-            "setting flags not implemented yet"))
+        if self.w_flags is None:
+            self.w_flags = W_FlagsObject(self)
+        return self.w_flags
 
     @unwrap_spec(offset=int)
     def descr_getfield(self, space, w_dtype, offset):
     size = GetSetProperty(W_NDimArray.descr_get_size),
     itemsize = GetSetProperty(W_NDimArray.descr_get_itemsize),
     nbytes = GetSetProperty(W_NDimArray.descr_get_nbytes),
+    flags = GetSetProperty(W_NDimArray.descr_get_flags),
 
     fill = interp2app(W_NDimArray.descr_fill),
     tostring = interp2app(W_NDimArray.descr_tostring),

pypy/module/micronumpy/test/test_flagsobj.py

+from pypy.module.micronumpy.test.test_base import BaseNumpyAppTest
+
+
+class AppTestFlagsObj(BaseNumpyAppTest):
+    def test_repr(self):
+        import numpy as np
+        a = np.array([1,2,3])
+        assert repr(type(a.flags)) == "<type 'numpy.flagsobj'>"
+
+    def test_flags(self):
+        import numpy as np
+        a = np.array([1,2,3])
+        assert a.flags.c_contiguous == True
+        assert a.flags['W'] == True
+        raises(KeyError, "a.flags['blah']")
+        raises(KeyError, "a.flags['C_CONTIGUOUS'] = False")
+        raises((TypeError, AttributeError), "a.flags.c_contiguous = False")
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.