Fix bug in xmlclass when allowing custom class attributes definition on inherited classes
authorlaurent
Tue, 17 Jan 2012 17:20:54 +0100
changeset 626 ac0a8f6462c3
parent 625 b7062a7018ec
child 627 99ab0b74865f
Fix bug in xmlclass when allowing custom class attributes definition on inherited classes
xmlclass/xmlclass.py
--- a/xmlclass/xmlclass.py	Tue Jan 17 16:44:00 2012 +0100
+++ b/xmlclass/xmlclass.py	Tue Jan 17 17:20:54 2012 +0100
@@ -1217,7 +1217,6 @@
             classmembers["get%s" % elmtname] = generateGetMethod(elmtname)
             
         classmembers["__init__"] = generateInitMethod(self, classinfos)
-        classmembers["__setattr__"] = generateSetattrMethod(self, classinfos)
         classmembers["getStructure"] = generateStructureMethod(classinfos)
         classmembers["loadXMLTree"] = generateLoadXMLTree(self, classinfos)
         classmembers["generateXMLText"] = generateGenerateXMLText(self, classinfos)
@@ -1228,6 +1227,7 @@
         classmembers["compatibility"] = lambda x, y: None
         
         class_definition = classobj(str(classname), bases, classmembers)
+        setattr(class_definition, "__setattr__", generateSetattrMethod(self, class_definition, classinfos))
         class_infos = {"type": COMPILEDCOMPLEXTYPE,
                 "name": classname,
                 "check": generateClassCheckFunction(class_definition),
@@ -1296,7 +1296,7 @@
 Method that generate the method for loading an xml tree by following the
 attributes list defined
 """
-def generateSetattrMethod(factory, classinfos):
+def generateSetattrMethod(factory, class_definition, classinfos):
     attributes = dict([(attr["name"], attr) for attr in classinfos["attributes"] if attr["use"] != "prohibited"])
     optional_attributes = dict([(attr["name"], True) for attr in classinfos["attributes"] if attr["use"] == "optional"])
     elements = dict([(element["name"], element) for element in classinfos["elements"]])
@@ -1322,7 +1322,7 @@
                 raise ValueError("Invalid value for attribute '%s'." % (name))
         elif classinfos.has_key("base"):
             return classinfos["base"].__setattr__(self, name, value)
-        elif self.__class__.__dict__.has_key(name):
+        elif class_definition.__dict__.has_key(name):
             return object.__setattr__(self, name, value)
         else:
             raise AttributeError("'%s' can't have an attribute '%s'." % (self.__class__.__name__, name))