[lxml-dev] objectify.DataElement() fixed to use stringify
Hi, I recently noticed that DataElement() doesn't make use of PyType.stringify. Patch committed to trunk, with test: Committed revision 75584. 0 pytaf@adevp02 .../lxml $ svn diff -rCOMMITTED Index: src/lxml/tests/test_objectify.py =================================================================== --- src/lxml/tests/test_objectify.py (revision 75482) +++ src/lxml/tests/test_objectify.py (working copy) @@ -318,6 +318,13 @@ arg = objectify.DataElement(3.1415) self.assertRaises(ValueError, objectify.DataElement, arg, _xsi="xsd:int") + + def test_data_element_element_arg(self): + arg = objectify.Element('arg') + value = objectify.DataElement(arg) + self.assert_(isinstance(value, objectify.ObjectifiedElement)) + for attr in arg.attrib: + self.assertEquals(value.get(attr), arg.get(attr)) def test_root(self): root = self.Element("test") @@ -1968,6 +1975,14 @@ self.assertEquals(r.date.pyval, parse_date(stringify_date(time))) self.assertEquals(r.date.text, stringify_date(time)) + date = objectify.DataElement(time) + + self.assert_(isinstance(date, DatetimeElement)) + self.assert_(isinstance(date.pyval, datetime)) + + self.assertEquals(date.pyval, parse_date(stringify_date(time))) + self.assertEquals(date.text, stringify_date(time)) + def test_object_path(self): root = self.XML(xml_str) path = objectify.ObjectPath( "root.c1.c2" ) Index: src/lxml/lxml.objectify.pyx =================================================================== --- src/lxml/lxml.objectify.pyx (revision 75482) +++ src/lxml/lxml.objectify.pyx (working copy) @@ -1973,6 +1973,9 @@ if dict_result is not NULL: _pytype = (<PyType>dict_result).name + if _pytype is None: + _pytype = _pytypename(_value) + if _value is None and _pytype != u"str": _pytype = _pytype or u"NoneType" strval = None @@ -1984,11 +1987,12 @@ else: strval = u"false" else: - strval = unicode(_value) + stringify = unicode + dict_result = python.PyDict_GetItem(_PYTYPE_DICT, _pytype) + if dict_result is not NULL: + stringify = (<PyType>dict_result).stringify + strval = stringify(_value) - if _pytype is None: - _pytype = _pytypename(_value) - if _pytype is not None: if _pytype == u"NoneType" or _pytype == u"none": strval = None Btw.: Before I noticed the existing stringify test in test_objectify I thought I'd add my custom pure-python DecimalElement class to have something to test. I didn't need to in the end but I wondered: What (and where?) about adding this very simple DecimalElement class to lxml? Maybe have an extra subdir for objectify element classes that are not enabled per default but can be imported & registered? I could also provide a DatetimeElement implementation, though this currently depends on dateutil - but I don't think this should be a bigproblem, it simply wouldn't work on machines without dateutil. Holger -- GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT! Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01
participants (1)
-
jholg@gmx.de