Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 3 commits
  • 2 files changed
  • 0 comments
  • 2 contributors
Mar 29, 2013
mike bayer zzzeek - repair issue in sax.ElementTreeContentHandler
whereby attributes passed to startElement() would be mis-interpreted
as containing a namespace attribute, leading to a TypeError,
as well as where attributes with namespaces wouldn't be split
up correctly when passed to startElement().
3222e75
Mar 30, 2013
mike bayer zzzeek - pass through non-NS aware attribute names as is; a ":" should just …
…go to

ValueError
- use boolean test for attributes dict
b844f9e
Mar 31, 2013
scoder scoder Merge pull request #107 from zzzeek/master
repair non-namespace-aware attribute mis-interpretation in ElementTreeContentHandler
103cd6c

Showing 2 changed files with 34 additions and 4 deletions. Show diff stats Hide diff stats

  1. +7 2 src/lxml/sax.py
  2. +27 2 src/lxml/tests/test_sax.py
9 src/lxml/sax.py
@@ -26,6 +26,7 @@ def _getNsTag(tag):
26 26 else:
27 27 return (None, tag)
28 28
  29 +
29 30 class ElementTreeContentHandler(ContentHandler):
30 31 """Build an lxml ElementTree from SAX events.
31 32 """
@@ -45,7 +46,7 @@ def _get_etree(self):
45 46 return ElementTree(self._root)
46 47
47 48 etree = property(_get_etree, doc=_get_etree.__doc__)
48   -
  49 +
49 50 def setDocumentLocator(self, locator):
50 51 pass
51 52
@@ -127,6 +128,10 @@ def endElementNS(self, ns_name, qname):
127 128 raise SaxError("Unexpected element closed: " + el_tag)
128 129
129 130 def startElement(self, name, attributes=None):
  131 + if attributes:
  132 + attributes = dict(
  133 + [((None, k), v) for k, v in attributes.items()]
  134 + )
130 135 self.startElementNS((None, name), name, attributes)
131 136
132 137 def endElement(self, name):
@@ -143,7 +148,7 @@ def characters(self, data):
143 148 last_element.text = (last_element.text or '') + data
144 149
145 150 ignorableWhitespace = characters
146   -
  151 +
147 152
148 153 class ElementTreeProducer(object):
149 154 """Produces SAX events for an element and children.
29 src/lxml/tests/test_sax.py
@@ -216,6 +216,31 @@ def test_etree_sax_no_ns(self):
216 216 self.assertEqual('b', root[0].tag)
217 217 self.assertEqual('c', root[1].tag)
218 218
  219 + def test_etree_sax_no_ns_attributes(self):
  220 + handler = sax.ElementTreeContentHandler()
  221 + handler.startDocument()
  222 + handler.startElement('a', {"attr_a1": "a1"})
  223 + handler.startElement('b', {"attr_b1": "b1"})
  224 + handler.endElement('b')
  225 + handler.endElement('a')
  226 + handler.endDocument()
  227 +
  228 + new_tree = handler.etree
  229 + root = new_tree.getroot()
  230 + self.assertEqual('a', root.tag)
  231 + self.assertEqual('b', root[0].tag)
  232 + self.assertEqual('a1', root.attrib["attr_a1"])
  233 + self.assertEqual('b1', root[0].attrib["attr_b1"])
  234 +
  235 + def test_etree_sax_ns_attributes(self):
  236 + handler = sax.ElementTreeContentHandler()
  237 + handler.startDocument()
  238 +
  239 + self.assertRaises(ValueError,
  240 + handler.startElement,
  241 + 'a', {"blaA:attr_a1": "a1"}
  242 + )
  243 +
219 244 def test_etree_sax_error(self):
220 245 handler = sax.ElementTreeContentHandler()
221 246 handler.startDocument()
@@ -233,14 +258,14 @@ def _saxify_unsaxify(self, saxifiable):
233 258 handler = sax.ElementTreeContentHandler()
234 259 sax.ElementTreeProducer(saxifiable, handler).saxify()
235 260 return handler.etree
236   -
  261 +
237 262 def _saxify_serialize(self, tree):
238 263 new_tree = self._saxify_unsaxify(tree)
239 264 f = BytesIO()
240 265 new_tree.write(f)
241 266 return f.getvalue().replace(_bytes('\n'), _bytes(''))
242 267
243   -
  268 +
244 269 def test_suite():
245 270 suite = unittest.TestSuite()
246 271 suite.addTests([unittest.makeSuite(ETreeSaxTestCase)])

No commit comments for this range

Something went wrong with that request. Please try again.