[Numpy-svn] r3980 - in trunk/numpy/core: . tests

numpy-svn@scip... numpy-svn@scip...
Tue Aug 21 01:59:33 CDT 2007


Author: stefan
Date: 2007-08-21 01:58:53 -0500 (Tue, 21 Aug 2007)
New Revision: 3980

Modified:
   trunk/numpy/core/records.py
   trunk/numpy/core/tests/test_records.py
Log:
Fix record assignment (based on a patch by Sameer DCosta).


Modified: trunk/numpy/core/records.py
===================================================================
--- trunk/numpy/core/records.py	2007-08-20 16:42:16 UTC (rev 3979)
+++ trunk/numpy/core/records.py	2007-08-21 06:58:53 UTC (rev 3980)
@@ -152,17 +152,16 @@
     def __setattr__(self, attr, val):
         if attr in ['setfield', 'getfield', 'dtype']:
             raise AttributeError, "Cannot set '%s' attribute" % attr
-        try:
-            return nt.void.__setattr__(self, attr, val)
-        except AttributeError:
-            pass
         fielddict = nt.void.__getattribute__(self, 'dtype').fields
         res = fielddict.get(attr, None)
         if res:
             return self.setfield(val, *res[:2])
         else:
-            raise AttributeError, "'record' object has no "\
-                  "attribute '%s'" % attr
+            if getattr(self,attr,None):
+                return nt.void.__setattr__(self, attr, val)
+            else:
+                raise AttributeError, "'record' object has no "\
+                      "attribute '%s'" % attr
 
     def pprint(self):
         # pretty-print all fields

Modified: trunk/numpy/core/tests/test_records.py
===================================================================
--- trunk/numpy/core/tests/test_records.py	2007-08-20 16:42:16 UTC (rev 3979)
+++ trunk/numpy/core/tests/test_records.py	2007-08-21 06:58:53 UTC (rev 3980)
@@ -85,5 +85,30 @@
         assert_array_equal(ra['field'], [[5,5,5]])
         assert callable(ra.field)
 
+class test_record(NumpyTestCase):
+    def setUp(self):
+        self.data = rec.fromrecords([(1,2,3),(4,5,6)],
+                            dtype=[("col1", "<i4"),
+                                   ("col2", "<i4"),
+                                   ("col3", "<i4")])
+
+    def test_assignment1(self):
+        a = self.data
+        assert_equal(a.col1[0],1)
+        a[0].col1 = 0
+        assert_equal(a.col1[0],0)
+
+    def test_assignment2(self):
+        a = self.data
+        assert_equal(a.col1[0],1)
+        a.col1[0] = 0
+        assert_equal(a.col1[0],0)
+
+    def test_invalid_assignment(self):
+        a = self.data
+        def assign_invalid_column(x):
+            x[0].col5 = 1
+        self.failUnlessRaises(AttributeError,assign_invalid_column,a)
+
 if __name__ == "__main__":
     NumpyTest().run()



More information about the Numpy-svn mailing list