[Numpy-svn] r3415 - trunk/numpy/core

numpy-svn at scipy.org numpy-svn at scipy.org
Sun Oct 29 01:39:42 CDT 2006


Author: oliphant
Date: 2006-10-29 01:39:30 -0500 (Sun, 29 Oct 2006)
New Revision: 3415

Modified:
   trunk/numpy/core/records.py
Log:
Fix the inability of record arrays to be able to set field data by setting the attribute of that field name to some value. 


Modified: trunk/numpy/core/records.py
===================================================================
--- trunk/numpy/core/records.py	2006-10-28 18:46:24 UTC (rev 3414)
+++ trunk/numpy/core/records.py	2006-10-29 06:39:30 UTC (rev 3415)
@@ -201,12 +201,23 @@
             return obj.view(chararray)
         return obj.view(sb.ndarray)
 
+# Save the dictionary
+#  If the attr is a field name and not in the saved dictionary
+#  Undo any "setting" of the attribute and do a setfield
+# Thus, you can't create attributes on-the-fly that are field names. 
+
     def __setattr__(self, attr, val):
+        newattr = attr not in self.__dict__
         try:
-            return object.__setattr__(self, attr, val)
-        except AttributeError: # Must be a fieldname
-            pass
-        fielddict = sb.ndarray.__getattribute__(self,'dtype').fields
+            res = object.__setattr__(self, attr, val)
+        except AttributeError:
+            fielddict = sb.ndarray.__getattribute__(self,'dtype').fields
+        else:
+            fielddict = sb.ndarray.__getattribute__(self,'dtype').fields
+            if attr not in fielddict:
+                return res
+            if newattr:         # We just added this one
+                object.__delattr__(self, attr)
         try:
             res = fielddict[attr][:2]
         except (TypeError,KeyError):



More information about the Numpy-svn mailing list