[Lxml-checkins] r52432 - in lxml/branch/lxml-2.0: . src/lxml

scoder at codespeak.net scoder at codespeak.net
Wed Mar 12 19:50:45 CET 2008


Author: scoder
Date: Wed Mar 12 19:50:45 2008
New Revision: 52432

Modified:
   lxml/branch/lxml-2.0/CHANGES.txt
   lxml/branch/lxml-2.0/src/lxml/lxml.objectify.pyx
Log:
merged fix from trunk rev 52430

Modified: lxml/branch/lxml-2.0/CHANGES.txt
==============================================================================
--- lxml/branch/lxml-2.0/CHANGES.txt	(original)
+++ lxml/branch/lxml-2.0/CHANGES.txt	Wed Mar 12 19:50:45 2008
@@ -11,6 +11,9 @@
 Bugs fixed
 ----------
 
+* Attribute assignment of custom PyTypes in objectify could fail to
+  correctly serialise the value to a string.
+
 Other changes
 -------------
 

Modified: lxml/branch/lxml-2.0/src/lxml/lxml.objectify.pyx
==============================================================================
--- lxml/branch/lxml-2.0/src/lxml/lxml.objectify.pyx	(original)
+++ lxml/branch/lxml-2.0/src/lxml/lxml.objectify.pyx	Wed Mar 12 19:50:45 2008
@@ -106,6 +106,9 @@
 XML_SCHEMA_INSTANCE_TYPE_ATTR = "{%s}type" % XML_SCHEMA_INSTANCE_NS
 
 
+# Forward declaration
+cdef class PyType
+
 ################################################################################
 # Element class for the main API
 
@@ -494,25 +497,27 @@
         _setElementValue(new_element, value)
 
 cdef _setElementValue(_Element element, value):
-    cdef python.PyObject* dict_result
+    cdef python.PyObject* _pytype
     if value is None:
         cetree.setAttributeValue(
             element, XML_SCHEMA_INSTANCE_NIL_ATTR, "true")
     elif isinstance(value, _Element):
         _replaceElement(element, value)
+        return
     else:
         cetree.delAttributeFromNsName(
             element._c_node, _XML_SCHEMA_INSTANCE_NS, "nil")
         if python._isString(value):
             pytype_name = "str"
+            _pytype = python.PyDict_GetItem(_PYTYPE_DICT, "str")
         else:
             pytype_name = _typename(value)
-            if isinstance(value, bool):
-                value = _lower_bool(value)
+            _pytype = python.PyDict_GetItem(_PYTYPE_DICT, pytype_name)
+            if _pytype is not NULL:
+                value = (<PyType>_pytype).stringify(value)
             else:
                 value = str(value)
-        dict_result = python.PyDict_GetItem(_PYTYPE_DICT, pytype_name)
-        if dict_result is not NULL:
+        if _pytype is not NULL:
             cetree.setAttributeValue(element, PYTYPE_ATTRIBUTE, pytype_name)
         else:
             cetree.delAttributeFromNsName(element._c_node, PYTYPE_NAMESPACE,
@@ -889,7 +894,7 @@
     """
     cdef readonly object name
     cdef readonly object type_check
-    cdef object _add_text
+    cdef readonly object stringify
     cdef object _type
     cdef object _schema_types
     def __init__(self, name, type_check, type_class, stringify=None):
@@ -905,9 +910,8 @@
         self._type = type_class
         self.type_check = type_check
         if stringify is None:
-            self._add_text = _StringValueSetter(str)
-        else:
-            self._add_text = _StringValueSetter(stringify)
+            stringify = str
+        self.stringify = stringify
         self._schema_types = []
 
     def __repr__(self):
@@ -978,14 +982,6 @@
         def __set__(self, types):
             self._schema_types = list(types)
 
-cdef class _StringValueSetter:
-    cdef object _stringify
-    def __init__(self, stringify):
-        self._stringify = stringify
-
-    def __call__(self, elem, value):
-        _add_text(elem, self._stringify(value))
-
 
 cdef object _PYTYPE_DICT
 _PYTYPE_DICT = {}
@@ -1209,7 +1205,7 @@
                 pytype_name = _typename(child)
                 pytype = python.PyDict_GetItem(_PYTYPE_DICT, pytype_name)
                 if pytype is not NULL:
-                    (<PyType>pytype)._add_text(element, child)
+                    _add_text(element, (<PyType>pytype).stringify(child))
                 else:
                     has_string_value = 1
                     child = str(child)


More information about the lxml-checkins mailing list